|
| 1 | +package bencode |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestEcodeInt(t *testing.T) { |
| 9 | + type test struct { |
| 10 | + Test uint64 `bencode:"test"` |
| 11 | + } |
| 12 | + have, err := encode(&test{1234567890}) |
| 13 | + want := []byte("d4:testi1234567890ee") |
| 14 | + if err != nil { |
| 15 | + t.Error(err) |
| 16 | + } |
| 17 | + if !reflect.DeepEqual(have, want) { |
| 18 | + t.Errorf("Struct not properly encoded: wanted %s but have %s", want, have) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func TestEncodeString(t *testing.T) { |
| 23 | + type test struct { |
| 24 | + Test string `bencode:"test"` |
| 25 | + } |
| 26 | + have, err := encode(&test{"test"}) |
| 27 | + want := []byte("d4:test4:teste") |
| 28 | + if err != nil { |
| 29 | + t.Error(err) |
| 30 | + } |
| 31 | + if !reflect.DeepEqual(have, want) { |
| 32 | + t.Errorf("Struct not properly encoded: wanted %s but have %s", want, have) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestEncodeStringInt(t *testing.T) { |
| 37 | + type test struct { |
| 38 | + TestString string `bencode:"teststring"` |
| 39 | + TestInt uint64 `bencode:"testint"` |
| 40 | + } |
| 41 | + have, err := encode(&test{"test", 1234567890}) |
| 42 | + want := []byte("d10:teststring4:test7:testinti1234567890ee") |
| 43 | + if err != nil { |
| 44 | + t.Error(err) |
| 45 | + } |
| 46 | + if !reflect.DeepEqual(have, want) { |
| 47 | + t.Errorf("Struct not properly encoded: wanted %s but have %s", want, have) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestEncodeMockTorrentFile(t *testing.T) { |
| 52 | + type info struct { |
| 53 | + Length uint64 `bencode:"length"` |
| 54 | + Name string `bencode:"name"` |
| 55 | + PieceLength uint64 `bencode:"piece length"` |
| 56 | + } |
| 57 | + type test struct { |
| 58 | + Announce string `bencode:"announce"` |
| 59 | + AnnounceList [][]string `bencode:"announce-list"` |
| 60 | + Comment string `bencode:"comment"` |
| 61 | + CreatedBy string `bencode:"created by"` |
| 62 | + CreationDate uint64 `bencode:"creation date"` |
| 63 | + Info info `bencode:"info"` |
| 64 | + } |
| 65 | + have, err := encode(&test{"https://torrent.ubuntu.com/announce", [][]string{{"https://torrent.ubuntu.com/announce"}, {"https://ipv6.torrent.ubuntu.com/announce"}}, "Ubuntu CD releases.ubuntu.com", "mktorrent 1.1", 1681992794, info{4932407296, "ubuntu-23.04-desktop-amd64.iso", 262144}}) |
| 66 | + want := []byte("d8:announce35:https://torrent.ubuntu.com/announce13:announce-listll35:https://torrent.ubuntu.com/announceel40:https://ipv6.torrent.ubuntu.com/announceee7:comment29:Ubuntu CD releases.ubuntu.com10:created by13:mktorrent 1.113:creation datei1681992794e4:infod6:lengthi4932407296e4:name30:ubuntu-23.04-desktop-amd64.iso12:piece lengthi262144eee") |
| 67 | + if err != nil { |
| 68 | + t.Error(err) |
| 69 | + } |
| 70 | + if !reflect.DeepEqual(have, want) { |
| 71 | + t.Errorf("Struct not properly encoded: wanted %s but have %s", want, have) |
| 72 | + } |
| 73 | +} |
0 commit comments