[lib.context] updated + cleaned up [client.keyset] added tests + cleaned up
This commit is contained in:
parent
938df1b63c
commit
907f16245a
|
@ -28,17 +28,14 @@ type Set struct {
|
||||||
|
|
||||||
/* (1) Creates a new KeySet
|
/* (1) Creates a new KeySet
|
||||||
*
|
*
|
||||||
* @min<uint16> Minimum depth value
|
* @ctx<Context> Context constants
|
||||||
* @max<uint16> Maximum depth value
|
|
||||||
*
|
|
||||||
* @return outName<outType> outDesc
|
|
||||||
*
|
*
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
func Create(ctx *context.Context, max uint16) (*Set, error) {
|
func Create(ctx *context.Context) (*Set, error) {
|
||||||
|
|
||||||
/* (1) Fail if min+thre >= max */
|
/* (1) Fail if min+thre >= max */
|
||||||
if max < ctx.MinDepth()+ctx.DepthThreshold() {
|
if ctx == nil {
|
||||||
return nil, errors.New("Maximum depth must be greater than Min+threshold (from given Context)")
|
return nil, errors.New("Context must not be null")
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) Instanciate */
|
/* (2) Instanciate */
|
||||||
|
@ -46,7 +43,6 @@ func Create(ctx *context.Context, max uint16) (*Set, error) {
|
||||||
|
|
||||||
/* (3) Set attributes */
|
/* (3) Set attributes */
|
||||||
instance.ctx = ctx
|
instance.ctx = ctx
|
||||||
instance.max = max
|
|
||||||
|
|
||||||
/* (4) Generate values if <nil> secret */
|
/* (4) Generate values if <nil> secret */
|
||||||
if instance.sec == nil {
|
if instance.sec == nil {
|
||||||
|
@ -77,7 +73,7 @@ func (s *Set) generate() {
|
||||||
/* (2) Manage other attributes
|
/* (2) Manage other attributes
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
/* (1) Random depth pick init */
|
/* (1) Random depth pick init */
|
||||||
var randMin, randMax uint16 = s.ctx.MinDepth() + (s.max-s.ctx.MinDepth())/2, s.max
|
var randMin, randMax = s.ctx.MinDepth() + (s.ctx.MaxDepth()-s.ctx.MinDepth()) / 2, s.ctx.MaxDepth()
|
||||||
|
|
||||||
/* (2) Select "random" depth */
|
/* (2) Select "random" depth */
|
||||||
s.depth = randMin + uint16(rand.Intn(int(randMax-randMin)))
|
s.depth = randMin + uint16(rand.Intn(int(randMax-randMin)))
|
||||||
|
|
|
@ -1,27 +1,21 @@
|
||||||
package keyset
|
package keyset
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.xdrm.io/schastsp/lib/context"
|
||||||
"git.xdrm.io/schastsp/lib/scha"
|
"git.xdrm.io/schastsp/lib/scha"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateNotMaxGreaterThanMin(t *testing.T) {
|
|
||||||
|
|
||||||
var _, err = Create(2, 1)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("Expected an error")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGenerationDepthBoundaries(t *testing.T) {
|
func TestGenerationDepthBoundaries(t *testing.T) {
|
||||||
|
|
||||||
var min, max uint16 = 0x0f0, 0xfff
|
var min, max uint16 = 0x0f0, 0xfff
|
||||||
var rangeMin = min + (max-min)/2
|
var rangeMin = min + (max-min)/2
|
||||||
var rangeMax = max
|
var rangeMax = max
|
||||||
|
var created *Set;
|
||||||
|
|
||||||
var created, err = Create(min, max)
|
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 {
|
if err != nil {
|
||||||
t.Errorf("Do not expected an error, got: %s", err)
|
t.Errorf("Do not expected an error, got: %s", err)
|
||||||
|
@ -34,12 +28,13 @@ func TestGenerationDepthBoundaries(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSchaDecrementingProperty(t *testing.T) {
|
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 h1, h2, hcheck []byte
|
||||||
var err error
|
|
||||||
var created *Set
|
var created *Set
|
||||||
|
|
||||||
created, err = Create(0x0f0, 0xfff)
|
created, err = Create(ctx)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Do not expected an error, got: %s", err)
|
t.Errorf("Do not expected an error, got: %s", err)
|
||||||
|
@ -70,7 +65,7 @@ func TestSchaDecrementingProperty(t *testing.T) {
|
||||||
for k, v := range h1 {
|
for k, v := range h1 {
|
||||||
|
|
||||||
if v != hcheck[k] {
|
if v != hcheck[k] {
|
||||||
t.Errorf("Expected h(h[x-1]) to equel h[x]; expected '%x' ; got '%x'", h1, hcheck)
|
t.Errorf("Expected h(h[x-1]) to equal h[x]; expected '%x' ; got '%x'", h1, hcheck)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,12 +74,13 @@ func TestSchaDecrementingProperty(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecrementMinimum(t *testing.T) {
|
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 h1, h2, hcheck []byte
|
||||||
var err error
|
|
||||||
var created *Set
|
var created *Set
|
||||||
|
|
||||||
created, err = Create(0x0f0, 0xfff)
|
created, err = Create(ctx)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Do not expected an error, got: %s", err)
|
t.Errorf("Do not expected an error, got: %s", err)
|
||||||
|
@ -115,7 +111,7 @@ func TestDecrementMinimum(t *testing.T) {
|
||||||
for k, v := range h1 {
|
for k, v := range h1 {
|
||||||
|
|
||||||
if v != hcheck[k] {
|
if v != hcheck[k] {
|
||||||
t.Errorf("Expected h(h[x-1]) to equel h[x]; expected '%x' ; got '%x'", h1, hcheck)
|
t.Errorf("Expected h(h[x-1]) to equal h[x]; expected '%x' ; got '%x'", h1, hcheck)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,34 +4,77 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* (1) Context class
|
/* (1) Definitions
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
/* (1) Attributes */
|
/* (1) Default values */
|
||||||
|
const DefaultMin = 0x00f0
|
||||||
|
const DefaultMax = 0x0fff
|
||||||
|
const DefaultThr = 0x000a
|
||||||
|
|
||||||
|
|
||||||
|
/* (2) Struct attributes */
|
||||||
type Context struct {
|
type Context struct {
|
||||||
win float64; // 'timeid' window size
|
win float64; // 'timeid' window size
|
||||||
dMin uint16; // minimum scha depth
|
min uint16; // minimum scha depth
|
||||||
dThreshold uint16; // scha depth threshold
|
max uint16; // maximum scha depth
|
||||||
|
thr uint16; // scha depth threshold
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) Constructor */
|
/* (3) Constructor
|
||||||
func Create(win float64, dMin uint16, dThreshold uint16) (*Context, error) {
|
*
|
||||||
|
* @win<float64> Window size for 'timeid' package
|
||||||
|
* @min<uint16> [OPT] Minimum scha depth
|
||||||
|
* @thr<uint16> [OPT] scha depth threshold before renewal
|
||||||
|
* @max<uint16> [OPT] Maximum scha depth
|
||||||
|
*
|
||||||
|
* @return outName<outType> outDesc
|
||||||
|
*
|
||||||
|
---------------------------------------------------------*/
|
||||||
|
|
||||||
|
func Create(win float64, optional... uint16) (*Context, error) {
|
||||||
|
|
||||||
var instance = new(Context);
|
var instance = new(Context);
|
||||||
|
|
||||||
/* (1) Window size error */
|
/* (1) Window size error */
|
||||||
if win < 0 { return nil, errors.New("Window size must be positive and is negative") }
|
if win < 0 { return nil, errors.New("Window size must be positive and is negative") }
|
||||||
instance.win = win;
|
instance.win = win;
|
||||||
|
|
||||||
/* (2) Minimum Depth error */
|
/* (2) Default values */
|
||||||
if dMin < 0x0f { return nil, errors.New("Minimum depth must be greater than 0x0f (decimal 15) for consistency issues") }
|
instance.min = DefaultMin
|
||||||
instance.dMin = dMin;
|
instance.thr = DefaultThr
|
||||||
|
instance.max = DefaultMax
|
||||||
|
|
||||||
/* (3) Depth Threshold error */
|
/* (3) Optional 'min' */
|
||||||
instance.dThreshold = dThreshold;
|
if len(optional) > 0 {
|
||||||
|
|
||||||
|
if optional[0] < 0x0f { return nil, errors.New("Minimum depth must be greater than 0x0f (decimal 15) for consistency issues") }
|
||||||
|
|
||||||
|
instance.min = optional[0];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (4) Optional 'thr' */
|
||||||
|
if len(optional) > 1 {
|
||||||
|
instance.thr = optional[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* (5) Optional 'max' */
|
||||||
|
if len(optional) > 2 {
|
||||||
|
|
||||||
|
if optional[2] <= instance.min+instance.thr {
|
||||||
|
return nil, errors.New("Minimum depth must be greater than 0x0f (decimal 15) for consistency issues")
|
||||||
|
}
|
||||||
|
|
||||||
|
instance.max = optional[2];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
return instance, nil
|
return instance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (3) Getters */
|
/* (4) Getters */
|
||||||
func (c Context) Window() float64 { return c.win }
|
func (c Context) Window() float64 { return c.win }
|
||||||
func (c Context) MinDepth() uint16 { return c.dMin }
|
func (c Context) MinDepth() uint16 { return c.min }
|
||||||
func (c Context) DepthThreshold() uint16 { return c.dThreshold }
|
func (c Context) MaxDepth() uint16 { return c.max }
|
||||||
|
func (c Context) DepthThreshold() uint16 { return c.thr }
|
Loading…
Reference in New Issue