node_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package key
  4. import (
  5. "bufio"
  6. "bytes"
  7. "encoding/json"
  8. "strings"
  9. "testing"
  10. )
  11. func TestNodeKey(t *testing.T) {
  12. k := NewNode()
  13. if k.IsZero() {
  14. t.Fatal("NodePrivate should not be zero")
  15. }
  16. p := k.Public()
  17. if p.IsZero() {
  18. t.Fatal("NodePublic should not be zero")
  19. }
  20. bs, err := p.MarshalText()
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. if full, got := string(bs), ":"+p.UntypedHexString(); !strings.HasSuffix(full, got) {
  25. t.Fatalf("NodePublic.UntypedHexString is not a suffix of the typed serialization, got %q want suffix of %q", got, full)
  26. }
  27. bs, err = p.MarshalBinary()
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if got, want := bs, append([]byte(nodePublicBinaryPrefix), p.k[:]...); !bytes.Equal(got, want) {
  32. t.Fatalf("Binary-encoded NodePublic = %x, want %x", got, want)
  33. }
  34. var decoded NodePublic
  35. if err := decoded.UnmarshalBinary(bs); err != nil {
  36. t.Fatalf("NodePublic.UnmarshalBinary(%x) failed: %v", bs, err)
  37. }
  38. if decoded != p {
  39. t.Errorf("unmarshaled and original NodePublic differ:\noriginal = %v\ndecoded = %v", p, decoded)
  40. }
  41. z := NodePublic{}
  42. if !z.IsZero() {
  43. t.Fatal("IsZero(NodePublic{}) is false")
  44. }
  45. if s := z.ShortString(); s != "" {
  46. t.Fatalf("NodePublic{}.ShortString() is %q, want \"\"", s)
  47. }
  48. }
  49. func TestNodeSerialization(t *testing.T) {
  50. serialized := `{
  51. "Priv": "privkey:40ab1b58e9076c7a4d9d07291f5edf9d1aa017eb949624ba683317f48a640369",
  52. "Pub":"nodekey:50d20b455ecf12bc453f83c2cfdb2a24925d06cf2598dcaa54e91af82ce9f765"
  53. }`
  54. // Carefully check that the expected serialized data decodes and
  55. // re-encodes to the expected keys. These types are serialized to
  56. // disk all over the place and need to be stable.
  57. priv := NodePrivate{
  58. k: [32]uint8{
  59. 0x40, 0xab, 0x1b, 0x58, 0xe9, 0x7, 0x6c, 0x7a, 0x4d, 0x9d, 0x7,
  60. 0x29, 0x1f, 0x5e, 0xdf, 0x9d, 0x1a, 0xa0, 0x17, 0xeb, 0x94,
  61. 0x96, 0x24, 0xba, 0x68, 0x33, 0x17, 0xf4, 0x8a, 0x64, 0x3, 0x69,
  62. },
  63. }
  64. pub := NodePublic{
  65. k: [32]uint8{
  66. 0x50, 0xd2, 0xb, 0x45, 0x5e, 0xcf, 0x12, 0xbc, 0x45, 0x3f, 0x83,
  67. 0xc2, 0xcf, 0xdb, 0x2a, 0x24, 0x92, 0x5d, 0x6, 0xcf, 0x25, 0x98,
  68. 0xdc, 0xaa, 0x54, 0xe9, 0x1a, 0xf8, 0x2c, 0xe9, 0xf7, 0x65,
  69. },
  70. }
  71. type keypair struct {
  72. Priv NodePrivate
  73. Pub NodePublic
  74. }
  75. var a keypair
  76. if err := json.Unmarshal([]byte(serialized), &a); err != nil {
  77. t.Fatal(err)
  78. }
  79. if !a.Priv.Equal(priv) {
  80. t.Errorf("wrong deserialization of private key, got %#v want %#v", a.Priv, priv)
  81. }
  82. if a.Pub != pub {
  83. t.Errorf("wrong deserialization of public key, got %#v want %#v", a.Pub, pub)
  84. }
  85. bs, err := json.MarshalIndent(a, "", " ")
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. var b bytes.Buffer
  90. json.Indent(&b, []byte(serialized), "", " ")
  91. if got, want := string(bs), b.String(); got != want {
  92. t.Error("json serialization doesn't roundtrip")
  93. }
  94. }
  95. func TestNodeReadRawWithoutAllocating(t *testing.T) {
  96. buf := make([]byte, 32)
  97. for i := range buf {
  98. buf[i] = 0x42
  99. }
  100. r := bytes.NewReader(buf)
  101. br := bufio.NewReader(r)
  102. got := testing.AllocsPerRun(1000, func() {
  103. r.Reset(buf)
  104. br.Reset(r)
  105. var k NodePublic
  106. if err := k.ReadRawWithoutAllocating(br); err != nil {
  107. t.Fatalf("ReadRawWithoutAllocating: %v", err)
  108. }
  109. })
  110. if want := 0.0; got != want {
  111. t.Fatalf("ReadRawWithoutAllocating got %f allocs, want %f", got, want)
  112. }
  113. }
  114. func TestNodeWriteRawWithoutAllocating(t *testing.T) {
  115. buf := make([]byte, 0, 32)
  116. w := bytes.NewBuffer(buf)
  117. bw := bufio.NewWriter(w)
  118. got := testing.AllocsPerRun(1000, func() {
  119. w.Reset()
  120. bw.Reset(w)
  121. var k NodePublic
  122. if err := k.WriteRawWithoutAllocating(bw); err != nil {
  123. t.Fatalf("WriteRawWithoutAllocating: %v", err)
  124. }
  125. })
  126. if want := 0.0; got != want {
  127. t.Fatalf("WriteRawWithoutAllocating got %f allocs, want %f", got, want)
  128. }
  129. }
  130. func TestChallenge(t *testing.T) {
  131. priv := NewChallenge()
  132. pub := priv.Public()
  133. txt, err := pub.MarshalText()
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. var back ChallengePublic
  138. if err := back.UnmarshalText(txt); err != nil {
  139. t.Fatal(err)
  140. }
  141. if back != pub {
  142. t.Errorf("didn't round trip: %v != %v", back, pub)
  143. }
  144. }
  145. // Test that NodePublic.Shard is uniformly distributed.
  146. func TestShard(t *testing.T) {
  147. const N = 1_000
  148. var shardCount [256]int
  149. for range N {
  150. shardCount[NewNode().Public().Shard()]++
  151. }
  152. e := float64(N) / 256 // expected
  153. var x2 float64 // chi-squared
  154. for _, c := range shardCount {
  155. r := float64(c) - e // residual
  156. x2 += r * r / e
  157. }
  158. t.Logf("x^2 = %v", x2)
  159. if x2 > 512 { // really want x^2 =~ (256 - 1), but leave slop
  160. t.Errorf("too much variation in shard distribution")
  161. for i, c := range shardCount {
  162. rj := float64(c) - e
  163. t.Logf("shard[%v] = %v (off by %v)", i, c, rj)
  164. }
  165. }
  166. }