From 459fe4075828eea6cd4740b670e487647560ff78 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sat, 17 Jul 2021 15:49:24 +0300 Subject: [PATCH] [#684] neofs-adm: add tests for `generate-alphabet` Signed-off-by: Evgenii Stratonikov --- .../internal/modules/morph/generate_test.go | 85 +++++++++++++++++++ go.mod | 1 + 2 files changed, 86 insertions(+) create mode 100644 cmd/neofs-adm/internal/modules/morph/generate_test.go diff --git a/cmd/neofs-adm/internal/modules/morph/generate_test.go b/cmd/neofs-adm/internal/modules/morph/generate_test.go new file mode 100644 index 000000000..dbfcda2cd --- /dev/null +++ b/cmd/neofs-adm/internal/modules/morph/generate_test.go @@ -0,0 +1,85 @@ +package morph + +import ( + "bytes" + "io/ioutil" + "math/rand" + "os" + "path" + "strconv" + "testing" + + "github.com/nspcc-dev/neo-go/cli/input" + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neo-go/pkg/wallet" + "github.com/nspcc-dev/neofs-node/pkg/innerring" + "github.com/spf13/viper" + "github.com/stretchr/testify/require" + "golang.org/x/term" +) + +func TestGenerateAlphabet(t *testing.T) { + const size = 4 + + walletDir := newTempDir(t) + buf := setupTestTerminal(t) + + cmd := generateAlphabetCmd + v := viper.GetViper() + + t.Run("no password provided", func(t *testing.T) { + buf.Reset() + v.Set(alphabetWalletsFlag, walletDir) + require.NoError(t, cmd.Flags().Set(alphabetSizeFlag, "1")) + require.Error(t, generateAlphabetCreds(cmd, nil)) + }) + t.Run("missing directory", func(t *testing.T) { + buf.Reset() + dir := path.Join(os.TempDir(), "notexist."+strconv.FormatUint(rand.Uint64(), 10)) + v.Set(alphabetWalletsFlag, dir) + require.NoError(t, cmd.Flags().Set(alphabetSizeFlag, "1")) + buf.WriteString("pass\r") + require.Error(t, generateAlphabetCreds(cmd, nil)) + }) + + buf.Reset() + v.Set(alphabetWalletsFlag, walletDir) + require.NoError(t, cmd.Flags().Set(alphabetSizeFlag, strconv.FormatUint(size, 10))) + for i := uint64(0); i < size; i++ { + buf.WriteString(strconv.FormatUint(i, 10) + "\r") + } + + require.NoError(t, generateAlphabetCreds(cmd, nil)) + + for i := uint64(0); i < size; i++ { + p := path.Join(walletDir, innerring.GlagoliticLetter(i).String()+".json") + w, err := wallet.NewWalletFromFile(p) + require.NoError(t, err, "wallet doesn't exist") + require.Equal(t, 3, len(w.Accounts), "not all accounts were created") + for _, a := range w.Accounts { + err := a.Decrypt(strconv.FormatUint(i, 10), keys.NEP2ScryptParams()) + require.NoError(t, err, "can't decrypt account") + } + } +} + +func setupTestTerminal(t *testing.T) *bytes.Buffer { + in := bytes.NewBuffer(nil) + input.Terminal = term.NewTerminal(input.ReadWriter{ + Reader: in, + Writer: ioutil.Discard, + }, "") + + t.Cleanup(func() { input.Terminal = nil }) + + return in +} + +func newTempDir(t *testing.T) string { + dir := path.Join(os.TempDir(), "neofs-adm.test."+strconv.FormatUint(rand.Uint64(), 10)) + require.NoError(t, os.Mkdir(dir, os.ModePerm)) + t.Cleanup(func() { + require.NoError(t, os.RemoveAll(dir)) + }) + return dir +} diff --git a/go.mod b/go.mod index 87b820d49..363fd0c20 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( go.etcd.io/bbolt v1.3.5 go.uber.org/atomic v1.8.0 go.uber.org/zap v1.17.0 + golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf google.golang.org/grpc v1.38.0 google.golang.org/protobuf v1.26.0 )