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 com.android.networkstack.netlink.util
18 
19 import android.net.DnsResolver
20 import android.net.DnsResolver.FLAG_EMPTY
21 import android.net.DnsResolver.TYPE_A
22 import android.net.DnsResolver.TYPE_AAAA
23 import android.net.Network
24 import com.android.testutils.FakeDns
25 import androidx.test.filters.SmallTest
26 import androidx.test.runner.AndroidJUnit4
27 import com.android.networkstack.util.DnsUtils
28 import com.android.networkstack.util.DnsUtils.TYPE_ADDRCONFIG
29 import com.android.server.connectivity.NetworkMonitor.DnsLogFunc
30 import java.net.InetAddress
31 import java.net.UnknownHostException
32 import kotlin.test.assertFailsWith
33 import org.junit.Assert.assertArrayEquals
34 import org.junit.Before
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 import org.mockito.Mock
38 import org.mockito.MockitoAnnotations
39 
40 const val DEFAULT_TIMEOUT_MS = 1000
41 const val SHORT_TIMEOUT_MS = 200
42 
43 @RunWith(AndroidJUnit4::class)
44 @SmallTest
45 class DnsUtilsTest {
46     val fakeNetwork: Network = Network(1234)
47     @Mock
48     lateinit var mockLogger: DnsLogFunc
49     @Mock
50     lateinit var mockResolver: DnsResolver
51     lateinit var fakeDns: FakeDns
52 
53     @Before
setupnull54     fun setup() {
55         MockitoAnnotations.initMocks(this)
56         fakeDns = FakeDns(mockResolver)
57         fakeDns.startMocking()
58     }
59 
assertIpAddressArrayEqualsnull60     private fun assertIpAddressArrayEquals(expect: Array<String>, actual: Array<InetAddress>) =
61             assertArrayEquals("Array of IP addresses differs", expect,
62                     actual.map { it.getHostAddress() }.toTypedArray())
63 
64     @Test
testGetAllByNameWithTypeSuccessnull65     fun testGetAllByNameWithTypeSuccess() {
66         // Test different query types.
67         verifyGetAllByName("www.google.com", arrayOf("2001:db8::1"), TYPE_AAAA)
68         verifyGetAllByName("www.google.com", arrayOf("192.168.0.1"), TYPE_A)
69         verifyGetAllByName("www.android.com", arrayOf("192.168.0.2", "2001:db8::2"),
70                 TYPE_ADDRCONFIG)
71     }
72 
verifyGetAllByNamenull73     private fun verifyGetAllByName(name: String, expected: Array<String>, type: Int) {
74         fakeDns.setAnswer(name, expected, type)
75         DnsUtils.getAllByName(mockResolver, fakeNetwork, name, type, FLAG_EMPTY, DEFAULT_TIMEOUT_MS,
76                 mockLogger).let { assertIpAddressArrayEquals(expected, it) }
77     }
78 
79     @Test
testGetAllByNameWithTypeNoResultnull80     fun testGetAllByNameWithTypeNoResult() {
81         verifyGetAllByNameFails("www.android.com", TYPE_A)
82         verifyGetAllByNameFails("www.android.com", TYPE_AAAA)
83         verifyGetAllByNameFails("www.android.com", TYPE_ADDRCONFIG)
84     }
85 
verifyGetAllByNameFailsnull86     private fun verifyGetAllByNameFails(name: String, type: Int) {
87         assertFailsWith<UnknownHostException> {
88             DnsUtils.getAllByName(mockResolver, fakeNetwork, name, type,
89                     FLAG_EMPTY, SHORT_TIMEOUT_MS, mockLogger)
90         }
91     }
92     // TODO: Add more tests. Verify timeout, logger and error.
93 }