github.com/decred/politeia@v1.4.0/util/signature_test.go (about) 1 // Copyright (c) 2021 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package util 6 7 import ( 8 "fmt" 9 "testing" 10 11 "github.com/decred/dcrd/chaincfg/v3" 12 ) 13 14 func TestErrorStatuses(t *testing.T) { 15 // Iterate through all error statuses and verify that a human 16 // readable error message exists. 17 missing := make([]ErrorStatusT, 0, len(ErrorStatuses)) 18 for i := 0; i < int(errorStatusLast); i++ { 19 e := ErrorStatusT(i) 20 _, ok := ErrorStatuses[e] 21 if !ok { 22 // We're missing a error message 23 missing = append(missing, e) 24 } 25 26 // The entry was found. Delete it. This will allow us to 27 // determine if there are any extra error messages in the 28 // map at the end. 29 delete(ErrorStatuses, e) 30 } 31 32 // Verify there are not any missing error messsages 33 if len(missing) > 0 { 34 t.Errorf("error messages missing for codes: %v", missing) 35 } 36 37 // Verify there are not any extra error messages. They should all 38 // be deleted at this point. If any still exists then those are 39 // extra and should not be there. 40 if len(ErrorStatuses) > 0 { 41 t.Errorf("extra error messages found: %v", ErrorStatuses) 42 } 43 } 44 45 func TestVerifyMessage(t *testing.T) { 46 // The following example is mimicking a politeia dcr ticket vote 47 var ( 48 token = "09bad4b668aec651" 49 ticket = "f30add902bd7ec56b2b27204dbd1219b875c9a8e8832ff845c4282847ea59918" 50 votebit = "1" 51 msg = token + ticket + votebit 52 address = "TsdjFrFyyKZMpPu1NNwnH9CTs5kkp4X7KVf" 53 net = chaincfg.TestNet3Params() 54 55 signature = "H5TQz6ASvJGobe/0V9g2lBKC8oraWxzNtliqxBwnPgXSU+4aennJ5zuY7uwOM/MBh/UuhBMJwYuWDQOctYwPouU=" 56 57 // This is a valid signature that uses a different message and 58 // address than the ones listed above. 59 wrongSignature = "INqYmFhIOaPFbtRbSBYs7xbQ976OgvdD5rKtbfnDe1uHOlxS+qIXmqxRnpodIvBHEGgU1dI0eSyZpZGharmPh2k=" 60 ) 61 62 var tests = []struct { 63 name string // Test name 64 addr string // P2PKH address 65 msg string // Message being signed 66 sig string // Signature 67 isValid bool // Is the signature valid 68 }{ 69 { 70 "address in not a valid", 71 "xxx", 72 msg, 73 signature, 74 false, 75 }, 76 { 77 "address is not p2pkh", 78 "TkdjFrFyyKZMpPu1NNwnH9CTs5kkp4X7KVf", 79 msg, 80 signature, 81 false, 82 }, 83 { 84 "signature is not base64", 85 address, 86 msg, 87 "xxx", 88 false, 89 }, 90 { 91 "signature is wrong", 92 address, 93 msg, 94 wrongSignature, 95 false, 96 }, 97 { 98 "success", 99 address, 100 msg, 101 signature, 102 true, 103 }, 104 } 105 for _, v := range tests { 106 t.Run(v.name, func(t *testing.T) { 107 isValid, err := VerifyMessage(v.addr, v.msg, v.sig, net) 108 if isValid != v.isValid { 109 fmt.Printf("isValid %v: %v\n", isValid, err) 110 t.Errorf("VerifyMessage: is valid got %v, want %v", 111 isValid, v.isValid) 112 } 113 }) 114 } 115 }