github.com/moreal/bencodex-go@v0.0.0-20231021172012-18277a477d15/decoder_test.go (about) 1 package bencode 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 var ( 10 unmarshalTestData = []byte("d8:announce38:udp://tracker.publicbt.com:80/announce13:announce-listll38:udp://tracker.publicbt.com:80/announceel44:udp://tracker.openbittorrent.com:80/announceee7:comment33:Debian CD from cdimage.debian.org4:infod6:lengthi170917888e4:name30:debian-8.8.0-arm64-netinst.iso12:piece lengthi262144eee") 11 unmarshalWrongTestData = []byte("d4:infod6:lengthi170917888e12:piece lengthi262144e4:name30:debian-8.8.0-arm64-netinst.isoe8:announce38:udp://tracker.publicbt.com:80/announce13:announce-listll38:udp://tracker.publicbt.com:80/announceel44:udp://tracker.openbittorrent.com:80/announceee7:comment35:Debian CD from cdimage.debian.orge") 12 ) 13 14 func TestUnmarshal(t *testing.T) { 15 assert := assert.New(t) 16 17 result, err := Unmarshal([]byte("i38e")) 18 if !assert.NoError(err) || !assert.Equal(int64(38), result) { 19 return 20 } 21 22 result, err = Unmarshal([]byte("38:udp://tracker.publicbt.com:80/announce")) 23 if !assert.NoError(err) || !assert.Equal("udp://tracker.publicbt.com:80/announce", string(result.([]byte))) { 24 return 25 } 26 27 result, err = Unmarshal([]byte("ll38:udp://tracker.publicbt.com:80/announceel44:udp://tracker.openbittorrent.com:80/announceee")) 28 if !assert.NoError(err) || !assert.Equal([]interface{}{ 29 []interface{}{[]byte("udp://tracker.publicbt.com:80/announce")}, 30 []interface{}{[]byte("udp://tracker.openbittorrent.com:80/announce")}, 31 }, result) { 32 return 33 } 34 35 result, err = Unmarshal([]byte("d8:announce38:udp://tracker.publicbt.com:80/announce13:announce-listll38:udp://tracker.publicbt.com:80/announceel44:udp://tracker.openbittorrent.com:80/announceeee")) 36 if !assert.NoError(err) || !assert.Equal(map[string]interface{}{ 37 "announce": []byte("udp://tracker.publicbt.com:80/announce"), 38 "announce-list": []interface{}{ 39 []interface{}{[]byte("udp://tracker.publicbt.com:80/announce")}, 40 []interface{}{[]byte("udp://tracker.openbittorrent.com:80/announce")}, 41 }, 42 }, result) { 43 return 44 } 45 46 _, err = Unmarshal(unmarshalTestData) 47 if !assert.NoError(err) { 48 return 49 } 50 51 _, err = Unmarshal(unmarshalWrongTestData) 52 if !assert.Error(err) || !assert.Equal("bencode: not a valid bencoded string", err.Error()) { 53 return 54 } 55 56 result, err = Unmarshal([]byte("i38")) 57 if !assert.Error(err) || !assert.Nil(result) || !assert.Equal("bencode: invalid integer field", err.Error()) { 58 return 59 } 60 61 result, err = Unmarshal([]byte("i38qe")) 62 if !assert.Error(err) || !assert.Nil(result) || !assert.Equal("bencode: invalid integer byte: 113", err.Error()) { 63 return 64 } 65 66 result, err = Unmarshal([]byte("l")) 67 if !assert.Error(err) || !assert.Nil(result) || !assert.Equal("bencode: invalid list field", err.Error()) { 68 return 69 } 70 71 result, err = Unmarshal([]byte("d")) 72 if !assert.Error(err) || !assert.Nil(result) || !assert.Equal("bencode: invalid dictionary field", err.Error()) { 73 return 74 } 75 76 result, err = Unmarshal([]byte("di38ee")) 77 if !assert.Error(err) || !assert.Nil(result) || !assert.Equal("bencode: non-string dictionary key", err.Error()) { 78 return 79 } 80 81 result, err = Unmarshal([]byte("38")) 82 if !assert.Error(err) || !assert.Nil(result) || !assert.Equal("bencode: invalid string field", err.Error()) { 83 return 84 } 85 86 result, err = Unmarshal([]byte("10:wasd")) 87 if !assert.Error(err) || !assert.Nil(result) || !assert.Equal("bencode: not a valid bencoded string", err.Error()) { 88 return 89 } 90 } 91 92 func BenchmarkUnmarshal(b *testing.B) { 93 b.ReportAllocs() 94 for n := 0; n < b.N; n++ { 95 Unmarshal(unmarshalTestData) 96 } 97 }