github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/event/arn_test.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package event
    19  
    20  import (
    21  	"encoding/xml"
    22  	"reflect"
    23  	"testing"
    24  )
    25  
    26  func TestARNString(t *testing.T) {
    27  	testCases := []struct {
    28  		arn            ARN
    29  		expectedResult string
    30  	}{
    31  		{ARN{}, ""},
    32  		{ARN{TargetID{"1", "webhook"}, ""}, "arn:minio:sqs::1:webhook"},
    33  		{ARN{TargetID{"1", "webhook"}, "us-east-1"}, "arn:minio:sqs:us-east-1:1:webhook"},
    34  	}
    35  
    36  	for i, testCase := range testCases {
    37  		result := testCase.arn.String()
    38  
    39  		if result != testCase.expectedResult {
    40  			t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
    41  		}
    42  	}
    43  }
    44  
    45  func TestARNMarshalXML(t *testing.T) {
    46  	testCases := []struct {
    47  		arn          ARN
    48  		expectedData []byte
    49  		expectErr    bool
    50  	}{
    51  		{ARN{}, []byte("<ARN></ARN>"), false},
    52  		{ARN{TargetID{"1", "webhook"}, ""}, []byte("<ARN>arn:minio:sqs::1:webhook</ARN>"), false},
    53  		{ARN{TargetID{"1", "webhook"}, "us-east-1"}, []byte("<ARN>arn:minio:sqs:us-east-1:1:webhook</ARN>"), false},
    54  	}
    55  
    56  	for i, testCase := range testCases {
    57  		data, err := xml.Marshal(testCase.arn)
    58  		expectErr := (err != nil)
    59  
    60  		if expectErr != testCase.expectErr {
    61  			t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
    62  		}
    63  
    64  		if !testCase.expectErr {
    65  			if !reflect.DeepEqual(data, testCase.expectedData) {
    66  				t.Fatalf("test %v: data: expected: %v, got: %v", i+1, string(testCase.expectedData), string(data))
    67  			}
    68  		}
    69  	}
    70  }
    71  
    72  func TestARNUnmarshalXML(t *testing.T) {
    73  	testCases := []struct {
    74  		data        []byte
    75  		expectedARN *ARN
    76  		expectErr   bool
    77  	}{
    78  		{[]byte("<ARN></ARN>"), nil, true},
    79  		{[]byte("<ARN>arn:minio:sqs:::</ARN>"), nil, true},
    80  		{[]byte("<ARN>arn:minio:sqs::1:webhook</ARN>"), &ARN{TargetID{"1", "webhook"}, ""}, false},
    81  		{[]byte("<ARN>arn:minio:sqs:us-east-1:1:webhook</ARN>"), &ARN{TargetID{"1", "webhook"}, "us-east-1"}, false},
    82  	}
    83  
    84  	for i, testCase := range testCases {
    85  		arn := &ARN{}
    86  		err := xml.Unmarshal(testCase.data, &arn)
    87  		expectErr := (err != nil)
    88  
    89  		if expectErr != testCase.expectErr {
    90  			t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
    91  		}
    92  
    93  		if !testCase.expectErr {
    94  			if *arn != *testCase.expectedARN {
    95  				t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedARN, arn)
    96  			}
    97  		}
    98  	}
    99  }
   100  
   101  func TestParseARN(t *testing.T) {
   102  	testCases := []struct {
   103  		s           string
   104  		expectedARN *ARN
   105  		expectErr   bool
   106  	}{
   107  		{"", nil, true},
   108  		{"arn:minio:sqs:::", nil, true},
   109  		{"arn:minio:sqs::1:webhook:remote", nil, true},
   110  		{"arn:aws:sqs::1:webhook", nil, true},
   111  		{"arn:minio:sns::1:webhook", nil, true},
   112  		{"arn:minio:sqs::1:webhook", &ARN{TargetID{"1", "webhook"}, ""}, false},
   113  		{"arn:minio:sqs:us-east-1:1:webhook", &ARN{TargetID{"1", "webhook"}, "us-east-1"}, false},
   114  	}
   115  
   116  	for i, testCase := range testCases {
   117  		arn, err := parseARN(testCase.s)
   118  		expectErr := (err != nil)
   119  
   120  		if expectErr != testCase.expectErr {
   121  			t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
   122  		}
   123  
   124  		if !testCase.expectErr {
   125  			if *arn != *testCase.expectedARN {
   126  				t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedARN, arn)
   127  			}
   128  		}
   129  	}
   130  }