github.com/cs3org/reva/v2@v2.27.7/pkg/utils/utils_test.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package utils
    20  
    21  import (
    22  	"testing"
    23  
    24  	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    25  )
    26  
    27  var skipTests = []struct {
    28  	name string
    29  	url  string
    30  	base []string
    31  	out  bool
    32  }{
    33  	{"valid subpath", "/a/b/c/d", []string{"/a/b/"}, true},
    34  	{"invalid subpath", "/a/b/c", []string{"/a/b/c/d"}, false},
    35  	{"equal values", "/a/b/c", []string{"/a/b/c"}, true},
    36  }
    37  
    38  func TestSkip(t *testing.T) {
    39  	for _, tt := range skipTests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			r := Skip(tt.url, tt.base)
    42  			if r != tt.out {
    43  				t.Errorf("expected %v, want %v", r, tt.out)
    44  			}
    45  		})
    46  	}
    47  }
    48  func TestIsRelativeReference(t *testing.T) {
    49  	tests := []struct {
    50  		ref      *provider.Reference
    51  		expected bool
    52  	}{
    53  		{
    54  			&provider.Reference{},
    55  			false,
    56  		},
    57  		{
    58  			&provider.Reference{
    59  				Path: ".",
    60  			},
    61  			false,
    62  		},
    63  		{
    64  			&provider.Reference{
    65  				ResourceId: &provider.ResourceId{
    66  					StorageId: "storageId",
    67  					OpaqueId:  "opaqueId",
    68  				},
    69  				Path: "/folder",
    70  			},
    71  			false,
    72  		},
    73  		{
    74  			&provider.Reference{
    75  				ResourceId: &provider.ResourceId{
    76  					StorageId: "storageId",
    77  					OpaqueId:  "opaqueId",
    78  				},
    79  				Path: "./folder",
    80  			},
    81  			true,
    82  		},
    83  		{
    84  			&provider.Reference{
    85  				ResourceId: &provider.ResourceId{},
    86  				Path:       "./folder",
    87  			},
    88  			true,
    89  		},
    90  	}
    91  
    92  	for _, tt := range tests {
    93  		result := IsRelativeReference(tt.ref)
    94  		if result != tt.expected {
    95  			t.Errorf("IsRelativeReference: ref %v expected %t got %t", tt.ref, tt.expected, result)
    96  		}
    97  	}
    98  }
    99  func TestIsAbsolutReference(t *testing.T) {
   100  	tests := []struct {
   101  		ref      *provider.Reference
   102  		expected bool
   103  	}{
   104  		{
   105  			&provider.Reference{},
   106  			false,
   107  		},
   108  		{
   109  			&provider.Reference{
   110  				Path: ".",
   111  			},
   112  			false,
   113  		},
   114  		{
   115  			&provider.Reference{
   116  				ResourceId: &provider.ResourceId{
   117  					StorageId: "storageId",
   118  					OpaqueId:  "opaqueId",
   119  				},
   120  				Path: "/folder",
   121  			},
   122  			false,
   123  		},
   124  		{
   125  			&provider.Reference{
   126  				Path: "/folder",
   127  			},
   128  			true,
   129  		},
   130  		{
   131  			&provider.Reference{
   132  				ResourceId: &provider.ResourceId{},
   133  			},
   134  			true,
   135  		},
   136  		{
   137  			&provider.Reference{
   138  				ResourceId: &provider.ResourceId{
   139  					StorageId: "storageId",
   140  					OpaqueId:  "opaqueId",
   141  				},
   142  			},
   143  			true,
   144  		},
   145  	}
   146  
   147  	for _, tt := range tests {
   148  		result := IsAbsoluteReference(tt.ref)
   149  		if result != tt.expected {
   150  			t.Errorf("IsAbsolutReference: ref %v expected %t got %t", tt.ref, tt.expected, result)
   151  		}
   152  	}
   153  }
   154  
   155  func TestMakeRelativePath(t *testing.T) {
   156  	tests := []struct {
   157  		path    string
   158  		relPath string
   159  	}{
   160  		{"", "."},
   161  		{"/", "."},
   162  		{"..", "."},
   163  		{"/folder", "./folder"},
   164  		{"/folder/../folder2", "./folder2"},
   165  		{"folder", "./folder"},
   166  	}
   167  	for _, tt := range tests {
   168  		rel := MakeRelativePath(tt.path)
   169  		if rel != tt.relPath {
   170  			t.Errorf("expected %s, got %s", tt.relPath, rel)
   171  		}
   172  	}
   173  }