Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(storage): add object validation case and test #9521

Merged
merged 7 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions storage/storage.go
Expand Up @@ -1096,6 +1096,10 @@ func (o *ObjectHandle) validate() error {
if !utf8.ValidString(o.object) {
return fmt.Errorf("storage: object name %q is not valid UTF-8", o.object)
}
// Names . and .. are not valid; see https://cloud.google.com/storage/docs/objects#naming
if o.object == "." || o.object == ".." {
return fmt.Errorf("Object name %v is not valid", o.object)
tritone marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}

Expand Down
51 changes: 51 additions & 0 deletions storage/storage_test.go
Expand Up @@ -2424,6 +2424,57 @@ func TestParseProjectNumber(t *testing.T) {
}
}

func TestObjectValidate(t *testing.T) {
for _, c := range []struct {
name string
bucket string
object string
wantSuccess bool
}{
{
name: "valid object",
bucket: "my-bucket",
object: "my-object",
wantSuccess: true,
},
{
name: "empty bucket name",
bucket: "",
object: "my-object",
wantSuccess: false,
},
{
name: "empty object name",
bucket: "my-bucket",
object: "",
wantSuccess: false,
},
{
name: "invalid utf-8",
bucket: "my-bucket",
object: "\xc3\x28",
wantSuccess: false,
},
{
name: "object name .",
bucket: "my-bucket",
object: ".",
wantSuccess: false,
},
} {
t.Run(c.name, func(r *testing.T) {
b := &BucketHandle{name: c.bucket}
err := b.Object(c.object).validate()
if c.wantSuccess && err != nil {
r.Errorf("want success, got error %v", err)
}
if !c.wantSuccess && err == nil {
r.Errorf("want error, got nil")
}
})
}
}

// isZeroValue reports whether v is the zero value for its type
// It errors if the argument is unknown
func isZeroValue(v reflect.Value) (bool, error) {
Expand Down