123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- // Copyright (C) 2019 The Syncthing Authors.
- //
- // This Source Code Form is subject to the terms of the Mozilla Public
- // License, v. 2.0. If a copy of the MPL was not distributed with this file,
- // You can obtain one at https://mozilla.org/MPL/2.0/.
- package protocol
- import (
- "bytes"
- "reflect"
- "strings"
- "testing"
- "github.com/syncthing/syncthing/lib/rand"
- )
- func TestEnDecryptName(t *testing.T) {
- var key [32]byte
- cases := []string{
- "",
- "foo",
- "a longer name/with/slashes and spaces",
- }
- for _, tc := range cases {
- var prev string
- for i := 0; i < 5; i++ {
- enc := encryptName(tc, &key)
- if prev != "" && prev != enc {
- t.Error("name should always encrypt the same")
- }
- prev = enc
- if tc != "" && strings.Contains(enc, tc) {
- t.Error("shouldn't contain plaintext")
- }
- dec, err := decryptName(enc, &key)
- if err != nil {
- t.Error(err)
- }
- if dec != tc {
- t.Error("mismatch after decryption")
- }
- t.Log(enc)
- }
- }
- }
- func TestEnDecryptBytes(t *testing.T) {
- var key [32]byte
- cases := [][]byte{
- {},
- {1, 2, 3, 4, 5},
- }
- for _, tc := range cases {
- var prev []byte
- for i := 0; i < 5; i++ {
- enc := encryptBytes(tc, &key)
- if bytes.Equal(enc, prev) {
- t.Error("encryption should not repeat")
- }
- prev = enc
- if len(tc) > 0 && bytes.Contains(enc, tc) {
- t.Error("shouldn't contain plaintext")
- }
- dec, err := DecryptBytes(enc, &key)
- if err != nil {
- t.Error(err)
- }
- if !bytes.Equal(dec, tc) {
- t.Error("mismatch after decryption")
- }
- }
- }
- }
- func TestEnDecryptFileInfo(t *testing.T) {
- var key [32]byte
- fi := FileInfo{
- Name: "hello",
- Size: 45,
- Permissions: 0755,
- ModifiedS: 8080,
- Blocks: []BlockInfo{
- {
- Size: 45,
- Hash: []byte{1, 2, 3},
- },
- },
- }
- enc := encryptFileInfo(fi, &key)
- dec, err := DecryptFileInfo(enc, &key)
- if err != nil {
- t.Error(err)
- }
- if !reflect.DeepEqual(fi, dec) {
- t.Error("mismatch after decryption")
- }
- }
- func TestIsEncryptedParent(t *testing.T) {
- comp := rand.String(maxPathComponent)
- cases := []struct {
- path string
- is bool
- }{
- {"", false},
- {".", false},
- {"/", false},
- {"12" + encryptedDirExtension, false},
- {"1" + encryptedDirExtension, true},
- {"1" + encryptedDirExtension + "/b", false},
- {"1" + encryptedDirExtension + "/bc", true},
- {"1" + encryptedDirExtension + "/bcd", false},
- {"1" + encryptedDirExtension + "/bc/foo", false},
- {"1.12/22", false},
- {"1" + encryptedDirExtension + "/bc/" + comp, true},
- {"1" + encryptedDirExtension + "/bc/" + comp + "/" + comp, true},
- {"1" + encryptedDirExtension + "/bc/" + comp + "a", false},
- {"1" + encryptedDirExtension + "/bc/" + comp + "/a/" + comp, false},
- }
- for _, tc := range cases {
- if res := IsEncryptedParent(tc.path); res != tc.is {
- t.Errorf("%v: got %v, expected %v", tc.path, res, tc.is)
- }
- }
- }
|