This repository was archived by the owner on Dec 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathfetch_test.go
More file actions
146 lines (129 loc) · 3.99 KB
/
fetch_test.go
File metadata and controls
146 lines (129 loc) · 3.99 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
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2016-2019 Google LLC. 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 fetch
import (
"fmt"
"net/http"
"path/filepath"
"strings"
"testing"
"testing/quick"
_ "github.com/googlecodelabs/tools/claat/parser/gdoc" // Explicitly register gdoc parser
)
type testTransport struct {
roundTripper func(*http.Request) (*http.Response, error)
}
func (tt *testTransport) RoundTrip(r *http.Request) (*http.Response, error) {
return tt.roundTripper(r)
}
// TODO: add tests of core functionality.
// Must be able to fake out disk access, which probably involves a refactor focused on dependency injection?
func TestGdocID(t *testing.T) {
tests := []struct{ in, out string }{
{"https://docs.google.com/document/d/foo", "foo"},
{"https://docs.google.com/document/d/foo/edit", "foo"},
{"https://docs.google.com/document/d/foo/edit#abc", "foo"},
{"https://docs.google.com/document/d/foo/edit?bar=baz#abc", "foo"},
{"foo", "foo"},
}
for i, test := range tests {
out := gdocID(test.in)
if out != test.out {
t.Errorf("%d: gdocID(%q) = %q; want %q", i, test.in, out, test.out)
}
}
}
func TestRestrictPathToParent(t *testing.T) {
tests := []struct {
asset string
parent string
wantPath string
wantErr bool
}{
{"imgroot.png", ".", "imgroot.png", false},
{"imgroot.png", "foo/", "foo/imgroot.png", false},
{"img/sub.png", "foo/", "foo/img/sub.png", false},
{"imgroot.png", "/tmp/foo", "/tmp/foo/imgroot.png", false},
{"/tmp/imgabs.png", "foo/", "", true},
{"../imgup.png", "foo/", "", true},
{"../imgup.png", "..", "", true},
{"imgroot.png", "", "imgroot.png", false},
{"", ".", ".", false},
{"", "", ".", false},
}
for _, tc := range tests {
t.Run(fmt.Sprintf("asset: %s, parent: %s", tc.asset, tc.parent), func(t *testing.T) {
tc.wantPath = safeAbs(t, tc.wantPath)
p, err := restrictPathToParent(tc.asset, tc.parent)
if err != nil != tc.wantErr {
t.Errorf("restrictPathToParent() error = %v, wantErr %v", err, tc.wantErr)
return
}
if p != tc.wantPath {
t.Errorf("restrictPathToParent() return: got %s, wanted %s", p, tc.wantPath)
}
})
}
}
func TestFuzzRestrictPathToParent(t *testing.T) {
checkInParent := func(elem, parent string) bool {
_, err := restrictPathToParent(elem, parent)
parent = safeAbs(t, parent)
if !strings.HasPrefix(elem, "/") {
elem = filepath.Join(parent, elem)
}
shouldOk := strings.HasPrefix(elem, parent)
return shouldOk == (err == nil)
}
if err := quick.Check(checkInParent, nil); err != nil {
t.Error(err)
}
}
func TestImgExtFromBytes(t *testing.T) {
tests := []struct {
bytes []byte
wantExt string
wantErr bool
}{
{[]byte("012345JFIF0"), ".jpeg", false},
{[]byte("GIF34567890"), ".gif", false},
{[]byte("SOMETHINGELSE"), ".png", false},
{[]byte("GIF345JFIF0"), ".jpeg", false},
{[]byte("toosmall"), "", true},
}
for _, tc := range tests {
t.Run(fmt.Sprintf("bytes: %s", tc.bytes), func(t *testing.T) {
ext, err := imgExtFromBytes(tc.bytes)
if err != nil != tc.wantErr {
t.Errorf("imgExtFromBytes() error = %v, wantErr %v", err, tc.wantErr)
return
}
if ext != tc.wantExt {
t.Errorf("imgExtFromBytes() return: got %s, wanted %s", ext, tc.wantExt)
}
})
}
}
// safeAbs compute Abs of p and fail the test if not valid.
// Empty string return empty path.
func safeAbs(t *testing.T, p string) string {
if p == "" {
return p
}
p, err := filepath.Abs(p)
if err != nil {
t.Fatalf("Error in converting %s to abs path: %v", p, err)
}
return p
}