-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathgoolib_test.go
More file actions
133 lines (126 loc) · 4.32 KB
/
Copy pathgoolib_test.go
File metadata and controls
133 lines (126 loc) · 4.32 KB
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
/*
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package goolib
import (
"fmt"
"math/rand"
"strings"
"testing"
"time"
)
func TestScriptInterpreter(t *testing.T) {
table := []struct {
script string
eitp string
}{
{"/file/path/script.ps1", "powershell.exe"},
{"/file/path/script.cmd", "cmd"},
{"/file/path/script.bat", "cmd"},
}
for _, tt := range table {
itp, err := scriptInterpreter(tt.script)
if err != nil {
t.Errorf("error parsing interpreter: %v", err)
}
if itp != tt.eitp {
t.Errorf("did not get expected interpreter: got %v, want %v", itp, tt.eitp)
}
}
}
func TestBadScriptInterpreter(t *testing.T) {
if _, err := scriptInterpreter("/file/path/script.ext"); err == nil {
t.Errorf("got no error from scriptInterpreter when processing bad extension, want error")
}
if _, err := scriptInterpreter("/file/path/script"); err == nil {
t.Errorf("got no error from scriptInterpreter when processing no extension, want error")
}
}
func randString(runes []rune, min, max int) string {
s := make([]rune, rand.Intn(1+max-min)+min)
for i := range s {
s[i] = runes[rand.Intn(len(runes))]
}
return string(s)
}
func TestSplitGCSUrl(t *testing.T) {
rand.Seed(time.Now().UnixNano())
const alphanum = "abcdefghijklmnopqrstuvwxyz0123456789"
objChars := alphanum + "ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.~@%^=+"
bucket := randString([]rune(alphanum), 1, 1) + randString([]rune(alphanum+"-_."), 0, 61) + randString([]rune(alphanum), 1, 1)
object := randString([]rune(objChars+"/"), 0, 49) + randString([]rune(objChars), 1, 1)
var domains = []string{
`storage.cloud.google.com`,
`storage.googleapis.com`,
`commondatastorage.googleapis.com`,
}
var urls, urlsNoObjs []string
for i := range domains {
for _, s := range []string{"", "s"} {
// Without Objects
urlsNoObjs = append(urlsNoObjs, fmt.Sprintf("http%s://%s/%s", s, domains[i], bucket))
urlsNoObjs = append(urlsNoObjs, fmt.Sprintf("http%s://%s/%s", s, strings.ToUpper(domains[i]), bucket))
// With objects
urls = append(urls, fmt.Sprintf("http%s://%s/%s/%s", s, domains[i], bucket, object))
urls = append(urls, fmt.Sprintf("http%s://%s/%s/%s", s, strings.ToUpper(domains[i]), bucket, object))
}
}
urls = append(urls, fmt.Sprintf(`http://%s.storage.googleapis.com/%s`, bucket, object))
urls = append(urls, fmt.Sprintf(`http://%s.Storage.googleapis.COM/%s`, bucket, object))
urls = append(urls, fmt.Sprintf(`https://%s.storage.googleapis.com/%s`, bucket, object))
urls = append(urls, fmt.Sprintf(`https://%s.Storage.googleapis.COM/%s`, bucket, object))
urlsNoObjs = append(urlsNoObjs, fmt.Sprintf(`gs://%s`, bucket))
urls = append(urls, fmt.Sprintf(`gs://%s/%s`, bucket, object))
for i := range urls {
urls = append(urls, urls[i]+"/")
}
for _, url := range urls {
ok := true
isGCSUrl, bkt, obj := SplitGCSUrl(url)
if !isGCSUrl {
t.Errorf("Failed to parse '%s', expecting bucket='%s', object='%s'", url, bucket, object)
ok = false
} else {
if bkt != bucket {
t.Errorf("Parsed bucket '%s' from '%s', expecting '%s'", bkt, url, bucket)
ok = false
}
if obj != object {
t.Errorf("Parsed object '%s' from '%s', expecting '%s'", obj, url, object)
ok = false
}
}
if ok {
t.Logf("Successfully parsed object='%s', bucket='%s' from '%s'", obj, bkt, url)
}
}
for _, url := range urlsNoObjs {
ok := true
isGCSUrl, bkt, obj := SplitGCSUrl(url)
if !isGCSUrl {
t.Errorf("Failed to parse '%s', expecting bucket='%s', no object", url, bucket)
ok = false
} else {
if bkt != bucket {
t.Errorf("Parsed bucket '%s' from '%s', expecting '%s'", bkt, url, bucket)
ok = false
}
if obj != "" {
t.Errorf("Parsed object '%s' from '%s', expecting an empty string", obj, url)
ok = false
}
}
if ok {
t.Logf("Successfully parsed object='%s', bucket='%s' from '%s'", obj, bkt, url)
}
}
}