| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math/rand" |
| 6 |
"os" |
| 7 |
"os/exec" |
| 8 |
"path/filepath" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/gorilla/websocket" |
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
"heckel.io/ntfy/v2/client" |
| 16 |
"heckel.io/ntfy/v2/test" |
| 17 |
"heckel.io/ntfy/v2/user" |
| 18 |
"heckel.io/ntfy/v2/util" |
| 19 |
) |
| 20 |
|
| 21 |
func TestParseUsers_Success(t *testing.T) { |
| 22 |
tests := []struct { |
| 23 |
name string |
| 24 |
input []string |
| 25 |
expected []*user.User |
| 26 |
}{ |
| 27 |
{ |
| 28 |
name: "single user", |
| 29 |
input: []string{"alice:$2a$10$320YlQeaMghYZsvtu9jzfOQZS32FysWY/T9qu5NWqcIh.DN.u5P5S:user"}, |
| 30 |
expected: []*user.User{ |
| 31 |
{ |
| 32 |
Name: "alice", |
| 33 |
Hash: "$2a$10$320YlQeaMghYZsvtu9jzfOQZS32FysWY/T9qu5NWqcIh.DN.u5P5S", |
| 34 |
Role: user.RoleUser, |
| 35 |
Provisioned: true, |
| 36 |
}, |
| 37 |
}, |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "multiple users with different roles", |
| 41 |
input: []string{ |
| 42 |
"alice:$2a$10$320YlQeaMghYZsvtu9jzfOQZS32FysWY/T9qu5NWqcIh.DN.u5P5S:user", |
| 43 |
"bob:$2a$10$jIcuBWcbxd6oW1aPvoJ5iOShzu3/UJ2kSxKbTZtDypG06nBflQagq:admin", |
| 44 |
}, |
| 45 |
expected: []*user.User{ |
| 46 |
{ |
| 47 |
Name: "alice", |
| 48 |
Hash: "$2a$10$320YlQeaMghYZsvtu9jzfOQZS32FysWY/T9qu5NWqcIh.DN.u5P5S", |
| 49 |
Role: user.RoleUser, |
| 50 |
Provisioned: true, |
| 51 |
}, |
| 52 |
{ |
| 53 |
Name: "bob", |
| 54 |
Hash: "$2a$10$jIcuBWcbxd6oW1aPvoJ5iOShzu3/UJ2kSxKbTZtDypG06nBflQagq", |
| 55 |
Role: user.RoleAdmin, |
| 56 |
Provisioned: true, |
| 57 |
}, |
| 58 |
}, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "empty input", |
| 62 |
input: []string{}, |
| 63 |
expected: []*user.User{}, |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "user with special characters in name", |
| 67 |
input: []string{"[email protected]:$2a$10$RYUYAsl5zOnAIp6fH7BPX.Eug0rUfEUk92r8WiVusb0VK.vGojWBe:user"}, |
| 68 |
expected: []*user.User{ |
| 69 |
{ |
| 70 |
Name: "[email protected]", |
| 71 |
Hash: "$2a$10$RYUYAsl5zOnAIp6fH7BPX.Eug0rUfEUk92r8WiVusb0VK.vGojWBe", |
| 72 |
Role: user.RoleUser, |
| 73 |
Provisioned: true, |
| 74 |
}, |
| 75 |
}, |
| 76 |
}, |
| 77 |
} |
| 78 |
|
| 79 |
for _, tt := range tests { |
| 80 |
t.Run(tt.name, func(t *testing.T) { |
| 81 |
result, err := parseUsers(tt.input) |
| 82 |
require.NoError(t, err) |
| 83 |
require.Len(t, result, len(tt.expected)) |
| 84 |
|
| 85 |
for i, expectedUser := range tt.expected { |
| 86 |
assert.Equal(t, expectedUser.Name, result[i].Name) |
| 87 |
assert.Equal(t, expectedUser.Hash, result[i].Hash) |
| 88 |
assert.Equal(t, expectedUser.Role, result[i].Role) |
| 89 |
assert.Equal(t, expectedUser.Provisioned, result[i].Provisioned) |
| 90 |
} |
| 91 |
}) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func TestParseUsers_Errors(t *testing.T) { |
| 96 |
tests := []struct { |
| 97 |
name string |
| 98 |
input []string |
| 99 |
error string |
| 100 |
}{ |
| 101 |
{ |
| 102 |
name: "invalid format - too few parts", |
| 103 |
input: []string{"alice:hash"}, |
| 104 |
error: "invalid auth-users: alice:hash, expected format: 'name:hash:role'", |
| 105 |
}, |
| 106 |
{ |
| 107 |
name: "invalid format - too many parts", |
| 108 |
input: []string{"alice:hash:role:extra"}, |
| 109 |
error: "invalid auth-users: alice:hash:role:extra, expected format: 'name:hash:role'", |
| 110 |
}, |
| 111 |
{ |
| 112 |
name: "invalid username", |
| 113 |
input: []string{"alice@#$%:$2a$10$320YlQeaMghYZsvtu9jzfOQZS32FysWY/T9qu5NWqcIh.DN.u5P5S:user"}, |
| 114 |
error: "invalid auth-users: alice@#$%:$2a$10$320YlQeaMghYZsvtu9jzfOQZS32FysWY/T9qu5NWqcIh.DN.u5P5S:user, username invalid", |
| 115 |
}, |
| 116 |
{ |
| 117 |
name: "invalid password hash - wrong prefix", |
| 118 |
input: []string{"alice:plaintext:user"}, |
| 119 |
error: "invalid auth-users: alice:plaintext:user, password hash invalid, password hash must be a bcrypt hash, use 'ntfy user hash' to generate", |
| 120 |
}, |
| 121 |
{ |
| 122 |
name: "invalid role", |
| 123 |
input: []string{"alice:$2a$10$320YlQeaMghYZsvtu9jzfOQZS32FysWY/T9qu5NWqcIh.DN.u5P5S:invalid"}, |
| 124 |
error: "invalid auth-users: alice:$2a$10$320YlQeaMghYZsvtu9jzfOQZS32FysWY/T9qu5NWqcIh.DN.u5P5S:invalid, role invalid is not allowed, allowed roles are 'admin' or 'user'", |
| 125 |
}, |
| 126 |
{ |
| 127 |
name: "empty username", |
| 128 |
input: []string{":$2a$10$320YlQeaMghYZsvtu9jzfOQZS32FysWY/T9qu5NWqcIh.DN.u5P5S:user"}, |
| 129 |
error: "invalid auth-users: :$2a$10$320YlQeaMghYZsvtu9jzfOQZS32FysWY/T9qu5NWqcIh.DN.u5P5S:user, username invalid", |
| 130 |
}, |
| 131 |
} |
| 132 |
|
| 133 |
for _, tt := range tests { |
| 134 |
t.Run(tt.name, func(t *testing.T) { |
| 135 |
result, err := parseUsers(tt.input) |
| 136 |
require.Error(t, err) |
| 137 |
require.Nil(t, result) |
| 138 |
assert.Contains(t, err.Error(), tt.error) |
| 139 |
}) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestParseAccess_Success(t *testing.T) { |
| 144 |
users := []*user.User{ |
| 145 |
{Name: "alice", Role: user.RoleUser}, |
| 146 |
{Name: "bob", Role: user.RoleUser}, |
| 147 |
} |
| 148 |
|
| 149 |
tests := []struct { |
| 150 |
name string |
| 151 |
users []*user.User |
| 152 |
input []string |
| 153 |
expected map[string][]*user.Grant |
| 154 |
}{ |
| 155 |
{ |
| 156 |
name: "single access entry", |
| 157 |
users: users, |
| 158 |
input: []string{"alice:mytopic:read-write"}, |
| 159 |
expected: map[string][]*user.Grant{ |
| 160 |
"alice": { |
| 161 |
{ |
| 162 |
TopicPattern: "mytopic", |
| 163 |
Permission: user.PermissionReadWrite, |
| 164 |
Provisioned: true, |
| 165 |
}, |
| 166 |
}, |
| 167 |
}, |
| 168 |
}, |
| 169 |
{ |
| 170 |
name: "multiple access entries for same user", |
| 171 |
users: users, |
| 172 |
input: []string{ |
| 173 |
"alice:topic1:read-only", |
| 174 |
"alice:topic2:write-only", |
| 175 |
}, |
| 176 |
expected: map[string][]*user.Grant{ |
| 177 |
"alice": { |
| 178 |
{ |
| 179 |
TopicPattern: "topic1", |
| 180 |
Permission: user.PermissionRead, |
| 181 |
Provisioned: true, |
| 182 |
}, |
| 183 |
{ |
| 184 |
TopicPattern: "topic2", |
| 185 |
Permission: user.PermissionWrite, |
| 186 |
Provisioned: true, |
| 187 |
}, |
| 188 |
}, |
| 189 |
}, |
| 190 |
}, |
| 191 |
{ |
| 192 |
name: "access for everyone", |
| 193 |
users: users, |
| 194 |
input: []string{"everyone:publictopic:read-only"}, |
| 195 |
expected: map[string][]*user.Grant{ |
| 196 |
user.Everyone: { |
| 197 |
{ |
| 198 |
TopicPattern: "publictopic", |
| 199 |
Permission: user.PermissionRead, |
| 200 |
Provisioned: true, |
| 201 |
}, |
| 202 |
}, |
| 203 |
}, |
| 204 |
}, |
| 205 |
{ |
| 206 |
name: "wildcard topic pattern", |
| 207 |
users: users, |
| 208 |
input: []string{"alice:topic*:read-write"}, |
| 209 |
expected: map[string][]*user.Grant{ |
| 210 |
"alice": { |
| 211 |
{ |
| 212 |
TopicPattern: "topic*", |
| 213 |
Permission: user.PermissionReadWrite, |
| 214 |
Provisioned: true, |
| 215 |
}, |
| 216 |
}, |
| 217 |
}, |
| 218 |
}, |
| 219 |
{ |
| 220 |
name: "empty input", |
| 221 |
users: users, |
| 222 |
input: []string{}, |
| 223 |
expected: map[string][]*user.Grant{}, |
| 224 |
}, |
| 225 |
{ |
| 226 |
name: "deny-all permission", |
| 227 |
users: users, |
| 228 |
input: []string{"alice:secretopic:deny-all"}, |
| 229 |
expected: map[string][]*user.Grant{ |
| 230 |
"alice": { |
| 231 |
{ |
| 232 |
TopicPattern: "secretopic", |
| 233 |
Permission: user.PermissionDenyAll, |
| 234 |
Provisioned: true, |
| 235 |
}, |
| 236 |
}, |
| 237 |
}, |
| 238 |
}, |
| 239 |
} |
| 240 |
|
| 241 |
for _, tt := range tests { |
| 242 |
t.Run(tt.name, func(t *testing.T) { |
| 243 |
result, err := parseAccess(tt.users, tt.input) |
| 244 |
require.NoError(t, err) |
| 245 |
assert.Equal(t, tt.expected, result) |
| 246 |
}) |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
func TestParseAccess_Errors(t *testing.T) { |
| 251 |
users := []*user.User{ |
| 252 |
{Name: "alice", Role: user.RoleUser}, |
| 253 |
{Name: "admin", Role: user.RoleAdmin}, |
| 254 |
} |
| 255 |
|
| 256 |
tests := []struct { |
| 257 |
name string |
| 258 |
users []*user.User |
| 259 |
input []string |
| 260 |
error string |
| 261 |
}{ |
| 262 |
{ |
| 263 |
name: "invalid format - too few parts", |
| 264 |
users: users, |
| 265 |
input: []string{"alice:topic"}, |
| 266 |
error: "invalid auth-access: alice:topic, expected format: 'user:topic:permission'", |
| 267 |
}, |
| 268 |
{ |
| 269 |
name: "invalid format - too many parts", |
| 270 |
users: users, |
| 271 |
input: []string{"alice:topic:read:extra"}, |
| 272 |
error: "invalid auth-access: alice:topic:read:extra, expected format: 'user:topic:permission'", |
| 273 |
}, |
| 274 |
{ |
| 275 |
name: "user not provisioned", |
| 276 |
users: users, |
| 277 |
input: []string{"charlie:topic:read"}, |
| 278 |
error: "invalid auth-access: charlie:topic:read, user charlie is not provisioned", |
| 279 |
}, |
| 280 |
{ |
| 281 |
name: "admin user cannot have ACL entries", |
| 282 |
users: users, |
| 283 |
input: []string{"admin:topic:read"}, |
| 284 |
error: "invalid auth-access: admin:topic:read, user admin is not a regular user, only regular users can have ACL entries", |
| 285 |
}, |
| 286 |
{ |
| 287 |
name: "invalid topic pattern", |
| 288 |
users: users, |
| 289 |
input: []string{"alice:topic-with-invalid-chars!:read"}, |
| 290 |
error: "invalid auth-access: alice:topic-with-invalid-chars!:read, topic pattern topic-with-invalid-chars! invalid", |
| 291 |
}, |
| 292 |
{ |
| 293 |
name: "invalid permission", |
| 294 |
users: users, |
| 295 |
input: []string{"alice:topic:invalid-permission"}, |
| 296 |
error: "invalid auth-access: alice:topic:invalid-permission, permission invalid-permission invalid", |
| 297 |
}, |
| 298 |
} |
| 299 |
|
| 300 |
for _, tt := range tests { |
| 301 |
t.Run(tt.name, func(t *testing.T) { |
| 302 |
result, err := parseAccess(tt.users, tt.input) |
| 303 |
require.Error(t, err) |
| 304 |
require.Nil(t, result) |
| 305 |
assert.Contains(t, err.Error(), tt.error) |
| 306 |
}) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
func TestParseTokens_Success(t *testing.T) { |
| 311 |
users := []*user.User{ |
| 312 |
{Name: "alice"}, |
| 313 |
{Name: "bob"}, |
| 314 |
} |
| 315 |
|
| 316 |
tests := []struct { |
| 317 |
name string |
| 318 |
users []*user.User |
| 319 |
input []string |
| 320 |
expected map[string][]*user.Token |
| 321 |
}{ |
| 322 |
{ |
| 323 |
name: "single token without label", |
| 324 |
users: users, |
| 325 |
input: []string{"alice:tk_abcdefghijklmnopqrstuvwxyz123"}, |
| 326 |
expected: map[string][]*user.Token{ |
| 327 |
"alice": { |
| 328 |
{ |
| 329 |
Value: "tk_abcdefghijklmnopqrstuvwxyz123", |
| 330 |
Label: "", |
| 331 |
Provisioned: true, |
| 332 |
}, |
| 333 |
}, |
| 334 |
}, |
| 335 |
}, |
| 336 |
{ |
| 337 |
name: "single token with label", |
| 338 |
users: users, |
| 339 |
input: []string{"alice:tk_abcdefghijklmnopqrstuvwxyz123:My Phone"}, |
| 340 |
expected: map[string][]*user.Token{ |
| 341 |
"alice": { |
| 342 |
{ |
| 343 |
Value: "tk_abcdefghijklmnopqrstuvwxyz123", |
| 344 |
Label: "My Phone", |
| 345 |
Provisioned: true, |
| 346 |
}, |
| 347 |
}, |
| 348 |
}, |
| 349 |
}, |
| 350 |
{ |
| 351 |
name: "multiple tokens for same user", |
| 352 |
users: users, |
| 353 |
input: []string{ |
| 354 |
"alice:tk_abcdefghijklmnopqrstuvwxyz123:Phone", |
| 355 |
"alice:tk_zyxwvutsrqponmlkjihgfedcba987:Laptop", |
| 356 |
}, |
| 357 |
expected: map[string][]*user.Token{ |
| 358 |
"alice": { |
| 359 |
{ |
| 360 |
Value: "tk_abcdefghijklmnopqrstuvwxyz123", |
| 361 |
Label: "Phone", |
| 362 |
Provisioned: true, |
| 363 |
}, |
| 364 |
{ |
| 365 |
Value: "tk_zyxwvutsrqponmlkjihgfedcba987", |
| 366 |
Label: "Laptop", |
| 367 |
Provisioned: true, |
| 368 |
}, |
| 369 |
}, |
| 370 |
}, |
| 371 |
}, |
| 372 |
{ |
| 373 |
name: "tokens for multiple users", |
| 374 |
users: users, |
| 375 |
input: []string{ |
| 376 |
"alice:tk_abcdefghijklmnopqrstuvwxyz123:Phone", |
| 377 |
"bob:tk_zyxwvutsrqponmlkjihgfedcba987:Tablet", |
| 378 |
}, |
| 379 |
expected: map[string][]*user.Token{ |
| 380 |
"alice": { |
| 381 |
{ |
| 382 |
Value: "tk_abcdefghijklmnopqrstuvwxyz123", |
| 383 |
Label: "Phone", |
| 384 |
Provisioned: true, |
| 385 |
}, |
| 386 |
}, |
| 387 |
"bob": { |
| 388 |
{ |
| 389 |
Value: "tk_zyxwvutsrqponmlkjihgfedcba987", |
| 390 |
Label: "Tablet", |
| 391 |
Provisioned: true, |
| 392 |
}, |
| 393 |
}, |
| 394 |
}, |
| 395 |
}, |
| 396 |
{ |
| 397 |
name: "empty input", |
| 398 |
users: users, |
| 399 |
input: []string{}, |
| 400 |
expected: map[string][]*user.Token{}, |
| 401 |
}, |
| 402 |
} |
| 403 |
|
| 404 |
for _, tt := range tests { |
| 405 |
t.Run(tt.name, func(t *testing.T) { |
| 406 |
result, err := parseTokens(tt.users, tt.input) |
| 407 |
require.NoError(t, err) |
| 408 |
assert.Equal(t, tt.expected, result) |
| 409 |
}) |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
func TestParseTokens_Errors(t *testing.T) { |
| 414 |
users := []*user.User{ |
| 415 |
{Name: "alice"}, |
| 416 |
} |
| 417 |
|
| 418 |
tests := []struct { |
| 419 |
name string |
| 420 |
users []*user.User |
| 421 |
input []string |
| 422 |
error string |
| 423 |
}{ |
| 424 |
{ |
| 425 |
name: "invalid format - too few parts", |
| 426 |
users: users, |
| 427 |
input: []string{"alice"}, |
| 428 |
error: "invalid auth-tokens: alice, expected format: 'user:token[:label]'", |
| 429 |
}, |
| 430 |
{ |
| 431 |
name: "invalid format - too many parts", |
| 432 |
users: users, |
| 433 |
input: []string{"alice:token:label:extra:parts"}, |
| 434 |
error: "invalid auth-tokens: alice:token:label:extra:parts, expected format: 'user:token[:label]'", |
| 435 |
}, |
| 436 |
{ |
| 437 |
name: "user not provisioned", |
| 438 |
users: users, |
| 439 |
input: []string{"charlie:tk_abcdefghijklmnopqrstuvwxyz123"}, |
| 440 |
error: "invalid auth-tokens: charlie:tk_abcdefghijklmnopqrstuvwxyz123, user charlie is not provisioned", |
| 441 |
}, |
| 442 |
{ |
| 443 |
name: "invalid token format", |
| 444 |
users: users, |
| 445 |
input: []string{"alice:invalid-token"}, |
| 446 |
error: "invalid auth-tokens: alice:invalid-token, token invalid-token invalid, use 'ntfy token generate' to generate a random token", |
| 447 |
}, |
| 448 |
{ |
| 449 |
name: "token too short", |
| 450 |
users: users, |
| 451 |
input: []string{"alice:tk_short"}, |
| 452 |
error: "invalid auth-tokens: alice:tk_short, token tk_short invalid, use 'ntfy token generate' to generate a random token", |
| 453 |
}, |
| 454 |
{ |
| 455 |
name: "token without prefix", |
| 456 |
users: users, |
| 457 |
input: []string{"alice:abcdefghijklmnopqrstuvwxyz12345"}, |
| 458 |
error: "invalid auth-tokens: alice:abcdefghijklmnopqrstuvwxyz12345, token abcdefghijklmnopqrstuvwxyz12345 invalid, use 'ntfy token generate' to generate a random token", |
| 459 |
}, |
| 460 |
} |
| 461 |
|
| 462 |
for _, tt := range tests { |
| 463 |
t.Run(tt.name, func(t *testing.T) { |
| 464 |
result, err := parseTokens(tt.users, tt.input) |
| 465 |
require.Error(t, err) |
| 466 |
require.Nil(t, result) |
| 467 |
assert.Contains(t, err.Error(), tt.error) |
| 468 |
}) |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
func TestCLI_Serve_Unix_Curl(t *testing.T) { |
| 473 |
sockFile := filepath.Join(t.TempDir(), "ntfy.sock") |
| 474 |
configFile := newEmptyFile(t) // Avoid issues with existing server.yml file on system |
| 475 |
go func() { |
| 476 |
app, _, _, _ := newTestApp() |
| 477 |
err := app.Run([]string{"ntfy", "serve", "--config=" + configFile, "--listen-http=-", "--listen-unix=" + sockFile}) |
| 478 |
require.Nil(t, err) |
| 479 |
}() |
| 480 |
for i := 0; i < 40 && !util.FileExists(sockFile); i++ { |
| 481 |
time.Sleep(50 * time.Millisecond) |
| 482 |
} |
| 483 |
require.True(t, util.FileExists(sockFile)) |
| 484 |
|
| 485 |
cmd := exec.Command("curl", "-s", "--unix-socket", sockFile, "-d", "this is a message", "localhost/mytopic") |
| 486 |
out, err := cmd.Output() |
| 487 |
require.Nil(t, err) |
| 488 |
m := toMessage(t, string(out)) |
| 489 |
require.Equal(t, "this is a message", m.Message) |
| 490 |
} |
| 491 |
|
| 492 |
func TestCLI_Serve_WebSocket(t *testing.T) { |
| 493 |
port := 10000 + rand.Intn(20000) |
| 494 |
go func() { |
| 495 |
configFile := newEmptyFile(t) // Avoid issues with existing server.yml file on system |
| 496 |
app, _, _, _ := newTestApp() |
| 497 |
err := app.Run([]string{"ntfy", "serve", "--config=" + configFile, fmt.Sprintf("--listen-http=:%d", port)}) |
| 498 |
require.Nil(t, err) |
| 499 |
}() |
| 500 |
test.WaitForPortUp(t, port) |
| 501 |
|
| 502 |
ws, _, err := websocket.DefaultDialer.Dial(fmt.Sprintf("ws://127.0.0.1:%d/mytopic/ws", port), nil) |
| 503 |
require.Nil(t, err) |
| 504 |
|
| 505 |
messageType, data, err := ws.ReadMessage() |
| 506 |
require.Nil(t, err) |
| 507 |
require.Equal(t, websocket.TextMessage, messageType) |
| 508 |
require.Equal(t, "open", toMessage(t, string(data)).Event) |
| 509 |
|
| 510 |
c := client.New(client.NewConfig()) |
| 511 |
_, err = c.Publish(fmt.Sprintf("http://127.0.0.1:%d/mytopic", port), "my message") |
| 512 |
require.Nil(t, err) |
| 513 |
|
| 514 |
messageType, data, err = ws.ReadMessage() |
| 515 |
require.Nil(t, err) |
| 516 |
require.Equal(t, websocket.TextMessage, messageType) |
| 517 |
|
| 518 |
m := toMessage(t, string(data)) |
| 519 |
require.Equal(t, "my message", m.Message) |
| 520 |
require.Equal(t, "mytopic", m.Topic) |
| 521 |
} |
| 522 |
|
| 523 |
func TestIP_Host_Parsing(t *testing.T) { |
| 524 |
cases := map[string]string{ |
| 525 |
"1.1.1.1": "1.1.1.1/32", |
| 526 |
"fd00::1234": "fd00::1234/128", |
| 527 |
"192.168.0.3/24": "192.168.0.0/24", |
| 528 |
"10.1.2.3/8": "10.0.0.0/8", |
| 529 |
"201:be93::4a6/21": "201:b800::/21", |
| 530 |
} |
| 531 |
for q, expectedAnswer := range cases { |
| 532 |
ips, err := parseIPHostPrefix(q) |
| 533 |
require.Nil(t, err) |
| 534 |
assert.Equal(t, 1, len(ips)) |
| 535 |
assert.Equal(t, expectedAnswer, ips[0].String()) |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
func newEmptyFile(t *testing.T) string { |
| 540 |
filename := filepath.Join(t.TempDir(), "empty") |
| 541 |
require.Nil(t, os.WriteFile(filename, []byte{}, 0600)) |
| 542 |
return filename |
| 543 |
} |