-
Notifications
You must be signed in to change notification settings - Fork 14
/
signature_test.go
98 lines (76 loc) · 2.24 KB
/
signature_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
/*
pipethis: Stop piping the internet into your shell
Copyright 2016 Ellotheth
Use of this source code is governed by the GNU Public License version 2
(GPLv2). You should have received a copy of the GPLv2 along with your copy of
the source. If not, see http://www.gnu.org/licenses/gpl-2.0.html.
*/
package main
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/suite"
)
type SigTest struct {
suite.Suite
}
func (s *SigTest) TestNewCreatesFilename() {
sig := NewSignature(nil, &Script{filename: "foo.sh"}, "")
s.Equal("foo.sh.sig", sig.Name())
}
func (s *SigTest) TestSourceUsesSaved() {
sig := Signature{source: "foosig"}
s.Equal("foosig", sig.Source())
}
func (s *SigTest) TestSourceBuildsFromScript() {
sig := Signature{script: &Script{source: "scriptsource"}}
s.Equal("scriptsource.sig", sig.Source())
}
func (s *SigTest) TestSourceEmptyWithClearsignedScript() {
sig := Signature{script: &Script{clearsigned: true}}
s.Equal("", sig.Source())
}
func (s *SigTest) TestDownloadIsNoopWithClearsignedScript() {
sig := Signature{script: &Script{clearsigned: true}}
s.NoError(sig.Download())
}
func (s *SigTest) TestDownloadFailsWithoutSource() {
sig := Signature{}
s.Error(sig.Download())
}
func (s *SigTest) TestDownloadFailsWithNonexistentSource() {
sig := Signature{source: "not-a-real-file"}
s.Error(sig.Download())
}
func (s *SigTest) TestDownloadFailsWithoutDestinationName() {
f, err := ioutil.TempFile("", "pipethis-test-")
if err != nil {
s.Fail("Failed creating the test file")
}
sig := Signature{source: f.Name()}
defer os.Remove(sig.Source())
s.Error(sig.Download())
}
func (s *SigTest) TestDownloadCopiesFile() {
f, err := ioutil.TempFile("", "pipethis-test-")
if err != nil {
s.Fail("Failed creating the test file")
}
sig := Signature{source: f.Name(), filename: "destination"}
defer os.Remove(sig.Source())
defer os.Remove(sig.Name())
expected := []byte("file contents, wooo")
ioutil.WriteFile(sig.Source(), expected, os.ModePerm)
s.NoError(sig.Download())
actual, _ := ioutil.ReadFile(sig.Name())
s.Equal(expected, actual)
}
func (s *SigTest) TestBodyFailsWithoutFiles() {
sig := Signature{}
_, err := sig.Body()
s.Error(err)
}
func TestSignatureTest(t *testing.T) {
suite.Run(t, new(SigTest))
}