MCPcopy
hub / github.com/syncthing/syncthing / TestEnDecryptName

Function TestEnDecryptName

lib/protocol/encryption_test.go:29–83  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

27)
28
29func TestEnDecryptName(t *testing.T) {
30 if cryptoIsBrokenUnderRaceDetector {
31 t.Skip("cannot test")
32 }
33
34 pattern := regexp.MustCompile(
35 fmt.Sprintf("^[0-9A-V]%s/[0-9A-V]{2}/([0-9A-V]{%d}/)*[0-9A-V]{1,%d}$",
36 regexp.QuoteMeta(encryptedDirExtension),
37 maxPathComponent, maxPathComponent-1))
38
39 makeName := func(n int) string {
40 b := make([]byte, n)
41 for i := range b {
42 b[i] = byte('a' + i%26)
43 }
44 return string(b)
45 }
46
47 var key [32]byte
48 cases := []string{
49 "",
50 "foo",
51 "a longer name/with/slashes and spaces",
52 makeName(maxPathComponent),
53 makeName(1 + maxPathComponent),
54 makeName(2 * maxPathComponent),
55 makeName(1 + 2*maxPathComponent),
56 }
57 for _, tc := range cases {
58 var prev string
59 for i := 0; i < 5; i++ {
60 enc := encryptName(tc, &key)
61 if prev != "" && prev != enc {
62 t.Error("name should always encrypt the same")
63 }
64 prev = enc
65 if tc != "" && strings.Contains(enc, tc) {
66 t.Error("shouldn't contain plaintext")
67 }
68 if !pattern.MatchString(enc) {
69 t.Fatalf("encrypted name %s doesn't match %s",
70 enc, pattern)
71 }
72
73 dec, err := decryptName(enc, &key)
74 if err != nil {
75 t.Error(err)
76 }
77 if dec != tc {
78 t.Error("mismatch after decryption")
79 }
80 t.Logf("%q encrypts as %q", tc, enc)
81 }
82 }
83}
84
85func TestKeyDerivation(t *testing.T) {
86 folderKey := testKeyGen.KeyFromPassword("my folder", "my password")

Callers

nothing calls this directly

Calls 4

encryptNameFunction · 0.85
decryptNameFunction · 0.85
ContainsMethod · 0.80
ErrorMethod · 0.65

Tested by

no test coverage detected