-
Notifications
You must be signed in to change notification settings - Fork 6
/
cen_test.go
165 lines (138 loc) · 4.51 KB
/
cen_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"net"
"net/http"
"testing"
"time"
"github.com/Co-Epi/cen-server/backend"
"github.com/Co-Epi/cen-server/server"
)
// DefaultTransport contains all HTTP client operation parameters
var DefaultTransport http.RoundTripper = &http.Transport{
Dial: (&net.Dialer{
// limits the time spent establishing a TCP connection (if a new one is needed)
Timeout: 120 * time.Second,
KeepAlive: 120 * time.Second, // 60 * time.Second,
}).Dial,
//MaxIdleConns: 5,
MaxIdleConnsPerHost: 25, // changed from 100 -> 25
// limits the time spent reading the headers of the response.
ResponseHeaderTimeout: 120 * time.Second,
IdleConnTimeout: 120 * time.Second, // 90 * time.Second,
// limits the time the client will wait between sending the request headers when including an Expect: 100-continue and receiving the go-ahead to send the body.
ExpectContinueTimeout: 1 * time.Second,
// limits the time spent performing the TLS handshake.
TLSHandshakeTimeout: 5 * time.Second,
}
func skipCI(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("Skipping testing in CI environment")
}
}
func httppost(url string, body []byte) (result []byte, err error) {
httpclient := &http.Client{Timeout: time.Second * 120, Transport: DefaultTransport}
bodyReader := bytes.NewReader(body)
req, err := http.NewRequest(http.MethodPost, url, bodyReader)
if err != nil {
return result, fmt.Errorf("[cen_test:httppost] %s", err)
}
resp, err := httpclient.Do(req)
if err != nil {
return result, fmt.Errorf("[cen_test:httppost] %s", err)
}
result, err = ioutil.ReadAll(resp.Body)
if err != nil {
return result, fmt.Errorf("[cen_test:httppost] %s", err)
}
resp.Body.Close()
return result, nil
}
func httpget(url string) (result []byte, err error) {
httpclient := &http.Client{Timeout: time.Second * 120, Transport: DefaultTransport}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return result, fmt.Errorf("[cen_test:httpget] %s", err)
}
resp, err := httpclient.Do(req)
if err != nil {
return result, fmt.Errorf("[cen_test:httpget] %s", err)
}
result, err = ioutil.ReadAll(resp.Body)
if err != nil {
return result, fmt.Errorf("[cen_test:httpget] %s", err)
}
resp.Body.Close()
return result, nil
}
//TODO This isn't a unit test, it's more of a functional/integration test
//It needs to be broken up, and it currently fails
func TestCENSimple(t *testing.T) {
//Have to skip this test since it's not CI-or-automation-friendly
skipCI(t)
endpoint := fmt.Sprintf("coepi.wolk.com:%d", server.DefaultPort)
// Post CENReport to /cenreport, along with CENKeys
cenReport, cenReportKeys := backend.GetSampleCENReportAndCENKeys(2)
cenReportJSON, err := json.Marshal(cenReport)
if err != nil {
t.Fatalf("err: %s", err)
}
// POST CENReport
result, err := httppost(fmt.Sprintf("https://%s/%s", endpoint, server.EndpointCENReport), cenReportJSON)
if err != nil {
t.Fatalf("EndpointCENReport: %s", err)
}
fmt.Printf("EndpointCENReport[%s]\n", string(result))
// GET CENKeys
curTS := uint64(time.Now().Unix())
resp, err := httpget(fmt.Sprintf("https://%s/%s/%d", endpoint, server.EndpointCENKeys, curTS-10))
if err != nil {
t.Fatalf("EndpointCENKeys: %s", err)
}
fmt.Printf("EndpointCENKeys: %s\n", string(resp))
var cenKeys []string
err = json.Unmarshal(resp, &cenKeys)
if err != nil {
t.Fatalf("EndpointCENKeys(check1): [%s] [%s]", resp, err)
}
if len(cenKeys) < 2 {
t.Fatalf("Incorrect response length [%d] -- should be at least 2", len(cenKeys))
}
found := make([]bool, len(cenKeys))
for _, cenKey := range cenKeys {
for j, reportKey := range cenReportKeys {
if cenKey == reportKey {
found[j] = true
}
}
}
// GET CENREport
for i := 0; i < 2; i++ {
if !found[i] {
t.Fatalf("EndpointCENKey key %d in report [%s] not found", i, cenReportKeys[i])
}
cenKey := cenReportKeys[i]
reportsRaw, err := httpget(fmt.Sprintf("https://%s/%s/%s", endpoint, server.EndpointCENReport, cenKey))
if err != nil {
t.Fatalf("EndpointCENReport: %s", err)
}
var reports []*backend.CENReport
err = json.Unmarshal(reportsRaw, &reports)
if err != nil {
t.Fatalf("EndpointCENReport(check1): %s", err)
}
if len(reports) > 0 {
report := reports[0]
fmt.Printf("EndpointCENKeys SUCCESS: [%s]\n", report.Report)
if !bytes.Equal(report.Report, cenReport.Report) {
t.Fatalf("EndpointCENKeys(check1) Expected %s, got %s", cenReport.Report, report.Report)
}
} else {
t.Fatalf("hmm, no report (%s)", cenKey)
}
}
}