forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
324 lines (293 loc) · 6.63 KB
/
util_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package tests
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"runtime"
"sort"
"strconv"
"strings"
"time"
)
const (
unlikelySep = "\x02\x01\x03"
)
func stringArrayCopy(a []string) []string {
if len(a) == 0 {
return nil
}
return append([]string{}, a...)
}
func stringArrayContains(a []string, s string) bool {
for _, el := range a {
if el == s {
return true
}
}
return false
}
// equivalent of Java's containsSequence http://joel-costigliola.github.io/assertj/core/api/org/assertj/core/api/ListAssert.html#containsSequence(ELEMENT...)
// checks if a1 contains sub-sequence a2
func stringArrayContainsSequence(a1, a2 []string) bool {
// TODO: technically it's possible for this to have false positive
// but it's very unlikely
s1 := strings.Join(a1, unlikelySep)
s2 := strings.Join(a2, unlikelySep)
return strings.Contains(s1, s2)
}
func stringArrayContainsExactly(a1, a2 []string) bool {
if len(a1) != len(a2) {
return false
}
for i, s := range a1 {
if s != a2[i] {
return false
}
}
return true
}
// stringArrayEq returns true if arrays have the same content, ignoring order
func stringArrayEq(a1, a2 []string) bool {
if len(a1) != len(a2) {
return false
}
if len(a1) == 0 {
return true
}
a1c := stringArrayCopy(a1)
a2c := stringArrayCopy(a2)
sort.Strings(a1c)
sort.Strings(a2c)
for i, s := range a1c {
if s != a2c[i] {
return false
}
}
return true
}
func stringArrayReverse(a []string) {
n := len(a)
for i := 0; i < n/2; i++ {
a[i], a[n-1-i] = a[n-1-i], a[i]
}
}
func int64ArrayHasDuplicates(a []int64) bool {
if len(a) == 0 {
return false
}
m := map[int64]int{}
for _, i := range a {
m[i]++
if m[i] > 1 {
return true
}
}
return false
}
func jsonGetAsText(doc map[string]interface{}, key string) (string, bool) {
v, ok := doc[key]
if !ok {
return "", false
}
s, ok := v.(string)
if !ok {
return "", false
}
return s, true
}
func objectNodeFieldNames(js map[string]interface{}) []string {
var res []string
for k := range js {
res = append(res, k)
}
return res
}
func isUnprintable(c byte) bool {
if c < 32 {
// 9 - tab, 10 - LF, 13 - CR
if c == 9 || c == 10 || c == 13 {
return false
}
return true
}
return c >= 127
}
func isBinaryData(d []byte) bool {
for _, b := range d {
if isUnprintable(b) {
return true
}
}
return false
}
func asHex(d []byte) ([]byte, bool) {
if !isBinaryData(d) {
return d, false
}
// convert unprintable characters to hex
var res []byte
for i, c := range d {
if i > 2048 {
break
}
if isUnprintable(c) {
s := fmt.Sprintf("x%02x ", c)
res = append(res, s...)
} else {
res = append(res, c)
}
}
return res, true
}
func isEnvVarTrue(name string) bool {
v := strings.TrimSpace(strings.ToLower(os.Getenv(name)))
switch v {
case "yes", "true":
return true
}
return false
}
// if d is a valid json, pretty-print it
// only used for debugging
func maybePrettyPrintJSON(d []byte) []byte {
if d2, ok := asHex(d); ok {
return d2
}
var m map[string]interface{}
err := json.Unmarshal(d, &m)
if err != nil {
return d
}
d2, err := json.MarshalIndent(m, "", " ")
if err != nil {
return d
}
return d2
}
func fileExists(path string) bool {
st, err := os.Lstat(path)
return err == nil && !st.IsDir()
}
func isWindows() bool {
return runtime.GOOS == "windows"
}
func timeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
return func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, cTimeout)
if err != nil {
return nil, err
}
conn.SetDeadline(time.Now().Add(rwTimeout))
return conn, nil
}
}
// can be used for http.Get() requests with better timeouts. New one must be created
// for each Get() request
func newTimeoutClient(connectTimeout time.Duration, readWriteTimeout time.Duration) *http.Client {
return &http.Client{
Transport: &http.Transport{
Dial: timeoutDialer(connectTimeout, readWriteTimeout),
Proxy: http.ProxyFromEnvironment,
},
}
}
var (
defaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
)
func downloadURL(url string) ([]byte, error) {
// default timeout for http.Get() is really long, so dial it down
// for both connection and read/write timeouts
timeoutClient := newTimeoutClient(time.Second*120, time.Second*120)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", defaultUserAgent)
resp, err := timeoutClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("'%s': status code not 200 (%d)", url, resp.StatusCode)
}
return ioutil.ReadAll(resp.Body)
}
func httpDl(url string, destPath string) error {
d, err := downloadURL(url)
if err != nil {
return err
}
return ioutil.WriteFile(destPath, d, 0755)
}
func getProcessId() string {
pid := os.Getpid()
return strconv.Itoa(pid)
}
func parsePrivateKey(der []byte) (crypto.PrivateKey, error) {
if key, err := x509.ParsePKCS1PrivateKey(der); err == nil {
return key, nil
}
if key, err := x509.ParsePKCS8PrivateKey(der); err == nil {
switch key := key.(type) {
case *rsa.PrivateKey, *ecdsa.PrivateKey:
return key, nil
default:
return nil, fmt.Errorf("Found unknown private key type in PKCS#8 wrapping")
}
}
if key, err := x509.ParseECPrivateKey(der); err == nil {
return key, nil
}
return nil, fmt.Errorf("Failed to parse private key")
}
// TODO: expose this in the library as a helper function
func loadCertficateAndKeyFromFile(path string) (*tls.Certificate, error) {
raw, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var cert tls.Certificate
for {
block, rest := pem.Decode(raw)
if block == nil {
break
}
if block.Type == "CERTIFICATE" {
cert.Certificate = append(cert.Certificate, block.Bytes)
} else {
cert.PrivateKey, err = parsePrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("Failure reading private key from \"%s\": %s", path, err)
}
}
raw = rest
}
if len(cert.Certificate) == 0 {
return nil, fmt.Errorf("No certificate found in \"%s\"", path)
} else if cert.PrivateKey == nil {
return nil, fmt.Errorf("No private key found in \"%s\"", path)
}
return &cert, nil
}
func entityToDocument(e interface{}) (map[string]interface{}, error) {
js, err := json.Marshal(e)
if err != nil {
return nil, err
}
var doc map[string]interface{}
err = json.Unmarshal(js, &doc)
if err != nil {
return nil, err
}
return doc, nil
}