File: encrypted_test.go

package info (click to toggle)
golang-github-endophage-gotuf 0.0~git20151020.0.2df1c8e-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 436 kB
  • ctags: 504
  • sloc: makefile: 27
file content (57 lines) | stat: -rw-r--r-- 1,348 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package encrypted

import (
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/assert"
)

var plaintext = []byte("reallyimportant")

func TestRoundtrip(t *testing.T) {
	passphrase := []byte("supersecret")

	enc, err := Encrypt(plaintext, passphrase)
	assert.NoError(t, err)

	// successful decrypt
	dec, err := Decrypt(enc, passphrase)
	assert.NoError(t, err)
	assert.Equal(t, dec, plaintext)

	// wrong passphrase
	passphrase[0] = 0
	dec, err = Decrypt(enc, passphrase)
	assert.Error(t, err)
	assert.Nil(t, dec)
}

func TestTamperedRoundtrip(t *testing.T) {
	passphrase := []byte("supersecret")

	enc, err := Encrypt(plaintext, passphrase)
	assert.NoError(t, err)

	data := &data{}
	err = json.Unmarshal(enc, data)
	assert.NoError(t, err)

	data.Ciphertext[0] = 0
	data.Ciphertext[1] = 0

	enc, _ = json.Marshal(data)

	dec, err := Decrypt(enc, passphrase)
	assert.Error(t, err)
	assert.Nil(t, dec)
}

func TestDecrypt(t *testing.T) {
	enc := []byte(`{"kdf":{"name":"scrypt","params":{"N":32768,"r":8,"p":1},"salt":"N9a7x5JFGbrtB2uBR81jPwp0eiLR4A7FV3mjVAQrg1g="},"cipher":{"name":"nacl/secretbox","nonce":"2h8HxMmgRfuYdpswZBQaU3xJ1nkA/5Ik"},"ciphertext":"SEW6sUh0jf2wfdjJGPNS9+bkk2uB+Cxamf32zR8XkQ=="}`)
	passphrase := []byte("supersecret")

	dec, err := Decrypt(enc, passphrase)
	assert.NoError(t, err)
	assert.Equal(t, dec, plaintext)
}