MCPcopy
hub / github.com/kopia/kopia / TestRoundTrip

Function TestRoundTrip

repo/encryption/encryption_test.go:25–95  ·  view source on GitHub ↗

nolint:gocyclo

(t *testing.T)

Source from the content-addressed store, hash-verified

23
24//nolint:gocyclo
25func 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)

Callers

nothing calls this directly

Calls 14

CloseMethod · 0.95
ToByteSliceMethod · 0.95
BytesMethod · 0.95
SupportedAlgorithmsFunction · 0.92
CreateEncryptorFunction · 0.92
FromSliceFunction · 0.92
EqualMethod · 0.80
ErrorfMethod · 0.80
RunMethod · 0.65
EncryptMethod · 0.65
DecryptMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected