storj/pkg/encryption/store_test.go
Jeff Wendling efcdaa43a3
lib/uplink: encryption context (#2349)
* lib/uplink: encryption context

Change-Id: I5c23dca3286a46b713b30c4997e9ae6e630b2280

* lib/uplink: bucket operation examples

Change-Id: Ia0f6e69f365dcff0cf11c731f51b30842bce053b

* lib/uplink: encryption key sharing test cases

Change-Id: I3a172d565f33f4e591402cdcb9460664a7cc7fbe

* fix encrypted path prefix restriction issue

Change-Id: I8f3921f9d52aaf4b84039de608b8cbbc88769554

* implement panics in libuplink encryption code

todo on cipher suite selection as well as an api concern

Change-Id: Ifa39eb3cc4b3443f7d96f9304df9b2ac4ec4085d

* implement GetProjectInfo api call to get salt

Change-Id: Ic5f6b3be9ea35df48c1aa214ab5d355fb328e2cf

* some fixes and accessors for encryption store

Change-Id: I3bb61f6712a037900e2a96e72ad4029ec1d3f718

* general fixes to builds/tests/etc

Change-Id: I9930fa96acb3b221d9a001f8e274af5729cc8a47

* java bindings changes

Change-Id: Ia2bd4c9c69739c8d3154d79616cff1f36fb403b6

* get libuplink examples passing

Change-Id: I828f09a144160e0a5dd932324f78491ae2ec8a07

* fix proto.lock file

Change-Id: I2fbbf4d0976a7d0473c2645e6dcb21aaa3be7651

* fix proto.lock again

Change-Id: I92702cf49e1a340eef6379c2be4f7c4a268112a9

* fix golint issues

Change-Id: I631ff9f43307a58e3b25a58cbb4a4cc2495f5eb6

* more linting fixes

Change-Id: I51f8f30b367b5bca14c94b15417b9a4c9e7aa0ce

* bug fixed by structs bump

Change-Id: Ibb03c691fce7606c35c08721b3ef0781ab48a38a

* retrigger

Change-Id: Ieee0470b6a2d07168a1578552e8e7f271ae93a13

* retrigger

Change-Id: I753d63853171e6a436c104ce176048892eb974c5

* semantic merge conflict

Change-Id: I9419448496de90340569047a6a16a1b858a7978a

* update total to match prod defaults

Change-Id: I693d55c1ebb28b5803ee1d26e9e198decf82308b

* retrigger

Change-Id: I28b74d5d6202f61aa3866fe407d423f6a0a14b9e

* retrigger

Change-Id: I6fd054885c715f602e2cef623fd464c42e88742c

* retrigger

Change-Id: I6a01bae88c72406d4ed5a8f13bf8a2b3c650bd2d
2019-06-27 17:36:51 +00:00

162 lines
5.2 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package encryption
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"storj.io/storj/pkg/paths"
"storj.io/storj/pkg/storj"
)
func printLookup(revealed map[string]string, consumed interface{ Raw() string }, base *Base) {
if base == nil {
fmt.Printf("<%q, %q, nil>\n", revealed, consumed.Raw())
} else {
fmt.Printf("<%q, %q, <%q, %q, %q>>\n",
revealed, consumed, base.Unencrypted, base.Encrypted, base.Key[:2])
}
}
func toKey(val string) (out storj.Key) {
copy(out[:], val)
return out
}
func abortIfError(err error) {
if err != nil {
panic(fmt.Sprintf("%+v", err))
}
}
func ExampleStore() {
s := NewStore()
ep := paths.NewEncrypted
up := paths.NewUnencrypted
// Add a fairly complicated tree to the store.
abortIfError(s.Add("b1", up("u1/u2/u3"), ep("e1/e2/e3"), toKey("k3")))
abortIfError(s.Add("b1", up("u1/u2/u3/u4"), ep("e1/e2/e3/e4"), toKey("k4")))
abortIfError(s.Add("b1", up("u1/u5"), ep("e1/e5"), toKey("k5")))
abortIfError(s.Add("b1", up("u6"), ep("e6"), toKey("k6")))
abortIfError(s.Add("b1", up("u6/u7/u8"), ep("e6/e7/e8"), toKey("k8")))
abortIfError(s.Add("b2", up("u1"), ep("e1'"), toKey("k1")))
abortIfError(s.Add("b3", paths.Unencrypted{}, paths.Encrypted{}, toKey("m1")))
// Look up some complicated queries by the unencrypted path.
printLookup(s.LookupUnencrypted("b1", up("u1")))
printLookup(s.LookupUnencrypted("b1", up("u1/u2/u3")))
printLookup(s.LookupUnencrypted("b1", up("u1/u2/u3/u6")))
printLookup(s.LookupUnencrypted("b1", up("u1/u2/u3/u4")))
printLookup(s.LookupUnencrypted("b1", up("u6/u7")))
printLookup(s.LookupUnencrypted("b2", up("u1")))
printLookup(s.LookupUnencrypted("b3", paths.Unencrypted{}))
printLookup(s.LookupUnencrypted("b3", up("z1")))
fmt.Println()
// Look up some complicated queries by the encrypted path.
printLookup(s.LookupEncrypted("b1", ep("e1")))
printLookup(s.LookupEncrypted("b1", ep("e1/e2/e3")))
printLookup(s.LookupEncrypted("b1", ep("e1/e2/e3/e6")))
printLookup(s.LookupEncrypted("b1", ep("e1/e2/e3/e4")))
printLookup(s.LookupEncrypted("b1", ep("e6/e7")))
printLookup(s.LookupEncrypted("b2", ep("e1'")))
printLookup(s.LookupEncrypted("b3", paths.Encrypted{}))
printLookup(s.LookupEncrypted("b3", ep("z1")))
// output:
//
// <map["e2":"u2" "e5":"u5"], "u1", nil>
// <map["e4":"u4"], "u1/u2/u3", <"u1/u2/u3", "e1/e2/e3", "k3">>
// <map[], "u1/u2/u3/", <"u1/u2/u3", "e1/e2/e3", "k3">>
// <map[], "u1/u2/u3/u4", <"u1/u2/u3/u4", "e1/e2/e3/e4", "k4">>
// <map["e8":"u8"], "u6/", <"u6", "e6", "k6">>
// <map[], "u1", <"u1", "e1'", "k1">>
// <map[], "", <"", "", "m1">>
// <map[], "", <"", "", "m1">>
//
// <map["u2":"e2" "u5":"e5"], "e1", nil>
// <map["u4":"e4"], "e1/e2/e3", <"u1/u2/u3", "e1/e2/e3", "k3">>
// <map[], "e1/e2/e3/", <"u1/u2/u3", "e1/e2/e3", "k3">>
// <map[], "e1/e2/e3/e4", <"u1/u2/u3/u4", "e1/e2/e3/e4", "k4">>
// <map["u8":"e8"], "e6/", <"u6", "e6", "k6">>
// <map[], "e1'", <"u1", "e1'", "k1">>
// <map[], "", <"", "", "m1">>
// <map[], "", <"", "", "m1">>
}
func TestStoreErrors(t *testing.T) {
s := NewStore()
ep := paths.NewEncrypted
up := paths.NewUnencrypted
// Too many encrypted parts
require.Error(t, s.Add("b1", up("u1"), ep("e1/e2/e3"), storj.Key{}))
// Too many unencrypted parts
require.Error(t, s.Add("b1", up("u1/u2/u3"), ep("e1"), storj.Key{}))
// Mismatches
require.NoError(t, s.Add("b1", up("u1"), ep("e1"), storj.Key{}))
require.Error(t, s.Add("b1", up("u2"), ep("e1"), storj.Key{}))
require.Error(t, s.Add("b1", up("u1"), ep("f1"), storj.Key{}))
}
func TestStoreErrorState(t *testing.T) {
s := NewStore()
ep := paths.NewEncrypted
up := paths.NewUnencrypted
// Do an empty lookup.
revealed1, consumed1, base1 := s.LookupUnencrypted("b1", up("u1/u2"))
// Attempt to do an addition that fails.
require.Error(t, s.Add("b1", up("u1/u2"), ep("e1/e2/e3"), storj.Key{}))
// Ensure that we get the same results as before
revealed2, consumed2, base2 := s.LookupUnencrypted("b1", up("u1/u2"))
assert.Equal(t, revealed1, revealed2)
assert.Equal(t, consumed1, consumed2)
assert.Equal(t, base1, base2)
}
func TestStoreIterate(t *testing.T) {
s := NewStore()
ep := paths.NewEncrypted
up := paths.NewUnencrypted
type storeEntry struct {
bucket string
unenc paths.Unencrypted
enc paths.Encrypted
key storj.Key
}
expected := map[storeEntry]struct{}{
{"b1", up("u1/u2/u3"), ep("e1/e2/e3"), toKey("k3")}: {},
{"b1", up("u1/u2/u3/u4"), ep("e1/e2/e3/e4"), toKey("k4")}: {},
{"b1", up("u1/u5"), ep("e1/e5"), toKey("k5")}: {},
{"b1", up("u6"), ep("e6"), toKey("k6")}: {},
{"b1", up("u6/u7/u8"), ep("e6/e7/e8"), toKey("k8")}: {},
{"b2", up("u1"), ep("e1'"), toKey("k1")}: {},
{"b3", paths.Unencrypted{}, paths.Encrypted{}, toKey("m1")}: {},
}
for entry := range expected {
require.NoError(t, s.Add(entry.bucket, entry.unenc, entry.enc, entry.key))
}
got := make(map[storeEntry]struct{})
require.NoError(t, s.Iterate(func(bucket string, unenc paths.Unencrypted, enc paths.Encrypted, key storj.Key) error {
got[storeEntry{bucket, unenc, enc, key}] = struct{}{}
return nil
}))
require.Equal(t, expected, got)
}