forked from andreimarcu/linx-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
torrent_test.go
73 lines (56 loc) · 1.46 KB
/
torrent_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"os"
"testing"
"github.com/zeebo/bencode"
)
func TestCreateTorrent(t *testing.T) {
fileName := "server.go"
var decoded Torrent
f, err := os.Open("server.go")
if err != nil {
t.Fatal(err)
}
defer f.Close()
encoded, err := createTorrent(fileName, f, nil)
if err != nil {
t.Fatal(err)
}
bencode.DecodeBytes(encoded, &decoded)
if decoded.Encoding != "UTF-8" {
t.Fatalf("Encoding was %s, expected UTF-8", decoded.Encoding)
}
if decoded.Info.Name != "server.go" {
t.Fatalf("Name was %s, expected server.go", decoded.Info.Name)
}
if decoded.Info.PieceLength <= 0 {
t.Fatal("Expected a piece length, got none")
}
if len(decoded.Info.Pieces) <= 0 {
t.Fatal("Expected at least one piece, got none")
}
if decoded.Info.Length <= 0 {
t.Fatal("Length was less than or equal to 0, expected more")
}
tracker := fmt.Sprintf("%sselif/%s", Config.siteURL, fileName)
if decoded.UrlList[0] != tracker {
t.Fatalf("First entry in URL list was %s, expected %s", decoded.UrlList[0], tracker)
}
}
func TestCreateTorrentWithImage(t *testing.T) {
var decoded Torrent
f, err := os.Open("static/images/404.jpg")
if err != nil {
t.Fatal(err)
}
defer f.Close()
encoded, err := createTorrent("test.jpg", f, nil)
if err != nil {
t.Fatal(err)
}
bencode.DecodeBytes(encoded, &decoded)
if decoded.Info.Pieces != "r\x01\x80j\x99\x84\n\xd3dZ;1NX\xec;\x9d$+f" {
t.Fatal("Torrent pieces did not match expected pieces for image")
}
}