node_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package key
  5. import (
  6. "bufio"
  7. "bytes"
  8. "encoding/json"
  9. "strings"
  10. "testing"
  11. )
  12. func TestNodeKey(t *testing.T) {
  13. k := NewNode()
  14. if k.IsZero() {
  15. t.Fatal("NodePrivate should not be zero")
  16. }
  17. p := k.Public()
  18. if p.IsZero() {
  19. t.Fatal("NodePublic should not be zero")
  20. }
  21. bs, err := p.MarshalText()
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. if full, got := string(bs), ":"+p.UntypedHexString(); !strings.HasSuffix(full, got) {
  26. t.Fatalf("NodePublic.UntypedHexString is not a suffix of the typed serialization, got %q want suffix of %q", got, full)
  27. }
  28. z := NodePublic{}
  29. if !z.IsZero() {
  30. t.Fatal("IsZero(NodePublic{}) is false")
  31. }
  32. if s := z.ShortString(); s != "" {
  33. t.Fatalf("NodePublic{}.ShortString() is %q, want \"\"", s)
  34. }
  35. }
  36. func TestNodeSerialization(t *testing.T) {
  37. serialized := `{
  38. "Priv": "privkey:40ab1b58e9076c7a4d9d07291f5edf9d1aa017eb949624ba683317f48a640369",
  39. "Pub":"nodekey:50d20b455ecf12bc453f83c2cfdb2a24925d06cf2598dcaa54e91af82ce9f765"
  40. }`
  41. // Carefully check that the expected serialized data decodes and
  42. // re-encodes to the expected keys. These types are serialized to
  43. // disk all over the place and need to be stable.
  44. priv := NodePrivate{
  45. k: [32]uint8{
  46. 0x40, 0xab, 0x1b, 0x58, 0xe9, 0x7, 0x6c, 0x7a, 0x4d, 0x9d, 0x7,
  47. 0x29, 0x1f, 0x5e, 0xdf, 0x9d, 0x1a, 0xa0, 0x17, 0xeb, 0x94,
  48. 0x96, 0x24, 0xba, 0x68, 0x33, 0x17, 0xf4, 0x8a, 0x64, 0x3, 0x69,
  49. },
  50. }
  51. pub := NodePublic{
  52. k: [32]uint8{
  53. 0x50, 0xd2, 0xb, 0x45, 0x5e, 0xcf, 0x12, 0xbc, 0x45, 0x3f, 0x83,
  54. 0xc2, 0xcf, 0xdb, 0x2a, 0x24, 0x92, 0x5d, 0x6, 0xcf, 0x25, 0x98,
  55. 0xdc, 0xaa, 0x54, 0xe9, 0x1a, 0xf8, 0x2c, 0xe9, 0xf7, 0x65,
  56. },
  57. }
  58. type keypair struct {
  59. Priv NodePrivate
  60. Pub NodePublic
  61. }
  62. var a keypair
  63. if err := json.Unmarshal([]byte(serialized), &a); err != nil {
  64. t.Fatal(err)
  65. }
  66. if !a.Priv.Equal(priv) {
  67. t.Errorf("wrong deserialization of private key, got %#v want %#v", a.Priv, priv)
  68. }
  69. if a.Pub != pub {
  70. t.Errorf("wrong deserialization of public key, got %#v want %#v", a.Pub, pub)
  71. }
  72. bs, err := json.MarshalIndent(a, "", " ")
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. var b bytes.Buffer
  77. json.Indent(&b, []byte(serialized), "", " ")
  78. if got, want := string(bs), b.String(); got != want {
  79. t.Error("json serialization doesn't roundtrip")
  80. }
  81. }
  82. func TestNodeReadRawWithoutAllocating(t *testing.T) {
  83. buf := make([]byte, 32)
  84. for i := range buf {
  85. buf[i] = 0x42
  86. }
  87. r := bytes.NewReader(buf)
  88. br := bufio.NewReader(r)
  89. got := testing.AllocsPerRun(1000, func() {
  90. r.Reset(buf)
  91. br.Reset(r)
  92. var k NodePublic
  93. if err := k.ReadRawWithoutAllocating(br); err != nil {
  94. t.Fatalf("ReadRawWithoutAllocating: %v", err)
  95. }
  96. })
  97. if want := 0.0; got != want {
  98. t.Fatalf("ReadRawWithoutAllocating got %f allocs, want %f", got, want)
  99. }
  100. }
  101. func TestNodeWriteRawWithoutAllocating(t *testing.T) {
  102. buf := make([]byte, 0, 32)
  103. w := bytes.NewBuffer(buf)
  104. bw := bufio.NewWriter(w)
  105. got := testing.AllocsPerRun(1000, func() {
  106. w.Reset()
  107. bw.Reset(w)
  108. var k NodePublic
  109. if err := k.WriteRawWithoutAllocating(bw); err != nil {
  110. t.Fatalf("WriteRawWithoutAllocating: %v", err)
  111. }
  112. })
  113. if want := 0.0; got != want {
  114. t.Fatalf("WriteRawWithoutAllocating got %f allocs, want %f", got, want)
  115. }
  116. }