[#31] morph: Fix container resource name transformer

Signed-off-by: Airat Arifullin <aarifullin@yadro.com>
This commit is contained in:
aarifullin 2023-12-12 17:29:34 +03:00
parent 8c673ee4f4
commit a7bb0e6b8b
2 changed files with 96 additions and 1 deletions

View file

@ -1,6 +1,7 @@
package policy
import (
"crypto/sha256"
"errors"
"fmt"
"math/big"
@ -49,7 +50,8 @@ func transformNameIfContainer(target engine.Target) (name string) {
if target.Type == engine.Container {
// Container name can be too long and, thus, cannot be
// used as a key name for policy-contract storage.
name = base58.FastBase58Encoding([]byte(target.Name))
encoded := sha256.Sum224([]byte(target.Name))
name = base58.FastBase58Encoding(encoded[:])
}
return
}

View file

@ -0,0 +1,93 @@
package policy
import (
"fmt"
"testing"
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
"git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
"github.com/stretchr/testify/require"
)
func TestTransformNameIfContainer(t *testing.T) {
for _, test := range []struct {
Name string
Target engine.Target
}{
{
Name: "check namespace",
Target: engine.NamespaceTarget("namespace1"),
},
{
Name: "check ResourceFormatNamespaceObjects",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatNamespaceObjects,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatNamespaceContainerObjects",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatNamespaceContainerObjects,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "AeZa5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatNamespaceContainerObject",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatNamespaceContainerObject,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "AeZa5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "LxGy5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatRootObjects",
Target: engine.ContainerTarget(native.ResourceFormatRootObjects),
},
{
Name: "check ResourceFormatRootContainerObjects",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatRootContainerObjects,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatRootContainerObject",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatRootContainerObject,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "AeZa5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatAllObjects",
Target: engine.ContainerTarget(native.ResourceFormatAllObjects),
},
{
Name: "check ResourceFormatNamespaceContainer",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatNamespaceContainer,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R", "AeZa5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatNamespaceContainers",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatNamespaceContainers,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatRootContainer",
Target: engine.ContainerTarget(
fmt.Sprintf(native.ResourceFormatRootContainer,
"BzQw5HH3feoxFDD5tCT87Y1726qzgLfxEE7wgtoRzB3R")),
},
{
Name: "check ResourceFormatRootContainers",
Target: engine.ContainerTarget(native.ResourceFormatRootContainers),
},
{
Name: "check ResourceFormatAllContainers",
Target: engine.ContainerTarget(native.ResourceFormatAllContainers),
},
} {
t.Run(test.Target.Name, func(t *testing.T) {
res := transformNameIfContainer(test.Target)
const contractStorageKeyLength = 64
const entityTypeLength = 1 // 'n', 'c' or 'i'
require.Less(t, len(res), contractStorageKeyLength-entityTypeLength)
})
}
}