From e70f808dc3ea64c5e00392e7044418ed87e2d653 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Tue, 25 Apr 2023 16:34:15 +0300 Subject: [PATCH] [#280] ir: Add parser unit tests Signed-off-by: Dmitrii Stepanov --- pkg/innerring/contracts.go | 10 +- pkg/innerring/contracts_test.go | 222 ++++++++++++++++++++++++++++++++ 2 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 pkg/innerring/contracts_test.go diff --git a/pkg/innerring/contracts.go b/pkg/innerring/contracts.go index 85a4bf946..32e817787 100644 --- a/pkg/innerring/contracts.go +++ b/pkg/innerring/contracts.go @@ -22,7 +22,7 @@ type contracts struct { alphabet alphabetContracts // in morph } -func parseContracts(cfg *viper.Viper, morph *client.Client, withoutMainNet, withoutMainNotary, withoutSideNotary bool) (*contracts, error) { +func parseContracts(cfg *viper.Viper, morph nnsResolver, withoutMainNet, withoutMainNotary, withoutSideNotary bool) (*contracts, error) { var ( result = new(contracts) err error @@ -76,7 +76,7 @@ func parseContracts(cfg *viper.Viper, morph *client.Client, withoutMainNet, with return result, nil } -func parseAlphabetContracts(cfg *viper.Viper, morph *client.Client) (alphabetContracts, error) { +func parseAlphabetContracts(cfg *viper.Viper, morph nnsResolver) (alphabetContracts, error) { num := GlagoliticLetter(cfg.GetUint("contracts.alphabet.amount")) alpha := newAlphabetContracts() @@ -115,7 +115,7 @@ func parseAlphabetContracts(cfg *viper.Viper, morph *client.Client) (alphabetCon return alpha, nil } -func parseContract(cfg *viper.Viper, morph *client.Client, cfgName, nnsName string) (res util.Uint160, err error) { +func parseContract(cfg *viper.Viper, morph nnsResolver, cfgName, nnsName string) (res util.Uint160, err error) { contractStr := cfg.GetString(cfgName) if len(contractStr) == 0 { return morph.NNSContractAddress(nnsName) @@ -123,3 +123,7 @@ func parseContract(cfg *viper.Viper, morph *client.Client, cfgName, nnsName stri return util.Uint160DecodeStringLE(contractStr) } + +type nnsResolver interface { + NNSContractAddress(name string) (sh util.Uint160, err error) +} diff --git a/pkg/innerring/contracts_test.go b/pkg/innerring/contracts_test.go new file mode 100644 index 000000000..2d6e69c05 --- /dev/null +++ b/pkg/innerring/contracts_test.go @@ -0,0 +1,222 @@ +package innerring + +import ( + "strings" + "testing" + + "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client" + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/spf13/viper" + "github.com/stretchr/testify/require" +) + +func TestParseContractsSuccess(t *testing.T) { + t.Parallel() + file := strings.NewReader(` +contracts: + frostfs: ee3dee6d05dc79c24a5b8f6985e10d68b7cacc62 + processing: 597f5894867113a41e192801709c02497f611de8 + audit: 219e37aed2180b87e7fe945dbf97d67125e8d73f + balance: d2aa48d14b17b11bc4c68205027884a96706dd16 + container: ed4a7a66fe3f9bfe50f214b49be8f215a3c886b6 + frostfsid: 9f5866decbc751a099e74c7c7bc89f609201755a + netmap: 83c600c81d47a1b1b7cf58eb49ae7ee7240dc742 + proxy: abc8794bb40a21f2db5f21ae62741eb46c8cad1c + alphabet: + amount: 2 + az: c1d211fceeb4b1dc76b8e4054d11fdf887e418ea + buky: e2ba789320899658b100f331bdebb74474757920 +`) + + v := viper.New() + v.SetConfigType("yaml") + err := v.ReadConfig(file) + require.NoError(t, err, "read config file failed") + + t.Run("all enabled", func(t *testing.T) { + t.Parallel() + c, err := parseContracts(v, nil, false, false, false) + require.NoError(t, err, "failed to parse contracts") + + frostfsExp, _ := util.Uint160DecodeStringLE("ee3dee6d05dc79c24a5b8f6985e10d68b7cacc62") + require.Equal(t, frostfsExp, c.frostfs, "invalid frostfs") + + processingExp, _ := util.Uint160DecodeStringLE("597f5894867113a41e192801709c02497f611de8") + require.Equal(t, processingExp, c.processing, "invalid processing") + + auditExp, _ := util.Uint160DecodeStringLE("219e37aed2180b87e7fe945dbf97d67125e8d73f") + require.Equal(t, auditExp, c.audit, "invalid audit") + + balanceExp, _ := util.Uint160DecodeStringLE("d2aa48d14b17b11bc4c68205027884a96706dd16") + require.Equal(t, balanceExp, c.balance, "invalid balance") + + containerExp, _ := util.Uint160DecodeStringLE("ed4a7a66fe3f9bfe50f214b49be8f215a3c886b6") + require.Equal(t, containerExp, c.container, "invalid container") + + frostfsIDExp, _ := util.Uint160DecodeStringLE("9f5866decbc751a099e74c7c7bc89f609201755a") + require.Equal(t, frostfsIDExp, c.frostfsID, "invalid frostfsID") + + netmapIDExp, _ := util.Uint160DecodeStringLE("83c600c81d47a1b1b7cf58eb49ae7ee7240dc742") + require.Equal(t, netmapIDExp, c.netmap, "invalid netmap") + + proxyExp, _ := util.Uint160DecodeStringLE("abc8794bb40a21f2db5f21ae62741eb46c8cad1c") + require.Equal(t, proxyExp, c.proxy, "invalid proxy") + + require.Equal(t, 2, len(c.alphabet), "invalid alphabet contracts length") + + azExp, _ := util.Uint160DecodeStringLE("c1d211fceeb4b1dc76b8e4054d11fdf887e418ea") + require.Equal(t, azExp, c.alphabet[az], "invalid az") + + bukyExp, _ := util.Uint160DecodeStringLE("e2ba789320899658b100f331bdebb74474757920") + require.Equal(t, bukyExp, c.alphabet[buky], "invalid buky") + }) + + t.Run("all disabled", func(t *testing.T) { + t.Parallel() + c, err := parseContracts(v, nil, true, true, true) + require.NoError(t, err, "failed to parse contracts") + + require.Equal(t, util.Uint160{}, c.frostfs, "invalid frostfs") + + require.Equal(t, util.Uint160{}, c.processing, "invalid processing") + + auditExp, _ := util.Uint160DecodeStringLE("219e37aed2180b87e7fe945dbf97d67125e8d73f") + require.Equal(t, auditExp, c.audit, "invalid audit") + + balanceExp, _ := util.Uint160DecodeStringLE("d2aa48d14b17b11bc4c68205027884a96706dd16") + require.Equal(t, balanceExp, c.balance, "invalid balance") + + containerExp, _ := util.Uint160DecodeStringLE("ed4a7a66fe3f9bfe50f214b49be8f215a3c886b6") + require.Equal(t, containerExp, c.container, "invalid container") + + frostfsIDExp, _ := util.Uint160DecodeStringLE("9f5866decbc751a099e74c7c7bc89f609201755a") + require.Equal(t, frostfsIDExp, c.frostfsID, "invalid frostfsID") + + netmapIDExp, _ := util.Uint160DecodeStringLE("83c600c81d47a1b1b7cf58eb49ae7ee7240dc742") + require.Equal(t, netmapIDExp, c.netmap, "invalid netmap") + + require.Equal(t, util.Uint160{}, c.proxy, "invalid proxy") + + require.Equal(t, 2, len(c.alphabet), "invalid alphabet contracts length") + + azExp, _ := util.Uint160DecodeStringLE("c1d211fceeb4b1dc76b8e4054d11fdf887e418ea") + require.Equal(t, azExp, c.alphabet[az], "invalid az") + + bukyExp, _ := util.Uint160DecodeStringLE("e2ba789320899658b100f331bdebb74474757920") + require.Equal(t, bukyExp, c.alphabet[buky], "invalid buky") + }) + + t.Run("main notary & side notary disabled", func(t *testing.T) { + t.Parallel() + c, err := parseContracts(v, nil, false, true, true) + require.NoError(t, err, "failed to parse contracts") + + frostfsExp, _ := util.Uint160DecodeStringLE("ee3dee6d05dc79c24a5b8f6985e10d68b7cacc62") + require.Equal(t, frostfsExp, c.frostfs, "invalid frostfs") + + require.Equal(t, util.Uint160{}, c.processing, "invalid processing") + + auditExp, _ := util.Uint160DecodeStringLE("219e37aed2180b87e7fe945dbf97d67125e8d73f") + require.Equal(t, auditExp, c.audit, "invalid audit") + + balanceExp, _ := util.Uint160DecodeStringLE("d2aa48d14b17b11bc4c68205027884a96706dd16") + require.Equal(t, balanceExp, c.balance, "invalid balance") + + containerExp, _ := util.Uint160DecodeStringLE("ed4a7a66fe3f9bfe50f214b49be8f215a3c886b6") + require.Equal(t, containerExp, c.container, "invalid container") + + frostfsIDExp, _ := util.Uint160DecodeStringLE("9f5866decbc751a099e74c7c7bc89f609201755a") + require.Equal(t, frostfsIDExp, c.frostfsID, "invalid frostfsID") + + netmapIDExp, _ := util.Uint160DecodeStringLE("83c600c81d47a1b1b7cf58eb49ae7ee7240dc742") + require.Equal(t, netmapIDExp, c.netmap, "invalid netmap") + + require.Equal(t, util.Uint160{}, c.proxy, "invalid proxy") + + require.Equal(t, 2, len(c.alphabet), "invalid alphabet contracts length") + + azExp, _ := util.Uint160DecodeStringLE("c1d211fceeb4b1dc76b8e4054d11fdf887e418ea") + require.Equal(t, azExp, c.alphabet[az], "invalid az") + + bukyExp, _ := util.Uint160DecodeStringLE("e2ba789320899658b100f331bdebb74474757920") + require.Equal(t, bukyExp, c.alphabet[buky], "invalid buky") + }) +} + +func TestParseContractsInvalid(t *testing.T) { + t.Parallel() + t.Run("invalid audit contract", func(t *testing.T) { + t.Parallel() + file := strings.NewReader(` +contracts: + frostfs: invalid_data + processing: 597f5894867113a41e192801709c02497f611de8 + audit: 219e37aed2180b87e7fe945dbf97d67125e8d73f + balance: d2aa48d14b17b11bc4c68205027884a96706dd16 + container: ed4a7a66fe3f9bfe50f214b49be8f215a3c886b6 + frostfsid: 9f5866decbc751a099e74c7c7bc89f609201755a + netmap: 83c600c81d47a1b1b7cf58eb49ae7ee7240dc742 + proxy: abc8794bb40a21f2db5f21ae62741eb46c8cad1c + alphabet: + amount: 2 + az: c1d211fceeb4b1dc76b8e4054d11fdf887e418ea + buky: e2ba789320899658b100f331bdebb74474757920 +`) + + v := viper.New() + v.SetConfigType("yaml") + err := v.ReadConfig(file) + require.NoError(t, err, "read config file failed") + + _, err = parseContracts(v, nil, false, false, false) + require.Error(t, err, "unexpected success") + }) + + t.Run("invalid alphabet count", func(t *testing.T) { + t.Parallel() + file := strings.NewReader(` +contracts: + frostfs: ee3dee6d05dc79c24a5b8f6985e10d68b7cacc62 + processing: 597f5894867113a41e192801709c02497f611de8 + audit: 219e37aed2180b87e7fe945dbf97d67125e8d73f + balance: d2aa48d14b17b11bc4c68205027884a96706dd16 + container: ed4a7a66fe3f9bfe50f214b49be8f215a3c886b6 + frostfsid: 9f5866decbc751a099e74c7c7bc89f609201755a + netmap: 83c600c81d47a1b1b7cf58eb49ae7ee7240dc742 + proxy: abc8794bb40a21f2db5f21ae62741eb46c8cad1c + alphabet: + amount: 3 + az: c1d211fceeb4b1dc76b8e4054d11fdf887e418ea + buky: e2ba789320899658b100f331bdebb74474757920 +`) + + v := viper.New() + v.SetConfigType("yaml") + err := v.ReadConfig(file) + require.NoError(t, err, "read config file failed") + + azExp, _ := util.Uint160DecodeStringLE("c1d211fceeb4b1dc76b8e4054d11fdf887e418ea") + bukyExp, _ := util.Uint160DecodeStringLE("e2ba789320899658b100f331bdebb74474757920") + + morph := &testParserMorph{ + values: map[string]util.Uint160{ + "az": azExp, + "buky": bukyExp, + }, + } + + _, err = parseContracts(v, morph, false, false, false) + require.ErrorContains(t, err, "could not read all contracts: required 3, read 2", "unexpected success") + }) +} + +type testParserMorph struct { + values map[string]util.Uint160 +} + +func (m *testParserMorph) NNSContractAddress(name string) (sh util.Uint160, err error) { + if value, found := m.values[name]; found { + return value, nil + } + return util.Uint160{}, client.ErrNNSRecordNotFound +}