(t *testing.T)
| 54 | } |
| 55 | |
| 56 | func TestLoadPrivateKey(t *testing.T) { |
| 57 | Convey("save and load", t, func() { |
| 58 | defer os.Remove(privateKeyPath) |
| 59 | pk, _, err := asymmetric.GenSecp256k1KeyPair() |
| 60 | So(pk, ShouldNotBeNil) |
| 61 | So(err, ShouldBeNil) |
| 62 | err = SavePrivateKey(privateKeyPath, pk, []byte(password)) |
| 63 | So(err, ShouldBeNil) |
| 64 | |
| 65 | lk, err := LoadPrivateKey(privateKeyPath, []byte(password)) |
| 66 | So(err, ShouldBeNil) |
| 67 | So(string(pk.Serialize()), ShouldEqual, string(lk.Serialize())) |
| 68 | }) |
| 69 | Convey("load error", t, func() { |
| 70 | lk, err := LoadPrivateKey("/path/not/exist", []byte(password)) |
| 71 | So(err, ShouldNotBeNil) |
| 72 | So(lk, ShouldBeNil) |
| 73 | }) |
| 74 | Convey("empty key file", t, func() { |
| 75 | defer os.Remove("./.empty") |
| 76 | os.Create("./.empty") |
| 77 | lk, err := LoadPrivateKey("./.empty", []byte(password)) |
| 78 | So(err, ShouldEqual, symmetric.ErrInputSize) |
| 79 | So(lk, ShouldBeNil) |
| 80 | }) |
| 81 | Convey("not key file1", t, func() { |
| 82 | lk, err := LoadPrivateKey("doc.go", []byte(password)) |
| 83 | So(err, ShouldEqual, symmetric.ErrInputSize) |
| 84 | So(lk, ShouldBeNil) |
| 85 | }) |
| 86 | Convey("not key file2", t, func() { |
| 87 | defer os.Remove("./.notkey") |
| 88 | enc, _ := symmetric.EncryptWithPassword([]byte("aa"), []byte(password), []byte(salt)) |
| 89 | ioutil.WriteFile("./.notkey", enc, 0600) |
| 90 | lk, err := LoadPrivateKey("./.notkey", []byte(password)) |
| 91 | So(err, ShouldEqual, ErrNotKeyFile) |
| 92 | So(lk, ShouldBeNil) |
| 93 | }) |
| 94 | Convey("hash not match", t, func() { |
| 95 | defer os.Remove("./.HashNotMatch") |
| 96 | enc, _ := symmetric.EncryptWithPassword(bytes.Repeat([]byte("a"), 64), []byte(password), []byte(salt)) |
| 97 | ioutil.WriteFile("./.HashNotMatch", enc, 0600) |
| 98 | lk, err := LoadPrivateKey("./.HashNotMatch", []byte(password)) |
| 99 | So(err, ShouldEqual, ErrHashNotMatch) |
| 100 | So(lk, ShouldBeNil) |
| 101 | }) |
| 102 | Convey("invalid base58 version", t, func() { |
| 103 | defer os.Remove("./.Base58VersionNotMatch") |
| 104 | var invalidPrivateKeyStoreVersion byte = 0x1 |
| 105 | privateKeyBytes, _ := hex.DecodeString("f7c0bc718eb0df81e796a11e6f62e23cd2be0a4bdcca30df40d4d915cc3be3ff") |
| 106 | privateKey, _ := asymmetric.PrivKeyFromBytes(privateKeyBytes) |
| 107 | serializedKey := privateKey.Serialize() |
| 108 | keyHash := hash.DoubleHashB(serializedKey) |
| 109 | rawData := append(keyHash, serializedKey...) |
| 110 | encKey, _ := symmetric.EncryptWithPassword(rawData, []byte(password), []byte(salt)) |
| 111 | invalidBase58EncKey := base58.CheckEncode(encKey, invalidPrivateKeyStoreVersion) |
| 112 | ioutil.WriteFile("./.Base58VersionNotMatch", []byte(invalidBase58EncKey), 0600) |
| 113 | lk, err := LoadPrivateKey("./.Base58VersionNotMatch", []byte(password)) |
nothing calls this directly
no test coverage detected