OSDN Git Service

add unit test (#1683)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Wed, 10 Apr 2019 01:35:18 +0000 (09:35 +0800)
committerPaladz <yzhu101@uottawa.ca>
Wed, 10 Apr 2019 01:35:18 +0000 (09:35 +0800)
protocol/vm/vmutil/script.go
protocol/vm/vmutil/script_test.go

index 7061491..1f0d82f 100644 (file)
@@ -141,7 +141,7 @@ func GetIssuanceProgramRestrictHeight(program []byte) (int64, error) {
                return 0, err
        }
 
-       if insts[0].IsPushdata() && insts[1].Op == vm.OP_BLOCKHEIGHT && insts[2].Op == vm.OP_GREATERTHAN && insts[3].Op == vm.OP_VERIFY {
+       if len(insts) >= 4 && insts[0].IsPushdata() && insts[1].Op == vm.OP_BLOCKHEIGHT && insts[2].Op == vm.OP_GREATERTHAN && insts[3].Op == vm.OP_VERIFY {
                return vm.AsInt64(insts[0].Data)
        }
        return 0, nil
index 5aea1cd..3e0d211 100644 (file)
@@ -188,3 +188,35 @@ func TestP2SPMultiSigProgramWithHeight(t *testing.T) {
                }
        }
 }
+
+func TestGetIssuanceProgramRestrictHeight(t *testing.T) {
+       tests := []struct {
+               issuanceProgram string
+               wantHeight      int64
+       }{
+               {
+                       issuanceProgram: "",
+                       wantHeight:      0,
+               },
+               {
+                       issuanceProgram: "ae20ac20f5cdb9ada2ae9836bcfff32126d6b885aa3f73ee111a95d1bf37f3904aca5151ad",
+                       wantHeight:      0,
+               },
+               {
+                       issuanceProgram: "01c8cda069ae20f44dd85be89de08b0f894476ccc7b3eebcf0a288c79504fa7e4c8033f5b7338020c86dc682ce3ecac64e165d9b5f8cca9ee05bd0d4df07adbfd11251ad7e88f1685152ad",
+                       wantHeight:      200,
+               },
+       }
+
+       for i, test := range tests {
+               program, err := hex.DecodeString(test.issuanceProgram)
+               if err != nil {
+                       t.Fatal(err)
+               }
+
+               gotHeight, _ := GetIssuanceProgramRestrictHeight(program)
+               if gotHeight != test.wantHeight {
+                       t.Errorf("TestGetIssuanceProgramRestrictHeight #%d failed: got %d want %d", i, gotHeight, test.wantHeight)
+               }
+       }
+}