1 /*
2  * Copyright (C) 2019 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 
17 package android.net
18 
19 import android.os.Build
20 import androidx.test.filters.SmallTest
21 import com.android.modules.utils.build.SdkLevel
22 import com.android.testutils.DevSdkIgnoreRule
23 import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
24 import com.android.testutils.DevSdkIgnoreRunner
25 import com.android.testutils.assertParcelingIsLossless
26 import kotlin.test.assertEquals
27 import kotlin.test.assertNotEquals
28 import org.junit.Assert.assertFalse
29 import org.junit.Assert.assertTrue
30 import org.junit.Rule
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 
34 @SmallTest
35 @RunWith(DevSdkIgnoreRunner::class)
36 class CaptivePortalDataTest {
37     @Rule @JvmField
38     val ignoreRule = DevSdkIgnoreRule()
39 
40     private val data = CaptivePortalData.Builder()
41             .setRefreshTime(123L)
42             .setUserPortalUrl(Uri.parse("https://portal.example.com/test"))
43             .setVenueInfoUrl(Uri.parse("https://venue.example.com/test"))
44             .setSessionExtendable(true)
45             .setBytesRemaining(456L)
46             .setExpiryTime(789L)
47             .setCaptive(true)
<lambda>null48             .apply {
49                 if (SdkLevel.isAtLeastS()) {
50                     setVenueFriendlyName("venue friendly name")
51                 }
52             }
53             .build()
54 
55     private val dataFromPasspoint = CaptivePortalData.Builder()
56             .setCaptive(true)
<lambda>null57             .apply {
58                 if (SdkLevel.isAtLeastS()) {
59                     setVenueFriendlyName("venue friendly name")
60                     setUserPortalUrl(Uri.parse("https://tc.example.com/passpoint"),
61                             CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_PASSPOINT)
62                     setVenueInfoUrl(Uri.parse("https://venue.example.com/passpoint"),
63                             CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_PASSPOINT)
64                 }
65             }
66             .build()
67 
makeBuildernull68     private fun makeBuilder() = CaptivePortalData.Builder(data)
69 
70     @Test
71     fun testParcelUnparcel() {
72         assertParcelingIsLossless(data)
73         assertParcelingIsLossless(dataFromPasspoint)
74 
75         assertParcelingIsLossless(makeBuilder().setUserPortalUrl(null).build())
76         assertParcelingIsLossless(makeBuilder().setVenueInfoUrl(null).build())
77     }
78 
79     @Test
testEqualsnull80     fun testEquals() {
81         assertEquals(data, makeBuilder().build())
82 
83         assertNotEqualsAfterChange { it.setRefreshTime(456L) }
84         assertNotEqualsAfterChange { it.setUserPortalUrl(Uri.parse("https://example.com/")) }
85         assertNotEqualsAfterChange { it.setUserPortalUrl(null) }
86         assertNotEqualsAfterChange { it.setVenueInfoUrl(Uri.parse("https://example.com/")) }
87         assertNotEqualsAfterChange { it.setVenueInfoUrl(null) }
88         assertNotEqualsAfterChange { it.setSessionExtendable(false) }
89         assertNotEqualsAfterChange { it.setBytesRemaining(789L) }
90         assertNotEqualsAfterChange { it.setExpiryTime(12L) }
91         assertNotEqualsAfterChange { it.setCaptive(false) }
92 
93         if (SdkLevel.isAtLeastS()) {
94             assertNotEqualsAfterChange { it.setVenueFriendlyName("another friendly name") }
95             assertNotEqualsAfterChange { it.setVenueFriendlyName(null) }
96 
97             assertEquals(dataFromPasspoint, CaptivePortalData.Builder(dataFromPasspoint).build())
98             assertNotEqualsAfterChange { it.setUserPortalUrl(
99                     Uri.parse("https://tc.example.com/passpoint")) }
100             assertNotEqualsAfterChange { it.setUserPortalUrl(
101                     Uri.parse("https://tc.example.com/passpoint"),
102                     CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_OTHER) }
103             assertNotEqualsAfterChange { it.setUserPortalUrl(
104                     Uri.parse("https://tc.example.com/other"),
105                     CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_PASSPOINT) }
106             assertNotEqualsAfterChange { it.setUserPortalUrl(
107                     Uri.parse("https://tc.example.com/passpoint"),
108                     CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_OTHER) }
109             assertNotEqualsAfterChange { it.setVenueInfoUrl(
110                     Uri.parse("https://venue.example.com/passpoint")) }
111             assertNotEqualsAfterChange { it.setVenueInfoUrl(
112                     Uri.parse("https://venue.example.com/other"),
113                     CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_PASSPOINT) }
114             assertNotEqualsAfterChange { it.setVenueInfoUrl(
115                     Uri.parse("https://venue.example.com/passpoint"),
116                     CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_OTHER) }
117         }
118     }
119 
120     @Test
testUserPortalUrlnull121     fun testUserPortalUrl() {
122         assertEquals(Uri.parse("https://portal.example.com/test"), data.userPortalUrl)
123     }
124 
125     @Test
testVenueInfoUrlnull126     fun testVenueInfoUrl() {
127         assertEquals(Uri.parse("https://venue.example.com/test"), data.venueInfoUrl)
128     }
129 
130     @Test
testIsSessionExtendablenull131     fun testIsSessionExtendable() {
132         assertTrue(data.isSessionExtendable)
133     }
134 
135     @Test
testByteLimitnull136     fun testByteLimit() {
137         assertEquals(456L, data.byteLimit)
138         // Test byteLimit unset.
139         assertEquals(-1L, CaptivePortalData.Builder(null).build().byteLimit)
140     }
141 
142     @Test
testRefreshTimeMillisnull143     fun testRefreshTimeMillis() {
144         assertEquals(123L, data.refreshTimeMillis)
145     }
146 
147     @Test
testExpiryTimeMillisnull148     fun testExpiryTimeMillis() {
149         assertEquals(789L, data.expiryTimeMillis)
150         // Test expiryTimeMillis unset.
151         assertEquals(-1L, CaptivePortalData.Builder(null).build().expiryTimeMillis)
152     }
153 
154     @Test
testIsCaptivenull155     fun testIsCaptive() {
156         assertTrue(data.isCaptive)
157         assertFalse(makeBuilder().setCaptive(false).build().isCaptive)
158     }
159 
160     @Test @IgnoreUpTo(Build.VERSION_CODES.R)
testVenueFriendlyNamenull161     fun testVenueFriendlyName() {
162         assertEquals("venue friendly name", data.venueFriendlyName)
163     }
164 
165     @Test @IgnoreUpTo(Build.VERSION_CODES.R)
testGetVenueInfoUrlSourcenull166     fun testGetVenueInfoUrlSource() {
167         assertEquals(CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_OTHER,
168                 data.venueInfoUrlSource)
169         assertEquals(CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_PASSPOINT,
170                 dataFromPasspoint.venueInfoUrlSource)
171     }
172 
173     @Test @IgnoreUpTo(Build.VERSION_CODES.R)
testGetUserPortalUrlSourcenull174     fun testGetUserPortalUrlSource() {
175         assertEquals(CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_OTHER,
176                 data.userPortalUrlSource)
177         assertEquals(CaptivePortalData.CAPTIVE_PORTAL_DATA_SOURCE_PASSPOINT,
178                 dataFromPasspoint.userPortalUrlSource)
179     }
180 
mutatenull181     private fun CaptivePortalData.mutate(mutator: (CaptivePortalData.Builder) -> Unit) =
182             CaptivePortalData.Builder(this).apply { mutator(this) }.build()
183 
assertNotEqualsAfterChangenull184     private fun assertNotEqualsAfterChange(mutator: (CaptivePortalData.Builder) -> Unit) {
185         assertNotEquals(data, data.mutate(mutator))
186     }
187 }