decred.org/dcrdex@v1.0.5/server/cmd/dcrdex/key_test.go (about) 1 // This code is available on the terms of the project LICENSE.md file, 2 // also available online at https://blueoakcouncil.org/license/1.0.0. 3 4 package main 5 6 import ( 7 "bytes" 8 "path/filepath" 9 "testing" 10 11 "github.com/decred/dcrd/dcrec/secp256k1/v4" 12 ) 13 14 func Test_createAndStoreKey(t *testing.T) { 15 dir := t.TempDir() 16 file := "newkey" 17 18 tests := []struct { 19 name string 20 path string 21 pass []byte 22 wantErr bool 23 }{ 24 { 25 "bad path", 26 "/totally/not/a/path", 27 []byte("pass1234"), 28 true, 29 }, 30 { 31 "ok new", 32 filepath.Join(dir, file), 33 []byte("pass1234"), 34 false, 35 }, 36 { 37 "already exists", 38 filepath.Join(dir, file), 39 []byte("pass1234"), 40 true, 41 }, 42 { 43 "empty pass", 44 filepath.Join(dir, "newkey2"), 45 []byte{}, 46 true, 47 }, 48 } 49 for _, tt := range tests { 50 t.Run(tt.name, func(t *testing.T) { 51 _, err := createAndStoreKey(tt.path, tt.pass) 52 if (err != nil) != tt.wantErr { 53 t.Errorf("createAndStoreKey() error = %v, wantErr %v", err, tt.wantErr) 54 return 55 } 56 }) 57 } 58 } 59 60 func Test_loadKeyFile(t *testing.T) { 61 dir := t.TempDir() 62 63 fullFile := filepath.Join(dir, "newkey") 64 pass := []byte("pass1234") 65 66 privKey, err := createAndStoreKey(fullFile, pass) 67 if err != nil { 68 t.Fatalf("createAndStoreKey: %v", err) 69 } 70 71 tests := []struct { 72 name string 73 path string 74 pass []byte 75 want *secp256k1.PrivateKey 76 wantErr bool 77 }{ 78 { 79 "ok", 80 fullFile, 81 []byte("pass1234"), 82 privKey, 83 false, 84 }, 85 { 86 "bad path", 87 filepath.Join(dir, "wrongName"), 88 []byte("pass1234"), 89 nil, 90 true, 91 }, 92 { 93 "wrong pass", 94 fullFile, 95 []byte("adsd"), 96 nil, 97 true, 98 }, 99 } 100 for _, tt := range tests { 101 t.Run(tt.name, func(t *testing.T) { 102 pk, err := loadKeyFile(tt.path, tt.pass) 103 if (err != nil) != tt.wantErr { 104 t.Errorf("loadKeyFile() error = %v, wantErr %v", err, tt.wantErr) 105 return 106 } 107 if tt.want == nil { 108 return 109 } 110 if !bytes.Equal(tt.want.Serialize(), pk.Serialize()) { 111 t.Errorf("private key mismatch") 112 } 113 }) 114 } 115 } 116 117 func Test_dexKey(t *testing.T) { 118 dir := t.TempDir() 119 120 file := "newkey" 121 fullFile := filepath.Join(dir, file) 122 123 tests := []struct { 124 name string 125 path string 126 pass []byte 127 wantErr bool 128 }{ 129 { 130 "bad path", 131 "/totally/not/a/path", 132 []byte("pass1234"), 133 true, 134 }, 135 { 136 "ok new", 137 fullFile, 138 []byte("pass1234"), 139 false, 140 }, 141 { 142 "ok exists", 143 fullFile, 144 []byte("pass1234"), 145 false, 146 }, 147 { 148 "wrong pass", 149 fullFile, 150 []byte("adsf"), 151 true, 152 }, 153 { 154 "empty pass for new", 155 filepath.Join(dir, "newkey2"), 156 []byte{}, 157 true, 158 }, 159 } 160 for _, tt := range tests { 161 t.Run(tt.name, func(t *testing.T) { 162 _, err := dexKey(tt.path, tt.pass) 163 if (err != nil) != tt.wantErr { 164 t.Errorf("dexKey() error = %v, wantErr %v", err, tt.wantErr) 165 return 166 } 167 }) 168 } 169 }