1 /*
2  * Copyright (C) 2023 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 com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
21 import com.android.testutils.DevSdkIgnoreRunner
22 
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 import java.net.Inet6Address
26 import kotlin.test.assertFalse
27 import kotlin.test.assertTrue
28 
29 import android.net.MulticastRoutingConfig.Builder
30 import android.net.MulticastRoutingConfig.FORWARD_NONE
31 import android.net.MulticastRoutingConfig.FORWARD_SELECTED
32 import android.net.MulticastRoutingConfig.FORWARD_WITH_MIN_SCOPE
33 
34 @RunWith(DevSdkIgnoreRunner::class)
35 @IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
36 class MulticastRoutingConfigTest {
37 
38     val address1 = Inet6Address.getByName("2000::8888") as Inet6Address
39     val address2 = Inet6Address.getByName("2000::9999") as Inet6Address
40 
configNonenull41     private fun configNone() = Builder(FORWARD_NONE).build()
42     private fun configMinScope(scope: Int) = Builder(FORWARD_WITH_MIN_SCOPE, scope).build()
43     private fun configSelected() = Builder(FORWARD_SELECTED).build()
44     private fun configSelectedWithAddress1AndAddress2() =
45             Builder(FORWARD_SELECTED).addListeningAddress(address1)
46             .addListeningAddress(address2).build()
47     private fun configSelectedWithAddress2AndAddress1() =
48             Builder(FORWARD_SELECTED).addListeningAddress(address2)
49             .addListeningAddress(address1).build()
50 
51     @Test
52     fun equalityTests() {
53 
54         assertTrue(configNone().equals(configNone()))
55 
56         assertTrue(configSelected().equals(configSelected()))
57 
58         assertTrue(configMinScope(4).equals(configMinScope(4)))
59 
60         assertTrue(configSelectedWithAddress1AndAddress2()
61                 .equals(configSelectedWithAddress2AndAddress1()))
62     }
63 
64     @Test
inequalityTestsnull65     fun inequalityTests() {
66 
67         assertFalse(configNone().equals(configSelected()))
68 
69         assertFalse(configNone().equals(configMinScope(4)))
70 
71         assertFalse(configSelected().equals(configMinScope(4)))
72 
73         assertFalse(configMinScope(4).equals(configMinScope(5)))
74 
75         assertFalse(configSelected().equals(configSelectedWithAddress1AndAddress2()))
76     }
77 
78     @Test
toString_equalObjects_returnsEqualStringsnull79     fun toString_equalObjects_returnsEqualStrings() {
80         val config1 = configSelectedWithAddress1AndAddress2()
81         val config2 = configSelectedWithAddress2AndAddress1()
82 
83         val str1 = config1.toString()
84         val str2 = config2.toString()
85 
86         assertTrue(str1.equals(str2))
87     }
88 
89     @Test
toString_unequalObjects_returnsUnequalStringsnull90     fun toString_unequalObjects_returnsUnequalStrings() {
91         val config1 = configSelected()
92         val config2 = configSelectedWithAddress1AndAddress2()
93 
94         val str1 = config1.toString()
95         val str2 = config2.toString()
96 
97         assertFalse(str1.equals(str2))
98     }
99 }
100