forked from fynelabs/selfupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_source_test.go
68 lines (54 loc) · 2.08 KB
/
http_source_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
package selfupdate
import (
"crypto/ed25519"
"io/ioutil"
"log"
"net/http"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestHTTPSourceLatestVersion(t *testing.T) {
client := http.Client{Timeout: time.Duration(60) * time.Second}
httpSource := NewHTTPSource(&client, "http://geoffrey-test-artefacts.fynelabs.com/self-update/Nomad.exe")
version, err := httpSource.LatestVersion()
assert.Nil(t, err)
assert.NotNil(t, version)
}
func TestHTTPSourceCheckSignature(t *testing.T) {
client := http.Client{Timeout: time.Duration(60) * time.Second}
publicKey := ed25519.PublicKey{231, 120, 42, 245, 227, 182, 133, 19, 197, 251, 215, 216, 34, 35, 16, 183, 184, 174, 55, 30, 107, 18, 43, 136, 111, 68, 168, 138, 176, 212, 156, 124}
wrongPublicKey := ed25519.PublicKey{42, 103, 83, 57, 61, 138, 18, 249, 244, 80, 163, 162, 24, 251, 190, 241, 11, 168, 179, 41, 245, 27, 166, 70, 220, 254, 118, 169, 101, 26, 199, 129}
httpSource := NewHTTPSource(&client, "http://geoffrey-test-artefacts.fynelabs.com/self-update/Nomad.exe")
signature, err := httpSource.GetSignature()
assert.Nil(t, err)
file, contentLength, err := httpSource.Get(&Version{Date: time.Unix(100, 0)})
log.Println(file, " -- ", err)
assert.Nil(t, err)
assert.NotNil(t, file)
assert.Equal(t, int64(19320832), contentLength)
body, err := ioutil.ReadAll(file)
assert.Nil(t, err)
assert.NotNil(t, body)
file.Close()
ok := ed25519.Verify(publicKey, body, signature[:])
assert.True(t, ok)
ok = ed25519.Verify(wrongPublicKey, body, signature[:])
assert.False(t, ok)
}
func TestReplaceURLTemplate(t *testing.T) {
nochange := "http://localhost/nomad-windows-amd64.exe"
change := "http://localhost/nomad-{{.OS}}-{{.Arch}}{{.Ext}}"
expected := ""
if runtime.GOOS == "windows" {
expected = "http://localhost/nomad-" + runtime.GOOS + "-" + runtime.GOARCH + ".exe"
} else {
expected = "http://localhost/nomad-" + runtime.GOOS + "-" + runtime.GOARCH
}
r := replaceURLTemplate(nochange)
assert.Equal(t, nochange, r)
r = replaceURLTemplate(change)
assert.NotEqual(t, change, r)
assert.Equal(t, expected, r)
}