Thanks to @anacrolix, we no longer need to monkey-patch the torrent!

Also added `operations_test.go` to ensure that it works as it's
advertised.
This commit is contained in:
Bora Alper 2017-11-07 21:20:40 +00:00
parent e4d7bcac2d
commit d0c1b68692
2 changed files with 47 additions and 2 deletions

View File

@ -235,10 +235,11 @@ func (ms *MetadataSink) awaitMetadata(infoHash metainfo.Hash, peer Peer) {
} else if rMessage[1] == 0x01 { } else if rMessage[1] == 0x01 {
// __on_ext_message(message[2:]) // __on_ext_message(message[2:])
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Run TestDecoder() function in operations_test.go in case you have any doubts.
rMessageBuf := bytes.NewBuffer(rMessage[2:]) rMessageBuf := bytes.NewBuffer(rMessage[2:])
rExtDict := new(extDict) rExtDict := new(extDict)
// TODO: We monkey-patched anacrolix/torrent! err := bencode.NewDecoder(rMessageBuf).Decode(rExtDict)
err := bencode.NewDecoder2(rMessageBuf).Decode(rExtDict)
if err != nil { if err != nil {
zap.L().Warn("Couldn't decode extension message in the loop!", zap.Error(err)) zap.L().Warn("Couldn't decode extension message in the loop!", zap.Error(err))
return return

View File

@ -0,0 +1,44 @@
package bittorrent
import (
"bytes"
"testing"
"github.com/anacrolix/torrent/bencode"
)
var operationsTest_instances = []struct{
dump []byte
surplus []byte
}{
// No Surplus
{
dump: []byte("d1:md11:ut_metadatai1ee13:metadata_sizei22528ee"),
surplus: []byte(""),
},
// Surplus is an ASCII string
{
dump: []byte("d1:md11:ut_metadatai1ee13:metadata_sizei22528eeDENEME"),
surplus: []byte("DENEME"),
},
// Surplus is a bencoded dictionary
{
dump: []byte("d1:md11:ut_metadatai1ee13:metadata_sizei22528eed3:inti1337ee"),
surplus: []byte("d3:inti1337ee"),
},
}
func TestDecoder(t *testing.T) {
for i, instance := range operationsTest_instances {
buf := bytes.NewBuffer(instance.dump)
err := bencode.NewDecoder(buf).Decode(&struct {}{})
if err != nil {
t.Errorf("Couldn't decode the dump #%d! %s", i + 1, err.Error())
}
bufSurplus := buf.Bytes()
if !bytes.Equal(bufSurplus, instance.surplus) {
t.Errorf("Surplus #%d is not equal to what we expected! `%s`", i + 1, bufSurplus)
}
}
}