frostfs-sdk-go/eacl/filter_test.go
Pavel Pogodaev 6ce73790ea
All checks were successful
DCO / DCO (pull_request) Successful in 38s
Tests and linters / Tests (pull_request) Successful in 1m13s
Tests and linters / Lint (pull_request) Successful in 2m36s
[#276] Merge repo with frostfs-api-go
Signed-off-by: Pavel Pogodaev <p.pogodaev@yadro.com>
2024-10-22 14:05:12 +00:00

101 lines
2.4 KiB
Go

package eacl
import (
"strconv"
"testing"
v2acl "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/acl"
"github.com/stretchr/testify/require"
)
func newObjectFilter(match Match, key, val string) *Filter {
return &Filter{
from: HeaderFromObject,
key: filterKey{
str: key,
},
matcher: match,
value: staticStringer(val),
}
}
func TestFilter(t *testing.T) {
filter := newObjectFilter(MatchStringEqual, "some name", "200")
v2 := filter.ToV2()
require.NotNil(t, v2)
require.Equal(t, v2acl.HeaderTypeObject, v2.GetHeaderType())
require.EqualValues(t, v2acl.MatchTypeStringEqual, v2.GetMatchType())
require.Equal(t, filter.Key(), v2.GetKey())
require.Equal(t, filter.Value(), v2.GetValue())
newFilter := NewFilterFromV2(v2)
require.Equal(t, filter, newFilter)
t.Run("from nil v2 filter", func(t *testing.T) {
require.Equal(t, new(Filter), NewFilterFromV2(nil))
})
}
func TestFilterEncoding(t *testing.T) {
f := newObjectFilter(MatchStringEqual, "key", "value")
t.Run("binary", func(t *testing.T) {
data, err := f.Marshal()
require.NoError(t, err)
f2 := NewFilter()
require.NoError(t, f2.Unmarshal(data))
require.Equal(t, f, f2)
})
t.Run("json", func(t *testing.T) {
data, err := f.MarshalJSON()
require.NoError(t, err)
d2 := NewFilter()
require.NoError(t, d2.UnmarshalJSON(data))
require.Equal(t, f, d2)
})
}
func TestFilter_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *Filter
require.Nil(t, x.ToV2())
})
t.Run("default values", func(t *testing.T) {
filter := NewFilter()
// check initial values
require.Empty(t, filter.Key())
require.Empty(t, filter.Value())
require.Equal(t, HeaderTypeUnknown, filter.From())
require.Equal(t, MatchUnknown, filter.Matcher())
// convert to v2 message
filterV2 := filter.ToV2()
require.Empty(t, filterV2.GetKey())
require.Empty(t, filterV2.GetValue())
require.Equal(t, v2acl.HeaderTypeUnknown, filterV2.GetHeaderType())
require.Equal(t, v2acl.MatchTypeUnknown, filterV2.GetMatchType())
})
t.Run("reserved types", func(t *testing.T) {
r := NewRecord()
for i := filterKeyType(1); i < fKeyObjLast; i++ {
r.addObjectReservedFilter(MatchStringEqual, i, staticStringer(strconv.FormatUint(uint64(i), 16)))
}
for i := range r.filters {
fv2 := r.filters[i].ToV2()
actual := NewFilterFromV2(fv2)
require.Equal(t, actual, &r.filters[i])
}
})
}