@@ -3,6 +3,7 @@ package shamir
33import (
44 "fmt"
55 . "math/big"
6+ "strings"
67 "testing"
78)
89
@@ -18,6 +19,47 @@ func TestFromString(t *testing.T) {
1819 t .Log (share )
1920}
2021
22+ func TestFromStringAllowsSurroundingWhitespace (t * testing.T ) {
23+ share , err := FromString (" \r \n 12333333333333333333333333\r \n 45666666666666666666666666666\n " )
24+ if err != nil {
25+ t .Fatal (err )
26+ }
27+ if share .X .String () != "12333333333333333333333333" {
28+ t .Fatalf ("unexpected x coordinate: %s" , share .X )
29+ }
30+ if share .Y .String () != "45666666666666666666666666666" {
31+ t .Fatalf ("unexpected y coordinate: %s" , share .Y )
32+ }
33+ }
34+
35+ func TestFromStringRejectsEmptyShare (t * testing.T ) {
36+ _ , err := FromString ("" )
37+ if err == nil {
38+ t .Fatal ("expected empty share to fail" )
39+ }
40+ if strings .Contains (err .Error (), "number has no digits" ) {
41+ t .Fatalf ("expected a clear share format error, got %q" , err )
42+ }
43+ }
44+
45+ func TestFromStringRejectsInvalidCoordinate (t * testing.T ) {
46+ _ , err := FromString ("123\n " )
47+ if err == nil {
48+ t .Fatal ("expected incomplete share to fail" )
49+ }
50+ if ! strings .Contains (err .Error (), "expected exactly two decimal integer coordinates" ) {
51+ t .Fatalf ("unexpected error: %v" , err )
52+ }
53+
54+ _ , err = FromString ("123\n abc" )
55+ if err == nil {
56+ t .Fatal ("expected invalid y coordinate to fail" )
57+ }
58+ if ! strings .Contains (err .Error (), "invalid share y coordinate" ) {
59+ t .Fatalf ("unexpected error: %v" , err )
60+ }
61+ }
62+
2163func TestEncryptAndDecrypt (t * testing.T ) {
2264 secret := "123456"
2365 shares , err := Encrypt (secret , 7 , 4 )
0 commit comments