nolint:gocyclo
(t *testing.T)
| 23 | |
| 24 | //nolint:gocyclo |
| 25 | func TestRoundTrip(t *testing.T) { |
| 26 | data := make([]byte, 100) |
| 27 | rand.Read(data) |
| 28 | |
| 29 | masterKey := make([]byte, 32) |
| 30 | rand.Read(masterKey) |
| 31 | |
| 32 | contentID1 := make([]byte, 16) |
| 33 | rand.Read(contentID1) |
| 34 | |
| 35 | contentID2 := make([]byte, 16) |
| 36 | rand.Read(contentID2) |
| 37 | |
| 38 | for _, encryptionAlgo := range encryption.SupportedAlgorithms(true) { |
| 39 | t.Run(encryptionAlgo, func(t *testing.T) { |
| 40 | e, err := encryption.CreateEncryptor(parameters{encryptionAlgo, masterKey}) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | |
| 45 | var cipherText1 gather.WriteBuffer |
| 46 | defer cipherText1.Close() |
| 47 | |
| 48 | var cipherText1b gather.WriteBuffer |
| 49 | defer cipherText1b.Close() |
| 50 | |
| 51 | require.NoError(t, e.Encrypt(gather.FromSlice(data), contentID1, &cipherText1)) |
| 52 | require.NoError(t, e.Encrypt(gather.FromSlice(data), contentID1, &cipherText1b)) |
| 53 | |
| 54 | if v := cipherText1.ToByteSlice(); bytes.Equal(v, cipherText1b.ToByteSlice()) { |
| 55 | t.Errorf("multiple Encrypt returned the same ciphertext: %x", v) |
| 56 | } |
| 57 | |
| 58 | var plainText1 gather.WriteBuffer |
| 59 | defer plainText1.Close() |
| 60 | |
| 61 | require.NoError(t, e.Decrypt(cipherText1.Bytes(), contentID1, &plainText1)) |
| 62 | |
| 63 | if v := plainText1.ToByteSlice(); !bytes.Equal(v, data) { |
| 64 | t.Errorf("Encrypt()/Decrypt() does not round-trip: %x %x", v, data) |
| 65 | } |
| 66 | |
| 67 | var cipherText2 gather.WriteBuffer |
| 68 | defer cipherText2.Close() |
| 69 | |
| 70 | require.NoError(t, e.Encrypt(gather.FromSlice(data), contentID2, &cipherText2)) |
| 71 | |
| 72 | var plainText2 gather.WriteBuffer |
| 73 | defer plainText2.Close() |
| 74 | |
| 75 | require.NoError(t, e.Decrypt(cipherText2.Bytes(), contentID2, &plainText2)) |
| 76 | |
| 77 | if v := plainText2.ToByteSlice(); !bytes.Equal(v, data) { |
| 78 | t.Errorf("Encrypt()/Decrypt() does not round-trip: %x %x", v, data) |
| 79 | } |
| 80 | |
| 81 | if v := cipherText1.ToByteSlice(); bytes.Equal(v, cipherText2.ToByteSlice()) { |
| 82 | t.Errorf("ciphertexts should be different, were %x", v) |
nothing calls this directly
no test coverage detected