-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
150 lines (118 loc) · 3.24 KB
/
main_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"archive/tar"
"bufio"
"compress/gzip"
"context"
"crypto/md5"
"fmt"
"io"
"math/rand"
"net/http"
"os/exec"
"strings"
"sync"
"testing"
"time"
"github.com/fasmide/schttp/scp"
"github.com/fasmide/schttp/web"
"github.com/spf13/viper"
)
// KnownTestDirectoryHash is the md5 digest of test-directory filenames and contents
const KnownTestDirectoryHash = "3e8a7eddba29589d389dc65082f840c6"
var scpPort, httpPort int
// init sets up some environment variables for testing
func init() {
// we dont need secure randomness
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
// we use random ports in the range 30000:uint16
scpPort = r.Intn(35535) + 30000
httpPort = r.Intn(35535) + 30000
viper.SetDefault("HTTP_LISTEN", fmt.Sprintf("127.0.0.1:%d", httpPort))
viper.SetDefault("ADVERTISE_URL", fmt.Sprintf("http://127.0.0.1:%d/", httpPort))
viper.SetDefault("SSH_LISTEN", fmt.Sprintf("127.0.0.1:%d", scpPort))
viper.SetDefault("PID_FILE", "/tmp/test_schttp.pid")
}
// TestMain tests schttp on a high level
// it tries to send files with standard unix tools
func TestMain(t *testing.T) {
schttp, err := NewSchttp()
if err != nil {
t.Fatalf("could not initialize schttp: %s", err)
}
var shutdown sync.WaitGroup
shutdown.Add(1)
go func() {
schttp.scpServer = scp.NewServer()
go schttp.scpServer.Listen(schttp.sshFd)
schttp.webServer = &web.Server{DB: schttp.scpServer}
go schttp.webServer.Listen(schttp.httpFd)
// wait here till we are finished testing
shutdown.Wait()
schttp.scpServer.Shutdown("shutting down test server")
schttp.webServer.Shutdown(context.TODO())
}()
scp := exec.Command("scp",
"-oStrictHostKeyChecking=no",
fmt.Sprintf("-P%d", scpPort),
"-r", "test-directory/", "127.0.0.1:",
)
reader, err := scp.StderrPipe()
if err != nil {
t.Fatalf("could not get stderr pipe from scp command: %s", err)
}
defer reader.Close()
t.Log("Executing ", scp.Args)
scp.Start()
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanLines)
var url string
for scanner.Scan() {
line := strings.Trim(scanner.Text(), "\n ")
if strings.HasSuffix(line, ".tar.gz") {
// we found our string
url = line
break
}
}
if url == "" {
t.Fatalf("no url found in stderr from scp")
}
// download this url and compare its checksum against a known value
response, err := http.Get(url)
if err != nil {
t.Fatalf("unable to http get %s: %s", url, err)
}
defer response.Body.Close()
gzipReader, err := gzip.NewReader(response.Body)
if err != nil {
t.Fatalf("unable to read gzip: %s", err)
}
tarReader := tar.NewReader(gzipReader)
h := md5.New()
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("tar error: %s", err)
}
// first just append the name of the file
_, err = h.Write([]byte(header.Name))
if err != nil {
t.Fatalf("could not update md5 digest: %s", err)
}
// then the whole content of the file
_, err = io.Copy(h, tarReader)
if err != nil {
t.Fatalf("could not update md5 digest: %s", err)
}
}
hexSum := fmt.Sprintf("%x", h.Sum(nil))
if hexSum != KnownTestDirectoryHash {
t.Fatalf("wrong md5 hash of test-directory zip file: %s != %s", hexSum, KnownTestDirectoryHash)
}
shutdown.Done()
}