-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
134 lines (121 loc) · 5.92 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
package main
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"gotest.tools/assert"
)
var correctTokenForTest string
func TestTrigger(t *testing.T) {
correctTokenForTest = "PzO0xhYhndAUu9xTwhOP85EyiyyZSk5dzAG39YYDzm9PEtTWa3yDbQZkV0DuuIRe"
jsonStr := []byte(`{"name":"xyz","value":"123", "token": "` + correctTokenForTest + `"}`)
request := httptest.NewRequest("POST", "/", bytes.NewBuffer(jsonStr))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
handler := http.HandlerFunc(handler)
handler.ServeHTTP(response, request)
assert.Equal(t, int(200), response.Code, "Should be succeed")
bodyBytes, _ := ioutil.ReadAll(response.Body)
bodyString := string(bodyBytes)
assert.Equal(t, true, strings.Contains(string(bodyString), "OK"))
result, _ := ioutil.ReadFile("./result")
assert.Equal(t, true, strings.Contains(string(result), "123"))
jsonStr = []byte(`{"name":"xyz","value":"abc-456", "token": "` + correctTokenForTest + `"}`)
request = httptest.NewRequest("POST", "/", bytes.NewBuffer(jsonStr))
request.Header.Set("Content-Type", "application/json")
response = httptest.NewRecorder()
handler = http.HandlerFunc(handler)
handler.ServeHTTP(response, request)
assert.Equal(t, int(200), response.Code, "Should be succeed")
bodyBytes, _ = ioutil.ReadAll(response.Body)
bodyString = string(bodyBytes)
assert.Equal(t, true, strings.Contains(string(bodyString), "OK"))
result, _ = ioutil.ReadFile("./result")
assert.Equal(t, true, strings.Contains(string(result), "abc-456"))
jsonStr = []byte(`{"name":"xyz","value":"abc_456", "token": "` + correctTokenForTest + `"}`)
request = httptest.NewRequest("POST", "/", bytes.NewBuffer(jsonStr))
request.Header.Set("Content-Type", "application/json")
response = httptest.NewRecorder()
handler = http.HandlerFunc(handler)
handler.ServeHTTP(response, request)
assert.Equal(t, int(200), response.Code, "Should be succeed")
bodyBytes, _ = ioutil.ReadAll(response.Body)
bodyString = string(bodyBytes)
assert.Equal(t, true, strings.Contains(string(bodyString), "OK"))
result, _ = ioutil.ReadFile("./result")
assert.Equal(t, true, strings.Contains(string(result), "abc_456"))
}
func TestTriggerArbitraryCodeExecution(t *testing.T) {
jsonStr := []byte(`{"name":"xyz","value":"456 && echo 'sip' > /tmp/ok", "token": "` + correctTokenForTest + `"}`)
request := httptest.NewRequest("POST", "/", bytes.NewBuffer(jsonStr))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
handler := http.HandlerFunc(handler)
handler.ServeHTTP(response, request)
assert.Equal(t, int(400), response.Code, "Should be fail")
jsonStr = []byte(`{"name":"xyz","value":"|", "token": "` + correctTokenForTest + `"}`)
request = httptest.NewRequest("POST", "/", bytes.NewBuffer(jsonStr))
request.Header.Set("Content-Type", "application/json")
response = httptest.NewRecorder()
handler = http.HandlerFunc(handler)
handler.ServeHTTP(response, request)
assert.Equal(t, int(400), response.Code, "Should be fail")
jsonStr = []byte(`{"name":"xyz","value":"\", "token": "` + correctTokenForTest + `"}`)
request = httptest.NewRequest("POST", "/", bytes.NewBuffer(jsonStr))
request.Header.Set("Content-Type", "application/json")
response = httptest.NewRecorder()
handler = http.HandlerFunc(handler)
handler.ServeHTTP(response, request)
assert.Equal(t, int(400), response.Code, "Should be fail")
jsonStr = []byte(`{"name":"xyz","value":">", "token": "` + correctTokenForTest + `"}`)
request = httptest.NewRequest("POST", "/", bytes.NewBuffer(jsonStr))
request.Header.Set("Content-Type", "application/json")
response = httptest.NewRecorder()
handler = http.HandlerFunc(handler)
handler.ServeHTTP(response, request)
assert.Equal(t, int(400), response.Code, "Should be fail")
jsonStr = []byte(`{"name":"xyz","value":"a b", "token": "` + correctTokenForTest + `"}`)
request = httptest.NewRequest("POST", "/", bytes.NewBuffer(jsonStr))
request.Header.Set("Content-Type", "application/json")
response = httptest.NewRecorder()
handler = http.HandlerFunc(handler)
handler.ServeHTTP(response, request)
assert.Equal(t, int(400), response.Code, "Should be fail")
}
func TestTriggerWithInvalidToken(t *testing.T) {
jsonStr := []byte(`{"name":"xyz","value":"456", "token": "XXWKkMSGK7tCb7jCSVZNmJzWneNDb2funq6kSLUPDVCgL8gAMPBfUWLyKtQdLpXX"}`)
request := httptest.NewRequest("POST", "/", bytes.NewBuffer(jsonStr))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
handler := http.HandlerFunc(handler)
handler.ServeHTTP(response, request)
assert.Equal(t, int(401), response.Code, "Should be failed")
}
func TestTriggerWithInvalidDeploymentName(t *testing.T) {
jsonStr := []byte(`{"name":"xxyzz","value":"456", "token": "XXWKkMSGK7tCb7jCSVZNmJzWneNDb2funq6kSLUPDVCgL8gAMPBfUWLyKtQdLpXX"}`)
request := httptest.NewRequest("POST", "/", bytes.NewBuffer(jsonStr))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
handler := http.HandlerFunc(handler)
handler.ServeHTTP(response, request)
assert.Equal(t, int(401), response.Code, "Should be failed")
}
func TestTriggerWithLongCommand(t *testing.T) {
correctTokenForTest = "ZghBUuaq82wIgClFeoqHty2OkZOFDjfmV9DOMIlC4VCHyP3gzc3SkT83f1eTisgo"
jsonStr := []byte(`{"name":"pqr","value":"123", "token": "` + correctTokenForTest + `"}`)
request := httptest.NewRequest("POST", "/", bytes.NewBuffer(jsonStr))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
handler := http.HandlerFunc(handler)
handler.ServeHTTP(response, request)
assert.Equal(t, int(200), response.Code, "Should be succeed")
bodyBytes, _ := ioutil.ReadAll(response.Body)
bodyString := string(bodyBytes)
assert.Equal(t, true, strings.Contains(string(bodyString), "OK"))
result, _ := ioutil.ReadFile("./result")
assert.Equal(t, true, strings.Contains(string(result), "pqr"))
assert.Equal(t, true, strings.Contains(string(result), "ok"))
}