Skip to content

Commit

Permalink
Merge pull request #148 from new0nebit/flourish
Browse files Browse the repository at this point in the history
Add Test Coverage for SatisfiedPolicy and enhance SpendPolicy tests
  • Loading branch information
lukechampine committed Apr 16, 2024
2 parents fdfc700 + c5cb20b commit f9d44a4
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions types/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package types

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
"time"
)
Expand Down Expand Up @@ -329,3 +331,77 @@ func TestPolicyRoundtrip(t *testing.T) {
t.Fatal("satisfied policy did not survive roundtrip:", sp, sp2)
}
}

func TestSpendPolicyMarshalJSON(t *testing.T) {
publicKey := NewPrivateKeyFromSeed(make([]byte, 32)).PublicKey()
hash := HashBytes(nil)

tests := []struct {
sp SpendPolicy
exp string
}{
{
sp: PolicyAbove(100),
exp: `"above(100)"`,
},
{
sp: PolicyAfter(time.Unix(1234567890, 0)),
exp: `"after(1234567890)"`,
},
{
sp: PolicyPublicKey(publicKey),
exp: fmt.Sprintf(`"pk(0x%x)"`, publicKey[:]),
},
{
sp: PolicyHash(hash),
exp: fmt.Sprintf(`"h(0x%x)"`, hash[:]),
},
}

for _, tt := range tests {
data, err := json.Marshal(tt.sp)
if err != nil {
t.Fatalf("Expected no error, but got %v", err)
}

if string(data) != tt.exp {
t.Fatalf("Expected %s, but got %s", tt.exp, string(data))
}
}
}

func TestSatisfiedPolicyUnmarshaling(t *testing.T) {
tests := []struct {
name string
jsonData string
expectErr bool
preimages [][]byte
}{
{
name: "InvalidHex",
jsonData: `{"Policy": null, "Signatures": null, "Preimages": ["InvalidHex"]}`,
expectErr: true,
},
{
name: "InvalidPolicy",
jsonData: `{"Policy": "ShouldBeAnObjectOrValidType", "Signatures": null, "Preimages": []}`,
expectErr: true,
},
{
name: "ValidPreimage",
jsonData: `{"Policy": null, "Signatures": null, "Preimages": ["68656c6c6f776f726c64"]}`,
expectErr: false,
preimages: [][]byte{[]byte("helloworld")},
},
}

for _, tt := range tests {
var sp SatisfiedPolicy
if err := sp.UnmarshalJSON([]byte(tt.jsonData)); (err != nil) != tt.expectErr {
t.Errorf("%s: UnmarshalJSON() error = %v, expectErr %v", tt.name, err, tt.expectErr)
}
if len(tt.preimages) != 0 && !reflect.DeepEqual(tt.preimages, sp.Preimages) {
t.Error("preimage mismatch")
}
}
}

0 comments on commit f9d44a4

Please sign in to comment.