From 18ac6b99bc38e638c5c5a5bff13c2e1d00c3584a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 13 Nov 2025 20:06:32 +0000 Subject: [PATCH] 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`. --- cmd/trix/main_test.go | 26 +++++++++++++++++++ pkg/crypt/std/pgp/pgp_test.go | 48 ++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/cmd/trix/main_test.go b/cmd/trix/main_test.go index f174be4..c99abf2 100644 --- a/cmd/trix/main_test.go +++ b/cmd/trix/main_test.go @@ -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) diff --git a/pkg/crypt/std/pgp/pgp_test.go b/pkg/crypt/std/pgp/pgp_test.go index 57fe0f6..176fa2f 100644 --- a/pkg/crypt/std/pgp/pgp_test.go +++ b/pkg/crypt/std/pgp/pgp_test.go @@ -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")