From 3636c2ec1261545768095a1e2b98e250d8ae0751 Mon Sep 17 00:00:00 2001 From: David Crawshaw Date: Sat, 2 May 2020 01:38:59 +1000 Subject: wgcfg: remove JSON marshal methods Followup from earlier code review. Signed-off-by: David Crawshaw --- wgcfg/key.go | 25 --------------- wgcfg/key_test.go | 91 +++++-------------------------------------------------- 2 files changed, 7 insertions(+), 109 deletions(-) diff --git a/wgcfg/key.go b/wgcfg/key.go index cfb59d3..d75f0d9 100644 --- a/wgcfg/key.go +++ b/wgcfg/key.go @@ -67,31 +67,6 @@ func (k PublicKey) IsZero() bool { return subtle.ConstantTimeCompare(zeros[:], k[:]) == 1 } -func (k *PublicKey) MarshalJSON() ([]byte, error) { - if k == nil { - return []byte("null"), nil - } - buf := new(bytes.Buffer) - fmt.Fprintf(buf, `"%x"`, k[:]) - return buf.Bytes(), nil -} - -func (k *PublicKey) UnmarshalJSON(b []byte) error { - if k == nil { - return errors.New("wgcfg.PublicKey: UnmarshalJSON on nil pointer") - } - if len(b) < 3 || b[0] != '"' || b[len(b)-1] != '"' { - return errors.New("wgcfg.PublicKey: UnmarshalJSON not given a string") - } - b = b[1 : len(b)-1] - key, err := ParseHexKey(string(b)) - if err != nil { - return fmt.Errorf("wgcfg.PublicKey: UnmarshalJSON: %v", err) - } - copy(k[:], key[:]) - return nil -} - // PrivateKey is curve25519 key. // It is used by WireGuard to represent private keys. type PrivateKey [KeySize]byte diff --git a/wgcfg/key_test.go b/wgcfg/key_test.go index 21bffbc..f4640d4 100644 --- a/wgcfg/key_test.go +++ b/wgcfg/key_test.go @@ -12,98 +12,21 @@ func TestKeyBasics(t *testing.T) { } k1 := pk1.Public() - b, err := k1.MarshalJSON() - if err != nil { - t.Fatal(err) - } - - t.Run("JSON round-trip", func(t *testing.T) { - // should preserve the keys - k2 := new(PublicKey) - if err := k2.UnmarshalJSON(b); err != nil { - t.Fatal(err) - } - if !bytes.Equal(k1[:], k2[:]) { - t.Fatalf("k1 %v != k2 %v", k1[:], k2[:]) - } - if b1, b2 := k1.String(), k2.String(); b1 != b2 { - t.Fatalf("base64-encoded keys do not match: %s, %s", b1, b2) - } - }) - - t.Run("JSON incompatible with PrivateKey", func(t *testing.T) { - k2 := new(PrivateKey) - if err := k2.UnmarshalText(b); err == nil { - t.Fatalf("successfully decoded key as private key") - } - }) - - t.Run("second key", func(t *testing.T) { - // A second call to NewPresharedKey should make a new key. - pk3, err := NewPrivateKey() - if err != nil { - t.Fatal(err) - } - k3 := pk3.Public() - if bytes.Equal(k1[:], k3[:]) { - t.Fatalf("k1 %v == k3 %v", k1[:], k3[:]) - } - // Check for obvious comparables to make sure we are not generating bad strings somewhere. - if b1, b2 := k1.String(), k3.String(); b1 == b2 { - t.Fatalf("base64-encoded keys match: %s, %s", b1, b2) - } - }) -} - -func TestPrivateKeyBasics(t *testing.T) { - pri, err := NewPrivateKey() - if err != nil { - t.Fatal(err) - } - - b, err := pri.MarshalText() - if err != nil { - t.Fatal(err) - } - - t.Run("JSON round-trip", func(t *testing.T) { - // should preserve the keys - pri2 := new(PrivateKey) - if err := pri2.UnmarshalText(b); err != nil { - t.Fatal(err) - } - if !bytes.Equal(pri[:], pri2[:]) { - t.Fatalf("pri %v != pri2 %v", pri[:], pri2[:]) - } - if b1, b2 := pri.String(), pri2.String(); b1 != b2 { - t.Fatalf("base64-encoded keys do not match: %s, %s", b1, b2) - } - if pub1, pub2 := pri.Public().String(), pri2.Public().String(); pub1 != pub2 { - t.Fatalf("base64-encoded public keys do not match: %s, %s", pub1, pub2) - } - }) - - t.Run("JSON incompatible with Key", func(t *testing.T) { - k2 := new(PublicKey) - if err := k2.UnmarshalJSON(b); err == nil { - t.Fatalf("successfully decoded private key as key") - } - }) - t.Run("second key", func(t *testing.T) { - // A second call to New should make a new key. - pri3, err := NewPrivateKey() + // Different keys should be different. + pk2, err := NewPrivateKey() if err != nil { t.Fatal(err) } - if bytes.Equal(pri[:], pri3[:]) { - t.Fatalf("pri %v == pri3 %v", pri[:], pri3[:]) + k2 := pk2.Public() + if bytes.Equal(k1[:], k2[:]) { + t.Fatalf("k1 %v == k2 %v", k1[:], k2[:]) } // Check for obvious comparables to make sure we are not generating bad strings somewhere. - if b1, b2 := pri.String(), pri3.String(); b1 == b2 { + if b1, b2 := k1.String(), k2.String(); b1 == b2 { t.Fatalf("base64-encoded keys match: %s, %s", b1, b2) } - if pub1, pub2 := pri.Public().String(), pri3.Public().String(); pub1 == pub2 { + if pub1, pub2 := pk1.Public().String(), pk2.Public().String(); pub1 == pub2 { t.Fatalf("base64-encoded public keys match: %s, %s", pub1, pub2) } }) -- cgit v1.2.3-54-g00ecf