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,
|
|
|
|
}},
|
2020-09-15 00:32:52 -04:00
|
|
|
{ObjectRemovedAll, []Name{ObjectRemovedDelete, ObjectRemovedDeleteMarkerCreated}},
|
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"},
|
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},
|
|
|
|
{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},
|
|
|
|
{[]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},
|
|
|
|
{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},
|
|
|
|
{[]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},
|
|
|
|
{"", 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|