|
5 | 5 | "encoding/hex" |
6 | 6 | "encoding/json" |
7 | 7 | "fmt" |
| 8 | + "io" |
8 | 9 | "os" |
9 | 10 | "reflect" |
10 | 11 | "testing" |
@@ -750,8 +751,14 @@ func TestSphinxHopVariableSizedPayloads(t *testing.T) { |
750 | 751 | } |
751 | 752 | } |
752 | 753 |
|
753 | | -// testMultiFrameFileName is the name of the multi-frame onion test file. |
754 | | -const testMultiFrameFileName = "testdata/onion-test-multi-frame.json" |
| 754 | +const ( |
| 755 | + // testMultiFrameFileName is the name of the multi-frame onion test |
| 756 | + // file. |
| 757 | + testMultiFrameFileName = "testdata/onion-test-multi-frame.json" |
| 758 | + |
| 759 | + // testTLVFileName is the name of the tlv-payload-only onion test file. |
| 760 | + testTLVFileName = "testdata/onion-test.json" |
| 761 | +) |
755 | 762 |
|
756 | 763 | type jsonHop struct { |
757 | 764 | Type string `json:"type"` |
@@ -796,6 +803,85 @@ func jsonTypeToPayloadType(jsonType string) PayloadType { |
796 | 803 | } |
797 | 804 | } |
798 | 805 |
|
| 806 | +// TestTLVPayloadOnion tests the construction of an onion where all the payloads |
| 807 | +// are of the TLV type. |
| 808 | +func TestTLVPayloadOnion(t *testing.T) { |
| 809 | + t.Parallel() |
| 810 | + |
| 811 | + // First, we'll read out the raw JSON file at the target location. |
| 812 | + jsonBytes, err := os.ReadFile(testTLVFileName) |
| 813 | + require.NoError(t, err) |
| 814 | + |
| 815 | + // Once we have the raw file, we'll unpack it into our jsonTestCase |
| 816 | + // struct defined above. |
| 817 | + testCase := &jsonTestCase{} |
| 818 | + require.NoError(t, json.Unmarshal(jsonBytes, testCase)) |
| 819 | + |
| 820 | + // Next, we'll populate a new OnionHop using the information included |
| 821 | + // in this test case. |
| 822 | + var route PaymentPath |
| 823 | + for i, hop := range testCase.Generate.Hops { |
| 824 | + pubKeyBytes, err := hex.DecodeString(hop.Pubkey) |
| 825 | + require.NoError(t, err) |
| 826 | + |
| 827 | + pubKey, err := btcec.ParsePubKey(pubKeyBytes) |
| 828 | + require.NoError(t, err) |
| 829 | + |
| 830 | + // The test has already encoded the length of the TLV payloads, |
| 831 | + // so to make it compatible with our PaymentPath, we just |
| 832 | + // extract the payload. |
| 833 | + payload, err := hex.DecodeString(hop.Payload) |
| 834 | + require.NoError(t, err) |
| 835 | + |
| 836 | + var ( |
| 837 | + bufReader = bytes.NewReader(payload) |
| 838 | + b [8]byte |
| 839 | + ) |
| 840 | + varInt, err := ReadVarInt(bufReader, &b) |
| 841 | + require.NoError(t, err) |
| 842 | + |
| 843 | + payloadSize := uint32(varInt) |
| 844 | + require.NoError(t, err) |
| 845 | + |
| 846 | + hopPayload := make([]byte, payloadSize) |
| 847 | + _, err = io.ReadFull(bufReader, hopPayload) |
| 848 | + require.NoError(t, err) |
| 849 | + |
| 850 | + route[i] = OnionHop{ |
| 851 | + NodePub: *pubKey, |
| 852 | + HopPayload: HopPayload{ |
| 853 | + Type: PayloadTLV, |
| 854 | + Payload: hopPayload, |
| 855 | + }, |
| 856 | + } |
| 857 | + } |
| 858 | + |
| 859 | + finalPacket, err := hex.DecodeString(testCase.Onion) |
| 860 | + require.NoError(t, err) |
| 861 | + |
| 862 | + sessionKeyBytes, err := hex.DecodeString(testCase.Generate.SessionKey) |
| 863 | + require.NoError(t, err) |
| 864 | + |
| 865 | + assocData, err := hex.DecodeString(testCase.Generate.AssociatedData) |
| 866 | + require.NoError(t, err) |
| 867 | + |
| 868 | + // With all the required data assembled, we'll craft a new packet. |
| 869 | + sessionKey, _ := btcec.PrivKeyFromBytes(sessionKeyBytes) |
| 870 | + pkt, err := NewOnionPacket( |
| 871 | + &route, sessionKey, assocData, DeterministicPacketFiller, |
| 872 | + ) |
| 873 | + require.NoError(t, err) |
| 874 | + |
| 875 | + var b bytes.Buffer |
| 876 | + require.NoError(t, pkt.Encode(&b)) |
| 877 | + |
| 878 | + // Finally, we expect that our packet matches the packet included in |
| 879 | + // the spec's test vectors. |
| 880 | + require.Equalf(t, b.Bytes(), finalPacket, "final packet does not "+ |
| 881 | + "match expected BOLT 4 packet, want: %s, got %s", |
| 882 | + hex.EncodeToString(finalPacket), hex.EncodeToString(b.Bytes())) |
| 883 | +} |
| 884 | + |
799 | 885 | // TestVariablePayloadOnion tests that if we construct a packet that contains a |
800 | 886 | // mix of the old and new payload format, that we match the version that's |
801 | 887 | // included in the spec. |
|
0 commit comments