gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/renter/siafile/rscode_test.go (about) 1 package siafile 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "testing" 7 8 "gitlab.com/SiaPrime/SiaPrime/modules" 9 10 "gitlab.com/NebulousLabs/errors" 11 "gitlab.com/NebulousLabs/fastrand" 12 ) 13 14 // TestRSEncode tests the rsCode type. 15 func TestRSEncode(t *testing.T) { 16 badParams := []struct { 17 data, parity int 18 }{ 19 {-1, -1}, 20 {-1, 0}, 21 {0, -1}, 22 {0, 0}, 23 {0, 1}, 24 {1, 0}, 25 } 26 for _, ps := range badParams { 27 if _, err := NewRSCode(ps.data, ps.parity); err == nil { 28 t.Error("expected bad parameter error, got nil") 29 } 30 } 31 32 rsc, err := NewRSCode(10, 3) 33 if err != nil { 34 t.Fatal(err) 35 } 36 37 data := fastrand.Bytes(777) 38 39 pieces, err := rsc.Encode(data) 40 if err != nil { 41 t.Fatal(err) 42 } 43 _, err = rsc.Encode(nil) 44 if err == nil { 45 t.Fatal("expected nil data error, got nil") 46 } 47 48 buf := new(bytes.Buffer) 49 err = rsc.Recover(pieces, 777, buf) 50 if err != nil { 51 t.Fatal(err) 52 } 53 err = rsc.Recover(nil, 777, buf) 54 if err == nil { 55 t.Fatal("expected nil pieces error, got nil") 56 } 57 58 if !bytes.Equal(data, buf.Bytes()) { 59 t.Fatal("recovered data does not match original") 60 } 61 } 62 63 // TestIdentifierAndCombinedSiaPath checks that different erasure coders produce 64 // unique identifiers and that CombinedSiaFilePath also produces unique siapaths 65 // using the identifiers. 66 func TestIdentifierAndCombinedSiaPath(t *testing.T) { 67 ec1, err1 := NewRSCode(1, 2) 68 ec2, err2 := NewRSCode(1, 2) 69 ec3, err3 := NewRSCode(1, 3) 70 ec4, err4 := NewRSSubCode(1, 2, 64) 71 if err := errors.Compose(err1, err2, err3, err4); err != nil { 72 t.Fatal(err) 73 } 74 if ec1.Identifier() != "1+1+2" { 75 t.Error("wrong identifier for ec1") 76 } 77 if ec2.Identifier() != "1+1+2" { 78 t.Error("wrong identifier for ec2") 79 } 80 if ec3.Identifier() != "1+1+3" { 81 t.Error("wrong identifier for ec3") 82 } 83 if ec4.Identifier() != "2+1+2" { 84 t.Error("wrong identifier for ec4") 85 } 86 sp1 := modules.CombinedSiaFilePath(ec1) 87 sp2 := modules.CombinedSiaFilePath(ec2) 88 sp3 := modules.CombinedSiaFilePath(ec3) 89 sp4 := modules.CombinedSiaFilePath(ec4) 90 if !sp1.Equals(sp2) { 91 t.Error("sp1 and sp2 should have the same path") 92 } 93 if sp1.Equals(sp3) { 94 t.Error("sp1 and sp3 should have different path") 95 } 96 if sp1.Equals(sp4) { 97 t.Error("sp1 and sp4 should have different path") 98 } 99 } 100 101 func BenchmarkRSEncode(b *testing.B) { 102 rsc, err := NewRSCode(80, 20) 103 if err != nil { 104 b.Fatal(err) 105 } 106 data := fastrand.Bytes(1 << 20) 107 108 b.SetBytes(1 << 20) 109 b.ResetTimer() 110 for i := 0; i < b.N; i++ { 111 rsc.Encode(data) 112 } 113 } 114 115 func BenchmarkRSRecover(b *testing.B) { 116 rsc, err := NewRSCode(50, 200) 117 if err != nil { 118 b.Fatal(err) 119 } 120 data := fastrand.Bytes(1 << 20) 121 pieces, err := rsc.Encode(data) 122 if err != nil { 123 b.Fatal(err) 124 } 125 126 b.SetBytes(1 << 20) 127 b.ResetTimer() 128 for i := 0; i < b.N; i++ { 129 for j := 0; j < len(pieces)/2; j += 2 { 130 pieces[j] = nil 131 } 132 rsc.Recover(pieces, 1<<20, ioutil.Discard) 133 } 134 }