From 2560da5bbc5b1f1ed30f0c714f408ab38182a3a3 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 21 Apr 2018 17:04:17 +0200 Subject: [PATCH] [client.keyset] added test for 'Store()'/'Fetch()' mecanism --- .../schastsp/client/keyset/keyset_test.go | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go b/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go index dde68a0..b64f5e5 100644 --- a/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go +++ b/src/git.xdrm.io/schastsp/client/keyset/keyset_test.go @@ -1,6 +1,7 @@ package keyset import ( + "bytes" "git.xdrm.io/schastsp/lib/context" "git.xdrm.io/schastsp/lib/scha" "testing" @@ -118,3 +119,50 @@ func TestDecrementMinimum(t *testing.T) { } } + + +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); + } + + + + + + + +} \ No newline at end of file