From dd6281f21f99d811196490a61a33e39fef0c72d7 Mon Sep 17 00:00:00 2001 From: "guozhi.li" Date: Thu, 18 Jan 2024 10:19:50 +0800 Subject: [PATCH 1/6] support more type to umnarshaJSON --- pkg/set/stringset.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/set/stringset.go b/pkg/set/stringset.go index c35e58e1a..fd638657a 100644 --- a/pkg/set/stringset.go +++ b/pkg/set/stringset.go @@ -153,18 +153,18 @@ func (set StringSet) MarshalJSON() ([]byte, error) { // If 'data' contains JSON string, the set contains the string as one element. // If 'data' contains Other JSON types, JSON parse error is returned. func (set *StringSet) UnmarshalJSON(data []byte) error { - sl := []string{} + sl := []interface{}{} var err error if err = json.Unmarshal(data, &sl); err == nil { *set = make(StringSet) for _, s := range sl { - set.Add(s) + set.Add(fmt.Sprintf("%v", s)) } } else { - var s string + var s interface{} if err = json.Unmarshal(data, &s); err == nil { *set = make(StringSet) - set.Add(s) + set.Add(fmt.Sprintf("%v", s)) } } From 1b320d488a34e3ebab783a591258b42d9b71f89a Mon Sep 17 00:00:00 2001 From: "guozhi.li" Date: Thu, 18 Jan 2024 10:34:26 +0800 Subject: [PATCH 2/6] support more type to umnarshaJSON --- pkg/set/stringset.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/set/stringset.go b/pkg/set/stringset.go index fd638657a..2566a3df7 100644 --- a/pkg/set/stringset.go +++ b/pkg/set/stringset.go @@ -149,9 +149,6 @@ func (set StringSet) MarshalJSON() ([]byte, error) { } // UnmarshalJSON - parses JSON data and creates new set with it. -// If 'data' contains JSON string array, the set contains each string. -// If 'data' contains JSON string, the set contains the string as one element. -// If 'data' contains Other JSON types, JSON parse error is returned. func (set *StringSet) UnmarshalJSON(data []byte) error { sl := []interface{}{} var err error From 89eae01f815a1d26e984c2208775b916aa216c5d Mon Sep 17 00:00:00 2001 From: "guozhi.li" Date: Thu, 18 Jan 2024 14:43:27 +0800 Subject: [PATCH 3/6] ut --- pkg/set/stringset_test.go | 87 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/pkg/set/stringset_test.go b/pkg/set/stringset_test.go index ec6dfbe63..442c774ad 100644 --- a/pkg/set/stringset_test.go +++ b/pkg/set/stringset_test.go @@ -19,6 +19,8 @@ package set import ( "fmt" + "reflect" + "sort" "strings" "testing" ) @@ -346,3 +348,88 @@ func TestStringSetToSlice(t *testing.T) { } } } + +func TestStringSet_UnmarshalJSON(t *testing.T) { + type args struct { + data []byte + expectResult []string + } + tests := []struct { + name string + set StringSet + args args + wantErr bool + }{ + { + name: "test strings", + set: NewStringSet(), + args: args{ + data: []byte(`["foo","bar"]`), + expectResult: []string{"foo", "bar"}, + }, + wantErr: false, + }, + { + name: "test string", + set: NewStringSet(), + args: args{ + data: []byte(`"foo"`), + expectResult: []string{"foo"}, + }, + wantErr: false, + }, + { + name: "test bools", + set: NewStringSet(), + args: args{ + data: []byte(`[false,true]`), + expectResult: []string{"false", "true"}, + }, + wantErr: false, + }, + { + name: "test bool", + set: NewStringSet(), + args: args{ + data: []byte(`false`), + expectResult: []string{"false"}, + }, + wantErr: false, + }, + { + name: "test ints", + set: NewStringSet(), + args: args{ + data: []byte(`[1,2]`), + expectResult: []string{"1", "2"}, + }, + wantErr: false, + }, + { + name: "test int", + set: NewStringSet(), + args: args{ + data: []byte(`1`), + expectResult: []string{"1"}, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.set.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + } + slice := tt.set.ToSlice() + sort.Slice(slice, func(i, j int) bool { + return slice[i] < slice[j] + }) + sort.Slice(tt.args.expectResult, func(i, j int) bool { + return tt.args.expectResult[i] < tt.args.expectResult[j] + }) + if !reflect.DeepEqual(slice, tt.args.expectResult) { + t.Errorf("StringSet() get %v, want %v", tt.set.ToSlice(), tt.args.expectResult) + } + }) + } +} From bb7d63cbdb7a084fc28795d74d8f8d24922e692c Mon Sep 17 00:00:00 2001 From: "guozhi.li" Date: Thu, 18 Jan 2024 14:53:14 +0800 Subject: [PATCH 4/6] ut --- pkg/policy/bucket-policy_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pkg/policy/bucket-policy_test.go b/pkg/policy/bucket-policy_test.go index 94cfe9050..3c7f938fc 100644 --- a/pkg/policy/bucket-policy_test.go +++ b/pkg/policy/bucket-policy_test.go @@ -210,6 +210,31 @@ func TestUnmarshalBucketPolicy(t *testing.T) { } } ] +}`, shouldSucceed: true}, + // Test 10 + {policyData: `{ + "Version": "2012-10-17", + "Statement": [{ + "Effect": "Deny", + "Principal": { + "AWS": [ + "*" + ] + }, + "Action": [ + "s3:PutObject" + ], + "Resource": [ + "arn:aws:s3:::mytest/*" + ], + "Condition": { + "Null": { + "s3:x-amz-server-side-encryption": [ + true + ] + } + } + }] }`, shouldSucceed: true}, } From 5e79a6d66e869b99fe56b5e308b04711c9fd6b3e Mon Sep 17 00:00:00 2001 From: "guozhi.li" Date: Thu, 18 Jan 2024 15:07:10 +0800 Subject: [PATCH 5/6] ut --- pkg/policy/bucket-policy_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pkg/policy/bucket-policy_test.go b/pkg/policy/bucket-policy_test.go index 3c7f938fc..3d10fcee0 100644 --- a/pkg/policy/bucket-policy_test.go +++ b/pkg/policy/bucket-policy_test.go @@ -235,6 +235,26 @@ func TestUnmarshalBucketPolicy(t *testing.T) { } } }] +}`, shouldSucceed: true}, + // Test 11 + {policyData: `{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Deny", + "Principal": "*", + "Action": "s3:PutObject", + "Resource": [ + "arn:aws:s3:::DOC-EXAMPLE-BUCKET1", + "arn:aws:s3:::DOC-EXAMPLE-BUCKET1/*" + ], + "Condition": { + "NumericLessThan": { + "s3:TlsVersion": 1.2 + } + } + } + ] }`, shouldSucceed: true}, } From 543b093afe8a7f60e7b4b5964e91890bf478b220 Mon Sep 17 00:00:00 2001 From: "guozhi.li" Date: Thu, 18 Jan 2024 15:09:12 +0800 Subject: [PATCH 6/6] ut --- pkg/set/stringset_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/set/stringset_test.go b/pkg/set/stringset_test.go index 442c774ad..278313135 100644 --- a/pkg/set/stringset_test.go +++ b/pkg/set/stringset_test.go @@ -414,6 +414,24 @@ func TestStringSet_UnmarshalJSON(t *testing.T) { }, wantErr: false, }, + { + name: "test floats", + set: NewStringSet(), + args: args{ + data: []byte(`[1.1,2.2]`), + expectResult: []string{"1.1", "2.2"}, + }, + wantErr: false, + }, + { + name: "test float", + set: NewStringSet(), + args: args{ + data: []byte(`1.1`), + expectResult: []string{"1.1"}, + }, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {