2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-03-15 16:03:41 -04:00
|
|
|
|
|
|
|
package event
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNameExpand(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name Name
|
|
|
|
expectedResult []Name
|
|
|
|
}{
|
2020-07-20 15:52:49 -04:00
|
|
|
{BucketCreated, []Name{BucketCreated}},
|
|
|
|
{BucketRemoved, []Name{BucketRemoved}},
|
2024-01-05 13:43:06 -05:00
|
|
|
{ObjectAccessedAll, []Name{ObjectAccessedGet, ObjectAccessedHead, ObjectAccessedGetRetention, ObjectAccessedGetLegalHold, ObjectAccessedAttributes}},
|
2022-01-02 12:15:06 -05:00
|
|
|
{ObjectCreatedAll, []Name{
|
|
|
|
ObjectCreatedCompleteMultipartUpload, ObjectCreatedCopy, ObjectCreatedPost, ObjectCreatedPut,
|
|
|
|
ObjectCreatedPutRetention, ObjectCreatedPutLegalHold, ObjectCreatedPutTagging, ObjectCreatedDeleteTagging,
|
|
|
|
}},
|
2024-02-13 10:34:17 -05:00
|
|
|
{ObjectRemovedAll, []Name{ObjectRemovedDelete, ObjectRemovedDeleteMarkerCreated, ObjectRemovedNoOP}},
|
2018-03-15 16:03:41 -04:00
|
|
|
{ObjectAccessedHead, []Name{ObjectAccessedHead}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
result := testCase.name.Expand()
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(result, testCase.expectedResult) {
|
2019-11-21 07:24:51 -05:00
|
|
|
t.Errorf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
2018-03-15 16:03:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameString(t *testing.T) {
|
|
|
|
var blankName Name
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name Name
|
|
|
|
expectedResult string
|
|
|
|
}{
|
2020-07-20 15:52:49 -04:00
|
|
|
{BucketCreated, "s3:BucketCreated:*"},
|
|
|
|
{BucketRemoved, "s3:BucketRemoved:*"},
|
2018-03-15 16:03:41 -04:00
|
|
|
{ObjectAccessedAll, "s3:ObjectAccessed:*"},
|
|
|
|
{ObjectAccessedGet, "s3:ObjectAccessed:Get"},
|
|
|
|
{ObjectAccessedHead, "s3:ObjectAccessed:Head"},
|
|
|
|
{ObjectCreatedAll, "s3:ObjectCreated:*"},
|
|
|
|
{ObjectCreatedCompleteMultipartUpload, "s3:ObjectCreated:CompleteMultipartUpload"},
|
|
|
|
{ObjectCreatedCopy, "s3:ObjectCreated:Copy"},
|
|
|
|
{ObjectCreatedPost, "s3:ObjectCreated:Post"},
|
|
|
|
{ObjectCreatedPut, "s3:ObjectCreated:Put"},
|
|
|
|
{ObjectRemovedAll, "s3:ObjectRemoved:*"},
|
|
|
|
{ObjectRemovedDelete, "s3:ObjectRemoved:Delete"},
|
2024-02-13 10:34:17 -05:00
|
|
|
{ObjectRemovedNoOP, "s3:ObjectRemoved:NoOP"},
|
2020-01-16 18:41:56 -05:00
|
|
|
{ObjectCreatedPutRetention, "s3:ObjectCreated:PutRetention"},
|
|
|
|
{ObjectCreatedPutLegalHold, "s3:ObjectCreated:PutLegalHold"},
|
|
|
|
{ObjectAccessedGetRetention, "s3:ObjectAccessed:GetRetention"},
|
|
|
|
{ObjectAccessedGetLegalHold, "s3:ObjectAccessed:GetLegalHold"},
|
|
|
|
|
2018-03-15 16:03:41 -04:00
|
|
|
{blankName, ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
result := testCase.name.String()
|
|
|
|
|
|
|
|
if result != testCase.expectedResult {
|
|
|
|
t.Fatalf("test %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameMarshalXML(t *testing.T) {
|
|
|
|
var blankName Name
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name Name
|
|
|
|
expectedData []byte
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{ObjectAccessedAll, []byte("<Name>s3:ObjectAccessed:*</Name>"), false},
|
|
|
|
{ObjectRemovedDelete, []byte("<Name>s3:ObjectRemoved:Delete</Name>"), false},
|
2024-02-13 10:34:17 -05:00
|
|
|
{ObjectRemovedNoOP, []byte("<Name>s3:ObjectRemoved:NoOP</Name>"), false},
|
2018-03-15 16:03:41 -04:00
|
|
|
{blankName, []byte("<Name></Name>"), false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
data, err := xml.Marshal(testCase.name)
|
|
|
|
expectErr := (err != nil)
|
|
|
|
|
|
|
|
if expectErr != testCase.expectErr {
|
|
|
|
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !testCase.expectErr {
|
|
|
|
if !reflect.DeepEqual(data, testCase.expectedData) {
|
|
|
|
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, string(testCase.expectedData), string(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameUnmarshalXML(t *testing.T) {
|
|
|
|
var blankName Name
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
data []byte
|
|
|
|
expectedName Name
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{[]byte("<Name>s3:ObjectAccessed:*</Name>"), ObjectAccessedAll, false},
|
|
|
|
{[]byte("<Name>s3:ObjectRemoved:Delete</Name>"), ObjectRemovedDelete, false},
|
2024-02-13 10:34:17 -05:00
|
|
|
{[]byte("<Name>s3:ObjectRemoved:NoOP</Name>"), ObjectRemovedNoOP, false},
|
2018-03-15 16:03:41 -04:00
|
|
|
{[]byte("<Name></Name>"), blankName, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
var name Name
|
|
|
|
err := xml.Unmarshal(testCase.data, &name)
|
|
|
|
expectErr := (err != nil)
|
|
|
|
|
|
|
|
if expectErr != testCase.expectErr {
|
|
|
|
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !testCase.expectErr {
|
|
|
|
if !reflect.DeepEqual(name, testCase.expectedName) {
|
|
|
|
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedName, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameMarshalJSON(t *testing.T) {
|
|
|
|
var blankName Name
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name Name
|
|
|
|
expectedData []byte
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{ObjectAccessedAll, []byte(`"s3:ObjectAccessed:*"`), false},
|
|
|
|
{ObjectRemovedDelete, []byte(`"s3:ObjectRemoved:Delete"`), false},
|
2024-02-13 10:34:17 -05:00
|
|
|
{ObjectRemovedNoOP, []byte(`"s3:ObjectRemoved:NoOP"`), false},
|
2018-03-15 16:03:41 -04:00
|
|
|
{blankName, []byte(`""`), false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
data, err := json.Marshal(testCase.name)
|
|
|
|
expectErr := (err != nil)
|
|
|
|
|
|
|
|
if expectErr != testCase.expectErr {
|
|
|
|
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !testCase.expectErr {
|
|
|
|
if !reflect.DeepEqual(data, testCase.expectedData) {
|
|
|
|
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, string(testCase.expectedData), string(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNameUnmarshalJSON(t *testing.T) {
|
|
|
|
var blankName Name
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
data []byte
|
|
|
|
expectedName Name
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{[]byte(`"s3:ObjectAccessed:*"`), ObjectAccessedAll, false},
|
|
|
|
{[]byte(`"s3:ObjectRemoved:Delete"`), ObjectRemovedDelete, false},
|
2024-02-13 10:34:17 -05:00
|
|
|
{[]byte(`"s3:ObjectRemoved:NoOP"`), ObjectRemovedNoOP, false},
|
2018-03-15 16:03:41 -04:00
|
|
|
{[]byte(`""`), blankName, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
var name Name
|
|
|
|
err := json.Unmarshal(testCase.data, &name)
|
|
|
|
expectErr := (err != nil)
|
|
|
|
|
|
|
|
if expectErr != testCase.expectErr {
|
|
|
|
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !testCase.expectErr {
|
|
|
|
if !reflect.DeepEqual(name, testCase.expectedName) {
|
|
|
|
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedName, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseName(t *testing.T) {
|
|
|
|
var blankName Name
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
s string
|
|
|
|
expectedName Name
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{"s3:ObjectAccessed:*", ObjectAccessedAll, false},
|
|
|
|
{"s3:ObjectRemoved:Delete", ObjectRemovedDelete, false},
|
2024-02-13 10:34:17 -05:00
|
|
|
{"s3:ObjectRemoved:NoOP", ObjectRemovedNoOP, false},
|
2018-03-15 16:03:41 -04:00
|
|
|
{"", blankName, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
name, err := ParseName(testCase.s)
|
|
|
|
expectErr := (err != nil)
|
|
|
|
|
|
|
|
if expectErr != testCase.expectErr {
|
|
|
|
t.Fatalf("test %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !testCase.expectErr {
|
|
|
|
if !reflect.DeepEqual(name, testCase.expectedName) {
|
|
|
|
t.Fatalf("test %v: data: expected: %v, got: %v", i+1, testCase.expectedName, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|