blob: f01057b2ba11a3432316b7cf1d8f90092e603116 [file] [log] [blame]
Yang Sund25444f2023-10-24 18:43:15 +08001/*
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
17package android.net
18
19import android.os.Build
20import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
21import com.android.testutils.DevSdkIgnoreRunner
22
23import org.junit.Test
24import org.junit.runner.RunWith
25import java.net.Inet6Address
26import kotlin.test.assertFalse
27import kotlin.test.assertTrue
28
29import android.net.MulticastRoutingConfig.Builder
30import android.net.MulticastRoutingConfig.FORWARD_NONE
31import android.net.MulticastRoutingConfig.FORWARD_SELECTED
32import android.net.MulticastRoutingConfig.FORWARD_WITH_MIN_SCOPE
33
34@RunWith(DevSdkIgnoreRunner::class)
35@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
36class MulticastRoutingConfigTest {
37
38 val address1 = Inet6Address.getByName("2000::8888") as Inet6Address
39 val address2 = Inet6Address.getByName("2000::9999") as Inet6Address
40
41 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
65 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
79 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
90 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}