schastsp/internal/keyset/keyset_test.go

187 lines
3.7 KiB
Go
Raw Permalink Normal View History

package keyset
import (
"bytes"
"git.xdrm.io/logauth/schastsp/context"
"git.xdrm.io/logauth/schastsp/internal/scha"
"testing"
)
func TestGenerationDepthBoundaries(t *testing.T) {
var min, max uint16 = 0x0f0, 0xfff
var rangeMin = min + (max-min)/2
var rangeMax = max
var created *T
ctx, err := context.Create(2.5, min, 0, max)
if err != nil {
t.Errorf("Do not expected an error: %s", err)
return
}
created, err = Create(ctx)
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
if created.depth < min || created.depth > max {
t.Errorf("Expected 'depth' to be in the range [%d, %d], got '%d'", rangeMin, rangeMax, created.depth)
}
}
func TestSchaDecrementingProperty(t *testing.T) {
ctx, err := context.Create(2.5)
if err != nil {
t.Errorf("Do not expected an error")
return
}
var h1, h2, hcheck []byte
var created *T
created, err = Create(ctx)
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
/* (1) Get current hash */
h1, err = created.CurrentHash()
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
/* (2) Decrement */
created.Decrement()
/* (3) Get new hash */
h2, err = created.CurrentHash()
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
/* (4) Try to guess h1 from h2 */
hcheck, err = scha.Hash(h2, 1)
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
/* (5) Fail if property is not fulfilled */
for k, v := range h1 {
if v != hcheck[k] {
t.Errorf("Expected h(h[x-1]) to equal h[x]; expected '%x' ; got '%x'", h1, hcheck)
return
}
}
}
func TestDecrementMinimum(t *testing.T) {
ctx, err := context.Create(2.5)
if err != nil {
t.Errorf("Do not expected an error")
return
}
var h1, h2, hcheck []byte
var created *T
created, err = Create(ctx)
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
/* (1) Get current hash */
h1, err = created.CurrentHash()
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
/* (2) Decrement */
created.Decrement()
/* (3) Get new hash */
h2, err = created.CurrentHash()
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
/* (4) Try to guess h1 from h2 */
hcheck, err = scha.Hash(h2, 1)
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
/* (5) Fail if property is not fulfilled */
for k, v := range h1 {
if v != hcheck[k] {
t.Errorf("Expected h(h[x-1]) to equal h[x]; expected '%x' ; got '%x'", h1, hcheck)
return
}
}
}
func TestRestore(t *testing.T) {
var buffer, srcData bytes.Buffer
var src, dst *T
var ctx *context.T
var err error
/* (1) Create a context */
ctx, err = context.Create(2.2)
if err != nil {
t.Fatalf("[Unexpected error] %s", err)
}
/* (2) Create our source */
src, err = Create(ctx)
if err != nil {
t.Fatalf("[Unexpected error] %s", err)
}
/* (3) Store it to buffer (+to another buffer to check after) */
buffer.Reset()
srcData.Reset()
err = src.Store(&buffer)
if err != nil {
t.Fatalf("[Unexpected error] %s", err)
}
err = src.Store(&srcData)
if err != nil {
t.Fatalf("[Unexpected error] %s", err)
}
/* (4) Create our target */
dst, err = Create(ctx)
if err != nil {
t.Fatalf("[Unexpected error] %s", err)
}
/* (5) Restore from buffer */
err = dst.Fetch(&buffer)
if err != nil {
t.Fatalf("[Unexpected error] %s", err)
}
/* (6) Get both data */
dst.Store(&buffer) // dst data is in buffer
if err != nil {
t.Fatalf("[Unexpected error] %s", err)
}
if len(buffer.Bytes()) != len(srcData.Bytes()) || buffer.String() != srcData.String() {
t.Fatalf("Expected restored keyset to be equal to original ;\n - expected %x\n - got %x", srcData.String(), buffer.String())
}
}