summaryrefslogtreecommitdiff
path: root/tests/cli/xchacha_test.go
blob: 7f24c8b74b83d2d9648acfc8d7da4c374517faa9 (plain)
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
package cli

import (
	"fmt"
	"io/ioutil"
	"os"
	"syscall"
	"testing"

	"github.com/rfjakob/gocryptfs/v2/internal/configfile"

	"github.com/rfjakob/gocryptfs/v2/tests/test_helpers"
)

// Create "-xchacha" fs
func TestInitXchacha(t *testing.T) {
	cDir := test_helpers.InitFS(nil, "-xchacha")
	// Check config file sanity
	_, c, err := configfile.LoadAndDecrypt(cDir+"/"+configfile.ConfDefaultName, testPw)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	if c.IsFeatureFlagSet(configfile.FlagGCMIV128) {
		t.Error("GCMIV128 flag should be off")
	}
	if !c.IsFeatureFlagSet(configfile.FlagXChaCha20Poly1305) {
		t.Error("XChaCha20Poly1305 flag should be on")
	}
	if !c.IsFeatureFlagSet(configfile.FlagHKDF) {
		t.Error("HKDF flag should be on")
	}
}

// Create and mount "-xchacha" fs, and see if we get the expected file sizes
// (xchacha has longer IVs).
func TestXchacha(t *testing.T) {
	cDir := test_helpers.InitFS(nil, "-xchacha", "-plaintextnames")
	pDir := cDir + ".mnt"
	test_helpers.MountOrExit(cDir, pDir, "-extpass", "echo test")
	defer test_helpers.UnmountPanic(pDir)

	if err := ioutil.WriteFile(pDir+"/1byte", []byte("x"), 0700); err != nil {
		t.Fatal(err)
	}
	var st syscall.Stat_t
	if err := syscall.Stat(cDir+"/1byte", &st); err != nil {
		t.Fatal(err)
	}
	// 2 byte version header + 16 byte file id + 192 bit xchacha iv + 1 byte payload + 16 byte mac
	if st.Size != 2+16+24+1+16 {
		t.Errorf("wrong size %d", st.Size)
	}

	// 1 MiB = 256 4kiB blocks
	if err := ioutil.WriteFile(pDir+"/1MiB", make([]byte, 1024*1024), 0700); err != nil {
		t.Fatal(err)
	}
	if err := syscall.Stat(cDir+"/1MiB", &st); err != nil {
		t.Fatal(err)
	}
	// 2 byte version header + (16 byte file id + 192 bit xchacha iv + 4096 byte payload + 16 byte mac)*256
	if st.Size != 2+16+(24+4096+16)*256 {
		t.Errorf("wrong size %d", st.Size)
	}
}