// TestMerkleBlockOverflowErrors performs tests to ensure encoding and decoding // merkle blocks that are intentionally crafted to use large values for the // number of hashes and flags are handled properly. This could otherwise // potentially be used as an attack vector. func TestMerkleBlockOverflowErrors(t *testing.T) { // Use protocol version 70001 specifically here instead of the latest // protocol version because the test data is using bytes encoded with // that version. pver := uint32(70001) // Create bytes for a merkle block that claims to have more than the max // allowed tx hashes. var buf bytes.Buffer wire.TstWriteVarInt(&buf, pver, wire.MaxTxPerBlock+1) numHashesOffset := 84 exceedMaxHashes := make([]byte, numHashesOffset) copy(exceedMaxHashes, merkleBlockOneBytes[:numHashesOffset]) exceedMaxHashes = append(exceedMaxHashes, buf.Bytes()...) // Create bytes for a merkle block that claims to have more than the max // allowed flag bytes. buf.Reset() wire.TstWriteVarInt(&buf, pver, wire.MaxFlagsPerMerkleBlock+1) numFlagBytesOffset := 117 exceedMaxFlagBytes := make([]byte, numFlagBytesOffset) copy(exceedMaxFlagBytes, merkleBlockOneBytes[:numFlagBytesOffset]) exceedMaxFlagBytes = append(exceedMaxFlagBytes, buf.Bytes()...) tests := []struct { buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding err error // Expected error }{ // Block that claims to have more than max allowed hashes. {exceedMaxHashes, pver, &wire.MessageError{}}, // Block that claims to have more than max allowed flag bytes. {exceedMaxFlagBytes, pver, &wire.MessageError{}}, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Decode from wire format. var msg wire.MsgMerkleBlock r := bytes.NewReader(test.buf) err := msg.BtcDecode(r, test.pver) if reflect.TypeOf(err) != reflect.TypeOf(test.err) { t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", i, err, reflect.TypeOf(test.err)) continue } } }
// TestVarIntWireErrors performs negative tests against wire encode and decode // of variable length integers to confirm error paths work correctly. func TestVarIntWireErrors(t *testing.T) { pver := wire.ProtocolVersion tests := []struct { in uint64 // Value to encode buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding max int // Max size of fixed buffer to induce errors writeErr error // Expected write error readErr error // Expected read error }{ // Force errors on discriminant. {0, []byte{0x00}, pver, 0, io.ErrShortWrite, io.EOF}, // Force errors on 2-byte read/write. {0xfd, []byte{0xfd}, pver, 2, io.ErrShortWrite, io.ErrUnexpectedEOF}, // Force errors on 4-byte read/write. {0x10000, []byte{0xfe}, pver, 2, io.ErrShortWrite, io.ErrUnexpectedEOF}, // Force errors on 8-byte read/write. {0x100000000, []byte{0xff}, pver, 2, io.ErrShortWrite, io.ErrUnexpectedEOF}, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode to wire format. w := newFixedWriter(test.max) err := wire.TstWriteVarInt(w, test.pver, test.in) if err != test.writeErr { t.Errorf("writeVarInt #%d wrong error got: %v, want: %v", i, err, test.writeErr) continue } // Decode from wire format. r := newFixedReader(test.max, test.buf) _, err = wire.TstReadVarInt(r, test.pver) if err != test.readErr { t.Errorf("readVarInt #%d wrong error got: %v, want: %v", i, err, test.readErr) continue } } }
// TestVarIntWire tests wire encode and decode for variable length integers. func TestVarIntWire(t *testing.T) { pver := wire.ProtocolVersion tests := []struct { in uint64 // Value to encode out uint64 // Expected decoded value buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding }{ // Latest protocol version. // Single byte {0, 0, []byte{0x00}, pver}, // Max single byte {0xfc, 0xfc, []byte{0xfc}, pver}, // Min 2-byte {0xfd, 0xfd, []byte{0xfd, 0x0fd, 0x00}, pver}, // Max 2-byte {0xffff, 0xffff, []byte{0xfd, 0xff, 0xff}, pver}, // Min 4-byte {0x10000, 0x10000, []byte{0xfe, 0x00, 0x00, 0x01, 0x00}, pver}, // Max 4-byte {0xffffffff, 0xffffffff, []byte{0xfe, 0xff, 0xff, 0xff, 0xff}, pver}, // Min 8-byte { 0x100000000, 0x100000000, []byte{0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}, pver, }, // Max 8-byte { 0xffffffffffffffff, 0xffffffffffffffff, []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, pver, }, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode to wire format. var buf bytes.Buffer err := wire.TstWriteVarInt(&buf, test.pver, test.in) if err != nil { t.Errorf("writeVarInt #%d error %v", i, err) continue } if !bytes.Equal(buf.Bytes(), test.buf) { t.Errorf("writeVarInt #%d\n got: %s want: %s", i, spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) continue } // Decode from wire format. rbuf := bytes.NewReader(test.buf) val, err := wire.TstReadVarInt(rbuf, test.pver) if err != nil { t.Errorf("readVarInt #%d error %v", i, err) continue } if val != test.out { t.Errorf("readVarInt #%d\n got: %d want: %d", i, val, test.out) continue } } }
// TestVersionWireErrors performs negative tests against wire encode and // decode of MsgGetHeaders to confirm error paths work correctly. func TestVersionWireErrors(t *testing.T) { // Use protocol version 60002 specifically here instead of the latest // because the test data is using bytes encoded with that protocol // version. pver := uint32(60002) wireErr := &wire.MessageError{} // Ensure calling MsgVersion.BtcDecode with a non *bytes.Buffer returns // error. fr := newFixedReader(0, []byte{}) if err := baseVersion.BtcDecode(fr, pver); err == nil { t.Errorf("Did not received error when calling " + "MsgVersion.BtcDecode with non *bytes.Buffer") } // Copy the base version and change the user agent to exceed max limits. bvc := *baseVersion exceedUAVer := &bvc newUA := "/" + strings.Repeat("t", wire.MaxUserAgentLen-8+1) + ":0.0.1/" exceedUAVer.UserAgent = newUA // Encode the new UA length as a varint. var newUAVarIntBuf bytes.Buffer err := wire.TstWriteVarInt(&newUAVarIntBuf, pver, uint64(len(newUA))) if err != nil { t.Errorf("writeVarInt: error %v", err) } // Make a new buffer big enough to hold the base version plus the new // bytes for the bigger varint to hold the new size of the user agent // and the new user agent string. Then stich it all together. newLen := len(baseVersionEncoded) - len(baseVersion.UserAgent) newLen = newLen + len(newUAVarIntBuf.Bytes()) - 1 + len(newUA) exceedUAVerEncoded := make([]byte, newLen) copy(exceedUAVerEncoded, baseVersionEncoded[0:80]) copy(exceedUAVerEncoded[80:], newUAVarIntBuf.Bytes()) copy(exceedUAVerEncoded[83:], []byte(newUA)) copy(exceedUAVerEncoded[83+len(newUA):], baseVersionEncoded[97:100]) tests := []struct { in *wire.MsgVersion // Value to encode buf []byte // Wire encoding pver uint32 // Protocol version for wire encoding max int // Max size of fixed buffer to induce errors writeErr error // Expected write error readErr error // Expected read error }{ // Force error in protocol version. {baseVersion, baseVersionEncoded, pver, 0, io.ErrShortWrite, io.EOF}, // Force error in services. {baseVersion, baseVersionEncoded, pver, 4, io.ErrShortWrite, io.EOF}, // Force error in timestamp. {baseVersion, baseVersionEncoded, pver, 12, io.ErrShortWrite, io.EOF}, // Force error in remote address. {baseVersion, baseVersionEncoded, pver, 20, io.ErrShortWrite, io.EOF}, // Force error in local address. {baseVersion, baseVersionEncoded, pver, 47, io.ErrShortWrite, io.ErrUnexpectedEOF}, // Force error in nonce. {baseVersion, baseVersionEncoded, pver, 73, io.ErrShortWrite, io.ErrUnexpectedEOF}, // Force error in user agent length. {baseVersion, baseVersionEncoded, pver, 81, io.ErrShortWrite, io.EOF}, // Force error in user agent. {baseVersion, baseVersionEncoded, pver, 82, io.ErrShortWrite, io.ErrUnexpectedEOF}, // Force error in last block. {baseVersion, baseVersionEncoded, pver, 98, io.ErrShortWrite, io.ErrUnexpectedEOF}, // Force error in relay tx - no read error should happen since // it's optional. { baseVersionBIP0037, baseVersionBIP0037Encoded, wire.BIP0037Version, 101, io.ErrShortWrite, nil, }, // Force error due to user agent too big {exceedUAVer, exceedUAVerEncoded, pver, newLen, wireErr, wireErr}, } t.Logf("Running %d tests", len(tests)) for i, test := range tests { // Encode to wire format. w := newFixedWriter(test.max) err := test.in.BtcEncode(w, test.pver) if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) { t.Errorf("BtcEncode #%d wrong error got: %v, want: %v", i, err, test.writeErr) continue } // For errors which are not of type wire.MessageError, check // them for equality. if _, ok := err.(*wire.MessageError); !ok { if err != test.writeErr { t.Errorf("BtcEncode #%d wrong error got: %v, "+ "want: %v", i, err, test.writeErr) continue } } // Decode from wire format. var msg wire.MsgVersion buf := bytes.NewBuffer(test.buf[0:test.max]) err = msg.BtcDecode(buf, test.pver) if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) { t.Errorf("BtcDecode #%d wrong error got: %v, want: %v", i, err, test.readErr) continue } // For errors which are not of type wire.MessageError, check // them for equality. if _, ok := err.(*wire.MessageError); !ok { if err != test.readErr { t.Errorf("BtcDecode #%d wrong error got: %v, "+ "want: %v", i, err, test.readErr) continue } } } }