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/>.
|
2020-01-16 18:41:56 -05:00
|
|
|
|
2020-01-27 17:12:34 -05:00
|
|
|
package lock
|
2020-01-16 18:41:56 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2021-03-30 02:52:30 -04:00
|
|
|
"errors"
|
2020-01-16 18:41:56 -05:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
2020-01-16 18:41:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseMode(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
value string
|
2020-04-06 16:44:16 -04:00
|
|
|
expectedMode RetMode
|
2020-01-16 18:41:56 -05:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
value: "governance",
|
2020-04-06 16:44:16 -04:00
|
|
|
expectedMode: RetGovernance,
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "complIAnce",
|
2020-04-06 16:44:16 -04:00
|
|
|
expectedMode: RetCompliance,
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "gce",
|
2020-04-06 16:44:16 -04:00
|
|
|
expectedMode: "",
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
2020-04-06 16:44:16 -04:00
|
|
|
if parseRetMode(tc.value) != tc.expectedMode {
|
|
|
|
t.Errorf("Expected Mode %s, got %s", tc.expectedMode, parseRetMode(tc.value))
|
2020-01-16 18:41:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-02 12:15:06 -05:00
|
|
|
|
2020-01-16 18:41:56 -05:00
|
|
|
func TestParseLegalHoldStatus(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
value string
|
|
|
|
expectedStatus LegalHoldStatus
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
value: "ON",
|
2020-04-06 16:44:16 -04:00
|
|
|
expectedStatus: LegalHoldOn,
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "Off",
|
2020-04-06 16:44:16 -04:00
|
|
|
expectedStatus: LegalHoldOff,
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "x",
|
|
|
|
expectedStatus: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
actualStatus := parseLegalHoldStatus(tt.value)
|
|
|
|
if actualStatus != tt.expectedStatus {
|
|
|
|
t.Errorf("Expected legal hold status %s, got %s", tt.expectedStatus, actualStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestUnmarshalDefaultRetention checks if default retention
|
|
|
|
// marshaling and unmarshaling work as expected
|
|
|
|
func TestUnmarshalDefaultRetention(t *testing.T) {
|
|
|
|
days := uint64(4)
|
|
|
|
years := uint64(1)
|
|
|
|
zerodays := uint64(0)
|
|
|
|
invalidDays := uint64(maximumRetentionDays + 1)
|
|
|
|
tests := []struct {
|
|
|
|
value DefaultRetention
|
|
|
|
expectedErr error
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
value: DefaultRetention{Mode: "retain"},
|
|
|
|
expectedErr: fmt.Errorf("unknown retention mode retain"),
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2020-04-06 16:44:16 -04:00
|
|
|
value: DefaultRetention{Mode: RetGovernance},
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: fmt.Errorf("either Days or Years must be specified"),
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2020-04-06 16:44:16 -04:00
|
|
|
value: DefaultRetention{Mode: RetGovernance, Days: &days},
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: nil,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
{
|
2020-04-06 16:44:16 -04:00
|
|
|
value: DefaultRetention{Mode: RetGovernance, Years: &years},
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: nil,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
{
|
2020-04-06 16:44:16 -04:00
|
|
|
value: DefaultRetention{Mode: RetGovernance, Days: &days, Years: &years},
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: fmt.Errorf("either Days or Years must be specified, not both"),
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2020-04-06 16:44:16 -04:00
|
|
|
value: DefaultRetention{Mode: RetGovernance, Days: &zerodays},
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: fmt.Errorf("Default retention period must be a positive integer value for 'Days'"),
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2020-04-06 16:44:16 -04:00
|
|
|
value: DefaultRetention{Mode: RetGovernance, Days: &invalidDays},
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: fmt.Errorf("Default retention period too large for 'Days' %d", invalidDays),
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
d, err := xml.MarshalIndent(&tt.value, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var dr DefaultRetention
|
|
|
|
err = xml.Unmarshal(d, &dr)
|
2021-11-16 12:28:29 -05:00
|
|
|
//nolint:gocritic
|
2020-01-16 18:41:56 -05:00
|
|
|
if tt.expectedErr == nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: expected = <nil>, got = %v", err)
|
|
|
|
}
|
|
|
|
} else if err == nil {
|
|
|
|
t.Fatalf("error: expected = %v, got = <nil>", tt.expectedErr)
|
|
|
|
} else if tt.expectedErr.Error() != err.Error() {
|
|
|
|
t.Fatalf("error: expected = %v, got = %v", tt.expectedErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseObjectLockConfig(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
value string
|
|
|
|
expectedErr error
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
2020-05-19 16:53:54 -04:00
|
|
|
value: `<ObjectLockConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><ObjectLockEnabled>yes</ObjectLockEnabled></ObjectLockConfiguration>`,
|
|
|
|
expectedErr: fmt.Errorf("only 'Enabled' value is allowed to ObjectLockEnabled element"),
|
2020-01-16 18:41:56 -05:00
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2020-05-19 16:53:54 -04:00
|
|
|
value: `<ObjectLockConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><ObjectLockEnabled>Enabled</ObjectLockEnabled><Rule><DefaultRetention><Mode>COMPLIANCE</Mode><Days>0</Days></DefaultRetention></Rule></ObjectLockConfiguration>`,
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: fmt.Errorf("Default retention period must be a positive integer value for 'Days'"),
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2020-05-19 16:53:54 -04:00
|
|
|
value: `<ObjectLockConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><ObjectLockEnabled>Enabled</ObjectLockEnabled><Rule><DefaultRetention><Mode>COMPLIANCE</Mode><Days>30</Days></DefaultRetention></Rule></ObjectLockConfiguration>`,
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: nil,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2022-10-08 16:11:00 -04:00
|
|
|
tt := tt
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
_, err := ParseObjectLockConfig(strings.NewReader(tt.value))
|
|
|
|
//nolint:gocritic
|
|
|
|
if tt.expectedErr == nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: expected = <nil>, got = %v", err)
|
|
|
|
}
|
|
|
|
} else if err == nil {
|
|
|
|
t.Fatalf("error: expected = %v, got = <nil>", tt.expectedErr)
|
|
|
|
} else if tt.expectedErr.Error() != err.Error() {
|
|
|
|
t.Fatalf("error: expected = %v, got = %v", tt.expectedErr, err)
|
2020-01-16 18:41:56 -05:00
|
|
|
}
|
2022-10-08 16:11:00 -04:00
|
|
|
})
|
2020-01-16 18:41:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseObjectRetention(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
value string
|
|
|
|
expectedErr error
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
2020-05-19 16:53:54 -04:00
|
|
|
value: `<?xml version="1.0" encoding="UTF-8"?><Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Mode>string</Mode><RetainUntilDate>2020-01-02T15:04:05Z</RetainUntilDate></Retention>`,
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: ErrUnknownWORMModeDirective,
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2020-05-19 16:53:54 -04:00
|
|
|
value: `<?xml version="1.0" encoding="UTF-8"?><Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Mode>COMPLIANCE</Mode><RetainUntilDate>2017-01-02T15:04:05Z</RetainUntilDate></Retention>`,
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: ErrPastObjectLockRetainDate,
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2020-05-19 16:53:54 -04:00
|
|
|
value: `<?xml version="1.0" encoding="UTF-8"?><Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Mode>GOVERNANCE</Mode><RetainUntilDate>2057-01-02T15:04:05Z</RetainUntilDate></Retention>`,
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: nil,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
2022-10-08 16:11:00 -04:00
|
|
|
{
|
|
|
|
value: `<?xml version="1.0" encoding="UTF-8"?><Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Mode>GOVERNANCE</Mode><RetainUntilDate>2057-01-02T15:04:05.000Z</RetainUntilDate></Retention>`,
|
|
|
|
expectedErr: nil,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
2020-01-16 18:41:56 -05:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2022-10-08 16:11:00 -04:00
|
|
|
tt := tt
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
_, err := ParseObjectRetention(strings.NewReader(tt.value))
|
|
|
|
//nolint:gocritic
|
|
|
|
if tt.expectedErr == nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error: expected = <nil>, got = %v", err)
|
|
|
|
}
|
|
|
|
} else if err == nil {
|
|
|
|
t.Fatalf("error: expected = %v, got = <nil>", tt.expectedErr)
|
|
|
|
} else if tt.expectedErr.Error() != err.Error() {
|
|
|
|
t.Fatalf("error: expected = %v, got = %v", tt.expectedErr, err)
|
2020-01-16 18:41:56 -05:00
|
|
|
}
|
2022-10-08 16:11:00 -04:00
|
|
|
})
|
2020-01-16 18:41:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsObjectLockRequested(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
header http.Header
|
|
|
|
expectedVal bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
"Authorization": []string{"AWS4-HMAC-SHA256 <cred_string>"},
|
|
|
|
"X-Amz-Content-Sha256": []string{""},
|
|
|
|
"Content-Encoding": []string{""},
|
|
|
|
},
|
|
|
|
expectedVal: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
2020-04-06 16:44:16 -04:00
|
|
|
AmzObjectLockLegalHold: []string{""},
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
expectedVal: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
2020-04-06 16:44:16 -04:00
|
|
|
AmzObjectLockRetainUntilDate: []string{""},
|
|
|
|
AmzObjectLockMode: []string{""},
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
expectedVal: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
2020-04-06 16:44:16 -04:00
|
|
|
AmzObjectLockBypassRetGovernance: []string{""},
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
expectedVal: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
actualVal := IsObjectLockRequested(tt.header)
|
|
|
|
if actualVal != tt.expectedVal {
|
|
|
|
t.Fatalf("error: expected %v, actual %v", tt.expectedVal, actualVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsObjectLockGovernanceBypassSet(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
header http.Header
|
|
|
|
expectedVal bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
"Authorization": []string{"AWS4-HMAC-SHA256 <cred_string>"},
|
|
|
|
"X-Amz-Content-Sha256": []string{""},
|
|
|
|
"Content-Encoding": []string{""},
|
|
|
|
},
|
|
|
|
expectedVal: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
2020-04-06 16:44:16 -04:00
|
|
|
AmzObjectLockLegalHold: []string{""},
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
expectedVal: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
2020-04-06 16:44:16 -04:00
|
|
|
AmzObjectLockRetainUntilDate: []string{""},
|
|
|
|
AmzObjectLockMode: []string{""},
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
expectedVal: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
2020-04-06 16:44:16 -04:00
|
|
|
AmzObjectLockBypassRetGovernance: []string{""},
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
expectedVal: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
2020-04-06 16:44:16 -04:00
|
|
|
AmzObjectLockBypassRetGovernance: []string{"true"},
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
expectedVal: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
actualVal := IsObjectLockGovernanceBypassSet(tt.header)
|
|
|
|
if actualVal != tt.expectedVal {
|
|
|
|
t.Fatalf("error: expected %v, actual %v", tt.expectedVal, actualVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseObjectLockRetentionHeaders(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
header http.Header
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
"Authorization": []string{"AWS4-HMAC-SHA256 <cred_string>"},
|
|
|
|
"X-Amz-Content-Sha256": []string{""},
|
|
|
|
"Content-Encoding": []string{""},
|
|
|
|
},
|
|
|
|
expectedErr: ErrObjectLockInvalidHeaders,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
xhttp.AmzObjectLockMode: []string{"lock"},
|
|
|
|
xhttp.AmzObjectLockRetainUntilDate: []string{"2017-01-02"},
|
|
|
|
},
|
|
|
|
expectedErr: ErrUnknownWORMModeDirective,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
xhttp.AmzObjectLockMode: []string{"governance"},
|
|
|
|
},
|
|
|
|
expectedErr: ErrObjectLockInvalidHeaders,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
xhttp.AmzObjectLockRetainUntilDate: []string{"2017-01-02"},
|
|
|
|
xhttp.AmzObjectLockMode: []string{"governance"},
|
|
|
|
},
|
|
|
|
expectedErr: ErrInvalidRetentionDate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
xhttp.AmzObjectLockRetainUntilDate: []string{"2017-01-02T15:04:05Z"},
|
|
|
|
xhttp.AmzObjectLockMode: []string{"governance"},
|
|
|
|
},
|
|
|
|
expectedErr: ErrPastObjectLockRetainDate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
xhttp.AmzObjectLockMode: []string{"governance"},
|
|
|
|
xhttp.AmzObjectLockRetainUntilDate: []string{"2017-01-02T15:04:05Z"},
|
|
|
|
},
|
|
|
|
expectedErr: ErrPastObjectLockRetainDate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
xhttp.AmzObjectLockMode: []string{"governance"},
|
|
|
|
xhttp.AmzObjectLockRetainUntilDate: []string{"2087-01-02T15:04:05Z"},
|
|
|
|
},
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
2022-10-08 16:11:00 -04:00
|
|
|
{
|
|
|
|
header: http.Header{
|
|
|
|
xhttp.AmzObjectLockMode: []string{"governance"},
|
|
|
|
xhttp.AmzObjectLockRetainUntilDate: []string{"2087-01-02T15:04:05.000Z"},
|
|
|
|
},
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
2020-01-16 18:41:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
_, _, err := ParseObjectLockRetentionHeaders(tt.header)
|
2021-11-16 12:28:29 -05:00
|
|
|
//nolint:gocritic
|
2020-01-16 18:41:56 -05:00
|
|
|
if tt.expectedErr == nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Case %d error: expected = <nil>, got = %v", i, err)
|
|
|
|
}
|
|
|
|
} else if err == nil {
|
|
|
|
t.Fatalf("Case %d error: expected = %v, got = <nil>", i, tt.expectedErr)
|
|
|
|
} else if tt.expectedErr.Error() != err.Error() {
|
|
|
|
t.Fatalf("Case %d error: expected = %v, got = %v", i, tt.expectedErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetObjectRetentionMeta(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
metadata map[string]string
|
|
|
|
expected ObjectRetention
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"Authorization": "AWS4-HMAC-SHA256 <cred_string>",
|
|
|
|
"X-Amz-Content-Sha256": "",
|
|
|
|
"Content-Encoding": "",
|
|
|
|
},
|
|
|
|
expected: ObjectRetention{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"x-amz-object-lock-mode": "governance",
|
|
|
|
},
|
2020-04-06 16:44:16 -04:00
|
|
|
expected: ObjectRetention{Mode: RetGovernance},
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"x-amz-object-lock-retain-until-date": "2020-02-01",
|
|
|
|
},
|
|
|
|
expected: ObjectRetention{RetainUntilDate: RetentionDate{time.Date(2020, 2, 1, 12, 0, 0, 0, time.UTC)}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
o := GetObjectRetentionMeta(tt.metadata)
|
|
|
|
if o.Mode != tt.expected.Mode {
|
|
|
|
t.Fatalf("Case %d expected %v, got %v", i, tt.expected.Mode, o.Mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetObjectLegalHoldMeta(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
metadata map[string]string
|
|
|
|
expected ObjectLegalHold
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"x-amz-object-lock-mode": "governance",
|
|
|
|
},
|
|
|
|
expected: ObjectLegalHold{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"x-amz-object-lock-legal-hold": "on",
|
|
|
|
},
|
2020-04-06 16:44:16 -04:00
|
|
|
expected: ObjectLegalHold{Status: LegalHoldOn},
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"x-amz-object-lock-legal-hold": "off",
|
|
|
|
},
|
2020-04-06 16:44:16 -04:00
|
|
|
expected: ObjectLegalHold{Status: LegalHoldOff},
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"x-amz-object-lock-legal-hold": "X",
|
|
|
|
},
|
|
|
|
expected: ObjectLegalHold{Status: ""},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
o := GetObjectLegalHoldMeta(tt.metadata)
|
|
|
|
if o.Status != tt.expected.Status {
|
|
|
|
t.Fatalf("Case %d expected %v, got %v", i, tt.expected.Status, o.Status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseObjectLegalHold(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
value string
|
|
|
|
expectedErr error
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{
|
2020-05-19 16:53:54 -04:00
|
|
|
value: `<?xml version="1.0" encoding="UTF-8"?><LegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>string</Status></LegalHold>`,
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: ErrMalformedXML,
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
{
|
2020-05-19 16:53:54 -04:00
|
|
|
value: `<?xml version="1.0" encoding="UTF-8"?><LegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>ON</Status></LegalHold>`,
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: nil,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
2021-03-30 02:52:30 -04:00
|
|
|
{
|
|
|
|
value: `<?xml version="1.0" encoding="UTF-8"?><ObjectLockLegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>ON</Status></ObjectLockLegalHold>`,
|
|
|
|
expectedErr: nil,
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
// invalid Status key
|
|
|
|
{
|
|
|
|
value: `<?xml version="1.0" encoding="UTF-8"?><ObjectLockLegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><MyStatus>ON</MyStatus></ObjectLockLegalHold>`,
|
|
|
|
expectedErr: errors.New("expected element type <Status> but have <MyStatus>"),
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
// invalid XML attr
|
|
|
|
{
|
|
|
|
value: `<?xml version="1.0" encoding="UTF-8"?><UnknownLegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>ON</Status></UnknownLegalHold>`,
|
|
|
|
expectedErr: errors.New("expected element type <LegalHold>/<ObjectLockLegalHold> but have <UnknownLegalHold>"),
|
|
|
|
expectErr: true,
|
|
|
|
},
|
2020-01-16 18:41:56 -05:00
|
|
|
{
|
2020-05-19 16:53:54 -04:00
|
|
|
value: `<?xml version="1.0" encoding="UTF-8"?><LegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>On</Status></LegalHold>`,
|
2020-01-16 18:41:56 -05:00
|
|
|
expectedErr: ErrMalformedXML,
|
|
|
|
expectErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
_, err := ParseObjectLegalHold(strings.NewReader(tt.value))
|
2021-11-16 12:28:29 -05:00
|
|
|
//nolint:gocritic
|
2020-01-16 18:41:56 -05:00
|
|
|
if tt.expectedErr == nil {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Case %d error: expected = <nil>, got = %v", i, err)
|
|
|
|
}
|
|
|
|
} else if err == nil {
|
|
|
|
t.Fatalf("Case %d error: expected = %v, got = <nil>", i, tt.expectedErr)
|
|
|
|
} else if tt.expectedErr.Error() != err.Error() {
|
|
|
|
t.Fatalf("Case %d error: expected = %v, got = %v", i, tt.expectedErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-02 12:15:06 -05:00
|
|
|
|
2020-01-16 18:41:56 -05:00
|
|
|
func TestFilterObjectLockMetadata(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
metadata map[string]string
|
|
|
|
filterRetention bool
|
|
|
|
filterLegalHold bool
|
|
|
|
expected map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"Authorization": "AWS4-HMAC-SHA256 <cred_string>",
|
|
|
|
"X-Amz-Content-Sha256": "",
|
|
|
|
"Content-Encoding": "",
|
|
|
|
},
|
|
|
|
expected: map[string]string{
|
|
|
|
"Authorization": "AWS4-HMAC-SHA256 <cred_string>",
|
|
|
|
"X-Amz-Content-Sha256": "",
|
|
|
|
"Content-Encoding": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"x-amz-object-lock-mode": "governance",
|
|
|
|
},
|
|
|
|
expected: map[string]string{
|
|
|
|
"x-amz-object-lock-mode": "governance",
|
|
|
|
},
|
|
|
|
filterRetention: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"x-amz-object-lock-mode": "governance",
|
|
|
|
"x-amz-object-lock-retain-until-date": "2020-02-01",
|
|
|
|
},
|
|
|
|
expected: map[string]string{},
|
|
|
|
filterRetention: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"x-amz-object-lock-legal-hold": "off",
|
|
|
|
},
|
|
|
|
expected: map[string]string{},
|
|
|
|
filterLegalHold: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"x-amz-object-lock-legal-hold": "on",
|
|
|
|
},
|
|
|
|
expected: map[string]string{"x-amz-object-lock-legal-hold": "on"},
|
|
|
|
filterLegalHold: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"x-amz-object-lock-legal-hold": "on",
|
|
|
|
"x-amz-object-lock-mode": "governance",
|
|
|
|
"x-amz-object-lock-retain-until-date": "2020-02-01",
|
|
|
|
},
|
|
|
|
expected: map[string]string{},
|
|
|
|
filterRetention: true,
|
|
|
|
filterLegalHold: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
metadata: map[string]string{
|
|
|
|
"x-amz-object-lock-legal-hold": "on",
|
|
|
|
"x-amz-object-lock-mode": "governance",
|
|
|
|
"x-amz-object-lock-retain-until-date": "2020-02-01",
|
|
|
|
},
|
2022-01-02 12:15:06 -05:00
|
|
|
expected: map[string]string{
|
|
|
|
"x-amz-object-lock-legal-hold": "on",
|
2020-01-16 18:41:56 -05:00
|
|
|
"x-amz-object-lock-mode": "governance",
|
2022-01-02 12:15:06 -05:00
|
|
|
"x-amz-object-lock-retain-until-date": "2020-02-01",
|
|
|
|
},
|
2020-01-16 18:41:56 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
o := FilterObjectLockMetadata(tt.metadata, tt.filterRetention, tt.filterLegalHold)
|
|
|
|
if !reflect.DeepEqual(o, tt.metadata) {
|
|
|
|
t.Fatalf("Case %d expected %v, got %v", i, tt.metadata, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|