types/key: add missing ChallengePublic.UnmarshalText

Forgot it when adding the Challenge types earlier.

Change-Id: Ie0872c4e6dc25e5d832aa58c7b3f66d450bf6b71
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/6092/head
Brad Fitzpatrick 2 years ago committed by Brad Fitzpatrick
parent 8c09ae9032
commit 4021ae6b9d

@ -7,6 +7,7 @@ package key
import (
"errors"
"go4.org/mem"
"tailscale.com/types/structs"
)
@ -76,3 +77,8 @@ func (k ChallengePublic) String() string {
func (k ChallengePublic) MarshalText() ([]byte, error) {
return toHex(k.k[:], chalPublicHexPrefix), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (k *ChallengePublic) UnmarshalText(b []byte) error {
return parseHex(k.k[:], mem.B(b), mem.S(chalPublicHexPrefix))
}

@ -142,3 +142,19 @@ func TestNodeWriteRawWithoutAllocating(t *testing.T) {
t.Fatalf("WriteRawWithoutAllocating got %f allocs, want %f", got, want)
}
}
func TestChallenge(t *testing.T) {
priv := NewChallenge()
pub := priv.Public()
txt, err := pub.MarshalText()
if err != nil {
t.Fatal(err)
}
var back ChallengePublic
if err := back.UnmarshalText(txt); err != nil {
t.Fatal(err)
}
if back != pub {
t.Errorf("didn't round trip: %v != %v", back, pub)
}
}

Loading…
Cancel
Save