@@ -2,11 +2,14 @@ package embeddedpostgres
2
2
3
3
import (
4
4
"archive/zip"
5
+ "crypto/sha256"
6
+ "encoding/hex"
5
7
"io/ioutil"
6
8
"net/http"
7
9
"net/http/httptest"
8
10
"os"
9
11
"path/filepath"
12
+ "strings"
10
13
"testing"
11
14
12
15
"github.com/stretchr/testify/assert"
@@ -54,7 +57,10 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenBodyReadIssue(t *testing.T) {
54
57
55
58
func Test_defaultRemoteFetchStrategy_ErrorWhenCannotUnzipSubFile (t * testing.T ) {
56
59
server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
57
-
60
+ if strings .HasSuffix (r .RequestURI , ".sha256" ) {
61
+ w .WriteHeader (http .StatusNotFound )
62
+ return
63
+ }
58
64
}))
59
65
defer server .Close ()
60
66
@@ -69,6 +75,11 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenCannotUnzipSubFile(t *testing.T) {
69
75
70
76
func Test_defaultRemoteFetchStrategy_ErrorWhenCannotUnzip (t * testing.T ) {
71
77
server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
78
+ if strings .HasSuffix (r .RequestURI , ".sha256" ) {
79
+ w .WriteHeader (404 )
80
+ return
81
+ }
82
+
72
83
if _ , err := w .Write ([]byte ("lolz" )); err != nil {
73
84
panic (err )
74
85
}
@@ -86,6 +97,11 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenCannotUnzip(t *testing.T) {
86
97
87
98
func Test_defaultRemoteFetchStrategy_ErrorWhenNoSubTarArchive (t * testing.T ) {
88
99
server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
100
+ if strings .HasSuffix (r .RequestURI , ".sha256" ) {
101
+ w .WriteHeader (http .StatusNotFound )
102
+ return
103
+ }
104
+
89
105
MyZipWriter := zip .NewWriter (w )
90
106
91
107
if err := MyZipWriter .Close (); err != nil {
@@ -114,6 +130,11 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenCannotExtractSubArchive(t *testing
114
130
}
115
131
116
132
server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
133
+ if strings .HasSuffix (r .RequestURI , ".sha256" ) {
134
+ w .WriteHeader (http .StatusNotFound )
135
+ return
136
+ }
137
+
117
138
bytes , err := ioutil .ReadFile (jarFile )
118
139
if err != nil {
119
140
panic (err )
@@ -148,6 +169,11 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenCannotCreateCacheDirectory(t *test
148
169
cacheLocation := filepath .Join (fileBlockingExtractDirectory , "cache_file.jar" )
149
170
150
171
server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
172
+ if strings .HasSuffix (r .RequestURI , ".sha256" ) {
173
+ w .WriteHeader (http .StatusNotFound )
174
+ return
175
+ }
176
+
151
177
bytes , err := ioutil .ReadFile (jarFile )
152
178
if err != nil {
153
179
panic (err )
@@ -181,6 +207,11 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenCannotCreateSubArchiveFile(t *test
181
207
}
182
208
183
209
server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
210
+ if strings .HasSuffix (r .RequestURI , ".sha256" ) {
211
+ w .WriteHeader (http .StatusNotFound )
212
+ return
213
+ }
214
+
184
215
bytes , err := ioutil .ReadFile (jarFile )
185
216
if err != nil {
186
217
panic (err )
@@ -202,6 +233,44 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenCannotCreateSubArchiveFile(t *test
202
233
assert .Regexp (t , "^unable to extract postgres archive:.+$" , err )
203
234
}
204
235
236
+ func Test_defaultRemoteFetchStrategy_ErrorWhenSHA256NotMatch (t * testing.T ) {
237
+ jarFile , cleanUp := createTempZipArchive ()
238
+ defer cleanUp ()
239
+
240
+ cacheLocation := filepath .Join (filepath .Dir (jarFile ), "extract_location" , "cache.jar" )
241
+
242
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
243
+ bytes , err := ioutil .ReadFile (jarFile )
244
+ if err != nil {
245
+ panic (err )
246
+ }
247
+
248
+ if strings .HasSuffix (r .RequestURI , ".sha256" ) {
249
+ w .WriteHeader (200 )
250
+ if _ , err := w .Write ([]byte ("literallyN3verGonnaWork" )); err != nil {
251
+ panic (err )
252
+ }
253
+
254
+ return
255
+ }
256
+
257
+ if _ , err := w .Write (bytes ); err != nil {
258
+ panic (err )
259
+ }
260
+ }))
261
+ defer server .Close ()
262
+
263
+ remoteFetchStrategy := defaultRemoteFetchStrategy (server .URL + "/maven2" ,
264
+ testVersionStrategy (),
265
+ func () (s string , b bool ) {
266
+ return cacheLocation , false
267
+ })
268
+
269
+ err := remoteFetchStrategy ()
270
+
271
+ assert .EqualError (t , err , "downloaded checksums do not match" )
272
+ }
273
+
205
274
func Test_defaultRemoteFetchStrategy (t * testing.T ) {
206
275
jarFile , cleanUp := createTempZipArchive ()
207
276
defer cleanUp ()
@@ -213,6 +282,17 @@ func Test_defaultRemoteFetchStrategy(t *testing.T) {
213
282
if err != nil {
214
283
panic (err )
215
284
}
285
+
286
+ if strings .HasSuffix (r .RequestURI , ".sha256" ) {
287
+ w .WriteHeader (200 )
288
+ contentHash := sha256 .Sum256 (bytes )
289
+ if _ , err := w .Write ([]byte (hex .EncodeToString (contentHash [:]))); err != nil {
290
+ panic (err )
291
+ }
292
+
293
+ return
294
+ }
295
+
216
296
if _ , err := w .Write (bytes ); err != nil {
217
297
panic (err )
218
298
}
0 commit comments