2018-04-21 09:58:47 +00:00
|
|
|
package keyset
|
|
|
|
|
|
|
|
import (
|
2018-04-21 15:04:17 +00:00
|
|
|
"bytes"
|
2018-04-21 14:39:24 +00:00
|
|
|
"git.xdrm.io/schastsp/lib/context"
|
2018-04-21 09:58:47 +00:00
|
|
|
"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
|
2018-04-21 14:39:24 +00:00
|
|
|
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 }
|
2018-04-21 09:58:47 +00:00
|
|
|
|
2018-04-21 14:39:24 +00:00
|
|
|
created, err = Create(ctx)
|
2018-04-21 09:58:47 +00:00
|
|
|
|
|
|
|
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) {
|
2018-04-21 14:39:24 +00:00
|
|
|
ctx, err := context.Create(2.5);
|
|
|
|
if err != nil { t.Errorf("Do not expected an error"); return }
|
2018-04-21 09:58:47 +00:00
|
|
|
|
|
|
|
var h1, h2, hcheck []byte
|
|
|
|
var created *Set
|
|
|
|
|
2018-04-21 14:39:24 +00:00
|
|
|
created, err = Create(ctx)
|
2018-04-21 09:58:47 +00:00
|
|
|
|
|
|
|
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] {
|
2018-04-21 14:39:24 +00:00
|
|
|
t.Errorf("Expected h(h[x-1]) to equal h[x]; expected '%x' ; got '%x'", h1, hcheck)
|
2018-04-21 09:58:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecrementMinimum(t *testing.T) {
|
2018-04-21 14:39:24 +00:00
|
|
|
ctx, err := context.Create(2.5);
|
|
|
|
if err != nil { t.Errorf("Do not expected an error"); return }
|
2018-04-21 09:58:47 +00:00
|
|
|
|
|
|
|
var h1, h2, hcheck []byte
|
|
|
|
var created *Set
|
|
|
|
|
2018-04-21 14:39:24 +00:00
|
|
|
created, err = Create(ctx)
|
2018-04-21 09:58:47 +00:00
|
|
|
|
|
|
|
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] {
|
2018-04-21 14:39:24 +00:00
|
|
|
t.Errorf("Expected h(h[x-1]) to equal h[x]; expected '%x' ; got '%x'", h1, hcheck)
|
2018-04-21 09:58:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-04-21 15:04:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
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() {
|
|
|
|
t.Fatalf("Expected restored keyset to be equal to original ; expected %x ; got %x", srcData, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|