control_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package key
  4. import (
  5. "encoding/json"
  6. "testing"
  7. )
  8. func TestControlKey(t *testing.T) {
  9. serialized := `{"PrivateKey":[36,132,249,6,73,141,249,49,9,96,49,60,240,217,253,57,3,69,248,64,178,62,121,73,121,88,115,218,130,145,68,254]}`
  10. want := ControlPrivate{
  11. MachinePrivate{
  12. k: [32]byte{36, 132, 249, 6, 73, 141, 249, 49, 9, 96, 49, 60, 240, 217, 253, 57, 3, 69, 248, 64, 178, 62, 121, 73, 121, 88, 115, 218, 130, 145, 68, 254},
  13. },
  14. }
  15. var got struct {
  16. PrivateKey ControlPrivate
  17. }
  18. if err := json.Unmarshal([]byte(serialized), &got); err != nil {
  19. t.Fatalf("decoding serialized ControlPrivate: %v", err)
  20. }
  21. if !got.PrivateKey.mkey.Equal(want.mkey) {
  22. t.Fatalf("Serialized ControlPrivate didn't deserialize as expected, got %v want %v", got.PrivateKey, want)
  23. }
  24. bs, err := json.Marshal(got)
  25. if err != nil {
  26. t.Fatalf("json reserialization of ControlPrivate failed: %v", err)
  27. }
  28. if got, want := string(bs), serialized; got != want {
  29. t.Fatalf("ControlPrivate didn't round-trip, got %q want %q", got, want)
  30. }
  31. }