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

Commit 101dcaa8 authored by Daniel Bright's avatar Daniel Bright Committed by Android (Google) Code Review
Browse files

Merge "Prevent non-cellular request additions"

parents fc07ef8a 359128f9
Loading
Loading
Loading
Loading
+16 −11
Original line number Diff line number Diff line
@@ -660,21 +660,26 @@ public class PhoneSwitcher extends Handler {
    }

    private void onRequestNetwork(NetworkRequest networkRequest) {
        final DcRequest dcRequest = new DcRequest(networkRequest, mContext);
        final DcRequest dcRequest = DcRequest.create(networkRequest);
        if (dcRequest != null) {
            if (!mPrioritizedDcRequests.contains(dcRequest)) {
                collectRequestNetworkMetrics(networkRequest);
                mPrioritizedDcRequests.add(dcRequest);
                Collections.sort(mPrioritizedDcRequests);
                onEvaluate(REQUESTS_CHANGED, "netRequest");
                log("Added DcRequest, size: " + mPrioritizedDcRequests.size());
            }
        }
    }

    private void onReleaseNetwork(NetworkRequest networkRequest) {
        final DcRequest dcRequest = new DcRequest(networkRequest, mContext);

        final DcRequest dcRequest = DcRequest.create(networkRequest);
        if (dcRequest != null) {
            if (mPrioritizedDcRequests.remove(dcRequest)) {
                onEvaluate(REQUESTS_CHANGED, "netReleased");
                collectReleaseNetworkMetrics(networkRequest);
                log("Removed DcRequest, size: " + mPrioritizedDcRequests.size());
            }
        }
    }

+51 −4
Original line number Diff line number Diff line
@@ -15,21 +15,68 @@
 */
package com.android.internal.telephony.dataconnection;

import android.content.Context;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.net.NetworkRequest;
import android.net.NetworkSpecifier;
import android.net.TelephonyNetworkSpecifier;
import android.telephony.Annotation.ApnType;

import com.android.telephony.Rlog;

/**
 * Wraps cellular network requests to configured apn types.
 */
public class DcRequest implements Comparable<DcRequest> {
    private static final String LOG_TAG = "DcRequest";

    @NonNull
    public final NetworkRequest networkRequest;
    public final int priority;
    public final @ApnType int apnType;

    public DcRequest(NetworkRequest nr, Context context) {
    private DcRequest(@NonNull final NetworkRequest nr, @ApnType final int type,
            int apnPriority) {
        networkRequest = nr;
        apnType = ApnContext.getApnTypeFromNetworkRequest(networkRequest);
        priority = ApnConfigTypeRepository.getDefault().getByType(apnType).getPriority();
        priority = apnPriority;
        apnType = type;
    }

    /**
     * Create a DcRequest based off of the network request.  If the network request is not cellular,
     * then null is returned and a warning is generated.
     * @param networkRequest sets the type of dc request
     * @return corresponding DcRequest
     *
     */
    @Nullable
    public static DcRequest create(@NonNull final NetworkRequest networkRequest) {
        final int apnType = ApnContext.getApnTypeFromNetworkRequest(networkRequest);
        final ApnConfigType apnConfigType = ApnConfigTypeRepository.getDefault().getByType(apnType);
        if (apnConfigType == null) {
            Rlog.d(LOG_TAG, "Non cellular request ignored: " + networkRequest.toString());
            checkForAnomalousNetworkRequest(networkRequest);
            return null;
        } else {
            Rlog.d(LOG_TAG, "Cellular request confirmed: " + networkRequest.toString());
            return new DcRequest(networkRequest, apnType, apnConfigType.getPriority());
        }
    }

    private static void checkForAnomalousNetworkRequest(NetworkRequest networkRequest) {
        NetworkSpecifier specifier = networkRequest.getNetworkSpecifier();
        if (specifier != null) {
            if (specifier instanceof TelephonyNetworkSpecifier) {
                reportAnomalousNetworkRequest(networkRequest);
            }
        }
    }

    private static void reportAnomalousNetworkRequest(NetworkRequest networkRequest) {
        //TODO: Report anomaly if this happens
        Rlog.w(LOG_TAG, "A TelephonyNetworkSpecifier for a non-cellular request is invalid: "
                + networkRequest.toString());

    }

    public String toString() {
+127 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.telephony.dataconnection;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.net.TelephonyNetworkSpecifier;

import com.android.internal.telephony.TelephonyTest;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DcRequestTest extends TelephonyTest {

    @Before
    public void setUp() throws Exception {
        super.setUp(getClass().getSimpleName());
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    public void whenNetworkRequestInternetThenPriorityZero() {
        NetworkRequest request =
                new NetworkRequest.Builder()
                        .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
                        .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                        .build();

        DcRequest dcRequest = DcRequest.create(request);

        assertEquals(0, dcRequest.priority);
    }

    @Test
    public void whenNetworkRequestMmsThenPriorityTwo() {
        NetworkRequest request =
                new NetworkRequest.Builder()
                        .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
                        .addCapability(NetworkCapabilities.NET_CAPABILITY_MMS)
                        .build();

        DcRequest dcRequest = DcRequest.create(request);

        assertEquals(2, dcRequest.priority);
    }

    @Test
    public void whenNetworkRequestMcxThenPriorityThree() {
        NetworkRequest request =
                new NetworkRequest.Builder()
                        .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
                        //Testing out multiple transport types here
                        .addTransportType(NetworkCapabilities.TRANSPORT_BLUETOOTH)
                        .addCapability(NetworkCapabilities.NET_CAPABILITY_MCX)
                        .build();

        DcRequest dcRequest = DcRequest.create(request);

        assertEquals(3, dcRequest.priority);
    }

    @Test
    public void whenNetworkRequestNotCellularThenDcRequestIsNull() {
        NetworkRequest request =
                new NetworkRequest.Builder()
                        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                        .addCapability(NetworkCapabilities.NET_CAPABILITY_MCX)
                        .build();

        DcRequest dcRequest = DcRequest.create(request);

        assertNull(dcRequest);
    }

    @Test
    public void whenNetworkRequestHasNoTransportThenPriorityStays() {
        //This seems like an invalid case that should be handled differently.
        NetworkRequest request =
                new NetworkRequest.Builder()
                        .addCapability(NetworkCapabilities.NET_CAPABILITY_MCX)
                        .build();

        DcRequest dcRequest = DcRequest.create(request);

        assertEquals(3, dcRequest.priority);
    }

    @Test
    public void whenNetworkRequestNotCellularWithTelephonySpecifierThenDcRequestIsNull() {
        TelephonyNetworkSpecifier telephonyNetworkSpecifier =
                new TelephonyNetworkSpecifier.Builder().setSubscriptionId(5).build();

        //This seems like an invalid case that should be handled differently.
        NetworkRequest request =
                new NetworkRequest.Builder()
                        .addTransportType(NetworkCapabilities.TRANSPORT_BLUETOOTH)
                        .setNetworkSpecifier(telephonyNetworkSpecifier)
                        .build();

        DcRequest dcRequest = DcRequest.create(request);

        assertNull(dcRequest);
    }
}