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.util; 18 19 import static android.net.util.DnsUtils.IPV6_ADDR_SCOPE_GLOBAL; 20 import static android.net.util.DnsUtils.IPV6_ADDR_SCOPE_LINKLOCAL; 21 import static android.net.util.DnsUtils.IPV6_ADDR_SCOPE_SITELOCAL; 22 23 import static org.junit.Assert.assertEquals; 24 25 import android.annotation.NonNull; 26 import android.annotation.Nullable; 27 import android.net.InetAddresses; 28 import android.os.Build; 29 30 import androidx.test.filters.SmallTest; 31 32 import com.android.testutils.DevSdkIgnoreRule; 33 import com.android.testutils.DevSdkIgnoreRunner; 34 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 38 import java.net.InetAddress; 39 import java.util.Arrays; 40 import java.util.Collections; 41 import java.util.List; 42 43 @RunWith(DevSdkIgnoreRunner.class) 44 @SmallTest 45 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R) 46 public class DnsUtilsTest { stringToAddress(@onNull String addr)47 private InetAddress stringToAddress(@NonNull String addr) { 48 return InetAddresses.parseNumericAddress(addr); 49 } 50 makeSortableAddress(@onNull String addr)51 private DnsUtils.SortableAddress makeSortableAddress(@NonNull String addr) { 52 return makeSortableAddress(addr, null); 53 } 54 makeSortableAddress(@onNull String addr, @Nullable String srcAddr)55 private DnsUtils.SortableAddress makeSortableAddress(@NonNull String addr, 56 @Nullable String srcAddr) { 57 return new DnsUtils.SortableAddress(stringToAddress(addr), 58 srcAddr != null ? stringToAddress(srcAddr) : null); 59 } 60 61 @Test testRfc6724Comparator()62 public void testRfc6724Comparator() { 63 final List<DnsUtils.SortableAddress> test = Arrays.asList( 64 // Ipv4 65 makeSortableAddress("216.58.200.36", "192.168.1.1"), 66 // global with different scope src 67 makeSortableAddress("2404:6800:4008:801::2004", "fe80::1111:2222"), 68 // global without src addr 69 makeSortableAddress("2404:6800:cafe:801::1"), 70 // loop back 71 makeSortableAddress("::1", "::1"), 72 // link local 73 makeSortableAddress("fe80::c46f:1cff:fe04:39b4", "fe80::1"), 74 // teredo tunneling 75 makeSortableAddress("2001::47c1", "2001::2"), 76 // 6bone without src addr 77 makeSortableAddress("3ffe::1234:5678"), 78 // IPv4-compatible 79 makeSortableAddress("::216.58.200.36", "::216.58.200.9"), 80 // 6bone 81 makeSortableAddress("3ffe::1234:5678", "3ffe::1234:1"), 82 // IPv4-mapped IPv6 83 makeSortableAddress("::ffff:192.168.95.7", "::ffff:192.168.95.1")); 84 85 final List<InetAddress> expected = Arrays.asList( 86 stringToAddress("::1"), // loop back 87 stringToAddress("fe80::c46f:1cff:fe04:39b4"), // link local 88 stringToAddress("216.58.200.36"), // Ipv4 89 stringToAddress("::ffff:192.168.95.7"), // IPv4-mapped IPv6 90 stringToAddress("2001::47c1"), // teredo tunneling 91 stringToAddress("::216.58.200.36"), // IPv4-compatible 92 stringToAddress("3ffe::1234:5678"), // 6bone 93 stringToAddress("2404:6800:4008:801::2004"), // global with different scope src 94 stringToAddress("2404:6800:cafe:801::1"), // global without src addr 95 stringToAddress("3ffe::1234:5678")); // 6bone without src addr 96 97 Collections.sort(test, new DnsUtils.Rfc6724Comparator()); 98 99 for (int i = 0; i < test.size(); ++i) { 100 assertEquals(test.get(i).address, expected.get(i)); 101 } 102 103 // TODO: add more combinations 104 } 105 106 @Test testV4SortableAddress()107 public void testV4SortableAddress() { 108 // Test V4 address 109 DnsUtils.SortableAddress test = makeSortableAddress("216.58.200.36"); 110 assertEquals(test.hasSrcAddr, 0); 111 assertEquals(test.prefixMatchLen, 0); 112 assertEquals(test.address, stringToAddress("216.58.200.36")); 113 assertEquals(test.labelMatch, 0); 114 assertEquals(test.scopeMatch, 0); 115 assertEquals(test.scope, IPV6_ADDR_SCOPE_GLOBAL); 116 assertEquals(test.label, 4); 117 assertEquals(test.precedence, 35); 118 119 // Test V4 loopback address with the same source address 120 test = makeSortableAddress("127.1.2.3", "127.1.2.3"); 121 assertEquals(test.hasSrcAddr, 1); 122 assertEquals(test.prefixMatchLen, 0); 123 assertEquals(test.address, stringToAddress("127.1.2.3")); 124 assertEquals(test.labelMatch, 1); 125 assertEquals(test.scopeMatch, 1); 126 assertEquals(test.scope, IPV6_ADDR_SCOPE_LINKLOCAL); 127 assertEquals(test.label, 4); 128 assertEquals(test.precedence, 35); 129 } 130 131 @Test testV6SortableAddress()132 public void testV6SortableAddress() { 133 // Test global address 134 DnsUtils.SortableAddress test = makeSortableAddress("2404:6800:4008:801::2004"); 135 assertEquals(test.address, stringToAddress("2404:6800:4008:801::2004")); 136 assertEquals(test.scope, IPV6_ADDR_SCOPE_GLOBAL); 137 assertEquals(test.label, 1); 138 assertEquals(test.precedence, 40); 139 140 // Test global address with global source address 141 test = makeSortableAddress("2404:6800:4008:801::2004", 142 "2401:fa00:fc:fd00:6d6c:7199:b8e7:41d6"); 143 assertEquals(test.address, stringToAddress("2404:6800:4008:801::2004")); 144 assertEquals(test.hasSrcAddr, 1); 145 assertEquals(test.scope, IPV6_ADDR_SCOPE_GLOBAL); 146 assertEquals(test.labelMatch, 1); 147 assertEquals(test.scopeMatch, 1); 148 assertEquals(test.label, 1); 149 assertEquals(test.precedence, 40); 150 assertEquals(test.prefixMatchLen, 13); 151 152 // Test global address with linklocal source address 153 test = makeSortableAddress("2404:6800:4008:801::2004", "fe80::c46f:1cff:fe04:39b4"); 154 assertEquals(test.hasSrcAddr, 1); 155 assertEquals(test.scope, IPV6_ADDR_SCOPE_GLOBAL); 156 assertEquals(test.labelMatch, 1); 157 assertEquals(test.scopeMatch, 0); 158 assertEquals(test.label, 1); 159 assertEquals(test.precedence, 40); 160 assertEquals(test.prefixMatchLen, 0); 161 162 // Test loopback address with the same source address 163 test = makeSortableAddress("::1", "::1"); 164 assertEquals(test.hasSrcAddr, 1); 165 assertEquals(test.prefixMatchLen, 16 * 8); 166 assertEquals(test.labelMatch, 1); 167 assertEquals(test.scopeMatch, 1); 168 assertEquals(test.scope, IPV6_ADDR_SCOPE_LINKLOCAL); 169 assertEquals(test.label, 0); 170 assertEquals(test.precedence, 50); 171 172 // Test linklocal address 173 test = makeSortableAddress("fe80::c46f:1cff:fe04:39b4"); 174 assertEquals(test.scope, IPV6_ADDR_SCOPE_LINKLOCAL); 175 assertEquals(test.label, 1); 176 assertEquals(test.precedence, 40); 177 178 // Test linklocal address 179 test = makeSortableAddress("fe80::"); 180 assertEquals(test.scope, IPV6_ADDR_SCOPE_LINKLOCAL); 181 assertEquals(test.label, 1); 182 assertEquals(test.precedence, 40); 183 184 // Test 6to4 address 185 test = makeSortableAddress("2002:c000:0204::"); 186 assertEquals(test.scope, IPV6_ADDR_SCOPE_GLOBAL); 187 assertEquals(test.label, 2); 188 assertEquals(test.precedence, 30); 189 190 // Test unique local address 191 test = makeSortableAddress("fc00::c000:13ab"); 192 assertEquals(test.scope, IPV6_ADDR_SCOPE_GLOBAL); 193 assertEquals(test.label, 13); 194 assertEquals(test.precedence, 3); 195 196 // Test teredo tunneling address 197 test = makeSortableAddress("2001::47c1"); 198 assertEquals(test.scope, IPV6_ADDR_SCOPE_GLOBAL); 199 assertEquals(test.label, 5); 200 assertEquals(test.precedence, 5); 201 202 // Test IPv4-compatible addresses 203 test = makeSortableAddress("::216.58.200.36"); 204 assertEquals(test.scope, IPV6_ADDR_SCOPE_GLOBAL); 205 assertEquals(test.label, 3); 206 assertEquals(test.precedence, 1); 207 208 // Test site-local address 209 test = makeSortableAddress("fec0::cafe:3ab2"); 210 assertEquals(test.scope, IPV6_ADDR_SCOPE_SITELOCAL); 211 assertEquals(test.label, 11); 212 assertEquals(test.precedence, 1); 213 214 // Test 6bone address 215 test = makeSortableAddress("3ffe::1234:5678"); 216 assertEquals(test.scope, IPV6_ADDR_SCOPE_GLOBAL); 217 assertEquals(test.label, 12); 218 assertEquals(test.precedence, 1); 219 } 220 } 221