github.com/aavshr/aws-sdk-go@v1.41.3/internal/s3shared/arn/arn_test.go (about)

     1  //go:build go1.7
     2  // +build go1.7
     3  
     4  package arn
     5  
     6  import (
     7  	"reflect"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/aavshr/aws-sdk-go/aws/arn"
    12  )
    13  
    14  func TestParseResource(t *testing.T) {
    15  	cases := map[string]struct {
    16  		Input           string
    17  		MappedResources map[string]func(arn.ARN, []string) (Resource, error)
    18  		Expect          Resource
    19  		ExpectErr       string
    20  	}{
    21  		"Empty ARN": {
    22  			Input:     "",
    23  			ExpectErr: "arn: invalid prefix",
    24  		},
    25  		"No Partition": {
    26  			Input:     "arn::sqs:us-west-2:012345678901:accesspoint",
    27  			ExpectErr: "partition not set",
    28  		},
    29  		"Not S3 ARN": {
    30  			Input:     "arn:aws:sqs:us-west-2:012345678901:accesspoint",
    31  			ExpectErr: "service is not supported",
    32  		},
    33  		"No Resource": {
    34  			Input:     "arn:aws:s3:us-west-2:012345678901:",
    35  			ExpectErr: "resource not set",
    36  		},
    37  		"Unknown Resource Type": {
    38  			Input:     "arn:aws:s3:us-west-2:012345678901:myresource",
    39  			ExpectErr: "unknown resource type",
    40  		},
    41  		"Unknown BucketARN Resource Type": {
    42  			Input:     "arn:aws:s3:us-west-2:012345678901:bucket_name:mybucket",
    43  			ExpectErr: "unknown resource type",
    44  		},
    45  		"Unknown Resource Type with Resource and Sub-Resource": {
    46  			Input:     "arn:aws:s3:us-west-2:012345678901:somethingnew:myresource/subresource",
    47  			ExpectErr: "unknown resource type",
    48  		},
    49  		"Access Point with sub resource": {
    50  			Input: "arn:aws:s3:us-west-2:012345678901:accesspoint:myresource/subresource",
    51  			MappedResources: map[string]func(arn.ARN, []string) (Resource, error){
    52  				"accesspoint": func(a arn.ARN, parts []string) (Resource, error) {
    53  					return ParseAccessPointResource(a, parts)
    54  				},
    55  			},
    56  			ExpectErr: "resource not supported",
    57  		},
    58  		"AccessPoint Resource Type": {
    59  			Input: "arn:aws:s3:us-west-2:012345678901:accesspoint:myendpoint",
    60  			MappedResources: map[string]func(arn.ARN, []string) (Resource, error){
    61  				"accesspoint": func(a arn.ARN, parts []string) (Resource, error) {
    62  					return ParseAccessPointResource(a, parts)
    63  				},
    64  			},
    65  			Expect: AccessPointARN{
    66  				ARN: arn.ARN{
    67  					Partition: "aws",
    68  					Service:   "s3",
    69  					Region:    "us-west-2",
    70  					AccountID: "012345678901",
    71  					Resource:  "accesspoint:myendpoint",
    72  				},
    73  				AccessPointName: "myendpoint",
    74  			},
    75  		},
    76  		"AccessPoint Resource Type With Path Syntax": {
    77  			Input: "arn:aws:s3:us-west-2:012345678901:accesspoint/myendpoint",
    78  			MappedResources: map[string]func(arn.ARN, []string) (Resource, error){
    79  				"accesspoint": func(a arn.ARN, parts []string) (Resource, error) {
    80  					return ParseAccessPointResource(a, parts)
    81  				},
    82  			},
    83  			Expect: AccessPointARN{
    84  				ARN: arn.ARN{
    85  					Partition: "aws",
    86  					Service:   "s3",
    87  					Region:    "us-west-2",
    88  					AccountID: "012345678901",
    89  					Resource:  "accesspoint/myendpoint",
    90  				},
    91  				AccessPointName: "myendpoint",
    92  			},
    93  		},
    94  		"invalid FIPS pseudo region in ARN (prefix)": {
    95  			Input:     "arn:aws:s3:fips-us-west-2:012345678901:accesspoint/myendpoint",
    96  			ExpectErr: "FIPS region not allowed in ARN",
    97  		},
    98  		"invalid FIPS pseudo region in ARN (suffix)": {
    99  			Input:     "arn:aws:s3:us-west-2-fips:012345678901:accesspoint/myendpoint",
   100  			ExpectErr: "FIPS region not allowed in ARN",
   101  		},
   102  	}
   103  
   104  	for name, c := range cases {
   105  		t.Run(name, func(t *testing.T) {
   106  			parsed, err := ParseResource(c.Input, mappedResourceParser(c.MappedResources))
   107  
   108  			if len(c.ExpectErr) == 0 && err != nil {
   109  				t.Fatalf("expect no error but got %v", err)
   110  			} else if len(c.ExpectErr) != 0 && err == nil {
   111  				t.Fatalf("expect error but got nil")
   112  			} else if len(c.ExpectErr) != 0 && err != nil {
   113  				if e, a := c.ExpectErr, err.Error(); !strings.Contains(a, e) {
   114  					t.Fatalf("expect error %q, got %q", e, a)
   115  				}
   116  				return
   117  			}
   118  
   119  			if e, a := c.Expect, parsed; !reflect.DeepEqual(e, a) {
   120  				t.Errorf("Expect %v, got %v", e, a)
   121  			}
   122  		})
   123  	}
   124  }
   125  
   126  func mappedResourceParser(kinds map[string]func(arn.ARN, []string) (Resource, error)) ResourceParser {
   127  	return func(a arn.ARN) (Resource, error) {
   128  		parts := SplitResource(a.Resource)
   129  
   130  		fn, ok := kinds[parts[0]]
   131  		if !ok {
   132  			return nil, InvalidARNError{ARN: a, Reason: "unknown resource type"}
   133  		}
   134  		return fn(a, parts[1:])
   135  	}
   136  }
   137  
   138  func TestSplitResource(t *testing.T) {
   139  	cases := []struct {
   140  		Input  string
   141  		Expect []string
   142  	}{
   143  		{
   144  			Input:  "accesspoint:myendpoint",
   145  			Expect: []string{"accesspoint", "myendpoint"},
   146  		},
   147  		{
   148  			Input:  "accesspoint/myendpoint",
   149  			Expect: []string{"accesspoint", "myendpoint"},
   150  		},
   151  		{
   152  			Input:  "accesspoint",
   153  			Expect: []string{"accesspoint"},
   154  		},
   155  		{
   156  			Input:  "accesspoint:",
   157  			Expect: []string{"accesspoint", ""},
   158  		},
   159  		{
   160  			Input:  "accesspoint:  ",
   161  			Expect: []string{"accesspoint", "  "},
   162  		},
   163  		{
   164  			Input:  "accesspoint:endpoint/object/key",
   165  			Expect: []string{"accesspoint", "endpoint", "object", "key"},
   166  		},
   167  	}
   168  
   169  	for _, c := range cases {
   170  		t.Run(c.Input, func(t *testing.T) {
   171  			parts := SplitResource(c.Input)
   172  			if e, a := c.Expect, parts; !reflect.DeepEqual(e, a) {
   173  				t.Errorf("expect %v, got %v", e, a)
   174  			}
   175  		})
   176  	}
   177  }