Skip to content

Commit

Permalink
improve revealSecret coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsv committed Nov 17, 2023
1 parent 2bf92d9 commit b001634
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
2 changes: 1 addition & 1 deletion server/blockchain/calypso/calypso.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func (c calypsoCommand) revealSecret(snap store.Snapshot, step execution.Step) e
if !found {
return xerrors.Errorf(
"'%s' was not found among the secrets of the smc (%v)",
name, smcKey)
name, string(smcKey))
}

secret, err := getSecret(snap, name)
Expand Down
77 changes: 74 additions & 3 deletions server/blockchain/calypso/calypso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,12 @@ func TestCommand_ListSecrets(t *testing.T) {

snap := fake.NewSnapshot()

err := cmd.advertiseSmc(snap, makeStep(t, SmcPublicKeyArg, "dummy", RosterArg, "node:12345"))
err := cmd.advertiseSmc(snap,
makeStep(t, SmcPublicKeyArg, "dummy", RosterArg, "node:12345"))
require.NoError(t, err)

err = cmd.advertiseSmc(snap, makeStep(t, SmcPublicKeyArg, "other", RosterArg, "node:32145"))
err = cmd.advertiseSmc(snap,
makeStep(t, SmcPublicKeyArg, "other", RosterArg, "node:32145"))
require.NoError(t, err)

err = cmd.createSecret(snap,
Expand All @@ -411,7 +413,8 @@ func TestCommand_ListSecrets(t *testing.T) {
require.Equal(t, 1, len(contract.secrets["other"]))

// Act
err = cmd.listSecrets(snap, makeStep(t, SmcPublicKeyArg, "dummy"))
err = cmd.listSecrets(snap,
makeStep(t, SmcPublicKeyArg, "dummy"))

// Assert
require.NoError(t, err)
Expand Down Expand Up @@ -541,6 +544,74 @@ func TestCommand_RevealSecret_Succeeds(t *testing.T) {
require.Equal(t, logs[0], token)
}

func TestCommand_RevealSecretFails(t *testing.T) {
contract := NewContract(fakeAccess{})
cmd := calypsoCommand{
Contract: &contract,
}

snap := fake.NewSnapshot()

const (
smcKey = "my_smc_key"
secretName = "my_secret"
secretValue = "my_value"
pubKey = "my_pubkey"
)

err := cmd.advertiseSmc(snap,
makeStep(t,
SmcPublicKeyArg, smcKey,
RosterArg, "node:12345"))
require.NoError(t, err)

err = cmd.createSecret(snap,
makeStep(t,
SmcPublicKeyArg, smcKey,
SecretNameArg, secretName,
SecretArg, secretValue))
require.NoError(t, err)

// Verify pre-conditions
smcSecrets, found := contract.secrets[smcKey]
require.True(t, found)
require.Equal(t, 1, len(smcSecrets))

_, found = smcSecrets[secretName]
require.True(t, found)

err = cmd.revealSecret(snap, makeStep(t,
SmcPublicKeyArg, "",
SecretNameArg, secretName,
PubKeyArg, pubKey))
require.EqualError(t, err, "'calypso:smc_key' not found in tx arg")

err = cmd.revealSecret(snap, makeStep(t,
SmcPublicKeyArg, smcKey,
SecretNameArg, "",
PubKeyArg, pubKey))
require.EqualError(t, err, "'calypso:secret_name' not found in tx arg")

err = cmd.revealSecret(snap, makeStep(t,
SmcPublicKeyArg, smcKey,
SecretNameArg, secretName,
PubKeyArg, ""))
require.EqualError(t, err, "'calypso:pub_key' not found in tx arg")

err = cmd.revealSecret(snap, makeStep(t,
SmcPublicKeyArg, "another_smc_key",
SecretNameArg, secretName,
PubKeyArg, pubKey))
require.EqualError(t, err, "'another_smc_key' was not found among the SMCs")

err = cmd.revealSecret(snap, makeStep(t,
SmcPublicKeyArg, smcKey,
SecretNameArg, "another_name",
PubKeyArg, pubKey))
require.EqualError(t, err,
"'another_name' was not found among the secrets of the smc (my_smc_key)")
}

func TestCommand_ListAuditLogs_Succeeds(t *testing.T) {

// Arrange
Expand Down

0 comments on commit b001634

Please sign in to comment.