OSDN Git Service

Add unit test sa (#1565)
authormuscle_boy <shenao.78@163.com>
Fri, 15 Feb 2019 07:47:50 +0000 (15:47 +0800)
committerPaladz <yzhu101@uottawa.ca>
Fri, 15 Feb 2019 07:47:50 +0000 (15:47 +0800)
* add setGasValid unit test

* add setGasValid unit test

* add validate tx unit test

* add mismatch reference unit test

protocol/validation/tx_test.go

index 3ac47ed..53faa40 100644 (file)
@@ -392,6 +392,13 @@ func TestTxValidation(t *testing.T) {
                        err: ErrMismatchedPosition,
                },
                {
+                       desc: "mismatched input dest / mux source position",
+                       f: func() {
+                               mux.Sources[0].Position = 1
+                       },
+                       err: ErrMismatchedPosition,
+               },
+               {
                        desc: "mismatched output source and mux dest",
                        f: func() {
                                // For this test, it's necessary to construct a mostly
@@ -408,6 +415,21 @@ func TestTxValidation(t *testing.T) {
                        err: ErrMismatchedReference,
                },
                {
+                       desc: "mismatched input dest and mux source",
+                       f: func() {
+                               fixture2 := sample(t, fixture)
+                               tx2 := types.NewTx(*fixture2.tx).Tx
+                               input2ID := tx2.InputIDs[2]
+                               input2 := tx2.Entries[input2ID].(*bc.Spend)
+                               dest2Ref := input2.WitnessDestination.Ref
+                               dest2 := tx2.Entries[*dest2Ref].(*bc.Mux)
+                               tx.Entries[*dest2Ref] = dest2
+                               tx.Entries[input2ID] = input2
+                               mux.Sources[0].Ref = &input2ID
+                       },
+                       err: ErrMismatchedReference,
+               },
+               {
                        desc: "invalid mux destination position",
                        f: func() {
                                mux.WitnessDestinations[0].Position = 1
@@ -878,6 +900,53 @@ func TestTimeRange(t *testing.T) {
        }
 }
 
+func TestStandardTx(t *testing.T) {
+       fixture := sample(t, nil)
+       tx := types.NewTx(*fixture.tx).Tx
+       
+       cases := []struct {
+               desc string
+               f    func()
+               err  error
+       }{
+               {
+                       desc: "normal standard tx",
+                       err:  nil,
+               },
+               {
+                       desc: "not standard tx in spend input",
+                       f: func() {
+                               inputID := tx.GasInputIDs[0]
+                               spend := tx.Entries[inputID].(*bc.Spend)
+                               spentOutput, err := tx.Output(*spend.SpentOutputId)
+                               if err != nil {
+                                       t.Fatal(err)
+                               }
+                               spentOutput.ControlProgram = &bc.Program{Code: []byte{0}}
+                       },
+                       err: ErrNotStandardTx,
+               },
+               {
+                       desc: "not standard tx in output",
+                       f: func() {
+                               outputID := tx.ResultIds[0]
+                               output := tx.Entries[*outputID].(*bc.Output)
+                               output.ControlProgram = &bc.Program{Code: []byte{0}}
+                       },
+                       err: ErrNotStandardTx,
+               },
+       }
+
+       for i, c := range cases {
+               if c.f != nil {
+                       c.f()
+               }
+               if err := checkStandardTx(tx, 0); err != c.err {
+                       t.Errorf("case #%d (%s) got error %t, want %t", i, c.desc, err, c.err)
+               }
+       }
+}
+
 // A txFixture is returned by sample (below) to produce a sample
 // transaction, which takes a separate, optional _input_ txFixture to
 // affect the transaction that's built. The components of the