storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/condition/key_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package condition
    18  
    19  import (
    20  	"encoding/json"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func TestKeyIsValid(t *testing.T) {
    26  	testCases := []struct {
    27  		key            Key
    28  		expectedResult bool
    29  	}{
    30  		{S3XAmzCopySource, true},
    31  		{S3XAmzServerSideEncryption, true},
    32  		{S3XAmzServerSideEncryptionCustomerAlgorithm, true},
    33  		{S3XAmzMetadataDirective, true},
    34  		{S3XAmzStorageClass, true},
    35  		{S3LocationConstraint, true},
    36  		{S3Prefix, true},
    37  		{S3Delimiter, true},
    38  		{S3MaxKeys, true},
    39  		{AWSReferer, true},
    40  		{AWSSourceIP, true},
    41  		{Key("foo"), false},
    42  	}
    43  
    44  	for i, testCase := range testCases {
    45  		result := testCase.key.IsValid()
    46  
    47  		if testCase.expectedResult != result {
    48  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
    49  		}
    50  	}
    51  }
    52  
    53  func TestKeyMarshalJSON(t *testing.T) {
    54  	testCases := []struct {
    55  		key            Key
    56  		expectedResult []byte
    57  		expectErr      bool
    58  	}{
    59  		{S3XAmzCopySource, []byte(`"s3:x-amz-copy-source"`), false},
    60  		{Key("foo"), nil, true},
    61  	}
    62  
    63  	for i, testCase := range testCases {
    64  		result, err := json.Marshal(testCase.key)
    65  		expectErr := (err != nil)
    66  
    67  		if testCase.expectErr != expectErr {
    68  			t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr)
    69  		}
    70  
    71  		if !testCase.expectErr {
    72  			if !reflect.DeepEqual(result, testCase.expectedResult) {
    73  				t.Fatalf("case %v: key: expected: %v, got: %v\n", i+1, string(testCase.expectedResult), string(result))
    74  			}
    75  		}
    76  	}
    77  }
    78  
    79  func TestKeyName(t *testing.T) {
    80  	testCases := []struct {
    81  		key            Key
    82  		expectedResult string
    83  	}{
    84  		{S3XAmzCopySource, "x-amz-copy-source"},
    85  		{AWSReferer, "Referer"},
    86  	}
    87  
    88  	for i, testCase := range testCases {
    89  		result := testCase.key.Name()
    90  
    91  		if testCase.expectedResult != result {
    92  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
    93  		}
    94  	}
    95  }
    96  
    97  func TestKeyUnmarshalJSON(t *testing.T) {
    98  	testCases := []struct {
    99  		data        []byte
   100  		expectedKey Key
   101  		expectErr   bool
   102  	}{
   103  		{[]byte(`"s3:x-amz-copy-source"`), S3XAmzCopySource, false},
   104  		{[]byte(`"foo"`), Key(""), true},
   105  	}
   106  
   107  	for i, testCase := range testCases {
   108  		var key Key
   109  		err := json.Unmarshal(testCase.data, &key)
   110  		expectErr := (err != nil)
   111  
   112  		if testCase.expectErr != expectErr {
   113  			t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr)
   114  		}
   115  
   116  		if !testCase.expectErr {
   117  			if testCase.expectedKey != key {
   118  				t.Fatalf("case %v: key: expected: %v, got: %v\n", i+1, testCase.expectedKey, key)
   119  			}
   120  		}
   121  	}
   122  }
   123  
   124  func TestKeySetAdd(t *testing.T) {
   125  	testCases := []struct {
   126  		set            KeySet
   127  		key            Key
   128  		expectedResult KeySet
   129  	}{
   130  		{NewKeySet(), S3XAmzCopySource, NewKeySet(S3XAmzCopySource)},
   131  		{NewKeySet(S3XAmzCopySource), S3XAmzCopySource, NewKeySet(S3XAmzCopySource)},
   132  	}
   133  
   134  	for i, testCase := range testCases {
   135  		testCase.set.Add(testCase.key)
   136  
   137  		if !reflect.DeepEqual(testCase.expectedResult, testCase.set) {
   138  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, testCase.set)
   139  		}
   140  	}
   141  }
   142  
   143  func TestKeySetDifference(t *testing.T) {
   144  	testCases := []struct {
   145  		set            KeySet
   146  		setToDiff      KeySet
   147  		expectedResult KeySet
   148  	}{
   149  		{NewKeySet(), NewKeySet(S3XAmzCopySource), NewKeySet()},
   150  		{NewKeySet(S3Prefix, S3Delimiter, S3MaxKeys), NewKeySet(S3Delimiter, S3MaxKeys), NewKeySet(S3Prefix)},
   151  	}
   152  
   153  	for i, testCase := range testCases {
   154  		result := testCase.set.Difference(testCase.setToDiff)
   155  
   156  		if !reflect.DeepEqual(testCase.expectedResult, result) {
   157  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   158  		}
   159  	}
   160  }
   161  
   162  func TestKeySetIsEmpty(t *testing.T) {
   163  	testCases := []struct {
   164  		set            KeySet
   165  		expectedResult bool
   166  	}{
   167  		{NewKeySet(), true},
   168  		{NewKeySet(S3Delimiter), false},
   169  	}
   170  
   171  	for i, testCase := range testCases {
   172  		result := testCase.set.IsEmpty()
   173  
   174  		if testCase.expectedResult != result {
   175  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   176  		}
   177  	}
   178  }
   179  
   180  func TestKeySetString(t *testing.T) {
   181  	testCases := []struct {
   182  		set            KeySet
   183  		expectedResult string
   184  	}{
   185  		{NewKeySet(), `[]`},
   186  		{NewKeySet(S3Delimiter), `[s3:delimiter]`},
   187  	}
   188  
   189  	for i, testCase := range testCases {
   190  		result := testCase.set.String()
   191  
   192  		if testCase.expectedResult != result {
   193  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   194  		}
   195  	}
   196  }
   197  
   198  func TestKeySetToSlice(t *testing.T) {
   199  	testCases := []struct {
   200  		set            KeySet
   201  		expectedResult []Key
   202  	}{
   203  		{NewKeySet(), []Key{}},
   204  		{NewKeySet(S3Delimiter), []Key{S3Delimiter}},
   205  	}
   206  
   207  	for i, testCase := range testCases {
   208  		result := testCase.set.ToSlice()
   209  
   210  		if !reflect.DeepEqual(testCase.expectedResult, result) {
   211  			t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result)
   212  		}
   213  	}
   214  }