aboutsummaryrefslogtreecommitdiffstats
path: root/oae_test.go
blob: 8a0a63963e472692fe47a75f6703fc93b067e5da (plain) (blame)
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
package main

import (
	"bytes"
	"io"
	"strings"
	"testing"
)

var testEncryptionMetadata = encryptionMetadata{
	EncryptionType: encryptionAlgAES256_GCM,
	SegmentSize:    18,
}

var testHashMetadata = hashMetadata{
	PasswordHashType: pwHashPBKDF2_HMAC_SHA256,
	Iterations:       10,
	SaltSize:         defaultSaltSize,
}

func TestOAEReadWrite(t *testing.T) {
	t.Parallel()

	const password = "asdf"
	input := strings.Repeat("test input", 1024)
	out := new(bytes.Buffer)
	writer := testEncryptionMetadata.newEncryptingWriter(out, password, &testHashMetadata)
	if _, err := io.WriteString(writer, input); err != nil {
		t.Fatalf("Failed to write: %s", err)
	}
	if err := writer.close(); err != nil {
		t.Fatalf("writer.Close() failed: %s", err)
	}
	got, err := io.ReadAll(testEncryptionMetadata.newDecryptingReader(bytes.NewReader(out.Bytes()), password, &testHashMetadata))
	if err != nil {
		t.Fatalf("Failed to decrypt: %s", err)
	}
	if string(got) != input {
		t.Errorf("Input failed to round-trip")
	}
}