2024-02-02 07:15:55 +00:00
|
|
|
package generate
|
2021-07-17 12:49:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-03-18 11:12:58 +00:00
|
|
|
"io"
|
2021-07-17 12:49:24 +00:00
|
|
|
"math/rand"
|
|
|
|
"os"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2021-07-17 12:49:24 +00:00
|
|
|
"strconv"
|
2023-05-05 13:45:43 +00:00
|
|
|
"sync"
|
2021-07-17 12:49:24 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-02-02 12:04:48 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/commonflags"
|
2024-01-31 14:26:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/util"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring"
|
2021-07-17 12:49:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/input"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-10-20 15:12:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2021-07-17 12:49:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerateAlphabet(t *testing.T) {
|
|
|
|
const size = 4
|
|
|
|
|
2022-10-24 12:45:13 +00:00
|
|
|
walletDir := t.TempDir()
|
2021-07-17 12:49:24 +00:00
|
|
|
buf := setupTestTerminal(t)
|
|
|
|
|
2024-02-02 07:15:55 +00:00
|
|
|
cmd := GenerateAlphabetCmd
|
2021-07-17 12:49:24 +00:00
|
|
|
v := viper.GetViper()
|
|
|
|
|
2021-07-17 12:51:43 +00:00
|
|
|
t.Run("zero size", func(t *testing.T) {
|
|
|
|
buf.Reset()
|
2024-02-02 12:04:48 +00:00
|
|
|
v.Set(commonflags.AlphabetWalletsFlag, walletDir)
|
|
|
|
require.NoError(t, cmd.Flags().Set(commonflags.AlphabetSizeFlag, "0"))
|
2021-07-17 12:51:43 +00:00
|
|
|
buf.WriteString("pass\r")
|
2024-02-02 07:15:55 +00:00
|
|
|
require.Error(t, AlphabetCreds(cmd, nil))
|
2021-07-17 12:51:43 +00:00
|
|
|
})
|
2021-07-17 12:49:24 +00:00
|
|
|
t.Run("no password provided", func(t *testing.T) {
|
|
|
|
buf.Reset()
|
2024-02-02 12:04:48 +00:00
|
|
|
v.Set(commonflags.AlphabetWalletsFlag, walletDir)
|
|
|
|
require.NoError(t, cmd.Flags().Set(commonflags.AlphabetSizeFlag, "1"))
|
2024-02-02 07:15:55 +00:00
|
|
|
require.Error(t, AlphabetCreds(cmd, nil))
|
2021-07-17 12:49:24 +00:00
|
|
|
})
|
|
|
|
t.Run("missing directory", func(t *testing.T) {
|
|
|
|
buf.Reset()
|
2022-02-02 13:28:08 +00:00
|
|
|
dir := filepath.Join(os.TempDir(), "notexist."+strconv.FormatUint(rand.Uint64(), 10))
|
2024-02-02 12:04:48 +00:00
|
|
|
v.Set(commonflags.AlphabetWalletsFlag, dir)
|
|
|
|
require.NoError(t, cmd.Flags().Set(commonflags.AlphabetSizeFlag, "1"))
|
2021-07-17 12:49:24 +00:00
|
|
|
buf.WriteString("pass\r")
|
2024-02-02 07:15:55 +00:00
|
|
|
require.Error(t, AlphabetCreds(cmd, nil))
|
2021-07-17 12:49:24 +00:00
|
|
|
})
|
2021-11-29 12:48:06 +00:00
|
|
|
t.Run("no password for contract group wallet", func(t *testing.T) {
|
|
|
|
buf.Reset()
|
2024-02-02 12:04:48 +00:00
|
|
|
v.Set(commonflags.AlphabetWalletsFlag, walletDir)
|
|
|
|
require.NoError(t, cmd.Flags().Set(commonflags.AlphabetSizeFlag, strconv.FormatUint(size, 10)))
|
2021-11-29 12:48:06 +00:00
|
|
|
for i := uint64(0); i < size; i++ {
|
|
|
|
buf.WriteString(strconv.FormatUint(i, 10) + "\r")
|
|
|
|
}
|
2024-02-02 07:15:55 +00:00
|
|
|
require.Error(t, AlphabetCreds(cmd, nil))
|
2021-11-29 12:48:06 +00:00
|
|
|
})
|
2021-07-17 12:49:24 +00:00
|
|
|
|
2022-10-24 11:38:55 +00:00
|
|
|
buf.Reset()
|
2024-02-02 12:04:48 +00:00
|
|
|
v.Set(commonflags.AlphabetWalletsFlag, walletDir)
|
|
|
|
require.NoError(t, GenerateAlphabetCmd.Flags().Set(commonflags.AlphabetSizeFlag, strconv.FormatUint(size, 10)))
|
2022-10-24 11:38:55 +00:00
|
|
|
for i := uint64(0); i < size; i++ {
|
|
|
|
buf.WriteString(strconv.FormatUint(i, 10) + "\r")
|
|
|
|
}
|
|
|
|
|
2024-02-02 07:15:55 +00:00
|
|
|
buf.WriteString(util.TestContractPassword + "\r")
|
|
|
|
require.NoError(t, AlphabetCreds(GenerateAlphabetCmd, nil))
|
2021-07-17 12:49:24 +00:00
|
|
|
|
2023-05-05 13:45:43 +00:00
|
|
|
var wg sync.WaitGroup
|
2021-07-17 12:49:24 +00:00
|
|
|
for i := uint64(0); i < size; i++ {
|
2023-05-05 13:45:43 +00:00
|
|
|
i := i
|
2023-08-11 09:13:09 +00:00
|
|
|
wg.Add(1)
|
2023-05-05 13:45:43 +00:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
p := filepath.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")
|
|
|
|
switch a.Label {
|
2024-01-31 14:26:26 +00:00
|
|
|
case util.ConsensusAccountName:
|
2023-05-05 13:45:43 +00:00
|
|
|
require.Equal(t, smartcontract.GetDefaultHonestNodeCount(size), len(a.Contract.Parameters))
|
2024-02-01 06:58:12 +00:00
|
|
|
case util.CommitteeAccountName:
|
2023-05-05 13:45:43 +00:00
|
|
|
require.Equal(t, smartcontract.GetMajorityHonestNodeCount(size), len(a.Contract.Parameters))
|
|
|
|
default:
|
2024-02-01 06:58:12 +00:00
|
|
|
require.Equal(t, util.SingleAccountName, a.Label)
|
2023-05-05 13:45:43 +00:00
|
|
|
}
|
2021-07-17 13:00:22 +00:00
|
|
|
}
|
2023-05-05 13:45:43 +00:00
|
|
|
}()
|
2021-07-17 12:49:24 +00:00
|
|
|
}
|
2023-05-05 13:45:43 +00:00
|
|
|
wg.Wait()
|
2021-11-29 12:48:06 +00:00
|
|
|
|
|
|
|
t.Run("check contract group wallet", func(t *testing.T) {
|
2024-02-01 12:21:51 +00:00
|
|
|
p := filepath.Join(walletDir, util.ContractWalletFilename)
|
2021-11-29 12:48:06 +00:00
|
|
|
w, err := wallet.NewWalletFromFile(p)
|
|
|
|
require.NoError(t, err, "contract wallet doesn't exist")
|
|
|
|
require.Equal(t, 1, len(w.Accounts), "contract wallet must have 1 accout")
|
2024-02-02 07:15:55 +00:00
|
|
|
require.NoError(t, w.Accounts[0].Decrypt(util.TestContractPassword, keys.NEP2ScryptParams()))
|
2021-11-29 12:48:06 +00:00
|
|
|
})
|
2021-07-17 12:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupTestTerminal(t *testing.T) *bytes.Buffer {
|
|
|
|
in := bytes.NewBuffer(nil)
|
|
|
|
input.Terminal = term.NewTerminal(input.ReadWriter{
|
|
|
|
Reader: in,
|
2022-03-18 11:12:58 +00:00
|
|
|
Writer: io.Discard,
|
2021-07-17 12:49:24 +00:00
|
|
|
}, "")
|
|
|
|
|
|
|
|
t.Cleanup(func() { input.Terminal = nil })
|
|
|
|
|
|
|
|
return in
|
|
|
|
}
|