derp_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package key
  4. import (
  5. "errors"
  6. "testing"
  7. "go4.org/mem"
  8. )
  9. func TestDERPMeshIsValid(t *testing.T) {
  10. for name, tt := range map[string]struct {
  11. input string
  12. want string
  13. wantErr error
  14. }{
  15. "good": {
  16. input: "0123456789012345678901234567890123456789012345678901234567890123",
  17. want: "0123456789012345678901234567890123456789012345678901234567890123",
  18. wantErr: nil,
  19. },
  20. "hex": {
  21. input: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
  22. want: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
  23. wantErr: nil,
  24. },
  25. "uppercase": {
  26. input: "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF",
  27. want: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
  28. wantErr: nil,
  29. },
  30. "whitespace": {
  31. input: " 0123456789012345678901234567890123456789012345678901234567890123 ",
  32. want: "0123456789012345678901234567890123456789012345678901234567890123",
  33. wantErr: nil,
  34. },
  35. "short": {
  36. input: "0123456789abcdef",
  37. wantErr: ErrInvalidMeshKey,
  38. },
  39. "long": {
  40. input: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0",
  41. wantErr: ErrInvalidMeshKey,
  42. },
  43. } {
  44. t.Run(name, func(t *testing.T) {
  45. k, err := ParseDERPMesh(tt.input)
  46. if !errors.Is(err, tt.wantErr) {
  47. t.Errorf("err %v, want %v", err, tt.wantErr)
  48. }
  49. got := k.String()
  50. if got != tt.want && tt.wantErr == nil {
  51. t.Errorf("got %q, want %q", got, tt.want)
  52. }
  53. })
  54. }
  55. }
  56. func TestDERPMesh(t *testing.T) {
  57. t.Parallel()
  58. for name, tt := range map[string]struct {
  59. str string
  60. hex []byte
  61. equal bool // are str and hex equal?
  62. }{
  63. "zero": {
  64. str: "0000000000000000000000000000000000000000000000000000000000000000",
  65. hex: []byte{
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. },
  71. equal: true,
  72. },
  73. "equal": {
  74. str: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
  75. hex: []byte{
  76. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  77. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  78. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  79. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  80. },
  81. equal: true,
  82. },
  83. "unequal": {
  84. str: "0badc0de00000000000000000000000000000000000000000000000000000000",
  85. hex: []byte{
  86. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  90. },
  91. equal: false,
  92. },
  93. } {
  94. t.Run(name, func(t *testing.T) {
  95. t.Parallel()
  96. k, err := ParseDERPMesh(tt.str)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. // string representation should round-trip
  101. s := k.String()
  102. if s != tt.str {
  103. t.Fatalf("string %s, want %s", s, tt.str)
  104. }
  105. // if tt.equal, then tt.hex is intended to be equal
  106. if k.k != [32]byte(tt.hex) && tt.equal {
  107. t.Fatalf("decoded %x, want %x", k.k, tt.hex)
  108. }
  109. h := DERPMeshFromRaw32(mem.B(tt.hex))
  110. if k.Equal(h) != tt.equal {
  111. if tt.equal {
  112. t.Fatalf("%v != %v", k, h)
  113. } else {
  114. t.Fatalf("%v == %v", k, h)
  115. }
  116. }
  117. })
  118. }
  119. }