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 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 
25 import org.junit.Test;
26 
27 import java.util.Set;
28 
29 public class VcnWifiUnderlyingNetworkTemplateTest extends VcnUnderlyingNetworkTemplateTestBase {
30     private static final String SSID = "TestWifi";
31 
32     // Package private for use in VcnGatewayConnectionConfigTest
getTestNetworkTemplate()33     static VcnWifiUnderlyingNetworkTemplate getTestNetworkTemplate() {
34         return new VcnWifiUnderlyingNetworkTemplate.Builder()
35                 .setMetered(MATCH_FORBIDDEN)
36                 .setMinUpstreamBandwidthKbps(
37                         TEST_MIN_ENTRY_UPSTREAM_BANDWIDTH_KBPS,
38                         TEST_MIN_EXIT_UPSTREAM_BANDWIDTH_KBPS)
39                 .setMinDownstreamBandwidthKbps(
40                         TEST_MIN_ENTRY_DOWNSTREAM_BANDWIDTH_KBPS,
41                         TEST_MIN_EXIT_DOWNSTREAM_BANDWIDTH_KBPS)
42                 .setSsids(Set.of(SSID))
43                 .build();
44     }
45 
46     @Test
testBuilderAndGetters()47     public void testBuilderAndGetters() {
48         final VcnWifiUnderlyingNetworkTemplate networkPriority = getTestNetworkTemplate();
49         assertEquals(MATCH_FORBIDDEN, networkPriority.getMetered());
50         assertEquals(
51                 TEST_MIN_ENTRY_UPSTREAM_BANDWIDTH_KBPS,
52                 networkPriority.getMinEntryUpstreamBandwidthKbps());
53         assertEquals(
54                 TEST_MIN_EXIT_UPSTREAM_BANDWIDTH_KBPS,
55                 networkPriority.getMinExitUpstreamBandwidthKbps());
56         assertEquals(
57                 TEST_MIN_ENTRY_DOWNSTREAM_BANDWIDTH_KBPS,
58                 networkPriority.getMinEntryDownstreamBandwidthKbps());
59         assertEquals(
60                 TEST_MIN_EXIT_DOWNSTREAM_BANDWIDTH_KBPS,
61                 networkPriority.getMinExitDownstreamBandwidthKbps());
62         assertEquals(Set.of(SSID), networkPriority.getSsids());
63     }
64 
65     @Test
testBuilderAndGettersForDefaultValues()66     public void testBuilderAndGettersForDefaultValues() {
67         final VcnWifiUnderlyingNetworkTemplate networkPriority =
68                 new VcnWifiUnderlyingNetworkTemplate.Builder().build();
69         assertEquals(MATCH_ANY, networkPriority.getMetered());
70 
71         // Explicitly expect 0, as documented in Javadoc on setter methods..
72         assertEquals(0, networkPriority.getMinEntryUpstreamBandwidthKbps());
73         assertEquals(0, networkPriority.getMinExitUpstreamBandwidthKbps());
74         assertEquals(0, networkPriority.getMinEntryDownstreamBandwidthKbps());
75         assertEquals(0, networkPriority.getMinExitDownstreamBandwidthKbps());
76 
77         assertTrue(networkPriority.getSsids().isEmpty());
78     }
79 
80     @Test
testBuilderRequiresStricterEntryCriteria()81     public void testBuilderRequiresStricterEntryCriteria() {
82         try {
83             new VcnWifiUnderlyingNetworkTemplate.Builder()
84                     .setMinUpstreamBandwidthKbps(
85                             TEST_MIN_EXIT_UPSTREAM_BANDWIDTH_KBPS,
86                             TEST_MIN_ENTRY_UPSTREAM_BANDWIDTH_KBPS);
87 
88             fail("Expected IAE for exit threshold > entry threshold");
89         } catch (IllegalArgumentException expected) {
90         }
91 
92         try {
93             new VcnWifiUnderlyingNetworkTemplate.Builder()
94                     .setMinDownstreamBandwidthKbps(
95                             TEST_MIN_EXIT_DOWNSTREAM_BANDWIDTH_KBPS,
96                             TEST_MIN_ENTRY_DOWNSTREAM_BANDWIDTH_KBPS);
97 
98             fail("Expected IAE for exit threshold > entry threshold");
99         } catch (IllegalArgumentException expected) {
100         }
101     }
102 
103     @Test
testPersistableBundle()104     public void testPersistableBundle() {
105         final VcnWifiUnderlyingNetworkTemplate networkPriority = getTestNetworkTemplate();
106         assertEquals(
107                 networkPriority,
108                 VcnUnderlyingNetworkTemplate.fromPersistableBundle(
109                         networkPriority.toPersistableBundle()));
110     }
111 }
112