@@ -2,11 +2,14 @@ package embeddedpostgres
22
33import (
44 "archive/zip"
5+ "crypto/sha256"
6+ "encoding/hex"
57 "io/ioutil"
68 "net/http"
79 "net/http/httptest"
810 "os"
911 "path/filepath"
12+ "strings"
1013 "testing"
1114
1215 "github.com/stretchr/testify/assert"
@@ -54,7 +57,10 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenBodyReadIssue(t *testing.T) {
5457
5558func Test_defaultRemoteFetchStrategy_ErrorWhenCannotUnzipSubFile (t * testing.T ) {
5659 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+ }
5864 }))
5965 defer server .Close ()
6066
@@ -69,6 +75,11 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenCannotUnzipSubFile(t *testing.T) {
6975
7076func Test_defaultRemoteFetchStrategy_ErrorWhenCannotUnzip (t * testing.T ) {
7177 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+
7283 if _ , err := w .Write ([]byte ("lolz" )); err != nil {
7384 panic (err )
7485 }
@@ -86,6 +97,11 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenCannotUnzip(t *testing.T) {
8697
8798func Test_defaultRemoteFetchStrategy_ErrorWhenNoSubTarArchive (t * testing.T ) {
8899 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+
89105 MyZipWriter := zip .NewWriter (w )
90106
91107 if err := MyZipWriter .Close (); err != nil {
@@ -114,6 +130,11 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenCannotExtractSubArchive(t *testing
114130 }
115131
116132 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+
117138 bytes , err := ioutil .ReadFile (jarFile )
118139 if err != nil {
119140 panic (err )
@@ -148,6 +169,11 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenCannotCreateCacheDirectory(t *test
148169 cacheLocation := filepath .Join (fileBlockingExtractDirectory , "cache_file.jar" )
149170
150171 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+
151177 bytes , err := ioutil .ReadFile (jarFile )
152178 if err != nil {
153179 panic (err )
@@ -181,6 +207,11 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenCannotCreateSubArchiveFile(t *test
181207 }
182208
183209 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+
184215 bytes , err := ioutil .ReadFile (jarFile )
185216 if err != nil {
186217 panic (err )
@@ -202,6 +233,44 @@ func Test_defaultRemoteFetchStrategy_ErrorWhenCannotCreateSubArchiveFile(t *test
202233 assert .Regexp (t , "^unable to extract postgres archive:.+$" , err )
203234}
204235
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+
205274func Test_defaultRemoteFetchStrategy (t * testing.T ) {
206275 jarFile , cleanUp := createTempZipArchive ()
207276 defer cleanUp ()
@@ -213,6 +282,17 @@ func Test_defaultRemoteFetchStrategy(t *testing.T) {
213282 if err != nil {
214283 panic (err )
215284 }
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+
216296 if _ , err := w .Write (bytes ); err != nil {
217297 panic (err )
218298 }
0 commit comments