package oltp
import (
"bytes"
"testing"
)
// A derived key is stable for its purpose across opens of the same data
// directory, differs between purposes, and is not the sealing key.
func TestDerivedKeyIsStablePerPurpose(t *testing.T) {
d := testDB(t)
a, err := d.DerivedKey("one")
if err != nil {
t.Fatalf("FAIL: %v", err)
}
b, _ := d.DerivedKey("one")
c, _ := d.DerivedKey("two")
if !bytes.Equal(a, b) || len(a) != 32 {
t.Errorf("FAIL: not stable, or not 32 bytes: %d", len(a))
}
if bytes.Equal(a, c) {
t.Errorf("FAIL: two purposes share a key")
}
if seal, _ := d.seal.load(); bytes.Equal(seal, a) {
t.Errorf("FAIL: the derived key is the sealing key")
}
again := &DB{seal: &sealer{path: d.seal.path}}
if x, _ := again.DerivedKey("one"); !bytes.Equal(x, a) {
t.Errorf("FAIL: another open of the same directory derived another key")
}
if !t.Failed() {
t.Log("ok")
}
}