From 15ae4dfa4c780c3114e9e65052d573892cf8867a Mon Sep 17 00:00:00 2001 From: Scott Suarez Date: Thu, 25 Feb 2021 21:46:46 -0800 Subject: [PATCH] add testcase and make generation check fail if invalid generation is provided --- get_gcs.go | 4 +--- get_gcs_test.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/get_gcs.go b/get_gcs.go index e2bd95ac9..4f2172b24 100644 --- a/get_gcs.go +++ b/get_gcs.go @@ -6,7 +6,6 @@ import ( "net/url" "os" "path/filepath" - "regexp" "strconv" "strings" @@ -135,8 +134,7 @@ func (g *GCSGetter) GetFile(dst string, u *url.URL) error { func (g *GCSGetter) getObject(ctx context.Context, client *storage.Client, dst, bucket, object, fragment string) error { var rc *storage.Reader var err error - fragmentHasGeneration := regexp.MustCompile("^\\d+$").MatchString(fragment) - if fragmentHasGeneration { + if fragment != "" { generation, err := strconv.ParseInt(fragment, 10, 64) if err != nil { return err diff --git a/get_gcs_test.go b/get_gcs_test.go index b9d972a66..233e0ba68 100644 --- a/get_gcs_test.go +++ b/get_gcs_test.go @@ -1,6 +1,7 @@ package getter import ( + "io/ioutil" "net/url" "os" "path/filepath" @@ -88,6 +89,47 @@ func TestGCSGetter_GetFile(t *testing.T) { assertContents(t, dst, "# Main\n") } +func TestGCSGetter_GetGenerationFile(t *testing.T) { + defer initGCPCredentials(t)() + + g := new(GCSGetter) + dst := tempTestFile(t) + defer os.RemoveAll(filepath.Dir(dst)) + + // Download + err := g.GetFile( + dst, testURL("https://www.googleapis.com/storage/v1/go-getter-testcase-data/DO_NOT_DELETE/generation_test.txt#1614317688843055")) + if err != nil { + t.Fatalf("err: %s", err) + } + + // Verify the main file exists + content, err := ioutil.ReadFile(dst) + if err != nil { + t.Fatalf("err: %s", err) + } + if string(content) != "a" { + t.Fatalf("expected file contents from generation to be `a` but got `%s`", content) + } + + // Download + err = g.GetFile( + dst, testURL("https://www.googleapis.com/storage/v1/go-getter-testcase-data/DO_NOT_DELETE/generation_test.txt#1614317705239142")) + if err != nil { + t.Fatalf("err: %s", err) + } + + // Verify the main file exists + content, err = ioutil.ReadFile(dst) + if err != nil { + t.Fatalf("err: %s", err) + } + if string(content) != "b" { + t.Fatalf("expected file contents from generation to be `a` but got `%s`", content) + } + +} + func TestGCSGetter_GetFile_notfound(t *testing.T) { g := new(GCSGetter) dst := tempTestFile(t) @@ -180,7 +222,7 @@ func TestGCSGetter_Url(t *testing.T) { t.Fatalf("expected forced protocol to be gcs") } - bucket, path, err := g.parseURL(u) + bucket, path, _, err := g.parseURL(u) if err != nil { t.Fatalf("err: %s", err)