2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-10-07 13:05:04 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// 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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-10-07 13:05:04 +00:00
|
|
|
|
2015-01-12 13:50:30 +00:00
|
|
|
package db
|
2014-10-07 13:05:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDeviceKey(t *testing.T) {
|
|
|
|
fld := []byte("folder6789012345678901234567890123456789012345678901234567890123")
|
|
|
|
dev := []byte("device67890123456789012345678901")
|
|
|
|
name := []byte("name")
|
|
|
|
|
2016-01-03 18:08:19 +00:00
|
|
|
db := OpenMemory()
|
|
|
|
db.folderIdx.ID(fld)
|
|
|
|
db.deviceIdx.ID(dev)
|
2014-10-07 13:05:04 +00:00
|
|
|
|
2015-10-31 06:20:35 +00:00
|
|
|
key := db.deviceKey(fld, dev, name)
|
|
|
|
|
|
|
|
fld2 := db.deviceKeyFolder(key)
|
2016-03-31 15:12:46 +00:00
|
|
|
if !bytes.Equal(fld2, fld) {
|
2014-10-07 13:05:04 +00:00
|
|
|
t.Errorf("wrong folder %q != %q", fld2, fld)
|
|
|
|
}
|
2015-10-31 06:20:35 +00:00
|
|
|
dev2 := db.deviceKeyDevice(key)
|
2016-03-31 15:12:46 +00:00
|
|
|
if !bytes.Equal(dev2, dev) {
|
2014-10-07 13:05:04 +00:00
|
|
|
t.Errorf("wrong device %q != %q", dev2, dev)
|
|
|
|
}
|
2015-10-31 06:20:35 +00:00
|
|
|
name2 := db.deviceKeyName(key)
|
2016-03-31 15:12:46 +00:00
|
|
|
if !bytes.Equal(name2, name) {
|
2014-10-07 13:05:04 +00:00
|
|
|
t.Errorf("wrong name %q != %q", name2, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGlobalKey(t *testing.T) {
|
|
|
|
fld := []byte("folder6789012345678901234567890123456789012345678901234567890123")
|
|
|
|
name := []byte("name")
|
|
|
|
|
2016-01-03 18:08:19 +00:00
|
|
|
db := OpenMemory()
|
|
|
|
db.folderIdx.ID(fld)
|
2015-10-31 06:20:35 +00:00
|
|
|
|
|
|
|
key := db.globalKey(fld, name)
|
2014-10-07 13:05:04 +00:00
|
|
|
|
2017-01-04 10:34:52 +00:00
|
|
|
fld2, ok := db.globalKeyFolder(key)
|
|
|
|
if !ok {
|
|
|
|
t.Error("should have been found")
|
|
|
|
}
|
2016-03-31 15:12:46 +00:00
|
|
|
if !bytes.Equal(fld2, fld) {
|
2014-10-07 13:05:04 +00:00
|
|
|
t.Errorf("wrong folder %q != %q", fld2, fld)
|
|
|
|
}
|
2015-10-31 06:20:35 +00:00
|
|
|
name2 := db.globalKeyName(key)
|
2016-03-31 15:12:46 +00:00
|
|
|
if !bytes.Equal(name2, name) {
|
2014-10-07 13:05:04 +00:00
|
|
|
t.Errorf("wrong name %q != %q", name2, name)
|
|
|
|
}
|
2017-01-04 10:34:52 +00:00
|
|
|
|
|
|
|
_, ok = db.globalKeyFolder([]byte{1, 2, 3, 4, 5})
|
|
|
|
if ok {
|
|
|
|
t.Error("should not have been found")
|
|
|
|
}
|
2014-10-07 13:05:04 +00:00
|
|
|
}
|