-
Notifications
You must be signed in to change notification settings - Fork 14
/
script_test.go
158 lines (130 loc) · 3.95 KB
/
script_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
151
152
153
154
155
156
157
158
/*
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 ScriptTest struct {
suite.Suite
}
func (s *ScriptTest) TestDetachSigReturnsUnsignedContents() {
script := Script{}
contents, err := script.detachSignature([]byte("foo"))
s.NoError(err)
s.Equal([]byte("foo"), contents)
}
func (s *ScriptTest) TestDetachSigReturnsContentsWithoutSig() {
expectedContents := `# PIPETHIS_AUTHOR 5AA6F296
echo this is my file and there will be one match`
expectedSig := `-----BEGIN PGP SIGNATURE-----
wsFcBAEBCAAQBQJW7YbQCRALxruWWqbylgAAAwcQACrkjS8FVy6T5+pdrcYQToo3
41ppFNARFLor94lgeDzzuR4XkfNR4Cms8saceKTLfkan5nxnS/w6c/koFPskDHyS
2mZL0dDia7KUNMPt2rZGC6CLkKXs83eWAFZr4S2Y4gUYQGatAzvhTc9kgFtPRyBn
DA6FT4zj5JK4CcmxHITHeKokCwOdje80kGYlX2u3e5bqQpTbKLQ2oLMMZFfEXrDJ
Y3QOOKmBBKrS1TXVBReauojXVNNjADPZHYQzGoxgZ0GjTESkEjzrCjQnnTMcIN4C
nlskH9q28xyeDWBj+H7gNOpQZ2B3fs0cUs05Ucce/xBZeHaXqaW3GFmfdCbv1J9A
CjgVMGojAjTZf47y1mmHL9yh9gkXaLTYyO37MNku+cR0ntKIi3VyIHopiljYPGOG
r3EFxZOHg40QalMezFIfUG0S2MLpJ9+d5cvgdzHHHZYeL49L17U6eePnbt++xmGy
RrnWb1C/OXxYfCveB42v1/gg9novYYZ8/n/OLCsOL37v+b8rwEjNufmv+7G7DqOU
ejljGRd27WQSBaYQMovWGpgLmMyCiW6wnUbYFivlOTcnMvOnRsBXqCJt0jdpFEBp
hfZr8sPYSgWIDnkt7DWIwd8/eap5mgMkC5j+Q81Lcv01OfDmppRlMWRf+a4BpHFd
FiV3SWOrHc2hIbLugeNf
=tnov
-----END PGP SIGNATURE-----`
script := Script{filename: "fixtures/signed.attached"}
sigFile := script.filename + ".sig"
body, err := getLocal(script.filename)
s.NoError(err)
contents, err := ioutil.ReadAll(body)
body.Close()
s.NoError(err)
contents, err = script.detachSignature(contents)
s.NoError(err)
s.True(script.clearsigned)
s.Equal([]byte(expectedContents), contents)
// check the (now detached) signature contents
body, err = getLocal(sigFile)
s.NoError(err)
contents, err = ioutil.ReadAll(body)
body.Close()
s.NoError(err)
s.Equal([]byte(expectedSig), contents)
err = os.Remove(sigFile)
s.NoError(err)
}
func (s *ScriptTest) TestAuthorUsesSavedName() {
script := Script{author: "foo"}
author, err := script.Author()
s.NoError(err)
s.Equal("foo", author)
}
func (s *ScriptTest) TestAuthorParsesFileForPattern() {
f, err := ioutil.TempFile("", "pipethis-test-")
if err != nil {
s.Fail("Failed creating the test file")
}
filename := f.Name()
authorTest := func(expectedAuthor string, expectedError bool, input string) {
ioutil.WriteFile(filename, []byte(input), os.ModePerm)
script := Script{filename: filename}
author, err := script.Author()
s.Equal(expectedAuthor, author)
if expectedError {
s.Error(err)
} else {
s.NoError(err)
}
}
for _, contents := range providerTestAuthorValid() {
authorTest(contents[0], false, contents[1])
}
for _, contents := range providerTestAuthorInvalid() {
authorTest(contents[0], true, contents[1])
}
os.Remove(filename)
}
func providerTestAuthorInvalid() [][]string {
return [][]string{
{"", ``},
{"", `no author here yo`},
{"", `
stuff things
more stuff
# comments to ignore
// reasons bar_STUFF_123 is my name but it should only pick up the first word
# more comments
things and stuff
`},
}
}
func providerTestAuthorValid() [][]string {
return [][]string{
{`bar`, `PIPETHIS_AUTHOR bar`},
{`bar`, `// PIPETHIS_AUTHOR bar `},
{`bar`, `# PIPETHIS_AUTHOR bar `},
{`bar`, `# PIPETHIS_AUTHOR bar `},
{`bar_STUFF_123`, `
stuff things
more stuff
# comments to ignore
// reasons PIPETHIS_AUTHOR bar_STUFF_123 is my name but it should only pick up the first word
# more comments
things and stuff
`},
{`bar_STUFF_123`, `
// PIPETHIS_AUTHOR bar_STUFF_123 should take this one
// PIPETHIS_AUTHOR other_author should ignore this one
`},
}
}
func TestScriptTest(t *testing.T) {
suite.Run(t, new(ScriptTest))
}