github.com/Psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/sss/polynomial_test.go (about)

     1  package sss
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  var (
     9  	p  = []byte{1, 0, 2, 3}
    10  	p2 = []byte{70, 32, 6}
    11  )
    12  
    13  func TestDegree(t *testing.T) {
    14  	if v, want := degree(p), 3; v != want {
    15  		t.Errorf("Was %v, but expected %v", v, want)
    16  	}
    17  }
    18  
    19  func TestEval(t *testing.T) {
    20  	if v, want := eval(p, 2), byte(17); v != want {
    21  		t.Errorf("Was %v, but expected %v", v, want)
    22  	}
    23  }
    24  
    25  func TestGenerate(t *testing.T) {
    26  	b := []byte{1, 2, 3}
    27  
    28  	expected := []byte{10, 1, 2, 3}
    29  	actual, err := generate(3, 10, bytes.NewReader(b))
    30  	if err != nil {
    31  		t.Error(err)
    32  	}
    33  
    34  	if !bytes.Equal(actual, expected) {
    35  		t.Errorf("Was %v, but expected %v", actual, expected)
    36  	}
    37  }
    38  
    39  func TestGenerateEOF(t *testing.T) {
    40  	b := []byte{1}
    41  
    42  	p, err := generate(3, 10, bytes.NewReader(b))
    43  	if p != nil {
    44  		t.Errorf("Was %v, but expected an error", p)
    45  	}
    46  
    47  	if err == nil {
    48  		t.Error("No error returned")
    49  	}
    50  }
    51  
    52  func TestGeneratePolyEOFFullSize(t *testing.T) {
    53  	b := []byte{1, 2, 0, 0, 0, 0}
    54  
    55  	p, err := generate(3, 10, bytes.NewReader(b))
    56  	if p != nil {
    57  		t.Errorf("Was %v, but xpected an error", p)
    58  	}
    59  
    60  	if err == nil {
    61  		t.Error("No error returned")
    62  	}
    63  }
    64  
    65  func TestGenerateFullSize(t *testing.T) {
    66  	b := []byte{1, 2, 0, 4}
    67  
    68  	expected := []byte{10, 1, 2, 4}
    69  	actual, err := generate(3, 10, bytes.NewReader(b))
    70  	if err != nil {
    71  		t.Error(err)
    72  	}
    73  
    74  	if !bytes.Equal(actual, expected) {
    75  		t.Errorf("Was %v but expected %v", actual, expected)
    76  	}
    77  }
    78  
    79  func TestInterpolate(t *testing.T) {
    80  	in := []pair{
    81  		pair{x: 1, y: 1},
    82  		pair{x: 2, y: 2},
    83  		pair{x: 3, y: 3},
    84  	}
    85  
    86  	if v, want := interpolate(in, 0), byte(0); v != want {
    87  		t.Errorf("Was %v, but expected %v", v, want)
    88  	}
    89  }