github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/utils/domain/whitelist_test.go (about) 1 /* 2 * Copyright (C) 2021 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package domain 19 20 import ( 21 "testing" 22 ) 23 24 type testcase struct { 25 input string 26 result bool 27 } 28 29 func TestMatch(t *testing.T) { 30 l := []string{ 31 "localhost", 32 ".localhost", 33 ".localdomain", 34 } 35 wl := NewWhitelist(l) 36 37 testVector := []testcase{ 38 { 39 input: "localhost", 40 result: true, 41 }, 42 { 43 input: "localhost1", 44 result: false, 45 }, 46 { 47 input: "localhost.localhost", 48 result: true, 49 }, 50 { 51 input: "localdomain", 52 result: false, 53 }, 54 { 55 input: "localhost.localdomain", 56 result: true, 57 }, 58 { 59 input: " localhost ", 60 result: true, 61 }, 62 { 63 input: "example.org", 64 result: false, 65 }, 66 { 67 input: "localhost.", 68 result: true, 69 }, 70 { 71 input: "localhost1.", 72 result: false, 73 }, 74 { 75 input: "localhost.localhost.", 76 result: true, 77 }, 78 { 79 input: "localdomain.", 80 result: false, 81 }, 82 { 83 input: "localhost.localdomain.", 84 result: true, 85 }, 86 { 87 input: " localhost. ", 88 result: true, 89 }, 90 { 91 input: "example.org.", 92 result: false, 93 }, 94 } 95 96 for _, tc := range testVector { 97 res := wl.Match(tc.input) 98 if res != tc.result { 99 t.Errorf("Whitelist(%#v).Match(%#v) returned wrong result. Expected: %v. Got: %v.", 100 l, tc.input, tc.result, res) 101 } 102 } 103 } 104 105 func TestRootWildcard(t *testing.T) { 106 l := []string{"."} 107 wl := NewWhitelist(l) 108 testVector := []testcase{ 109 { 110 input: "localhost", 111 result: true, 112 }, 113 { 114 input: "localhost1", 115 result: true, 116 }, 117 { 118 input: "localhost.localhost", 119 result: true, 120 }, 121 { 122 input: "localdomain", 123 result: true, 124 }, 125 { 126 input: "localhost.localdomain", 127 result: true, 128 }, 129 { 130 input: " localhost ", 131 result: true, 132 }, 133 { 134 input: "example.org", 135 result: true, 136 }, 137 { 138 input: "localhost.", 139 result: true, 140 }, 141 { 142 input: "localhost1.", 143 result: true, 144 }, 145 { 146 input: "localhost.localhost.", 147 result: true, 148 }, 149 { 150 input: "localdomain.", 151 result: true, 152 }, 153 { 154 input: "localhost.localdomain.", 155 result: true, 156 }, 157 { 158 input: " localhost. ", 159 result: true, 160 }, 161 { 162 input: "example.org.", 163 result: true, 164 }, 165 { 166 input: ".", 167 result: false, 168 }, 169 { 170 input: "", 171 result: false, 172 }, 173 } 174 175 for _, tc := range testVector { 176 res := wl.Match(tc.input) 177 if res != tc.result { 178 t.Errorf("Whitelist(%#v).Match(%#v) returned wrong result. Expected: %v. Got: %v.", 179 l, tc.input, tc.result, res) 180 } 181 } 182 } 183 184 func TestRootExact(t *testing.T) { 185 l := []string{""} 186 wl := NewWhitelist(l) 187 testVector := []testcase{ 188 { 189 input: "localhost", 190 result: false, 191 }, 192 { 193 input: "localhost1", 194 result: false, 195 }, 196 { 197 input: "localhost.localhost", 198 result: false, 199 }, 200 { 201 input: "localdomain", 202 result: false, 203 }, 204 { 205 input: "localhost.localdomain", 206 result: false, 207 }, 208 { 209 input: " localhost ", 210 result: false, 211 }, 212 { 213 input: "example.org", 214 result: false, 215 }, 216 { 217 input: "localhost.", 218 result: false, 219 }, 220 { 221 input: "localhost1.", 222 result: false, 223 }, 224 { 225 input: "localhost.localhost.", 226 result: false, 227 }, 228 { 229 input: "localdomain.", 230 result: false, 231 }, 232 { 233 input: "localhost.localdomain.", 234 result: false, 235 }, 236 { 237 input: " localhost. ", 238 result: false, 239 }, 240 { 241 input: "example.org.", 242 result: false, 243 }, 244 { 245 input: ".", 246 result: true, 247 }, 248 { 249 input: "", 250 result: true, 251 }, 252 } 253 254 for _, tc := range testVector { 255 res := wl.Match(tc.input) 256 if res != tc.result { 257 t.Errorf("Whitelist(%#v).Match(%#v) returned wrong result. Expected: %v. Got: %v.", 258 l, tc.input, tc.result, res) 259 } 260 } 261 }