1/* 2 * Copyright (C) 2016 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 com.android.server.wifi.hotspot2; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertFalse; 21import static org.junit.Assert.assertTrue; 22 23import android.support.test.filters.SmallTest; 24import android.util.Pair; 25 26import org.junit.Test; 27 28import java.util.Arrays; 29import java.util.HashMap; 30import java.util.Map; 31 32/** 33 * Unit tests for {@link com.android.server.wifi.hotspot2.DomainMatcher}. 34 */ 35@SmallTest 36public class DomainMatcherTest { 37 private static final String PRIMARY_DOMAIN = "google.com"; 38 private static final String SECONDARY_DOMAIN1 = "android.com"; 39 private static final String SECONDARY_DOMAIN2 = "testing.test.com"; 40 41 /** 42 * Test data for isSubDomain function. 43 */ 44 private static final Map<String, Integer> TEST_DOMAIN_MAP = new HashMap<>(); 45 static { 46 TEST_DOMAIN_MAP.put("", DomainMatcher.MATCH_NONE); 47 TEST_DOMAIN_MAP.put("com", DomainMatcher.MATCH_NONE); 48 TEST_DOMAIN_MAP.put("test.com", DomainMatcher.MATCH_NONE); 49 TEST_DOMAIN_MAP.put("google.com", DomainMatcher.MATCH_PRIMARY); 50 TEST_DOMAIN_MAP.put("test.google.com", DomainMatcher.MATCH_PRIMARY); 51 TEST_DOMAIN_MAP.put("android.com", DomainMatcher.MATCH_SECONDARY); 52 TEST_DOMAIN_MAP.put("test.android.com", DomainMatcher.MATCH_SECONDARY); 53 TEST_DOMAIN_MAP.put("testing.test.com", DomainMatcher.MATCH_SECONDARY); 54 TEST_DOMAIN_MAP.put("adbcd.testing.test.com", DomainMatcher.MATCH_SECONDARY); 55 } 56 57 /** 58 * Test data for arg2SubdomainOfArg1 function. 59 */ 60 private static final Map<Pair<String, String>, Boolean> TEST_ARG_DOMAIN_MAP = new HashMap<>(); 61 static { 62 TEST_ARG_DOMAIN_MAP.put(new Pair<String, String>("test.com", "abc.test.com"), true); 63 TEST_ARG_DOMAIN_MAP.put(new Pair<String, String>("test.com", "ad.abc.test.com"), true); 64 TEST_ARG_DOMAIN_MAP.put(new Pair<String, String>("com", "test.com"), true); 65 TEST_ARG_DOMAIN_MAP.put(new Pair<String, String>("abc.test.com", "test.com"), false); 66 TEST_ARG_DOMAIN_MAP.put(new Pair<String, String>("test1.com", "test.com"), false); 67 TEST_ARG_DOMAIN_MAP.put(new Pair<String, String>("test.com", "com"), false); 68 } 69 70 /** 71 * Verify that creating a matcher with empty domains doesn't cause any exceptions. 72 * 73 * @throws Exception 74 */ 75 @Test 76 public void createMatcherWithEmptyDomains() throws Exception { 77 DomainMatcher domainMatcher = new DomainMatcher(null, null); 78 assertEquals(DomainMatcher.MATCH_NONE, domainMatcher.isSubDomain("google.com")); 79 } 80 81 /** 82 * Verify that matching a null domain doesn't cause any exceptions. 83 * 84 * @throws Exception 85 */ 86 @Test 87 public void matchNullDomain() throws Exception { 88 DomainMatcher domainMatcher = new DomainMatcher(PRIMARY_DOMAIN, 89 Arrays.asList(SECONDARY_DOMAIN1, SECONDARY_DOMAIN2)); 90 assertEquals(DomainMatcher.MATCH_NONE, domainMatcher.isSubDomain(null)); 91 } 92 93 /** 94 * Verify the domain matching expectations based on the predefined {@link #TEST_DOMAIN_MAP}. 95 * 96 * @throws Exception 97 */ 98 @Test 99 public void matchTestDomains() throws Exception { 100 DomainMatcher domainMatcher = new DomainMatcher(PRIMARY_DOMAIN, 101 Arrays.asList(SECONDARY_DOMAIN1, SECONDARY_DOMAIN2)); 102 for (Map.Entry<String, Integer> entry : TEST_DOMAIN_MAP.entrySet()) { 103 assertEquals(entry.getValue().intValue(), domainMatcher.isSubDomain(entry.getKey())); 104 } 105 } 106 107 /** 108 * Verify that the correct match status is returned when a domain matches both primary 109 * and secondary domain (primary domain have precedence over secondary). 110 * 111 * @throws Exception 112 */ 113 @Test 114 public void matchDomainWithBothPrimaryAndSecondary() throws Exception { 115 DomainMatcher domainMatcher = new DomainMatcher(PRIMARY_DOMAIN, 116 Arrays.asList(PRIMARY_DOMAIN)); 117 assertEquals(DomainMatcher.MATCH_PRIMARY, domainMatcher.isSubDomain(PRIMARY_DOMAIN)); 118 } 119 120 /** 121 * Verify domain matching expectation when the secondary domain is a sub-domain of the 122 * primary domain. 123 * 124 * @throws Exception 125 */ 126 @Test 127 public void matchDomainWhenSecondaryIsSubdomainOfPrimary() throws Exception { 128 DomainMatcher domainMatcher = new DomainMatcher("google.com", 129 Arrays.asList("test.google.com")); 130 assertEquals(DomainMatcher.MATCH_PRIMARY, domainMatcher.isSubDomain("google.com")); 131 assertEquals(DomainMatcher.MATCH_PRIMARY, domainMatcher.isSubDomain("test.google.com")); 132 assertEquals(DomainMatcher.MATCH_PRIMARY, 133 domainMatcher.isSubDomain("abcd.test.google.com")); 134 } 135 136 /** 137 * Verify domain matching expectations when the secondary domain is a sub-domain of the 138 * primary domain. 139 * 140 * @throws Exception 141 */ 142 @Test 143 public void matchDomainWhenPrimaryIsSubdomainOfSecondary() throws Exception { 144 DomainMatcher domainMatcher = new DomainMatcher("test.google.com", 145 Arrays.asList("google.com")); 146 assertEquals(DomainMatcher.MATCH_SECONDARY, domainMatcher.isSubDomain("google.com")); 147 assertEquals(DomainMatcher.MATCH_SECONDARY, domainMatcher.isSubDomain("test2.google.com")); 148 assertEquals(DomainMatcher.MATCH_PRIMARY, domainMatcher.isSubDomain("test.google.com")); 149 assertEquals(DomainMatcher.MATCH_PRIMARY, 150 domainMatcher.isSubDomain("adcd.test.google.com")); 151 } 152 153 /** 154 * Verify domain matching expectations when the domain names contained empty label (domain 155 * name that contained ".."). 156 * 157 * @throws Exception 158 */ 159 @Test 160 public void matchDomainWithEmptyLabel() throws Exception { 161 DomainMatcher domainMatcher = new DomainMatcher("test.google..com", 162 Arrays.asList("google..com")); 163 assertEquals(DomainMatcher.MATCH_PRIMARY, domainMatcher.isSubDomain("test.google..com")); 164 assertEquals(DomainMatcher.MATCH_SECONDARY, domainMatcher.isSubDomain("google..com")); 165 } 166 167 /** 168 * Verify domain matching expectation for arg2SubdomainOfArg1 based on predefined 169 * {@link #TEST_ARG_DOMAIN_MAP}. 170 * 171 * @throws Exception 172 */ 173 @Test 174 public void verifyArg2SubdomainOfArg1() throws Exception { 175 for (Map.Entry<Pair<String, String>, Boolean> entry : TEST_ARG_DOMAIN_MAP.entrySet()) { 176 assertEquals(entry.getValue().booleanValue(), 177 DomainMatcher.arg2SubdomainOfArg1(entry.getKey().first, entry.getKey().second)); 178 } 179 } 180 181 /** 182 * Verify that arg2SubdomainOfArg1 works as expected when pass in null domains. 183 * 184 * @throws Exception 185 */ 186 @Test 187 public void arg2SubdomainOfArg1WithNullDomain() throws Exception { 188 assertFalse(DomainMatcher.arg2SubdomainOfArg1(null, "test.com")); 189 assertFalse(DomainMatcher.arg2SubdomainOfArg1("test.com", null)); 190 assertFalse(DomainMatcher.arg2SubdomainOfArg1(null, null)); 191 } 192 193 /** 194 * Verify that arg2SubdomainOfArg1 works as expected when domain contains empty label. 195 * 196 * @throws Exception 197 */ 198 @Test 199 public void arg2SubdomainOfArg1WithEmptyLabel() throws Exception { 200 assertTrue(DomainMatcher.arg2SubdomainOfArg1("test..com", "adsf.test..com")); 201 } 202 203} 204