OSDN Git Service

Add BlockWitness (#54)
[bytom/vapor.git] / protocol / bc / types / block_witness_test.go
diff --git a/protocol/bc/types/block_witness_test.go b/protocol/bc/types/block_witness_test.go
new file mode 100644 (file)
index 0000000..048a629
--- /dev/null
@@ -0,0 +1,60 @@
+package types
+
+import (
+       "bytes"
+       "encoding/hex"
+       "testing"
+
+       "github.com/vapor/encoding/blockchain"
+       "github.com/vapor/testutil"
+)
+
+func TestReadWriteBlockWitness(t *testing.T) {
+       cases := []struct {
+               bw        BlockWitness
+               hexString string
+       }{
+               {
+                       bw:        BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
+                       hexString: "0102beef",
+               },
+               {
+                       bw:        BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}, []byte{0xab, 0xcd}, []byte{0xcd, 0x68}}},
+                       hexString: "0302beef02abcd02cd68",
+               },
+               {
+                       bw:        BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}, nil, []byte{0xcd, 0x68}}},
+                       hexString: "0302beef0002cd68",
+               },
+               {
+                       bw:        BlockWitness{Witness: [][]byte{[]byte{}}},
+                       hexString: "0100",
+               },
+               {
+                       bw:        BlockWitness{},
+                       hexString: "00",
+               },
+       }
+
+       for _, c := range cases {
+               buff := []byte{}
+               buffer := bytes.NewBuffer(buff)
+               if err := c.bw.writeTo(buffer); err != nil {
+                       t.Fatal(err)
+               }
+
+               hexString := hex.EncodeToString(buffer.Bytes())
+               if hexString != c.hexString {
+                       t.Errorf("test write block commitment fail, got:%s, want:%s", hexString, c.hexString)
+               }
+
+               bc := &BlockWitness{}
+               if err := bc.readFrom(blockchain.NewReader(buffer.Bytes())); err != nil {
+                       t.Fatal(err)
+               }
+
+               if !testutil.DeepEqual(*bc, c.bw) {
+                       t.Errorf("test read block commitment fail, got:%v, want:%v", *bc, c.bw)
+               }
+       }
+}