github.com/anacrolix/torrent@v1.61.0/tests/issue-952/issue-952_test.go (about)

     1  package issue_952
     2  
     3  import (
     4  	"testing"
     5  
     6  	qt "github.com/go-quicktest/qt"
     7  
     8  	"github.com/anacrolix/torrent/bencode"
     9  	"github.com/anacrolix/torrent/metainfo"
    10  	"github.com/anacrolix/torrent/types/infohash"
    11  )
    12  
    13  type scrapeResponse struct {
    14  	Files map[metainfo.Hash]scrapeResponseFile `bencode:"files"`
    15  }
    16  
    17  type scrapeResponseFile struct {
    18  	Complete   int `bencode:"complete"`
    19  	Downloaded int `bencode:"downloaded"`
    20  	Incomplete int `bencode:"incomplete"`
    21  }
    22  
    23  // This tests unmarshalling to a map with a non-string dict key.
    24  func TestUnmarshalStringToByteArray(t *testing.T) {
    25  	var s scrapeResponse
    26  	const hashStr = "\x05a~F\xfd{c\xd1`\xb8\xd9\x89\xceM\xb9t\x1d\\\x0b\xded"
    27  	err := bencode.Unmarshal([]byte("d5:filesd20:\x05a~F\xfd{c\xd1`\xb8\xd9\x89\xceM\xb9t\x1d\\\x0b\xded9:completedi1e10:downloadedi1eeee"), &s)
    28  	qt.Assert(t, qt.IsNil(err))
    29  	qt.Check(t, qt.HasLen(s.Files, 1))
    30  	file, ok := s.Files[(infohash.T)([]byte(hashStr))]
    31  	qt.Assert(t, qt.IsTrue(ok))
    32  	qt.Check(t, qt.Equals(file, scrapeResponseFile{
    33  		// Note that complete is misspelled in the example. I don't know why.
    34  		Complete:   0,
    35  		Downloaded: 1,
    36  		Incomplete: 0,
    37  	}))
    38  
    39  }