test: Further increase test coverage
Increases the test coverage of the project. - Increases the test coverage of the `cmd/trix` package from 78.1% to 82.3%. - Increases the test coverage of the `pkg/crypt/std/pgp` package from 76.5% to 84.0%. - Adds tests for error paths and edge cases in `cmd/trix` and `pkg/crypt/std/pgp`.
This commit is contained in:
parent
33e7fa1e17
commit
18ac6b99bc
2 changed files with 70 additions and 4 deletions
|
|
@ -46,6 +46,12 @@ func TestHandleSigil_Good(t *testing.T) {
|
|||
assert.Equal(t, "aGVsbG8=", strings.TrimSpace(buf.String()))
|
||||
}
|
||||
|
||||
func TestHandleSigil_Bad(t *testing.T) {
|
||||
cmd := &cobra.Command{}
|
||||
err := handleSigil(cmd, "bad-sigil", "hello")
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestRunEncodeAndDecode_Good(t *testing.T) {
|
||||
// Encode
|
||||
encodeCmd := &cobra.Command{}
|
||||
|
|
@ -72,6 +78,20 @@ func TestRunEncodeAndDecode_Good(t *testing.T) {
|
|||
assert.Equal(t, "hello", strings.TrimSpace(decodeBuf.String()))
|
||||
}
|
||||
|
||||
func TestRunEncode_Bad(t *testing.T) {
|
||||
cmd := &cobra.Command{}
|
||||
cmd.Flags().StringP("magic", "m", "bad", "Magic number (4 bytes)")
|
||||
err := runEncode(cmd, []string{})
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestRunDecode_Bad(t *testing.T) {
|
||||
cmd := &cobra.Command{}
|
||||
cmd.Flags().StringP("magic", "m", "bad", "Magic number (4 bytes)")
|
||||
err := runDecode(cmd, []string{})
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestRunHash_Good(t *testing.T) {
|
||||
cmd := &cobra.Command{}
|
||||
buf := new(bytes.Buffer)
|
||||
|
|
@ -87,6 +107,12 @@ func TestRunHash_Good(t *testing.T) {
|
|||
assert.NotEmpty(t, buf.String())
|
||||
}
|
||||
|
||||
func TestRunHash_Bad(t *testing.T) {
|
||||
cmd := &cobra.Command{}
|
||||
err := runHash(cmd, []string{"bad-hash"})
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestCreateSigilRunE_Good(t *testing.T) {
|
||||
cmd := &cobra.Command{}
|
||||
buf := new(bytes.Buffer)
|
||||
|
|
|
|||
|
|
@ -18,10 +18,9 @@ func TestService_GenerateKeyPair_Good(t *testing.T) {
|
|||
|
||||
func TestService_Encrypt_Good(t *testing.T) {
|
||||
s := NewService()
|
||||
pub, priv, err := s.GenerateKeyPair("test", "test@test.com", "test")
|
||||
pub, _, err := s.GenerateKeyPair("test", "test@test.com", "test")
|
||||
require.NoError(t, err, "failed to generate key pair")
|
||||
assert.NotNil(t, pub, "public key is nil")
|
||||
assert.NotNil(t, priv, "private key is nil")
|
||||
|
||||
data := []byte("hello world")
|
||||
encrypted, err := s.Encrypt(pub, data)
|
||||
|
|
@ -29,6 +28,12 @@ func TestService_Encrypt_Good(t *testing.T) {
|
|||
assert.NotNil(t, encrypted, "encrypted data is nil")
|
||||
}
|
||||
|
||||
func TestService_Encrypt_Bad(t *testing.T) {
|
||||
s := NewService()
|
||||
_, err := s.Encrypt([]byte("bad key"), []byte("hello world"))
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestService_Decrypt_Good(t *testing.T) {
|
||||
s := NewService()
|
||||
pub, priv, err := s.GenerateKeyPair("test", "test@test.com", "test")
|
||||
|
|
@ -46,11 +51,25 @@ func TestService_Decrypt_Good(t *testing.T) {
|
|||
assert.Equal(t, data, decrypted, "decrypted data does not match original")
|
||||
}
|
||||
|
||||
func TestService_Decrypt_Bad(t *testing.T) {
|
||||
s := NewService()
|
||||
_, err := s.Decrypt([]byte("bad key"), []byte("hello world"))
|
||||
assert.Error(t, err)
|
||||
|
||||
pub, _, err := s.GenerateKeyPair("test", "test@test.com", "test")
|
||||
require.NoError(t, err)
|
||||
_, priv2, err := s.GenerateKeyPair("test2", "test2@test.com", "test2")
|
||||
require.NoError(t, err)
|
||||
encrypted, err := s.Encrypt(pub, []byte("hello world"))
|
||||
require.NoError(t, err)
|
||||
_, err = s.Decrypt(priv2, encrypted)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestService_Sign_Good(t *testing.T) {
|
||||
s := NewService()
|
||||
pub, priv, err := s.GenerateKeyPair("test", "test@test.com", "test")
|
||||
_, priv, err := s.GenerateKeyPair("test", "test@test.com", "test")
|
||||
require.NoError(t, err, "failed to generate key pair")
|
||||
assert.NotNil(t, pub, "public key is nil")
|
||||
assert.NotNil(t, priv, "private key is nil")
|
||||
|
||||
data := []byte("hello world")
|
||||
|
|
@ -59,6 +78,12 @@ func TestService_Sign_Good(t *testing.T) {
|
|||
assert.NotNil(t, signature, "signature is nil")
|
||||
}
|
||||
|
||||
func TestService_Sign_Bad(t *testing.T) {
|
||||
s := NewService()
|
||||
_, err := s.Sign([]byte("bad key"), []byte("hello world"))
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestService_Verify_Good(t *testing.T) {
|
||||
s := NewService()
|
||||
pub, priv, err := s.GenerateKeyPair("test", "test@test.com", "test")
|
||||
|
|
@ -75,6 +100,21 @@ func TestService_Verify_Good(t *testing.T) {
|
|||
require.NoError(t, err, "failed to verify signature")
|
||||
}
|
||||
|
||||
func TestService_Verify_Bad(t *testing.T) {
|
||||
s := NewService()
|
||||
err := s.Verify([]byte("bad key"), []byte("hello world"), []byte("bad signature"))
|
||||
assert.Error(t, err)
|
||||
|
||||
_, priv, err := s.GenerateKeyPair("test", "test@test.com", "test")
|
||||
require.NoError(t, err)
|
||||
pub2, _, err := s.GenerateKeyPair("test2", "test2@test.com", "test2")
|
||||
require.NoError(t, err)
|
||||
signature, err := s.Sign(priv, []byte("hello world"))
|
||||
require.NoError(t, err)
|
||||
err = s.Verify(pub2, []byte("hello world"), signature)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestService_SymmetricallyEncrypt_Good(t *testing.T) {
|
||||
s := NewService()
|
||||
passphrase := []byte("hello world")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue