Better assertions + flow

This commit is contained in:
ekzyis 2023-09-30 19:24:00 +02:00
parent c03e3a5d44
commit 907edd1e91
1 changed files with 21 additions and 12 deletions

View File

@ -15,30 +15,39 @@ func assertCrypt(t *testing.T, key string, salt string, plaintext string, expect
actual string actual string
decrypted string decrypted string
err error err error
ok bool
) )
if k, err = hex.DecodeString(key); err != nil { k, err = hex.DecodeString(key)
t.Errorf("hex decode failed for key") if ok = assert.NoErrorf(t, err, "hex decode failed for key: %v", err); !ok {
return
} }
if s, err = hex.DecodeString(salt); err != nil { s, err = hex.DecodeString(salt)
t.Errorf("hex decode failed for salt") if ok = assert.NoErrorf(t, err, "hex decode failed for salt: %v", err); !ok {
return
} }
actual, err = nip44.Encrypt(k, plaintext, &nip44.EncryptOptions{Salt: s}) actual, err = nip44.Encrypt(k, plaintext, &nip44.EncryptOptions{Salt: s})
if assert.NoError(t, err) { if ok = assert.NoError(t, err, "encryption failed: %v", err); !ok {
assert.Equal(t, expected, actual) return
decrypted, err = nip44.Decrypt(k, expected)
if assert.NoError(t, err) {
assert.Equal(t, decrypted, plaintext)
}
} }
if ok = assert.Equalf(t, expected, actual, "wrong encryption"); !ok {
return
}
decrypted, err = nip44.Decrypt(k, expected)
if ok = assert.NoErrorf(t, err, "decryption failed: %v", err); !ok {
return
}
assert.Equal(t, decrypted, plaintext, "wrong decryption")
} }
func assertDecryptFail(t *testing.T, key string, ciphertext string, msg string) { func assertDecryptFail(t *testing.T, key string, ciphertext string, msg string) {
var ( var (
k []byte k []byte
err error err error
ok bool
) )
if k, err = hex.DecodeString(key); err != nil { k, err = hex.DecodeString(key)
t.Errorf("hex decode failed for key") if ok = assert.NoErrorf(t, err, "hex decode failed for key: %v", err); !ok {
return
} }
_, err = nip44.Decrypt(k, ciphertext) _, err = nip44.Decrypt(k, ciphertext)
assert.ErrorContains(t, err, msg) assert.ErrorContains(t, err, msg)