aboutsummaryrefslogtreecommitdiffstats
path: root/sym_test.go
blob: 4d648d321d5ba606d4fd0cb48e08656b0baf3d9a (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main

import (
	"bytes"
	"os"
	"path/filepath"
	"testing"
)

var testEncryptOptions = func() encryptOptions {
	opts := defaultEncryptOptions
	opts.iterations = 10
	return opts
}()

var testDecryptOptions = func() decryptOptions {
	opts := defaultDecryptOptions
	opts.iterations = 10
	return opts
}()

func mustWriteFile(t *testing.T, path string, content []byte) {
	t.Helper()
	if err := os.WriteFile(path, content, 0600); err != nil {
		t.Fatalf("Failed to write test file: %s", err)
	}
}

func mustReadFile(t *testing.T, path string) []byte {
	t.Helper()
	content, err := os.ReadFile(path)
	if err != nil {
		t.Fatalf("Failed to read file: %s", err)
	}
	return content
}

func mustRename(t *testing.T, src, dst string) {
	t.Helper()
	if err := os.Rename(src, dst); err != nil {
		t.Fatalf("Failed to rename: %s", err)
	}
}

func mustRemove(t *testing.T, path string) {
	t.Helper()
	if err := os.Remove(path); err != nil {
		t.Fatalf("Failed to remove file: %s", err)
	}
}

func mustChmod(t *testing.T, path string, mod os.FileMode) {
	t.Helper()
	if err := os.Chmod(path, mod); err != nil {
		t.Fatalf("Failed to chmod: %s", err)
	}
}

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

	buf := make([]byte, 12*1024*1024)
	for i := range buf {
		buf[i] = byte(i)
	}

	fileName := filepath.Join(t.TempDir(), "file")
	mustWriteFile(t, fileName, buf)
	const password = "karp cache tidal mars fed rajah uses graze pobox flew"
	if err := testEncryptOptions.encryptFile(fileName, password); err != nil {
		t.Fatalf("EncryptFile failed: %s", err)
	}
	mustRemove(t, fileName)
	if err := testDecryptOptions.decryptFile(fileName+".enc", password); err != nil {
		t.Fatalf("DecryptFile failed: %s", err)
	}
	gotContents := mustReadFile(t, fileName)
	if !bytes.Equal(gotContents, buf) {
		t.Errorf("contents differ")
	}
}