crypto_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package payment
  2. import (
  3. "crypto/rand"
  4. "strings"
  5. "testing"
  6. )
  7. func makeKey(t *testing.T) []byte {
  8. t.Helper()
  9. key := make([]byte, 32)
  10. if _, err := rand.Read(key); err != nil {
  11. t.Fatalf("generate random key: %v", err)
  12. }
  13. return key
  14. }
  15. func TestEncryptDecryptRoundTrip(t *testing.T) {
  16. t.Parallel()
  17. key := makeKey(t)
  18. plaintexts := []string{
  19. "hello world",
  20. "short",
  21. "a longer string with special chars: !@#$%^&*()",
  22. `{"key":"value","num":42}`,
  23. "你好世界 unicode test 🎉",
  24. strings.Repeat("x", 10000),
  25. }
  26. for _, pt := range plaintexts {
  27. encrypted, err := Encrypt(pt, key)
  28. if err != nil {
  29. t.Fatalf("Encrypt(%q) error: %v", pt[:min(len(pt), 30)], err)
  30. }
  31. decrypted, err := Decrypt(encrypted, key)
  32. if err != nil {
  33. t.Fatalf("Decrypt error for plaintext %q: %v", pt[:min(len(pt), 30)], err)
  34. }
  35. if decrypted != pt {
  36. t.Fatalf("round-trip failed: got %q, want %q", decrypted[:min(len(decrypted), 30)], pt[:min(len(pt), 30)])
  37. }
  38. }
  39. }
  40. func TestEncryptProducesDifferentCiphertexts(t *testing.T) {
  41. t.Parallel()
  42. key := makeKey(t)
  43. ct1, err := Encrypt("same plaintext", key)
  44. if err != nil {
  45. t.Fatalf("first Encrypt error: %v", err)
  46. }
  47. ct2, err := Encrypt("same plaintext", key)
  48. if err != nil {
  49. t.Fatalf("second Encrypt error: %v", err)
  50. }
  51. if ct1 == ct2 {
  52. t.Fatal("two encryptions of the same plaintext should produce different ciphertexts (random nonce)")
  53. }
  54. }
  55. func TestDecryptWithWrongKeyFails(t *testing.T) {
  56. t.Parallel()
  57. key1 := makeKey(t)
  58. key2 := makeKey(t)
  59. encrypted, err := Encrypt("secret data", key1)
  60. if err != nil {
  61. t.Fatalf("Encrypt error: %v", err)
  62. }
  63. _, err = Decrypt(encrypted, key2)
  64. if err == nil {
  65. t.Fatal("Decrypt with wrong key should fail, but got nil error")
  66. }
  67. }
  68. func TestEncryptRejectsInvalidKeyLength(t *testing.T) {
  69. t.Parallel()
  70. badKeys := [][]byte{
  71. nil,
  72. make([]byte, 0),
  73. make([]byte, 16),
  74. make([]byte, 31),
  75. make([]byte, 33),
  76. make([]byte, 64),
  77. }
  78. for _, key := range badKeys {
  79. _, err := Encrypt("test", key)
  80. if err == nil {
  81. t.Fatalf("Encrypt should reject key of length %d", len(key))
  82. }
  83. }
  84. }
  85. func TestDecryptRejectsInvalidKeyLength(t *testing.T) {
  86. t.Parallel()
  87. badKeys := [][]byte{
  88. nil,
  89. make([]byte, 16),
  90. make([]byte, 33),
  91. }
  92. for _, key := range badKeys {
  93. _, err := Decrypt("dummydata:dummydata:dummydata", key)
  94. if err == nil {
  95. t.Fatalf("Decrypt should reject key of length %d", len(key))
  96. }
  97. }
  98. }
  99. func TestEncryptEmptyPlaintext(t *testing.T) {
  100. t.Parallel()
  101. key := makeKey(t)
  102. encrypted, err := Encrypt("", key)
  103. if err != nil {
  104. t.Fatalf("Encrypt empty plaintext error: %v", err)
  105. }
  106. decrypted, err := Decrypt(encrypted, key)
  107. if err != nil {
  108. t.Fatalf("Decrypt empty plaintext error: %v", err)
  109. }
  110. if decrypted != "" {
  111. t.Fatalf("expected empty string, got %q", decrypted)
  112. }
  113. }
  114. func TestEncryptDecryptUnicodeJSON(t *testing.T) {
  115. t.Parallel()
  116. key := makeKey(t)
  117. jsonContent := `{"name":"测试用户","email":"test@example.com","balance":100.50}`
  118. encrypted, err := Encrypt(jsonContent, key)
  119. if err != nil {
  120. t.Fatalf("Encrypt JSON error: %v", err)
  121. }
  122. decrypted, err := Decrypt(encrypted, key)
  123. if err != nil {
  124. t.Fatalf("Decrypt JSON error: %v", err)
  125. }
  126. if decrypted != jsonContent {
  127. t.Fatalf("JSON round-trip failed: got %q, want %q", decrypted, jsonContent)
  128. }
  129. }
  130. func TestDecryptInvalidFormat(t *testing.T) {
  131. t.Parallel()
  132. key := makeKey(t)
  133. invalidInputs := []string{
  134. "",
  135. "nodelimiter",
  136. "only:two",
  137. "invalid:base64:!!!",
  138. }
  139. for _, input := range invalidInputs {
  140. _, err := Decrypt(input, key)
  141. if err == nil {
  142. t.Fatalf("Decrypt(%q) should fail but got nil error", input)
  143. }
  144. }
  145. }
  146. func TestCiphertextFormat(t *testing.T) {
  147. t.Parallel()
  148. key := makeKey(t)
  149. encrypted, err := Encrypt("test", key)
  150. if err != nil {
  151. t.Fatalf("Encrypt error: %v", err)
  152. }
  153. parts := strings.SplitN(encrypted, ":", 3)
  154. if len(parts) != 3 {
  155. t.Fatalf("ciphertext should have format iv:authTag:ciphertext, got %d parts", len(parts))
  156. }
  157. for i, part := range parts {
  158. if part == "" {
  159. t.Fatalf("ciphertext part %d is empty", i)
  160. }
  161. }
  162. }