-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rpcserver: initial rapid test for uni ID unmarshal
- Loading branch information
Showing
3 changed files
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,306 @@ | ||
package taprootassets | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/btcsuite/btcd/btcec/v2" | ||
"github.com/btcsuite/btcd/btcec/v2/schnorr" | ||
"github.com/lightninglabs/taproot-assets/asset" | ||
"github.com/lightninglabs/taproot-assets/fn" | ||
"github.com/lightninglabs/taproot-assets/taprpc" | ||
"github.com/lightninglabs/taproot-assets/taprpc/universerpc" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/proto" | ||
"pgregory.net/rapid" | ||
) | ||
|
||
func formatProtoJSON(resp proto.Message) (string, error) { | ||
jsonBytes, err := taprpc.ProtoJSONMarshalOpts.Marshal(resp) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(jsonBytes), nil | ||
} | ||
|
||
func toJSON(t *rapid.T, resp proto.Message) string { | ||
jsonStr, err := formatProtoJSON(resp) | ||
require.NoError(t, err) | ||
|
||
return jsonStr | ||
} | ||
|
||
func testUnmarshalUniId(t *rapid.T) { | ||
KnownProofTypes := fn.NewSet( | ||
universerpc.ProofType_PROOF_TYPE_UNSPECIFIED, | ||
universerpc.ProofType_PROOF_TYPE_ISSUANCE, | ||
universerpc.ProofType_PROOF_TYPE_TRANSFER, | ||
) | ||
|
||
KnownProofTypesInt := fn.NewSet( | ||
int32(universerpc.ProofType_PROOF_TYPE_UNSPECIFIED), | ||
int32(universerpc.ProofType_PROOF_TYPE_ISSUANCE), | ||
int32(universerpc.ProofType_PROOF_TYPE_TRANSFER), | ||
) | ||
|
||
genBytes := rapid.SliceOf(rapid.Byte()) | ||
|
||
IDBytes := genBytes.Draw(t, "ID") | ||
IDStr := rapid.String().Draw(t, "ID string") | ||
IDFieldSelector := rapid.ByteMax(0x5).Draw(t, "ID field selector") | ||
proofType := rapid.Int32().Draw(t, "proofType") | ||
rpcProofType := universerpc.ProofType(proofType) | ||
|
||
lenIDBytes := len(IDBytes) | ||
lenIDStr := len(IDStr) | ||
|
||
uniId := &universerpc.ID{ | ||
ProofType: rpcProofType, | ||
} | ||
|
||
// Set the ID to random data, of a random type. | ||
switch IDFieldSelector { | ||
case 0: | ||
uniId.Id = &universerpc.ID_AssetId{ | ||
AssetId: IDBytes, | ||
} | ||
|
||
case 1: | ||
uniId.Id = &universerpc.ID_AssetIdStr{ | ||
AssetIdStr: IDStr, | ||
} | ||
|
||
case 2: | ||
uniId.Id = &universerpc.ID_GroupKey{ | ||
GroupKey: IDBytes, | ||
} | ||
|
||
case 3: | ||
uniId.Id = &universerpc.ID_GroupKeyStr{ | ||
GroupKeyStr: IDStr, | ||
} | ||
|
||
// Empty ID field. | ||
case 4: | ||
|
||
// Empty universe ID. | ||
case 5: | ||
uniId = nil | ||
} | ||
|
||
nativeUniID, err := UnmarshalUniID(uniId) | ||
|
||
// Unmarhsalling failure is acceptable if part of the input was nil, | ||
// or the input was the wrong size. | ||
if err != nil { | ||
if !KnownProofTypes.Contains(rpcProofType) { | ||
return | ||
} | ||
|
||
switch int(IDFieldSelector) { | ||
case 0: | ||
if lenIDBytes == sha256.Size { | ||
t.Fatalf("unmarshal asset ID bytes failed: %v", err) | ||
} | ||
|
||
case 1: | ||
|
||
if lenIDStr == 0 { | ||
return | ||
} | ||
|
||
// Invalid hex should cause an error. | ||
_, hexErr := hex.DecodeString(IDStr) | ||
if hexErr != nil { | ||
return | ||
} | ||
|
||
if lenIDStr == sha256.Size*2 { | ||
t.Fatalf("unmarshal asset ID string failed: %v", err) | ||
} | ||
|
||
case 2: | ||
if lenIDBytes == sha256.Size || | ||
lenIDBytes == btcec.PubKeyBytesLenCompressed { | ||
|
||
_, keyErr := parseUserKey(IDBytes) | ||
if keyErr != nil { | ||
return | ||
} | ||
|
||
t.Fatalf("unmarshal group key bytes failed: %v", err) | ||
} | ||
|
||
case 3: | ||
if lenIDStr == 0 { | ||
return | ||
} | ||
|
||
// Invalid hex should cause an error. | ||
keyBytes, hexErr := hex.DecodeString(IDStr) | ||
if hexErr != nil { | ||
return | ||
} | ||
|
||
if lenIDStr == sha256.Size*2 || | ||
lenIDStr == btcec.PubKeyBytesLenCompressed*2 { | ||
|
||
_, keyErr := parseUserKey(keyBytes) | ||
if keyErr != nil { | ||
return | ||
} | ||
|
||
t.Fatalf("unmarshal group key string failed: %v", err) | ||
} | ||
|
||
// Unmarshalling an empty universe ID, or an ID with an empty | ||
// ID field, should fail. | ||
case 4, 5: | ||
} | ||
|
||
return | ||
} | ||
|
||
// Unmarshalling an unknown proof type should fail. | ||
if !KnownProofTypes.Contains(rpcProofType) { | ||
t.Fatalf("unknown proof type not rejected: %v", rpcProofType) | ||
} | ||
|
||
// Asset IDs must be 32 bytes (or 64 hex chars). Group keys | ||
// must be 32 or 33 bytes (64 or 66 chars). | ||
switch int(IDFieldSelector) { | ||
case 0: | ||
if lenIDBytes != sha256.Size { | ||
t.Fatalf("invalid asset ID not "+ | ||
"rejected: generated %v", | ||
IDBytes) | ||
} | ||
|
||
case 1: | ||
if lenIDStr != sha256.Size*2 { | ||
t.Fatalf("invalid asset ID string "+ | ||
"not rejected: generated %v", | ||
IDStr) | ||
} | ||
|
||
case 2: | ||
if lenIDBytes != sha256.Size && | ||
lenIDBytes != btcec.PubKeyBytesLenCompressed { | ||
|
||
t.Fatalf("invalid group key not "+ | ||
"rejected: generated %v", | ||
IDBytes) | ||
} | ||
|
||
case 3: | ||
if lenIDStr != sha256.Size*2 && | ||
lenIDStr != btcec.PubKeyBytesLenCompressed*2 { | ||
|
||
t.Fatalf("invalid group key string "+ | ||
"not rejected: generated %v", | ||
IDStr) | ||
} | ||
|
||
case 4: | ||
t.Fatalf("unmarshal empty ID not rejected: %v", err) | ||
|
||
case 5: | ||
t.Fatalf("unmarshal ID with empty ID not rejected: %v", | ||
err) | ||
} | ||
|
||
// Check equality of the proof type. | ||
proofTypeInt := int32(nativeUniID.ProofType) | ||
if KnownProofTypesInt.Contains(proofTypeInt) && proofTypeInt != proofType { | ||
t.Fatalf("proof type mismatch: generated %v, unmarshalled %v", | ||
proofType, int32(nativeUniID.ProofType)) | ||
} | ||
|
||
// Check equality of the ID field. | ||
switch IDFieldSelector { | ||
case 0: | ||
if len(nativeUniID.AssetID) == 0 { | ||
t.Fatalf("asset ID bytes not unmarshalled: generated %v", | ||
IDBytes) | ||
} | ||
|
||
if !bytes.Equal(nativeUniID.AssetID[:], IDBytes) { | ||
t.Fatalf("asset ID mismatch: generated %x, "+ | ||
"unmarshalled %x", IDBytes, | ||
nativeUniID.AssetID[:]) | ||
} | ||
|
||
case 1: | ||
if len(nativeUniID.AssetID) == 0 { | ||
t.Fatalf("asset ID string not unmarshalled: generated %v", | ||
IDBytes) | ||
} | ||
|
||
if nativeUniID.AssetID.String() != IDStr { | ||
t.Fatalf("asset ID string mismatch: generated %s, "+ | ||
"unmarshalled %s, %v, %v", IDStr, | ||
nativeUniID.AssetID.String(), | ||
nativeUniID, err) | ||
} | ||
|
||
case 2: | ||
if nativeUniID.GroupKey == nil { | ||
t.Fatalf("group key not unmarshalled: generated %v", | ||
IDBytes) | ||
} | ||
|
||
// We ignore the leading sign byte for keys passsed as | ||
// compressed. | ||
groupKeyBytes := schnorr.SerializePubKey(nativeUniID.GroupKey) | ||
switch len(IDBytes) { | ||
case 32: | ||
if !bytes.Equal(groupKeyBytes, IDBytes) { | ||
t.Fatalf("group key mismatch: generated %x, "+ | ||
"unmarshalled %x", IDBytes, | ||
groupKeyBytes[1:]) | ||
} | ||
|
||
case 33: | ||
if !bytes.Equal(groupKeyBytes, IDBytes[1:]) { | ||
t.Fatalf("group key mismatch: generated %x, "+ | ||
"unmarshalled %x", IDBytes, | ||
groupKeyBytes) | ||
} | ||
} | ||
|
||
case 3: | ||
if nativeUniID.GroupKey == nil { | ||
t.Fatalf("group key not unmarshalled: generated %v", | ||
IDBytes) | ||
} | ||
|
||
groupKeyBytes := nativeUniID.GroupKey.SerializeCompressed() | ||
groupKeyStr := hex.EncodeToString(groupKeyBytes) | ||
|
||
switch len(IDStr) { | ||
case 64: | ||
if groupKeyStr[2:] != IDStr { | ||
t.Fatalf("group key string mismatch: "+ | ||
"generated %s, "+ | ||
"unmarshalled %s", IDStr, | ||
groupKeyStr[2:]) | ||
} | ||
|
||
case 66: | ||
if groupKeyStr != IDStr { | ||
t.Fatalf("group key string mismatch: "+ | ||
"generated %s, "+ | ||
"unmarshalled %s", IDStr, | ||
groupKeyStr) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestUnmarshalUniId(t *testing.T) { | ||
rapid.Check(t, testUnmarshalUniId) | ||
} |