-
Notifications
You must be signed in to change notification settings - Fork 7
/
dirwatch_test.go
77 lines (65 loc) · 1.55 KB
/
dirwatch_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
74
75
76
77
package main
import (
"crypto/sha1"
"io/ioutil"
"os"
"testing"
)
type testVector struct {
dir string
expectedTorrent string
}
var vecs = []testVector{
{
dir: "testData/dsl-4.4.10.iso",
expectedTorrent: "testData/dsl-1M.torrent",
},
{
dir: "testData/1Mrandom",
expectedTorrent: "testData/1Mrandom.torrent",
},
}
func TestTorrentify(t *testing.T) {
if testing.Short() {
t.Skip("This test requires the iso")
}
for _, vec := range vecs {
if _, err := os.Stat("testData/dsl-4.4.10.iso"); err != nil && os.IsNotExist(err) {
t.Fatal("You need to download the iso relative to a.torrent to run this test")
}
actualMeta, err := createMeta(vec.dir)
if err != nil {
t.Fatal(err)
}
actualPieces := split(actualMeta.Info.Pieces)
expected, err := ioutil.ReadFile(vec.expectedTorrent)
if err != nil {
t.Fatal(err)
}
expectedMeta, err := NewMetaInfoFromContent(expected)
if err != nil {
t.Fatal(err)
}
expectedPieces := split(expectedMeta.Info.Pieces)
if len(expectedPieces) != len(actualPieces) {
t.Fatalf("Invalid length! Expected %d piece(s), got %d\n", len(expectedPieces), len(actualPieces))
}
for i, exp := range expectedPieces {
if actualPieces[i] != exp {
t.Fatalf("%x - %x\n", exp, actualPieces[i])
}
}
}
}
func split(pieces string) []string {
out := make([]string, len(pieces)/sha1.Size)
for i := 0; i < len(out); i++ {
from := i * sha1.Size
to := (i + 1) * sha1.Size
if i == len(out)-1 {
to = len(pieces)
}
out[i] = pieces[from:to]
}
return out
}