From 114145264bac48ea5d751ce9e411ec3a114ce3cc Mon Sep 17 00:00:00 2001 From: Lucas Menendez Date: Sat, 27 Apr 2024 00:44:09 +0200 Subject: [PATCH] more test coverage --- config.go | 3 +++ config_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++- encode_test.go | 17 ++++++++++++++ sss.go | 9 +++----- 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/config.go b/config.go index 6486928..5037c2e 100644 --- a/config.go +++ b/config.go @@ -52,6 +52,9 @@ func (c *Config) ValidPrime() error { if !c.Prime.ProbablyPrime(0) { return ErrConfigInvalidPrime } + if len(c.Prime.Bytes()) < 2 { + return ErrConfigInvalidPrime + } return nil } diff --git a/config_test.go b/config_test.go index 5222e01..e262896 100644 --- a/config_test.go +++ b/config_test.go @@ -17,9 +17,69 @@ func Test_prepare(t *testing.T) { if c.Prime.Cmp(newPrime) != 0 { t.Errorf("expected %v, got %v", newPrime, c.Prime) } + if err := c.ValidPrime(); err == nil { + t.Errorf("expected error, got nil") + } + c.Prime = DefaultPrime + if err := c.ValidPrime(); err != nil { + t.Errorf("unexpected error: %v", err) + } + c.Prime = nil + if err := c.ValidPrime(); err == nil { + t.Errorf("expected error, got nil") + } +} + +func TestValidPrime(t *testing.T) { + c := Config{} + c.prepare() + if err := c.ValidPrime(); err != nil { + t.Errorf("unexpected error: %v", err) + } + c.Prime = nil + if err := c.ValidPrime(); err == nil { + t.Errorf("expected error, got nil") + } + c.Prime = big.NewInt(1002) + if err := c.ValidPrime(); err == nil { + t.Errorf("expected error, got nil") + } +} + +func TestValidConfig(t *testing.T) { + c := Config{} + c.prepare() + if err := c.ValidConfig([]byte{}); err == nil { + t.Errorf("expected error, got nil") + } + c.Shares = MinShares - 1 + c.Min = MinMinShares + if err := c.ValidConfig([]byte{}); err == nil { + t.Errorf("expected error, got nil") + } + c.Shares = MinShares + c.Min = MinMinShares - 1 + if err := c.ValidConfig([]byte{}); err == nil { + t.Errorf("expected error, got nil") + } + c.Shares = MinShares + 1 + c.Min = MinMinShares - 1 + if err := c.ValidConfig([]byte{}); err == nil { + t.Errorf("expected error, got nil") + } + c.Shares = MinShares + c.Min = MinMinShares + c.Prime = nil + if err := c.ValidConfig([]byte{}); err == nil { + t.Errorf("expected error, got nil") + } + c.Prime = new(big.Int).SetUint64(10007) + if err := c.ValidConfig([]byte("12345")); err == nil { + t.Errorf("expected error, got nil") + } } -func Test_maxSecretPartSize(t *testing.T) { +func TestMaxMessageLen(t *testing.T) { c := Config{Prime: big.NewInt(13)} if c.MaxMessageLen() != 0 { t.Errorf("expected 0, got %d", c.MaxMessageLen()) diff --git a/encode_test.go b/encode_test.go index edd247d..6df2844 100644 --- a/encode_test.go +++ b/encode_test.go @@ -1,6 +1,7 @@ package gosss import ( + "math/big" "testing" ) @@ -32,4 +33,20 @@ func Test_shareToStrStrToShare(t *testing.T) { t.Errorf("unexpected share: %s", y) } } + // test coords + invalidX, _ := new(big.Int).SetString("1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 10) + invalidY, _ := new(big.Int).SetString("1", 10) + _, err := shareToStr(invalidX, invalidY) + if err == nil { + t.Errorf("expected error, got nil") + } + // test invalid share + _, _, err = strToShare("3") + if err == nil { + t.Errorf("expected error, got nil") + } + _, _, err = strToShare("10") + if err == nil { + t.Errorf("expected error, got nil") + } } diff --git a/sss.go b/sss.go index 093d0fc..4f99c9f 100644 --- a/sss.go +++ b/sss.go @@ -1,9 +1,6 @@ package gosss -import ( - "fmt" - "math/big" -) +import "math/big" // HideMessage generates the shares of the message using the Shamir Secret // Sharing algorithm. It returns the shares as strings. The message is encoded @@ -37,7 +34,7 @@ func HideMessage(message []byte, conf *Config) ([]string, error) { for i := 0; i < len(xs); i++ { share, err := shareToStr(xs[i], ys[i]) if err != nil { - return nil, fmt.Errorf("error encoding shares: %w", err) + return nil, err } shares = append(shares, share) } @@ -69,7 +66,7 @@ func RecoverMessage(inputs []string, conf *Config) ([]byte, error) { for _, input := range inputs { x, y, err := strToShare(input) if err != nil { - return nil, fmt.Errorf("error decoding shares: %w", err) + return nil, err } xs = append(xs, x) ys = append(ys, y)