Skip to content

Commit

Permalink
Add unit tests for multisig
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Apr 1, 2024
1 parent aadd296 commit ac7d5e2
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
7 changes: 5 additions & 2 deletions tm2/pkg/crypto/keys/client/add_multisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
"github.com/gnolang/gno/tm2/pkg/crypto/multisig"
)

var errOverwriteAborted = errors.New("overwrite aborted")
var (
errOverwriteAborted = errors.New("overwrite aborted")
errUnableToVerifyMultisig = errors.New("unable to verify multisig threshold")
)

type AddMultisigCfg struct {
RootCfg *AddCfg
Expand Down Expand Up @@ -75,7 +78,7 @@ func execAddMultisig(cfg *AddMultisigCfg, args []string, io commands.IO) error {
cfg.MultisigThreshold,
len(cfg.Multisig),
); err != nil {
return fmt.Errorf("unable to verify multisig threshold")
return errUnableToVerifyMultisig
}

name := args[0]
Expand Down
121 changes: 121 additions & 0 deletions tm2/pkg/crypto/keys/client/add_multisig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package client

import (
"context"
"strings"
"testing"
"time"

"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/crypto/keys"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAdd_Multisig(t *testing.T) {
t.Parallel()

t.Run("invalid multisig threshold", func(t *testing.T) {
t.Parallel()

var (
kbHome = t.TempDir()
baseOptions = BaseOptions{
InsecurePasswordStdin: true,
Home: kbHome,
}

keyName = "key-name"
)

ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()

io := commands.NewTestIO()
io.SetIn(strings.NewReader("test1234\ntest1234\n"))

// Create the command
cmd := NewRootCmdWithBaseConfig(io, baseOptions)

args := []string{
"add",
"multisig",
"--insecure-password-stdin",
"--home",
kbHome,
"--multisig",
"example",
"--threshold",
"2",
keyName,
}

require.ErrorIs(t, cmd.ParseAndRun(ctx, args), errUnableToVerifyMultisig)
})

t.Run("valid multisig reference added", func(t *testing.T) {
t.Parallel()

var (
kbHome = t.TempDir()
baseOptions = BaseOptions{
InsecurePasswordStdin: true,
Home: kbHome,
}
mnemonic = generateTestMnemonic(t)

keyNames = []string{
"key-1",
"key-2",
}
)

ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()

io := commands.NewTestIO()
io.SetIn(strings.NewReader("y\ntest1234\ntest1234\n"))

// Create the command
cmd := NewRootCmdWithBaseConfig(io, baseOptions)

args := []string{
"add",
"multisig",
"--insecure-password-stdin",
"--home",
kbHome,
"--multisig",
keyNames[0],
"--multisig",
keyNames[1],
keyNames[0],
}

// Prepare the multisig keys
kb, err := keys.NewKeyBaseFromDir(kbHome)
require.NoError(t, err)

for index, keyName := range keyNames {
_, err = kb.CreateAccount(
keyName,
mnemonic,
"",
"123",
0,
uint32(index),
)

require.NoError(t, err)
}

require.NoError(t, cmd.ParseAndRun(ctx, args))

// Verify the key is multisig
original, err := kb.GetByName(keyNames[0])
require.NoError(t, err)
require.NotNil(t, original)

assert.Equal(t, original.GetType(), keys.TypeMulti)
})
}

0 comments on commit ac7d5e2

Please sign in to comment.