-
Notifications
You must be signed in to change notification settings - Fork 144
rpcserver: return InvalidArgument for RPC validation errors #2112
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kaldun-tech
wants to merge
2
commits into
lightninglabs:main
Choose a base branch
from
kaldun-tech:rpc-handlers-invalid-argument-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+267
−17
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,235 @@ | ||
| package rpcserver | ||
|
|
||
| // validation_test.go contains unit tests for RPC input validation. | ||
| // These tests verify that invalid input returns codes.InvalidArgument | ||
| // (not codes.Unknown). | ||
| // | ||
| // Happy-path testing (valid input -> successful response) is covered by | ||
| // integration tests in itest/ which exercise complete request flows. | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/btcsuite/btcd/btcec/v2" | ||
| "github.com/lightninglabs/taproot-assets/address" | ||
| "github.com/lightninglabs/taproot-assets/proof" | ||
| "github.com/lightninglabs/taproot-assets/tapconfig" | ||
| "github.com/lightninglabs/taproot-assets/taprpc" | ||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| // assertCode checks that the error is a gRPC status error with the expected | ||
| // status code. | ||
| func assertCode(t *testing.T, err error, wantCode codes.Code) { | ||
| t.Helper() | ||
|
|
||
| require.Error(t, err) | ||
|
|
||
| st, ok := status.FromError(err) | ||
| require.True(t, ok, "error should be a gRPC status error") | ||
| require.Equal(t, wantCode, st.Code()) | ||
| } | ||
|
|
||
| // newTestServer creates a minimal RPCServer for validation testing. | ||
| func newTestServer() *RPCServer { | ||
| return &RPCServer{ | ||
| cfg: &tapconfig.Config{ | ||
| ChainParams: address.MainNetTap, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // TestDecodeAddrValidation tests that DecodeAddr returns InvalidArgument | ||
| // for validation errors. | ||
| func TestDecodeAddrValidation(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| server := newTestServer() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| req *taprpc.DecodeAddrRequest | ||
| wantCode codes.Code | ||
| }{ | ||
| { | ||
| name: "empty address", | ||
| req: &taprpc.DecodeAddrRequest{Addr: ""}, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| { | ||
| name: "invalid address format", | ||
| req: &taprpc.DecodeAddrRequest{Addr: "not-valid"}, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| { | ||
| name: "wrong network prefix", | ||
| req: &taprpc.DecodeAddrRequest{ | ||
| // Testnet address on mainnet config. | ||
| Addr: "taptb1qqqszqspqqqqqqqqqqqqqqqqqqq" + | ||
| "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpqqq" + | ||
| "sqqspqqqqp8hlm7nfnydq5wvs6j5mczq8tf" + | ||
| "vemhy7082", | ||
| }, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| _, err := server.DecodeAddr( | ||
| context.Background(), tc.req) | ||
| assertCode(t, err, tc.wantCode) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestDecodeProofValidation tests that DecodeProof returns InvalidArgument | ||
| // for validation errors. | ||
| func TestDecodeProofValidation(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| server := newTestServer() | ||
|
|
||
| // Invalid proof bytes that don't match either magic prefix. | ||
| invalidMagicBytes := []byte{0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03} | ||
|
|
||
| // Bytes with single proof magic but invalid content. | ||
| invalidSingleProof := append( | ||
| proof.PrefixMagicBytes[:], []byte{0x00, 0x01, 0x02, 0x03}..., | ||
| ) | ||
|
|
||
| // Bytes with file magic but invalid content. | ||
| invalidFileProof := append( | ||
| proof.FilePrefixMagicBytes[:], | ||
| []byte{0x00, 0x01, 0x02, 0x03}..., | ||
| ) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| req *taprpc.DecodeProofRequest | ||
| wantCode codes.Code | ||
| }{ | ||
| { | ||
| name: "empty proof", | ||
| req: &taprpc.DecodeProofRequest{RawProof: nil}, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| { | ||
| name: "invalid magic bytes", | ||
| req: &taprpc.DecodeProofRequest{ | ||
| RawProof: invalidMagicBytes, | ||
| }, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| { | ||
| name: "invalid single proof", | ||
| req: &taprpc.DecodeProofRequest{ | ||
| RawProof: invalidSingleProof, | ||
| }, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| { | ||
| name: "invalid file proof", | ||
| req: &taprpc.DecodeProofRequest{ | ||
| RawProof: invalidFileProof, | ||
| }, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| _, err := server.DecodeProof( | ||
| context.Background(), tc.req) | ||
| assertCode(t, err, tc.wantCode) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestExportProofValidation tests that ExportProof returns InvalidArgument | ||
| // for validation errors. | ||
| func TestExportProofValidation(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| server := newTestServer() | ||
|
|
||
| // Generate a valid compressed public key for testing. | ||
| privKey, err := btcec.NewPrivateKey() | ||
| require.NoError(t, err) | ||
| validScriptKey := privKey.PubKey().SerializeCompressed() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| req *taprpc.ExportProofRequest | ||
| wantCode codes.Code | ||
| }{ | ||
| { | ||
| name: "empty script key", | ||
| req: &taprpc.ExportProofRequest{ScriptKey: nil}, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| { | ||
| name: "invalid script key length", | ||
| req: &taprpc.ExportProofRequest{ | ||
| ScriptKey: []byte{0x01, 0x02, 0x03}, | ||
| }, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| { | ||
| name: "invalid script key prefix", | ||
| req: &taprpc.ExportProofRequest{ | ||
| // 33 bytes is correct length for compressed | ||
| // pubkey, but 0x00 prefix is invalid. | ||
| ScriptKey: make([]byte, 33), | ||
| }, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| { | ||
| name: "empty asset ID", | ||
| req: &taprpc.ExportProofRequest{ | ||
| ScriptKey: validScriptKey, | ||
| AssetId: nil, | ||
| }, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| { | ||
| name: "asset ID too short", | ||
| req: &taprpc.ExportProofRequest{ | ||
| ScriptKey: validScriptKey, | ||
| AssetId: make([]byte, 31), | ||
| }, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| { | ||
| name: "asset ID too long", | ||
| req: &taprpc.ExportProofRequest{ | ||
| ScriptKey: validScriptKey, | ||
| AssetId: make([]byte, 33), | ||
| }, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| { | ||
| name: "invalid outpoint", | ||
| req: &taprpc.ExportProofRequest{ | ||
| ScriptKey: validScriptKey, | ||
| AssetId: make([]byte, 32), | ||
| Outpoint: &taprpc.OutPoint{ | ||
| Txid: []byte{0x01, 0x02}, | ||
| OutputIndex: 0, | ||
| }, | ||
| }, | ||
| wantCode: codes.InvalidArgument, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| _, err := server.ExportProof( | ||
| context.Background(), tc.req) | ||
| assertCode(t, err, tc.wantCode) | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.