2024-01-17 14:49:03 +00:00
|
|
|
package chain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestID(t *testing.T) {
|
|
|
|
key, err := keys.NewPrivateKeyFromWIF("L5eVx6HcHaFpQpvjQ3fy29uKDZ8rQ34bfMVx4XfZMm52EqafpNMg") // s3-gw key
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
chain1 := &Chain{ID: ID(key.PublicKey().GetScriptHash().BytesBE())}
|
|
|
|
data := chain1.Bytes()
|
|
|
|
|
|
|
|
var chain2 Chain
|
|
|
|
require.NoError(t, chain2.DecodeBytes(data))
|
|
|
|
|
|
|
|
require.Equal(t, chain1.ID, chain2.ID)
|
|
|
|
|
|
|
|
data, err = chain1.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, chain2.UnmarshalJSON(data))
|
|
|
|
|
|
|
|
require.Equal(t, chain1.ID, chain2.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMatchTypeJson(t *testing.T) {
|
|
|
|
for _, mt := range []MatchType{MatchTypeDenyPriority, MatchTypeFirstMatch, MatchType(100)} {
|
|
|
|
var chain Chain
|
|
|
|
chain.MatchType = mt
|
|
|
|
|
|
|
|
data, err := chain.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
if mt == MatchTypeDenyPriority {
|
2024-01-18 08:01:39 +00:00
|
|
|
require.Equal(t, []byte("{\"ID\":null,\"Rules\":null,\"MatchType\":\"DenyPriority\"}"), data)
|
2024-01-17 14:49:03 +00:00
|
|
|
} else if mt == MatchTypeFirstMatch {
|
2024-01-18 08:01:39 +00:00
|
|
|
require.Equal(t, []byte("{\"ID\":null,\"Rules\":null,\"MatchType\":\"FirstMatch\"}"), data)
|
2024-01-17 14:49:03 +00:00
|
|
|
} else {
|
2024-01-18 08:01:39 +00:00
|
|
|
require.Equal(t, []byte(fmt.Sprintf("{\"ID\":null,\"Rules\":null,\"MatchType\":\"%d\"}", mt)), data)
|
2024-01-17 14:49:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var parsed Chain
|
|
|
|
require.NoError(t, parsed.UnmarshalJSON(data))
|
|
|
|
require.Equal(t, chain, parsed)
|
|
|
|
|
|
|
|
require.Error(t, parsed.UnmarshalJSON([]byte("{\"ID\":\"\",\"Rules\":null,\"MatchType\":\"NotValid\"}")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonEnums(t *testing.T) {
|
|
|
|
chain := Chain{
|
2024-01-18 08:01:39 +00:00
|
|
|
ID: []byte("2cca5ae7-cee8-428d-b45f-567fb1d03f01"), // will be encoded to base64
|
2024-01-17 14:49:03 +00:00
|
|
|
MatchType: MatchTypeFirstMatch,
|
|
|
|
Rules: []Rule{
|
|
|
|
{
|
|
|
|
Status: AccessDenied,
|
|
|
|
Actions: Actions{
|
|
|
|
Names: []string{native.MethodDeleteObject, native.MethodGetContainer},
|
|
|
|
},
|
|
|
|
Resources: Resources{
|
|
|
|
Names: []string{native.ResourceFormatAllObjects},
|
|
|
|
},
|
|
|
|
Condition: []Condition{
|
|
|
|
{
|
2024-05-13 10:54:37 +00:00
|
|
|
Op: CondStringEquals,
|
|
|
|
Kind: KindRequest,
|
|
|
|
Key: native.PropertyKeyActorRole,
|
|
|
|
Value: native.PropertyValueContainerRoleOthers,
|
2024-01-17 14:49:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: QuotaLimitReached,
|
|
|
|
Actions: Actions{
|
|
|
|
Inverted: true,
|
|
|
|
Names: []string{native.MethodPutObject},
|
|
|
|
},
|
|
|
|
Resources: Resources{
|
|
|
|
Names: []string{fmt.Sprintf(native.ResourceFormatRootContainerObjects, "9LPLUFZpEmfidG4n44vi2cjXKXSqWT492tCvLJiJ8W1J")},
|
|
|
|
},
|
|
|
|
Any: true,
|
|
|
|
Condition: []Condition{
|
|
|
|
{
|
2024-05-13 10:54:37 +00:00
|
|
|
Op: CondStringNotLike,
|
|
|
|
Kind: KindResource,
|
|
|
|
Key: native.PropertyKeyObjectType,
|
|
|
|
Value: "regular",
|
2024-01-17 14:49:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: Status(100),
|
|
|
|
Condition: []Condition{
|
|
|
|
{
|
2024-05-13 10:54:37 +00:00
|
|
|
Op: ConditionType(255),
|
|
|
|
Kind: ConditionKindType(128),
|
2024-01-17 14:49:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := chain.MarshalJSON()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var parsed Chain
|
|
|
|
require.NoError(t, parsed.UnmarshalJSON(data))
|
|
|
|
require.Equal(t, chain, parsed)
|
|
|
|
|
|
|
|
expected, err := os.ReadFile("./testdata/test_status_json.json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, parsed.UnmarshalJSON(expected))
|
|
|
|
require.Equal(t, chain, parsed)
|
|
|
|
}
|