• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.net.vcn;
17 
18 import static android.net.vcn.VcnUnderlyingNetworkTemplate.MATCH_ANY;
19 import static android.net.vcn.VcnUnderlyingNetworkTemplate.MATCH_FORBIDDEN;
20 import static android.net.vcn.VcnUnderlyingNetworkTemplate.MATCH_REQUIRED;
21 
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotEquals;
24 import static org.junit.Assert.fail;
25 
26 import androidx.test.filters.SmallTest;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 import java.util.HashSet;
33 import java.util.Set;
34 
35 @RunWith(AndroidJUnit4.class)
36 @SmallTest
37 public class VcnCellUnderlyingNetworkTemplateTest extends VcnUnderlyingNetworkTemplateTestBase {
38     private static final Set<String> ALLOWED_PLMN_IDS = new HashSet<>();
39     private static final Set<Integer> ALLOWED_CARRIER_IDS = new HashSet<>();
40 
41     // Public for use in UnderlyingNetworkControllerTest
getTestNetworkTemplateBuilder()42     public static VcnCellUnderlyingNetworkTemplate.Builder getTestNetworkTemplateBuilder() {
43         return new VcnCellUnderlyingNetworkTemplate.Builder()
44                 .setMetered(MATCH_FORBIDDEN)
45                 .setMinUpstreamBandwidthKbps(
46                         TEST_MIN_ENTRY_UPSTREAM_BANDWIDTH_KBPS,
47                         TEST_MIN_EXIT_UPSTREAM_BANDWIDTH_KBPS)
48                 .setMinDownstreamBandwidthKbps(
49                         TEST_MIN_ENTRY_DOWNSTREAM_BANDWIDTH_KBPS,
50                         TEST_MIN_EXIT_DOWNSTREAM_BANDWIDTH_KBPS)
51                 .setOperatorPlmnIds(ALLOWED_PLMN_IDS)
52                 .setSimSpecificCarrierIds(ALLOWED_CARRIER_IDS)
53                 .setRoaming(MATCH_FORBIDDEN)
54                 .setOpportunistic(MATCH_REQUIRED);
55     }
56 
57     // Package private for use in VcnGatewayConnectionConfigTest
getTestNetworkTemplate()58     static VcnCellUnderlyingNetworkTemplate getTestNetworkTemplate() {
59         return getTestNetworkTemplateBuilder().build();
60     }
61 
setAllCapabilities( VcnCellUnderlyingNetworkTemplate.Builder builder, int matchCriteria)62     private void setAllCapabilities(
63             VcnCellUnderlyingNetworkTemplate.Builder builder, int matchCriteria) {
64         builder.setCbs(matchCriteria);
65         builder.setDun(matchCriteria);
66         builder.setIms(matchCriteria);
67         builder.setInternet(matchCriteria);
68         builder.setMms(matchCriteria);
69         builder.setRcs(matchCriteria);
70     }
71 
verifyAllCapabilities( VcnCellUnderlyingNetworkTemplate template, int expectMatchCriteriaforNonInternet, int expectMatchCriteriaforInternet)72     private void verifyAllCapabilities(
73             VcnCellUnderlyingNetworkTemplate template,
74             int expectMatchCriteriaforNonInternet,
75             int expectMatchCriteriaforInternet) {
76         assertEquals(expectMatchCriteriaforNonInternet, template.getCbs());
77         assertEquals(expectMatchCriteriaforNonInternet, template.getDun());
78         assertEquals(expectMatchCriteriaforNonInternet, template.getIms());
79         assertEquals(expectMatchCriteriaforNonInternet, template.getMms());
80         assertEquals(expectMatchCriteriaforNonInternet, template.getRcs());
81 
82         assertEquals(expectMatchCriteriaforInternet, template.getInternet());
83     }
84 
85     @Test
testBuilderAndGetters()86     public void testBuilderAndGetters() {
87         final VcnCellUnderlyingNetworkTemplate.Builder builder = getTestNetworkTemplateBuilder();
88         setAllCapabilities(builder, MATCH_REQUIRED);
89 
90         final VcnCellUnderlyingNetworkTemplate networkPriority = builder.build();
91 
92         assertEquals(MATCH_FORBIDDEN, networkPriority.getMetered());
93         assertEquals(
94                 TEST_MIN_ENTRY_UPSTREAM_BANDWIDTH_KBPS,
95                 networkPriority.getMinEntryUpstreamBandwidthKbps());
96         assertEquals(
97                 TEST_MIN_EXIT_UPSTREAM_BANDWIDTH_KBPS,
98                 networkPriority.getMinExitUpstreamBandwidthKbps());
99         assertEquals(
100                 TEST_MIN_ENTRY_DOWNSTREAM_BANDWIDTH_KBPS,
101                 networkPriority.getMinEntryDownstreamBandwidthKbps());
102         assertEquals(
103                 TEST_MIN_EXIT_DOWNSTREAM_BANDWIDTH_KBPS,
104                 networkPriority.getMinExitDownstreamBandwidthKbps());
105         assertEquals(ALLOWED_PLMN_IDS, networkPriority.getOperatorPlmnIds());
106         assertEquals(ALLOWED_CARRIER_IDS, networkPriority.getSimSpecificCarrierIds());
107         assertEquals(MATCH_FORBIDDEN, networkPriority.getRoaming());
108         assertEquals(MATCH_REQUIRED, networkPriority.getOpportunistic());
109 
110         verifyAllCapabilities(networkPriority, MATCH_REQUIRED, MATCH_REQUIRED);
111     }
112 
113     @Test
testBuilderAndGettersForDefaultValues()114     public void testBuilderAndGettersForDefaultValues() {
115         final VcnCellUnderlyingNetworkTemplate networkPriority =
116                 new VcnCellUnderlyingNetworkTemplate.Builder().build();
117         assertEquals(MATCH_ANY, networkPriority.getMetered());
118 
119         // Explicitly expect 0, as documented in Javadoc on setter methods.
120         assertEquals(0, networkPriority.getMinEntryUpstreamBandwidthKbps());
121         assertEquals(0, networkPriority.getMinExitUpstreamBandwidthKbps());
122         assertEquals(0, networkPriority.getMinEntryDownstreamBandwidthKbps());
123         assertEquals(0, networkPriority.getMinExitDownstreamBandwidthKbps());
124 
125         assertEquals(new HashSet<String>(), networkPriority.getOperatorPlmnIds());
126         assertEquals(new HashSet<Integer>(), networkPriority.getSimSpecificCarrierIds());
127         assertEquals(MATCH_ANY, networkPriority.getRoaming());
128         assertEquals(MATCH_ANY, networkPriority.getOpportunistic());
129 
130         verifyAllCapabilities(networkPriority, MATCH_ANY, MATCH_REQUIRED);
131     }
132 
133     @Test
testBuilderRequiresStricterEntryCriteria()134     public void testBuilderRequiresStricterEntryCriteria() {
135         try {
136             new VcnCellUnderlyingNetworkTemplate.Builder()
137                     .setMinUpstreamBandwidthKbps(
138                             TEST_MIN_EXIT_UPSTREAM_BANDWIDTH_KBPS,
139                             TEST_MIN_ENTRY_UPSTREAM_BANDWIDTH_KBPS);
140 
141             fail("Expected IAE for exit threshold > entry threshold");
142         } catch (IllegalArgumentException expected) {
143         }
144 
145         try {
146             new VcnCellUnderlyingNetworkTemplate.Builder()
147                     .setMinDownstreamBandwidthKbps(
148                             TEST_MIN_EXIT_DOWNSTREAM_BANDWIDTH_KBPS,
149                             TEST_MIN_ENTRY_DOWNSTREAM_BANDWIDTH_KBPS);
150 
151             fail("Expected IAE for exit threshold > entry threshold");
152         } catch (IllegalArgumentException expected) {
153         }
154     }
155 
156     @Test
testBuildFailWithoutRequiredCapabilities()157     public void testBuildFailWithoutRequiredCapabilities() {
158         try {
159             new VcnCellUnderlyingNetworkTemplate.Builder().setInternet(MATCH_ANY).build();
160 
161             fail("Expected IAE for missing required capabilities");
162         } catch (IllegalArgumentException expected) {
163         }
164     }
165 
166     @Test
testEqualsWithDifferentCapabilities()167     public void testEqualsWithDifferentCapabilities() {
168         final VcnCellUnderlyingNetworkTemplate left =
169                 new VcnCellUnderlyingNetworkTemplate.Builder().setDun(MATCH_REQUIRED).build();
170         final VcnCellUnderlyingNetworkTemplate right =
171                 new VcnCellUnderlyingNetworkTemplate.Builder().setMms(MATCH_REQUIRED).build();
172         assertNotEquals(left, right);
173     }
174 
175     @Test
testPersistableBundle()176     public void testPersistableBundle() {
177         final VcnCellUnderlyingNetworkTemplate networkPriority = getTestNetworkTemplate();
178         assertEquals(
179                 networkPriority,
180                 VcnUnderlyingNetworkTemplate.fromPersistableBundle(
181                         networkPriority.toPersistableBundle()));
182     }
183 
184     @Test
testPersistableBundleWithCapabilities()185     public void testPersistableBundleWithCapabilities() {
186         final VcnCellUnderlyingNetworkTemplate.Builder builder = getTestNetworkTemplateBuilder();
187         setAllCapabilities(builder, MATCH_REQUIRED);
188 
189         final VcnCellUnderlyingNetworkTemplate networkPriority = builder.build();
190         assertEquals(
191                 networkPriority,
192                 VcnUnderlyingNetworkTemplate.fromPersistableBundle(
193                         networkPriority.toPersistableBundle()));
194     }
195 }
196