schastsp/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go

168 lines
3.7 KiB
Go
Raw Normal View History

package keyset
import (
"bytes"
"git.xdrm.io/schastsp/lib/context"
"git.xdrm.io/schastsp/lib/scha"
"testing"
)
func TestGenerationDepthBoundaries(t *testing.T) {
var min, max uint16 = 0x0f0, 0xfff
var rangeMin = min + (max-min)/2
var rangeMax = max
var created *Set;
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 *Set
created, err = Create(ctx)
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
/* (1) Get current hash */
h1, err = created.Hash()
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
/* (2) Decrement */
created.Decrement()
/* (3) Get new hash */
h2, err = created.Hash()
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, nil, nil)
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 *Set
created, err = Create(ctx)
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
/* (1) Get current hash */
h1, err = created.Hash()
if err != nil {
t.Errorf("Do not expected an error, got: %s", err)
}
/* (2) Decrement */
created.Decrement()
/* (3) Get new hash */
h2, err = created.Hash()
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, nil, nil)
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 *Set;
var ctx *context.Context;
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() {
2018-04-21 15:06:14 +00:00
t.Fatalf("Expected restored keyset to be equal to original ;\n - expected %x\n - got %x", srcData.String(), buffer.String());
}
}