[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
|
||||
*
|
||||
* @min<uint16> Minimum depth value
|
||||
* @max<uint16> Maximum depth value
|
||||
*
|
||||
* @return outName<outType> outDesc
|
||||
* @ctx<Context> Context constants
|
||||
*
|
||||
---------------------------------------------------------*/
|
||||
func Create(ctx *context.Context, max uint16) (*Set, error) {
|
||||
func Create(ctx *context.Context) (*Set, error) {
|
||||
|
||||
/* (1) Fail if min+thre >= max */
|
||||
if max < ctx.MinDepth()+ctx.DepthThreshold() {
|
||||
return nil, errors.New("Maximum depth must be greater than Min+threshold (from given Context)")
|
||||
if ctx == nil {
|
||||
return nil, errors.New("Context must not be null")
|
||||
}
|
||||
|
||||
/* (2) Instanciate */
|
||||
|
@ -46,7 +43,6 @@ func Create(ctx *context.Context, max uint16) (*Set, error) {
|
|||
|
||||
/* (3) Set attributes */
|
||||
instance.ctx = ctx
|
||||
instance.max = max
|
||||
|
||||
/* (4) Generate values if <nil> secret */
|
||||
if instance.sec == nil {
|
||||
|
@ -77,7 +73,7 @@ func (s *Set) generate() {
|
|||
/* (2) Manage other attributes
|
||||
---------------------------------------------------------*/
|
||||
/* (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 */
|
||||
s.depth = randMin + uint16(rand.Intn(int(randMax-randMin)))
|
||||
|
|
|
@ -1,27 +1,21 @@
|
|||
package keyset
|
||||
|
||||
import (
|
||||
"git.xdrm.io/schastsp/lib/context"
|
||||
"git.xdrm.io/schastsp/lib/scha"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateNotMaxGreaterThanMin(t *testing.T) {
|
||||
|
||||
var _, err = Create(2, 1)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("Expected an error")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestGenerationDepthBoundaries(t *testing.T) {
|
||||
|
||||
var min, max uint16 = 0x0f0, 0xfff
|
||||
var rangeMin = min + (max-min)/2
|
||||
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 {
|
||||
t.Errorf("Do not expected an error, got: %s", err)
|
||||
|
@ -34,12 +28,13 @@ func TestGenerationDepthBoundaries(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 err error
|
||||
var created *Set
|
||||
|
||||
created, err = Create(0x0f0, 0xfff)
|
||||
created, err = Create(ctx)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Do not expected an error, got: %s", err)
|
||||
|
@ -70,7 +65,7 @@ func TestSchaDecrementingProperty(t *testing.T) {
|
|||
for k, v := range h1 {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -79,12 +74,13 @@ func TestSchaDecrementingProperty(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 err error
|
||||
var created *Set
|
||||
|
||||
created, err = Create(0x0f0, 0xfff)
|
||||
created, err = Create(ctx)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Do not expected an error, got: %s", err)
|
||||
|
@ -115,7 +111,7 @@ func TestDecrementMinimum(t *testing.T) {
|
|||
for k, v := range h1 {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -4,34 +4,77 @@ import (
|
|||
"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 {
|
||||
win float64; // 'timeid' window size
|
||||
dMin uint16; // minimum scha depth
|
||||
dThreshold uint16; // scha depth threshold
|
||||
win float64; // 'timeid' window size
|
||||
min uint16; // minimum scha depth
|
||||
max uint16; // maximum scha depth
|
||||
thr uint16; // scha depth threshold
|
||||
}
|
||||
|
||||
/* (2) Constructor */
|
||||
func Create(win float64, dMin uint16, dThreshold uint16) (*Context, error) {
|
||||
/* (3) Constructor
|
||||
*
|
||||
* @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);
|
||||
|
||||
/* (1) Window size error */
|
||||
if win < 0 { return nil, errors.New("Window size must be positive and is negative") }
|
||||
instance.win = win;
|
||||
|
||||
/* (2) Minimum Depth error */
|
||||
if dMin < 0x0f { return nil, errors.New("Minimum depth must be greater than 0x0f (decimal 15) for consistency issues") }
|
||||
instance.dMin = dMin;
|
||||
/* (2) Default values */
|
||||
instance.min = DefaultMin
|
||||
instance.thr = DefaultThr
|
||||
instance.max = DefaultMax
|
||||
|
||||
/* (3) Depth Threshold error */
|
||||
instance.dThreshold = dThreshold;
|
||||
/* (3) Optional 'min' */
|
||||
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
|
||||
}
|
||||
|
||||
/* (3) Getters */
|
||||
/* (4) Getters */
|
||||
func (c Context) Window() float64 { return c.win }
|
||||
func (c Context) MinDepth() uint16 { return c.dMin }
|
||||
func (c Context) DepthThreshold() uint16 { return c.dThreshold }
|
||||
func (c Context) MinDepth() uint16 { return c.min }
|
||||
func (c Context) MaxDepth() uint16 { return c.max }
|
||||
func (c Context) DepthThreshold() uint16 { return c.thr }
|
Loading…
Reference in New Issue