Skip to content

Commit deff3a8

Browse files
authored
Merge pull request #35 from Snider/feature-add-good-bad-ugly-tests
Add Good, Bad, and Ugly tests
2 parents d649e9e + e112ec3 commit deff3a8

6 files changed

Lines changed: 246 additions & 18 deletions

File tree

pkg/crypt/crypt.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package crypt
33
import (
44
"crypto/md5"
55
"crypto/sha1"
6+
"errors"
67
"crypto/sha256"
78
"crypto/sha512"
89
"encoding/binary"
@@ -217,5 +218,8 @@ func (s *Service) VerifyPGP(publicKey, data, signature []byte) error {
217218
// SymmetricallyEncryptPGP encrypts data with a passphrase.
218219
func (s *Service) SymmetricallyEncryptPGP(passphrase, data []byte) ([]byte, error) {
219220
s.ensurePGP()
221+
if len(passphrase) == 0 {
222+
return nil, errors.New("passphrase cannot be empty")
223+
}
220224
return s.pgp.SymmetricallyEncrypt(passphrase, data)
221225
}

pkg/crypt/crypt_internal_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,28 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9-
func TestEnsureRSA(t *testing.T) {
9+
// TestEnsureRSA_Good tests that the RSA service is initialized correctly.
10+
func TestEnsureRSA_Good(t *testing.T) {
1011
s := &Service{}
1112
s.ensureRSA()
1213
assert.NotNil(t, s.rsa)
1314
}
15+
16+
// TestEnsureRSA_Bad tests that calling ensureRSA multiple times does not change the RSA service.
17+
func TestEnsureRSA_Bad(t *testing.T) {
18+
s := &Service{}
19+
s.ensureRSA()
20+
rsa1 := s.rsa
21+
s.ensureRSA()
22+
rsa2 := s.rsa
23+
assert.Same(t, rsa1, rsa2)
24+
}
25+
26+
// TestEnsureRSA_Ugly tests that ensureRSA works correctly on a service with a pre-initialized RSA service.
27+
func TestEnsureRSA_Ugly(t *testing.T) {
28+
s := NewService() // NewService initializes the RSA service
29+
rsa1 := s.rsa
30+
s.ensureRSA()
31+
rsa2 := s.rsa
32+
assert.Same(t, rsa1, rsa2)
33+
}

pkg/crypt/crypt_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,63 @@ func TestPGP_Good(t *testing.T) {
155155
assert.NotNil(t, ciphertext)
156156
}
157157

158+
func TestPGP_Bad(t *testing.T) {
159+
// Generate two key pairs
160+
pubKey1, privKey1, err := service.GeneratePGPKeyPair("test1", "test1@test.com", "")
161+
assert.NoError(t, err)
162+
pubKey2, privKey2, err := service.GeneratePGPKeyPair("test2", "test2@test.com", "")
163+
assert.NoError(t, err)
164+
165+
message := []byte("secret message")
166+
167+
// Test decryption with the wrong key
168+
ciphertext, err := service.EncryptPGP(pubKey1, message)
169+
assert.NoError(t, err)
170+
// This should fail because we are using the wrong private key.
171+
_, err = service.DecryptPGP(privKey2, ciphertext) // Intentionally using wrong key
172+
assert.Error(t, err)
173+
174+
// Test verification with the wrong key
175+
signature, err := service.SignPGP(privKey1, message)
176+
assert.NoError(t, err)
177+
err = service.VerifyPGP(pubKey2, message, signature)
178+
assert.Error(t, err)
179+
180+
// Test verification with a tampered message
181+
tamperedMessage := []byte("tampered message")
182+
err = service.VerifyPGP(pubKey1, tamperedMessage, signature)
183+
assert.Error(t, err)
184+
}
185+
186+
func TestPGP_Ugly(t *testing.T) {
187+
// Test with malformed keys
188+
_, err := service.EncryptPGP([]byte("not a real key"), []byte("message"))
189+
assert.Error(t, err)
190+
191+
_, err = service.DecryptPGP([]byte("not a real key"), []byte("message"))
192+
assert.Error(t, err)
193+
194+
_, err = service.SignPGP([]byte("not a real key"), []byte("message"))
195+
assert.Error(t, err)
196+
197+
err = service.VerifyPGP([]byte("not a real key"), []byte("message"), []byte("not a real signature"))
198+
assert.Error(t, err)
199+
200+
// Test with empty message
201+
pubKey, privKey, err := service.GeneratePGPKeyPair("test", "test@test.com", "")
202+
assert.NoError(t, err)
203+
message := []byte("")
204+
ciphertext, err := service.EncryptPGP(pubKey, message)
205+
assert.NoError(t, err)
206+
plaintext, err := service.DecryptPGP(privKey, ciphertext, )
207+
assert.NoError(t, err)
208+
assert.Equal(t, message, plaintext)
209+
210+
// Test symmetric encryption with empty passphrase
211+
_, err = service.SymmetricallyEncryptPGP([]byte(""), message)
212+
assert.Error(t, err)
213+
}
214+
158215
// --- IsHashAlgo Tests ---
159216

160217
func TestIsHashAlgo_Good(t *testing.T) {

pkg/enchantrix/enchantrix_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ func TestTransmute_Bad(t *testing.T) {
4040
assert.Error(t, err)
4141
}
4242

43+
func TestTransmute_Ugly(t *testing.T) {
44+
// Test with nil data
45+
_, err := enchantrix.Transmute(nil, []enchantrix.Sigil{&enchantrix.ReverseSigil{}})
46+
assert.NoError(t, err)
47+
48+
// Test with nil sigils
49+
_, err = enchantrix.Transmute([]byte("hello"), nil)
50+
assert.NoError(t, err)
51+
52+
// Test with no sigils
53+
result, err := enchantrix.Transmute([]byte("hello"), []enchantrix.Sigil{})
54+
assert.NoError(t, err)
55+
assert.Equal(t, "hello", string(result))
56+
}
57+
4358
// --- Factory Tests ---
4459

4560
func TestNewSigil_Good(t *testing.T) {
@@ -63,3 +78,20 @@ func TestNewSigil_Bad(t *testing.T) {
6378
assert.Nil(t, sigil)
6479
assert.Contains(t, err.Error(), "unknown sigil name")
6580
}
81+
82+
func TestNewSigil_Ugly(t *testing.T) {
83+
// Test with empty string
84+
sigil, err := enchantrix.NewSigil("")
85+
assert.Error(t, err)
86+
assert.Nil(t, sigil)
87+
88+
// Test with whitespace
89+
sigil, err = enchantrix.NewSigil(" ")
90+
assert.Error(t, err)
91+
assert.Nil(t, sigil)
92+
93+
// Test with non-printable characters
94+
sigil, err = enchantrix.NewSigil("\x00\x01\x02")
95+
assert.Error(t, err)
96+
assert.Nil(t, sigil)
97+
}

pkg/enchantrix/sigils.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type ReverseSigil struct{}
2626

2727
// In reverses the bytes of the data.
2828
func (s *ReverseSigil) In(data []byte) ([]byte, error) {
29+
if data == nil {
30+
return nil, nil
31+
}
2932
reversed := make([]byte, len(data))
3033
for i, j := 0, len(data)-1; i < len(data); i, j = i+1, j-1 {
3134
reversed[i] = data[j]
@@ -43,13 +46,19 @@ type HexSigil struct{}
4346

4447
// In encodes the data to hexadecimal.
4548
func (s *HexSigil) In(data []byte) ([]byte, error) {
49+
if data == nil {
50+
return nil, nil
51+
}
4652
dst := make([]byte, hex.EncodedLen(len(data)))
4753
hex.Encode(dst, data)
4854
return dst, nil
4955
}
5056

5157
// Out decodes the data from hexadecimal.
5258
func (s *HexSigil) Out(data []byte) ([]byte, error) {
59+
if data == nil {
60+
return nil, nil
61+
}
5362
dst := make([]byte, hex.DecodedLen(len(data)))
5463
_, err := hex.Decode(dst, data)
5564
return dst, err
@@ -60,13 +69,19 @@ type Base64Sigil struct{}
6069

6170
// In encodes the data to base64.
6271
func (s *Base64Sigil) In(data []byte) ([]byte, error) {
72+
if data == nil {
73+
return nil, nil
74+
}
6375
dst := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
6476
base64.StdEncoding.Encode(dst, data)
6577
return dst, nil
6678
}
6779

6880
// Out decodes the data from base64.
6981
func (s *Base64Sigil) Out(data []byte) ([]byte, error) {
82+
if data == nil {
83+
return nil, nil
84+
}
7085
dst := make([]byte, base64.StdEncoding.DecodedLen(len(data)))
7186
n, err := base64.StdEncoding.Decode(dst, data)
7287
return dst[:n], err
@@ -79,6 +94,9 @@ type GzipSigil struct {
7994

8095
// In compresses the data using gzip.
8196
func (s *GzipSigil) In(data []byte) ([]byte, error) {
97+
if data == nil {
98+
return nil, nil
99+
}
82100
var b bytes.Buffer
83101
w := s.writer
84102
if w == nil {
@@ -96,6 +114,9 @@ func (s *GzipSigil) In(data []byte) ([]byte, error) {
96114

97115
// Out decompresses the data using gzip.
98116
func (s *GzipSigil) Out(data []byte) ([]byte, error) {
117+
if data == nil {
118+
return nil, nil
119+
}
99120
r, err := gzip.NewReader(bytes.NewReader(data))
100121
if err != nil {
101122
return nil, err

0 commit comments

Comments
 (0)