-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathremote_test.go
More file actions
242 lines (199 loc) · 6.69 KB
/
remote_test.go
File metadata and controls
242 lines (199 loc) · 6.69 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
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
package main
import (
"archive/tar"
"bytes"
"context"
"encoding/json"
"math/rand/v2"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/creativeprojects/resticprofile/constants"
"github.com/creativeprojects/resticprofile/remote"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// buildTar creates a tar archive in memory from the given entries.
// Each entry is a map with keys "name" and "content".
func buildTar(t *testing.T, entries []struct{ name, content string }) []byte {
t.Helper()
buf := &bytes.Buffer{}
tw := tar.NewWriter(buf)
for _, e := range entries {
data := []byte(e.content)
hdr := &tar.Header{
Name: e.name,
Size: int64(len(data)),
Mode: 0644,
}
require.NoError(t, tw.WriteHeader(hdr))
_, err := tw.Write(data)
require.NoError(t, err)
}
require.NoError(t, tw.Close())
return buf.Bytes()
}
// newTarServer starts an httptest server that serves a tar archive with the given content-type.
func newTarServer(t *testing.T, body []byte) *httptest.Server {
t.Helper()
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/x-tar")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(body)
}))
}
func TestLoadRemoteFilesSuccess(t *testing.T) {
manifest := remote.Manifest{
Version: "1.0",
ConfigurationFile: "profiles.toml",
ProfileName: "default",
}
manifestJSON, err := json.Marshal(manifest)
require.NoError(t, err)
entries := []struct{ name, content string }{
{constants.ManifestFilename, string(manifestJSON)},
{"profiles.toml", "[profile]\n"},
}
tarBody := buildTar(t, entries)
srv := newTarServer(t, tarBody)
defer srv.Close()
files, params, err := loadRemoteFiles(context.Background(), srv.URL)
require.NoError(t, err)
// manifest should be returned
require.NotNil(t, params)
assert.Equal(t, manifest.Version, params.Version)
assert.Equal(t, manifest.ConfigurationFile, params.ConfigurationFile)
assert.Equal(t, manifest.ProfileName, params.ProfileName)
// one non-manifest file should be returned
require.Len(t, files, 1)
}
func TestLoadRemoteFilesErrorNotFound(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not found", http.StatusNotFound)
}))
defer srv.Close()
_, _, err := loadRemoteFiles(context.Background(), srv.URL)
require.Error(t, err)
assert.Contains(t, err.Error(), "http error 404")
}
func TestLoadRemoteFilesWrongContentType(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("{}"))
}))
defer srv.Close()
_, _, err := loadRemoteFiles(context.Background(), srv.URL)
require.Error(t, err)
assert.Contains(t, err.Error(), "unexpected content type")
}
func TestLoadRemoteFilesInvalidEndpoint(t *testing.T) {
// not a valid URL at all — NewRequestWithContext should fail
_, _, err := loadRemoteFiles(context.Background(), "://invalid-url")
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to create request")
}
func TestLoadRemoteFilesUnreachableServer(t *testing.T) {
_, _, err := loadRemoteFiles(context.Background(), "http://127.0.0.1:1") // nothing listening
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to send request")
}
func TestLoadRemoteFilesPathTraversalRejected(t *testing.T) {
entries := []struct{ name, content string }{
{"../evil.sh", "rm -rf /"},
}
tarBody := buildTar(t, entries)
srv := newTarServer(t, tarBody)
defer srv.Close()
_, _, err := loadRemoteFiles(context.Background(), srv.URL)
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid file name")
}
func TestLoadRemoteFilesCorruptTar(t *testing.T) {
srv := newTarServer(t, []byte("this is not a tar archive"))
defer srv.Close()
_, _, err := loadRemoteFiles(context.Background(), srv.URL)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to read tar header")
}
func TestLoadRemoteFilesInvalidManifestJSON(t *testing.T) {
entries := []struct{ name, content string }{
{constants.ManifestFilename, "not valid json {{{"},
}
tarBody := buildTar(t, entries)
srv := newTarServer(t, tarBody)
defer srv.Close()
_, _, err := loadRemoteFiles(context.Background(), srv.URL)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to read manifest")
}
func TestLoadRemoteFilesEmptyTar(t *testing.T) {
tarBody := buildTar(t, nil)
srv := newTarServer(t, tarBody)
defer srv.Close()
files, params, err := loadRemoteFiles(context.Background(), srv.URL)
require.NoError(t, err)
assert.Nil(t, params)
assert.Empty(t, files)
}
func TestLoadRemoteFilesCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // cancel immediately
srv := newTarServer(t, nil)
defer srv.Close()
_, _, err := loadRemoteFiles(ctx, srv.URL)
require.Error(t, err)
}
func TestLoadMultipleRemoteFiles(t *testing.T) {
manifest := remote.Manifest{ProfileName: "myprofile"}
manifestJSON, err := json.Marshal(manifest)
require.NoError(t, err)
entries := []struct{ name, content string }{
{constants.ManifestFilename, string(manifestJSON)},
{"profiles.toml", "[profile]\n"},
{"extra.conf", "key=value\n"},
}
tarBody := buildTar(t, entries)
srv := newTarServer(t, tarBody)
defer srv.Close()
files, params, err := loadRemoteFiles(context.Background(), srv.URL)
require.NoError(t, err)
require.NotNil(t, params)
assert.Equal(t, "myprofile", params.ProfileName)
assert.Len(t, files, 2)
}
func TestLoadRemoteFilesWithBigFile(t *testing.T) {
var size int64 = 10 * 1024 * 1024
buffer := make([]byte, size) // 10 MB
_, err := rand.NewChaCha8([32]byte{}).Read(buffer)
require.NoError(t, err)
entries := []struct{ name, content string }{
{"bigfile", string(buffer)},
}
tarBody := buildTar(t, entries)
srv := newTarServer(t, tarBody)
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
files, _, err := loadRemoteFiles(ctx, srv.URL)
require.NoError(t, err)
assert.Len(t, files, 1)
assert.Equal(t, "bigfile", files[0].Name())
assert.Equal(t, size, files[0].FileInfo().Size())
}
func TestLoadRemoteFilesWithEmptyFile(t *testing.T) {
entries := []struct{ name, content string }{
{"emptyfile", ""},
}
tarBody := buildTar(t, entries)
srv := newTarServer(t, tarBody)
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
files, _, err := loadRemoteFiles(ctx, srv.URL)
require.NoError(t, err)
assert.Len(t, files, 1)
assert.Equal(t, "emptyfile", files[0].Name())
assert.Equal(t, int64(0), files[0].FileInfo().Size())
}