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 org.junit.Test;
27 
28 import java.util.HashSet;
29 import java.util.Set;
30 
31 public class VcnCellUnderlyingNetworkTemplateTest extends VcnUnderlyingNetworkTemplateTestBase {
32     private static final Set<String> ALLOWED_PLMN_IDS = new HashSet<>();
33     private static final Set<Integer> ALLOWED_CARRIER_IDS = new HashSet<>();
34 
35     // Public for use in UnderlyingNetworkControllerTest
getTestNetworkTemplateBuilder()36     public static VcnCellUnderlyingNetworkTemplate.Builder getTestNetworkTemplateBuilder() {
37         return new VcnCellUnderlyingNetworkTemplate.Builder()
38                 .setMetered(MATCH_FORBIDDEN)
39                 .setMinUpstreamBandwidthKbps(
40                         TEST_MIN_ENTRY_UPSTREAM_BANDWIDTH_KBPS,
41                         TEST_MIN_EXIT_UPSTREAM_BANDWIDTH_KBPS)
42                 .setMinDownstreamBandwidthKbps(
43                         TEST_MIN_ENTRY_DOWNSTREAM_BANDWIDTH_KBPS,
44                         TEST_MIN_EXIT_DOWNSTREAM_BANDWIDTH_KBPS)
45                 .setOperatorPlmnIds(ALLOWED_PLMN_IDS)
46                 .setSimSpecificCarrierIds(ALLOWED_CARRIER_IDS)
47                 .setRoaming(MATCH_FORBIDDEN)
48                 .setOpportunistic(MATCH_REQUIRED);
49     }
50 
51     // Package private for use in VcnGatewayConnectionConfigTest
getTestNetworkTemplate()52     static VcnCellUnderlyingNetworkTemplate getTestNetworkTemplate() {
53         return getTestNetworkTemplateBuilder().build();
54     }
55 
setAllCapabilities( VcnCellUnderlyingNetworkTemplate.Builder builder, int matchCriteria)56     private void setAllCapabilities(
57             VcnCellUnderlyingNetworkTemplate.Builder builder, int matchCriteria) {
58         builder.setCbs(matchCriteria);
59         builder.setDun(matchCriteria);
60         builder.setIms(matchCriteria);
61         builder.setInternet(matchCriteria);
62         builder.setMms(matchCriteria);
63         builder.setRcs(matchCriteria);
64     }
65 
verifyAllCapabilities( VcnCellUnderlyingNetworkTemplate template, int expectMatchCriteriaforNonInternet, int expectMatchCriteriaforInternet)66     private void verifyAllCapabilities(
67             VcnCellUnderlyingNetworkTemplate template,
68             int expectMatchCriteriaforNonInternet,
69             int expectMatchCriteriaforInternet) {
70         assertEquals(expectMatchCriteriaforNonInternet, template.getCbs());
71         assertEquals(expectMatchCriteriaforNonInternet, template.getDun());
72         assertEquals(expectMatchCriteriaforNonInternet, template.getIms());
73         assertEquals(expectMatchCriteriaforNonInternet, template.getMms());
74         assertEquals(expectMatchCriteriaforNonInternet, template.getRcs());
75 
76         assertEquals(expectMatchCriteriaforInternet, template.getInternet());
77     }
78 
79     @Test
testBuilderAndGetters()80     public void testBuilderAndGetters() {
81         final VcnCellUnderlyingNetworkTemplate.Builder builder = getTestNetworkTemplateBuilder();
82         setAllCapabilities(builder, MATCH_REQUIRED);
83 
84         final VcnCellUnderlyingNetworkTemplate networkPriority = builder.build();
85 
86         assertEquals(MATCH_FORBIDDEN, networkPriority.getMetered());
87         assertEquals(
88                 TEST_MIN_ENTRY_UPSTREAM_BANDWIDTH_KBPS,
89                 networkPriority.getMinEntryUpstreamBandwidthKbps());
90         assertEquals(
91                 TEST_MIN_EXIT_UPSTREAM_BANDWIDTH_KBPS,
92                 networkPriority.getMinExitUpstreamBandwidthKbps());
93         assertEquals(
94                 TEST_MIN_ENTRY_DOWNSTREAM_BANDWIDTH_KBPS,
95                 networkPriority.getMinEntryDownstreamBandwidthKbps());
96         assertEquals(
97                 TEST_MIN_EXIT_DOWNSTREAM_BANDWIDTH_KBPS,
98                 networkPriority.getMinExitDownstreamBandwidthKbps());
99         assertEquals(ALLOWED_PLMN_IDS, networkPriority.getOperatorPlmnIds());
100         assertEquals(ALLOWED_CARRIER_IDS, networkPriority.getSimSpecificCarrierIds());
101         assertEquals(MATCH_FORBIDDEN, networkPriority.getRoaming());
102         assertEquals(MATCH_REQUIRED, networkPriority.getOpportunistic());
103 
104         verifyAllCapabilities(networkPriority, MATCH_REQUIRED, MATCH_REQUIRED);
105     }
106 
107     @Test
testBuilderAndGettersForDefaultValues()108     public void testBuilderAndGettersForDefaultValues() {
109         final VcnCellUnderlyingNetworkTemplate networkPriority =
110                 new VcnCellUnderlyingNetworkTemplate.Builder().build();
111         assertEquals(MATCH_ANY, networkPriority.getMetered());
112 
113         // Explicitly expect 0, as documented in Javadoc on setter methods.
114         assertEquals(0, networkPriority.getMinEntryUpstreamBandwidthKbps());
115         assertEquals(0, networkPriority.getMinExitUpstreamBandwidthKbps());
116         assertEquals(0, networkPriority.getMinEntryDownstreamBandwidthKbps());
117         assertEquals(0, networkPriority.getMinExitDownstreamBandwidthKbps());
118 
119         assertEquals(new HashSet<String>(), networkPriority.getOperatorPlmnIds());
120         assertEquals(new HashSet<Integer>(), networkPriority.getSimSpecificCarrierIds());
121         assertEquals(MATCH_ANY, networkPriority.getRoaming());
122         assertEquals(MATCH_ANY, networkPriority.getOpportunistic());
123 
124         verifyAllCapabilities(networkPriority, MATCH_ANY, MATCH_REQUIRED);
125     }
126 
127     @Test
testBuilderRequiresStricterEntryCriteria()128     public void testBuilderRequiresStricterEntryCriteria() {
129         try {
130             new VcnCellUnderlyingNetworkTemplate.Builder()
131                     .setMinUpstreamBandwidthKbps(
132                             TEST_MIN_EXIT_UPSTREAM_BANDWIDTH_KBPS,
133                             TEST_MIN_ENTRY_UPSTREAM_BANDWIDTH_KBPS);
134 
135             fail("Expected IAE for exit threshold > entry threshold");
136         } catch (IllegalArgumentException expected) {
137         }
138 
139         try {
140             new VcnCellUnderlyingNetworkTemplate.Builder()
141                     .setMinDownstreamBandwidthKbps(
142                             TEST_MIN_EXIT_DOWNSTREAM_BANDWIDTH_KBPS,
143                             TEST_MIN_ENTRY_DOWNSTREAM_BANDWIDTH_KBPS);
144 
145             fail("Expected IAE for exit threshold > entry threshold");
146         } catch (IllegalArgumentException expected) {
147         }
148     }
149 
150     @Test
testBuildFailWithoutRequiredCapabilities()151     public void testBuildFailWithoutRequiredCapabilities() {
152         try {
153             new VcnCellUnderlyingNetworkTemplate.Builder().setInternet(MATCH_ANY).build();
154 
155             fail("Expected IAE for missing required capabilities");
156         } catch (IllegalArgumentException expected) {
157         }
158     }
159 
160     @Test
testEqualsWithDifferentCapabilities()161     public void testEqualsWithDifferentCapabilities() {
162         final VcnCellUnderlyingNetworkTemplate left =
163                 new VcnCellUnderlyingNetworkTemplate.Builder().setDun(MATCH_REQUIRED).build();
164         final VcnCellUnderlyingNetworkTemplate right =
165                 new VcnCellUnderlyingNetworkTemplate.Builder().setMms(MATCH_REQUIRED).build();
166         assertNotEquals(left, right);
167     }
168 
169     @Test
testPersistableBundle()170     public void testPersistableBundle() {
171         final VcnCellUnderlyingNetworkTemplate networkPriority = getTestNetworkTemplate();
172         assertEquals(
173                 networkPriority,
174                 VcnUnderlyingNetworkTemplate.fromPersistableBundle(
175                         networkPriority.toPersistableBundle()));
176     }
177 
178     @Test
testPersistableBundleWithCapabilities()179     public void testPersistableBundleWithCapabilities() {
180         final VcnCellUnderlyingNetworkTemplate.Builder builder = getTestNetworkTemplateBuilder();
181         setAllCapabilities(builder, MATCH_REQUIRED);
182 
183         final VcnCellUnderlyingNetworkTemplate networkPriority = builder.build();
184         assertEquals(
185                 networkPriority,
186                 VcnUnderlyingNetworkTemplate.fromPersistableBundle(
187                         networkPriority.toPersistableBundle()));
188     }
189 }
190