asn_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package config
  2. import (
  3. "errors"
  4. "fmt"
  5. "testing"
  6. )
  7. func TestASNsValid(t *testing.T) {
  8. for _, tt := range []struct {
  9. err error
  10. input *ASNs
  11. name string
  12. }{
  13. {
  14. name: "basic valid",
  15. input: &ASNs{
  16. Match: []uint32{13335}, // Cloudflare
  17. },
  18. },
  19. {
  20. name: "private ASN",
  21. input: &ASNs{
  22. Match: []uint32{64513, 4206942069}, // 16 and 32 bit private ASN
  23. },
  24. err: ErrPrivateASN,
  25. },
  26. } {
  27. t.Run(tt.name, func(t *testing.T) {
  28. if err := tt.input.Valid(); !errors.Is(err, tt.err) {
  29. t.Logf("want: %v", tt.err)
  30. t.Logf("got: %v", err)
  31. t.Error("got wrong validation error")
  32. }
  33. })
  34. }
  35. }
  36. func TestIsPrivateASN(t *testing.T) {
  37. for _, tt := range []struct {
  38. input uint32
  39. output bool
  40. }{
  41. {13335, false}, // Cloudflare
  42. {64513, true}, // 16 bit private ASN
  43. {4206942069, true}, // 32 bit private ASN
  44. } {
  45. t.Run(fmt.Sprint(tt.input, "->", tt.output), func(t *testing.T) {
  46. result := isPrivateASN(tt.input)
  47. if result != tt.output {
  48. t.Errorf("wanted isPrivateASN(%d) == %v, got: %v", tt.input, tt.output, result)
  49. }
  50. })
  51. }
  52. }