Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Test Coverage for SatisfiedPolicy and enhance SpendPolicy tests #148

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
124 changes: 124 additions & 0 deletions types/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package types

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

func TestPolicyMarshaling(t *testing.T) {
privateKey := GeneratePrivateKey()
publicKey := privateKey.PublicKey()
publicKeyHex := hex.EncodeToString(publicKey[:])

// Generate a unique SHA256 hash from the current Unix time in nanoseconds for PolicyTypeHash test.
currentTime := time.Now().UnixNano()
currentTimeBytes := []byte(strconv.FormatInt(currentTime, 10))
hash := sha256.Sum256(currentTimeBytes)
hashHex := hex.EncodeToString(hash[:])
lukechampine marked this conversation as resolved.
Show resolved Hide resolved

tests := []struct {
name string
input interface{}
output string
marshalFunc func(interface{}) ([]byte, error)
lukechampine marked this conversation as resolved.
Show resolved Hide resolved
}{
{
name: "MarshalText",
input: SpendPolicy{Type: PolicyTypeAbove(100)},
lukechampine marked this conversation as resolved.
Show resolved Hide resolved
output: "above(100)",
marshalFunc: func(v interface{}) ([]byte, error) {
return v.(SpendPolicy).MarshalText()
},
},
{
name: "MarshalText with PolicyTypeAfter",
input: SpendPolicy{Type: PolicyTypeAfter(time.Unix(1234567890, 0))},
output: "after(1234567890)",
marshalFunc: func(v interface{}) ([]byte, error) {
return v.(SpendPolicy).MarshalText()
},
},
{
name: "MarshalText with PolicyTypePublicKey",
input: SpendPolicy{Type: PolicyTypePublicKey(publicKey)},
output: "pk(0x" + publicKeyHex + ")",
lukechampine marked this conversation as resolved.
Show resolved Hide resolved
marshalFunc: func(v interface{}) ([]byte, error) {
return v.(SpendPolicy).MarshalText()
},
},
{
name: "MarshalText with PolicyTypeHash",
input: SpendPolicy{Type: PolicyTypeHash(hash)},
output: "h(0x" + hashHex + ")",
lukechampine marked this conversation as resolved.
Show resolved Hide resolved
marshalFunc: func(v interface{}) ([]byte, error) {
return v.(SpendPolicy).MarshalText()
},
},
{
name: "MarshalJSON",
input: SatisfiedPolicy{
Preimages: [][]byte{{0xde, 0xad, 0xbe, 0xef}, {0xba, 0xad, 0xf0, 0x0d}},
},
output: "preimages",
marshalFunc: json.Marshal,
},
lukechampine marked this conversation as resolved.
Show resolved Hide resolved
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lukechampine marked this conversation as resolved.
Show resolved Hide resolved
data, err := tt.marshalFunc(tt.input)
lukechampine marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatalf("Expected no error, but got %v", err)
}

if !bytes.Contains(data, []byte(tt.output)) {
lukechampine marked this conversation as resolved.
Show resolved Hide resolved
t.Fatalf("Expected %v in the output, but it was not found. Got: %s", tt.output, string(data))
}
})
}
}

func TestPolicyUnmarshaling(t *testing.T) {
tests := []struct {
name string
jsonData string
expectErr bool
preimage string
checkPreimage bool
}{
{
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,
preimage: "helloworld",
lukechampine marked this conversation as resolved.
Show resolved Hide resolved
checkPreimage: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var sp SatisfiedPolicy
if err := sp.UnmarshalJSON([]byte(tt.jsonData)); (err != nil) != tt.expectErr {
t.Fatalf("UnmarshalJSON() error = %v, expectErr %v", err, tt.expectErr)
}

if tt.checkPreimage {
expectedPreimage, err := hex.DecodeString("68656c6c6f776f726c64")
if err != nil {
t.Fatalf("hex.DecodeString error: %v", err)
}
if !bytes.Equal(sp.Preimages[0], expectedPreimage) {
t.Errorf("Preimage mismatch")
}
}
lukechampine marked this conversation as resolved.
Show resolved Hide resolved
})
}
}