-
Notifications
You must be signed in to change notification settings - Fork 0
/
pasteme_cli_test.go
192 lines (158 loc) · 5.25 KB
/
pasteme_cli_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/pbkdf2"
)
func TestMain(m *testing.M) {
code := m.Run()
os.Exit(code)
}
func TestGenerateRandomBytes(t *testing.T) {
a := assert.New(t)
rb, err := GenerateRandomBytes(24)
expected := len(make([]byte, 24))
actual := len(rb)
if err != nil {
t.Fatalf("We could not get enough random bytes")
}
if !a.Equal(expected, actual) {
t.Fatalf("We could not get the speicifed amount of bytes")
}
}
func TestDeriveKey(t *testing.T) {
a := assert.New(t)
//test with predefined passphrase and salt
mockPassphrase := "passphrase"
mockSalt := []byte("randomSalt")
mockPbkdf2 := pbkdf2.Key([]byte(mockPassphrase), mockSalt, 1000, 32, sha256.New)
pbkdf2result, saltResult := deriveKey(mockPassphrase, mockSalt)
if !a.Equal(mockPbkdf2, pbkdf2result) {
t.Fatalf("The derived keys do not match!")
}
if !a.Equal(mockSalt, saltResult) {
t.Fatalf("The salts do not match!")
}
//test with random passphrase and salt
randomPassphrase, _ := GenerateRandomBytes(28)
randomSalt, _ := GenerateRandomBytes(8)
randomPbkdf2result, randomSaltresult := deriveKey(string(randomPassphrase), randomSalt)
if !a.NotEqual(randomPbkdf2result, pbkdf2result) {
t.Fatalf("The mock matches the random generated pbkdf2 data. This is not good!")
}
if !a.NotEqual(randomSaltresult, saltResult) {
t.Fatalf("The mock matches the random generated salt data. This is not good!")
}
}
func TestEncrypt(t *testing.T) {
a := assert.New(t)
//test with predefined passphrase and salt
mockPassphrase := "passphrase"
plainText := "Some random text to encrypt"
result := encrypt(mockPassphrase, []byte(plainText))
resultArr := strings.Split(result, "-")
if !a.Equal(len(resultArr), 3) {
t.Fatalf("The result is not in the correct format")
}
}
func TestIsValidMinutes(t *testing.T) {
a := assert.New(t)
validMinutes := []int64{5, 10, 60, 1440, 10080, 43800}
for _, minute := range validMinutes {
result := IsValidMinutes(minute)
if !a.Equal(result, true) {
t.Fatalf("The result does not match the expectations.")
}
}
}
func TestAction(t *testing.T) {
type error interface {
Error() string
}
var err error
//var result string
a := assert.New(t)
err = SetupApp([]string{"cmd", ""})
if !a.Equal("paste_name_error", err.Error()) {
t.Fatalf("The expected result did not match the required result. (%s, %s)", err, "paste_name_error")
}
//second run with name parameter
err = SetupApp([]string{"cmd", "--name", "Asddd"})
//
if !a.Equal("paste_length_error", err.Error()) {
t.Fatalf("The expected result did not match the required result. (%s, %s)", err, "paste_length_error")
}
//third run without minutes flag
err = SetupApp([]string{"cmd", "--name", "Asddd", "--body", "This is a random paste body"})
//
if !a.Equal("expires_not_found", err.Error()) {
t.Fatalf("The expected result did not match the required result. (%s, %s)", err.Error(), "expires_not_found")
}
//with minutes flag
err = SetupApp([]string{"cmd", "--name", "Asddd", "--body", "This is a random paste body", "--expires", "5"})
//
if !a.Equal(nil, err) {
t.Fatalf("The expected result did not match the required result. (%s, %s)", err.Error(), "minutes_not_found")
}
//with destroy flag
err = SetupApp([]string{"cmd", "--name", "Asddd", "--body", "This is a random paste body", "--destroy"})
//
if !a.Equal(nil, err) {
t.Fatalf("The expected result did not match the required result. (%s, %s)", err.Error(), "minutes_not_found")
}
//with source flag
err = SetupApp([]string{"cmd", "--name", "Asddd", "--body", "This is a random paste body", "--source", "--destroy"})
//
if !a.Equal(nil, err) {
t.Fatalf("The expected result did not match the required result. (%s, %s)", err.Error(), "minutes_not_found")
}
fmt.Println(err)
}
func SetupApp(args []string) error {
//var res string
var err error
app := cli.NewApp()
app.Writer = ioutil.Discard
app.Name = "Paste.me"
app.Version = "v0.0.3"
app.Usage = "Share your pastes securely"
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "name",
Usage: "Insert the name of the paste here.",
},
&cli.StringFlag{
Name: "body",
Usage: "Here you can insert the paste body or send it through cli.",
},
&cli.Int64Flag{
Name: "expires",
Usage: "Here you will be able to set an expiration time for your pastes. The expiration time should be defined in minutes. Allowed values for the time being: 5,10,60,1440,10080,43800.",
},
&cli.BoolFlag{
Name: "destroy",
Usage: "With this flag, you are posting the paste with a 'Self Destruct' flag. The link will work only once.",
},
&cli.BoolFlag{
Name: "source",
Usage: "With this flag, you are posting a paste which is some kind of source code. Syntax highlighting will be applied.",
},
}
app.Action = Action
err = app.Run(args)
return err
}
func TestBase(t *testing.T) {
os.Args = []string{"cmd", "--name", "Asddd", "--body", "This is a random paste body", "--source", "--destroy"}
main()
}
func TestWithFile(t *testing.T) {
os.Args = []string{"cmd", "--name", "Asddd", "--body", "This is a random paste body", "--source", "--destroy", "--file", "./samplefiles/test1.txt", "--file", "./samplefiles/test2.txt"}
main()
}