Skip to content

Commit

Permalink
more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Apr 26, 2024
1 parent 863562a commit 1141452
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
62 changes: 61 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
17 changes: 17 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gosss

import (
"math/big"
"testing"
)

Expand Down Expand Up @@ -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")
}
}
9 changes: 3 additions & 6 deletions sss.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1141452

Please sign in to comment.