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

switch references #1721

Merged
merged 11 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net"
"net/http"
"os/user"
"path"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -196,3 +197,46 @@ func UnmarshalJSONToProtoV1(b []byte, m proto.Message) error {
}
return nil
}

// IsRelativeReference returns true if the given reference qualifies as relative
// when the resource id is set and the path starts with a .
//
// TODO(corby): Currently if the path begins with a dot, the ResourceId is set but has empty storageId and OpaqueId
// then the reference is still being viewed as relative. We need to check if we want that because in some
// places we might not want to set both StorageId and OpaqueId so we can't do a hard check if they are set.
func IsRelativeReference(ref *provider.Reference) bool {
return ref.ResourceId != nil && strings.HasPrefix(ref.Path, ".")
}

// IsAbsoluteReference returns true if the given reference qualifies as absolute
// when either only the resource id is set or only the path is set and starts with /
//
// TODO(corby): Currently if the path is empty, the ResourceId is set but has empty storageId and OpaqueId
// then the reference is still being viewed as absolute. We need to check if we want that because in some
// places we might not want to set both StorageId and OpaqueId so we can't do a hard check if they are set.
func IsAbsoluteReference(ref *provider.Reference) bool {
return (ref.ResourceId != nil && ref.Path == "") || (ref.ResourceId == nil) && strings.HasPrefix(ref.Path, "/")
}

// MakeRelativePath prefixes the path with a . to use it in a relative reference
func MakeRelativePath(p string) string {
p = path.Join("/", p)

if p == "/" {
return "."
}
return "." + p
}

// IsSameUserID compares to UserIds for equallity.
// TODO move to a comparison package
func IsSameUserID(i *userpb.UserId, j *userpb.UserId) bool {
ishank011 marked this conversation as resolved.
Show resolved Hide resolved
switch {
case i == nil, j == nil:
return false
case i.OpaqueId == j.OpaqueId && i.Idp == j.Idp:
return true
default:
return false
}
}
132 changes: 131 additions & 1 deletion pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

package utils

import "testing"
import (
"testing"

provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
)

var skipTests = []struct {
name string
Expand All @@ -42,3 +46,129 @@ func TestSkip(t *testing.T) {
})
}
}
func TestIsRelativeReference(t *testing.T) {
tests := []struct {
ref *provider.Reference
expected bool
}{
{
&provider.Reference{},
false,
},
{
&provider.Reference{
Path: ".",
},
false,
},
{
&provider.Reference{
ResourceId: &provider.ResourceId{
StorageId: "storageId",
OpaqueId: "opaqueId",
},
Path: "/folder",
},
false,
},
{
&provider.Reference{
ResourceId: &provider.ResourceId{
StorageId: "storageId",
OpaqueId: "opaqueId",
},
Path: "./folder",
},
true,
},
{
&provider.Reference{
ResourceId: &provider.ResourceId{},
Path: "./folder",
},
true,
},
}

for _, tt := range tests {
result := IsRelativeReference(tt.ref)
if result != tt.expected {
t.Errorf("IsRelativeReference: ref %v expected %t got %t", tt.ref, tt.expected, result)
}
}
}
func TestIsAbsolutReference(t *testing.T) {
tests := []struct {
ref *provider.Reference
expected bool
}{
{
&provider.Reference{},
false,
},
{
&provider.Reference{
Path: ".",
},
false,
},
{
&provider.Reference{
ResourceId: &provider.ResourceId{
StorageId: "storageId",
OpaqueId: "opaqueId",
},
Path: "/folder",
},
false,
},
{
&provider.Reference{
Path: "/folder",
},
true,
},
{
&provider.Reference{
ResourceId: &provider.ResourceId{},
},
true,
},
{
&provider.Reference{
ResourceId: &provider.ResourceId{
StorageId: "storageId",
OpaqueId: "opaqueId",
},
},
true,
},
}

for _, tt := range tests {
result := IsAbsoluteReference(tt.ref)
if result != tt.expected {
t.Errorf("IsAbsolutReference: ref %v expected %t got %t", tt.ref, tt.expected, result)
}
}
}

func TestMakeRelativePath(t *testing.T) {
tests := []struct {
path string
relPath string
}{
{"", "."},
{"/", "."},
{"..", "."},
{"/folder", "./folder"},
{"/folder/../folder2", "./folder2"},
{"folder", "./folder"},
}
for _, tt := range tests {
rel := MakeRelativePath(tt.path)
if rel != tt.relPath {
t.Errorf("expected %s, got %s", tt.relPath, rel)
}
}
}