Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7fc260a0 authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN
Browse files

Fix NetworkStackTests compatibility with Q

NetworkStackTests use some JNI code for APF tests, which does not link
all libc++ dependencies statically. Because of this, the test stopped
passing on Q.
Compile the JNI code against stable SDK to guarantee backwards
compatibility.

Also fix usage of the CellIdentity* constructors for Q devices: the
constructors have new parameters in later APIs. Use reflection on older
devices to reference the old constructor; the reflection call is not
likely to break as there should be no change to the Q constructor in the
future.

Bug: 148753875
Test: atest NetworkStackTests with a Q device connected
Change-Id: Ie35d6a2618847fa1de4c093c21df7c38584bbaef
parent ef60e461
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -28,14 +28,13 @@ cc_library_shared {
        "hardware/google/apf",
    ],
    shared_libs: [
        "libbinder",
        "liblog",
        "libcutils",
        "libnativehelper",
        "netd_aidl_interface-cpp",
        "libnativehelper_compat_libc++",
    ],
    static_libs: [
        "libapf",
        "libpcap",
    ],
    sdk_version: "29",
    stl: "c++_static",
}
+3 −2
Original line number Diff line number Diff line
@@ -14,19 +14,20 @@
 * limitations under the License.
 */

#include <android/log.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedUtfChars.h>
#include <jni.h>
#include <pcap.h>
#include <stdlib.h>
#include <string>
#include <utils/Log.h>
#include <vector>

#include "apf_interpreter.h"
#include "nativehelper/scoped_primitive_array.h"

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define LOG_TAG "NetworkStackUtils-JNI"

// JNI function acting as simply call-through to native APF interpreter.
static jint com_android_server_ApfTest_apfSimulate(
@@ -226,7 +227,7 @@ static jboolean com_android_server_ApfTest_dropsAllPackets(JNIEnv* env, jclass,
extern "C" jint JNI_OnLoad(JavaVM* vm, void*) {
    JNIEnv *env;
    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
        ALOGE("ERROR: GetEnv failed");
        __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "ERROR: GetEnv failed");
        return -1;
    }

+38 −5
Original line number Diff line number Diff line
@@ -83,6 +83,7 @@ import android.net.shared.PrivateDnsConfig;
import android.net.util.SharedLog;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.ConditionVariable;
import android.os.Handler;
@@ -105,6 +106,7 @@ import androidx.test.runner.AndroidJUnit4;

import com.android.internal.util.CollectionUtils;
import com.android.networkstack.R;
import com.android.networkstack.apishim.ShimUtils;
import com.android.networkstack.metrics.DataStallDetectionStats;
import com.android.networkstack.metrics.DataStallStatsUtils;
import com.android.networkstack.netlink.TcpSocketTracker;
@@ -123,6 +125,7 @@ import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
@@ -542,11 +545,10 @@ public class NetworkMonitorTest {
        final CellInfoGsm cellInfoGsm1 = new CellInfoGsm();
        final CellInfoGsm cellInfoGsm2 = new CellInfoGsm();
        final CellInfoLte cellInfoLte = new CellInfoLte();
        final CellIdentityGsm cellIdentityGsm =
                new CellIdentityGsm(0, 0, 0, 0, "460", "01", "", "", Collections.emptyList());
        final CellIdentityLte cellIdentityLte =
                new CellIdentityLte(0, 0, 0, 0, 0, "466", "01", "", "",
                        Collections.emptyList(), null);
        final CellIdentityGsm cellIdentityGsm = makeCellIdentityGsm(
                0, 0, 0, 0, "460", "01", "", "");
        final CellIdentityLte cellIdentityLte = makeCellIdentityLte(
                0, 0, 0, 0, 0, "466", "01", "", "");
        cellInfoGsm1.setCellIdentity(cellIdentityGsm);
        cellInfoGsm2.setCellIdentity(cellIdentityGsm);
        cellInfoLte.setCellIdentity(cellIdentityLte);
@@ -569,6 +571,37 @@ public class NetworkMonitorTest {
        assertEquals(wnm.getContext(), wnm.getContextByMccIfNoSimCardOrDefault());
    }

    private static CellIdentityGsm makeCellIdentityGsm(int lac, int cid, int arfcn, int bsic,
            String mccStr, String mncStr, String alphal, String alphas)
            throws ReflectiveOperationException {
        if (ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)) {
            return new CellIdentityGsm(lac, cid, arfcn, bsic, mccStr, mncStr, alphal, alphas,
                    Collections.emptyList() /* additionalPlmns */);
        } else {
            // API <= Q does not have the additionalPlmns parameter
            final Constructor<CellIdentityGsm> constructor = CellIdentityGsm.class.getConstructor(
                    int.class, int.class, int.class, int.class, String.class, String.class,
                    String.class, String.class);
            return constructor.newInstance(lac, cid, arfcn, bsic, mccStr, mncStr, alphal, alphas);
        }
    }

    private static CellIdentityLte makeCellIdentityLte(int ci, int pci, int tac, int earfcn,
            int bandwidth, String mccStr, String mncStr, String alphal, String alphas)
            throws ReflectiveOperationException {
        if (ShimUtils.isReleaseOrDevelopmentApiAbove(Build.VERSION_CODES.Q)) {
            return new CellIdentityLte(ci, pci, tac, earfcn, bandwidth, mccStr, mncStr, alphal,
                    alphas, Collections.emptyList() /* additionalPlmns */, null /* csgInfo */);
        } else {
            // API <= Q does not have the additionalPlmns and csgInfo parameters
            final Constructor<CellIdentityLte> constructor = CellIdentityLte.class.getConstructor(
                    int.class, int.class, int.class, int.class, int.class, String.class,
                    String.class, String.class, String.class);
            return constructor.newInstance(ci, pci, tac, earfcn, bandwidth, mccStr, mncStr, alphal,
                    alphas);
        }
    }

    @Test
    public void testGetIntSetting() throws Exception {
        WrappedNetworkMonitor wnm = makeNotMeteredNetworkMonitor();