2018-09-18 01:41:06 -07:00
|
|
|
// Copyright (C) 2018 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 db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDeviceKey(t *testing.T) {
|
|
|
|
fld := []byte("folder6789012345678901234567890123456789012345678901234567890123")
|
|
|
|
dev := []byte("device67890123456789012345678901")
|
|
|
|
name := []byte("name")
|
|
|
|
|
2020-12-21 04:59:22 -07:00
|
|
|
db := newLowlevelMemory(t)
|
2020-02-11 06:31:43 -07:00
|
|
|
defer db.Close()
|
2018-09-18 01:41:06 -07:00
|
|
|
|
2019-11-29 01:11:52 -07:00
|
|
|
key, err := db.keyer.GenerateDeviceFileKey(nil, fld, dev, name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-18 01:41:06 -07:00
|
|
|
|
|
|
|
fld2, ok := db.keyer.FolderFromDeviceFileKey(key)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("unexpectedly not found")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(fld2, fld) {
|
|
|
|
t.Errorf("wrong folder %q != %q", fld2, fld)
|
|
|
|
}
|
|
|
|
dev2, ok := db.keyer.DeviceFromDeviceFileKey(key)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("unexpectedly not found")
|
|
|
|
}
|
|
|
|
if !bytes.Equal(dev2, dev) {
|
|
|
|
t.Errorf("wrong device %q != %q", dev2, dev)
|
|
|
|
}
|
|
|
|
name2 := db.keyer.NameFromDeviceFileKey(key)
|
|
|
|
if !bytes.Equal(name2, name) {
|
|
|
|
t.Errorf("wrong name %q != %q", name2, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGlobalKey(t *testing.T) {
|
|
|
|
fld := []byte("folder6789012345678901234567890123456789012345678901234567890123")
|
|
|
|
name := []byte("name")
|
|
|
|
|
2020-12-21 04:59:22 -07:00
|
|
|
db := newLowlevelMemory(t)
|
2020-02-11 06:31:43 -07:00
|
|
|
defer db.Close()
|
2018-09-18 01:41:06 -07:00
|
|
|
|
2019-11-29 01:11:52 -07:00
|
|
|
key, err := db.keyer.GenerateGlobalVersionKey(nil, fld, name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-18 01:41:06 -07:00
|
|
|
|
|
|
|
name2 := db.keyer.NameFromGlobalVersionKey(key)
|
|
|
|
if !bytes.Equal(name2, name) {
|
|
|
|
t.Errorf("wrong name %q != %q", name2, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSequenceKey(t *testing.T) {
|
|
|
|
fld := []byte("folder6789012345678901234567890123456789012345678901234567890123")
|
|
|
|
|
2020-12-21 04:59:22 -07:00
|
|
|
db := newLowlevelMemory(t)
|
2020-02-11 06:31:43 -07:00
|
|
|
defer db.Close()
|
2018-09-18 01:41:06 -07:00
|
|
|
|
|
|
|
const seq = 1234567890
|
2019-11-29 01:11:52 -07:00
|
|
|
key, err := db.keyer.GenerateSequenceKey(nil, fld, seq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-18 01:41:06 -07:00
|
|
|
outSeq := db.keyer.SequenceFromSequenceKey(key)
|
|
|
|
if outSeq != seq {
|
|
|
|
t.Errorf("sequence number mangled, %d != %d", outSeq, seq)
|
|
|
|
}
|
|
|
|
}
|