github.com/decred/politeia@v1.4.0/politeiad/api/v1/identity/identity_test.go (about) 1 // Copyright (c) 2016-2017 Company 0, LLC. 2 // Copyright (c) 2017-2019 The Decred developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package identity 7 8 import ( 9 "encoding/hex" 10 "fmt" 11 "reflect" 12 "testing" 13 14 "github.com/davecgh/go-spew/spew" 15 "github.com/pmezard/go-difflib/difflib" 16 ) 17 18 var ( 19 alice *FullIdentity 20 bob *FullIdentity 21 ) 22 23 func TestNew(t *testing.T) { 24 var err error 25 26 alice, err = New() 27 if err != nil { 28 t.Fatalf("New alice: %v", err) 29 } 30 31 bob, err = New() 32 if err != nil { 33 t.Fatalf("New bob: %v", err) 34 } 35 } 36 37 func TestMarshalUnmarshal(t *testing.T) { 38 am, err := alice.Marshal() 39 if err != nil { 40 t.Fatal(err) 41 } 42 43 a, err := UnmarshalFullIdentity(am) 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 if !reflect.DeepEqual(a, alice) { 49 t.Fatalf("marshal/unmarshal failed") 50 } 51 } 52 53 func TestMarshalUnmarshalPublic(t *testing.T) { 54 am, err := alice.Public.Marshal() 55 if err != nil { 56 t.Fatal(err) 57 } 58 59 a, err := UnmarshalPublicIdentity(am) 60 if err != nil { 61 t.Fatal(err) 62 } 63 64 if !reflect.DeepEqual(*a, alice.Public) { 65 d := difflib.UnifiedDiff{ 66 A: difflib.SplitLines(spew.Sdump(*a)), 67 B: difflib.SplitLines(spew.Sdump(alice.Public)), 68 FromFile: "original", 69 ToFile: "current", 70 Context: 3, 71 } 72 text, err := difflib.GetUnifiedDiffString(d) 73 if err != nil { 74 panic(err) 75 } 76 t.Fatalf("marshal/unmarshal failed %v", text) 77 } 78 } 79 80 func TestString(t *testing.T) { 81 s := fmt.Sprintf("%v", alice.Public) 82 ss := hex.EncodeToString(alice.Public.Key[:]) 83 if s != ss { 84 t.Fatalf("stringer not working") 85 } 86 } 87 88 func TestSign(t *testing.T) { 89 message := []byte("this is a message") 90 signature := alice.SignMessage(message) 91 if !alice.Public.VerifyMessage(message, signature) { 92 t.Fatalf("corrupt signature") 93 } 94 }