Skip to content

Commit

Permalink
fix registration unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexh-scrt committed Mar 28, 2024
1 parent 7f9e620 commit f95fa7f
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 204 deletions.
133 changes: 71 additions & 62 deletions x/registration/internal/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,73 @@
package keeper

// func TestInitGenesisNoMaster(t *testing.T) {
// tempDir, err := os.MkdirTemp("", "wasm")
// require.NoError(t, err)
// defer os.RemoveAll(tempDir)
// ctx, keeper := CreateTestInput(t, false, tempDir, true)

// data := types.GenesisState{
// Registration: nil,
// IoMasterKey: nil,
// NodeExchMasterKey: nil,
// }

// assert.Panics(t, func() { InitGenesis(ctx, keeper, data) }, "Init genesis didn't panic without master certificate")
// }

// func TestInitGenesis(t *testing.T) {
// tempDir, err := os.MkdirTemp("", "wasm")
// require.NoError(t, err)
// defer os.RemoveAll(tempDir)
// ctx, keeper := CreateTestInput(t, false, tempDir, true)

// cert, err := os.ReadFile("../../testdata/attestation_cert_sw")
// require.NoError(t, err)

// key, err := FetchRawPubKeyFromLegacyCert(cert)
// require.NoError(t, err)

// data := types.GenesisState{
// Registration: nil,
// IoMasterKey: &types.MasterKey{Bytes: key},
// NodeExchMasterKey: &types.MasterKey{Bytes: key},
// }

// InitGenesis(ctx, keeper, data)
// }

// func TestExportGenesis(t *testing.T) {
// tempDir, err := os.MkdirTemp("", "wasm")
// require.NoError(t, err)
// defer os.RemoveAll(tempDir)
// ctx, keeper := CreateTestInput(t, false, tempDir, true)

// cert, err := os.ReadFile("../../testdata/attestation_cert_sw")
// require.NoError(t, err)

// key, err := FetchRawPubKeyFromLegacyCert(cert)
// require.NoError(t, err)

// data := types.GenesisState{
// Registration: nil,
// IoMasterKey: &types.MasterKey{Bytes: key},
// NodeExchMasterKey: &types.MasterKey{Bytes: key},
// }

// InitGenesis(ctx, keeper, data)

// data2 := ExportGenesis(ctx, keeper)

// require.Equal(t, string(data.IoMasterKey.Bytes), string(data2.IoMasterKey.Bytes))
// require.Equal(t, string(data.NodeExchMasterKey.Bytes), string(data2.NodeExchMasterKey.Bytes))
// require.Equal(t, data2.Registration, data2.Registration)
// }
import (
"os"
"testing"

"github.com/scrtlabs/SecretNetwork/x/registration/internal/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestInitGenesisNoMaster(t *testing.T) {
tempDir, err := os.MkdirTemp("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keeper := CreateTestInput(t, false, tempDir, true)

data := types.GenesisState{
Registration: nil,
IoMasterKey: nil,
NodeExchMasterKey: nil,
}

assert.Panics(t, func() { InitGenesis(ctx, keeper, data) }, "Init genesis didn't panic without master certificate")
}

func TestInitGenesis(t *testing.T) {
tempDir, err := os.MkdirTemp("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keeper := CreateTestInput(t, false, tempDir, true)

cert, err := os.ReadFile("../../testdata/attestation_cert_sw")
require.NoError(t, err)

key, err := FetchRawPubKeyFromLegacyCert(cert)
require.NoError(t, err)

data := types.GenesisState{
Registration: nil,
IoMasterKey: &types.MasterKey{Bytes: key},
NodeExchMasterKey: &types.MasterKey{Bytes: key},
}

InitGenesis(ctx, keeper, data)
}

func TestExportGenesis(t *testing.T) {
tempDir, err := os.MkdirTemp("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, keeper := CreateTestInput(t, false, tempDir, true)

cert, err := os.ReadFile("../../testdata/attestation_cert_sw")
require.NoError(t, err)

key, err := FetchRawPubKeyFromLegacyCert(cert)
require.NoError(t, err)

data := types.GenesisState{
Registration: nil,
IoMasterKey: &types.MasterKey{Bytes: key},
NodeExchMasterKey: &types.MasterKey{Bytes: key},
}

InitGenesis(ctx, keeper, data)

data2 := ExportGenesis(ctx, keeper)

require.Equal(t, string(data.IoMasterKey.Bytes), string(data2.IoMasterKey.Bytes))
require.Equal(t, string(data.NodeExchMasterKey.Bytes), string(data2.NodeExchMasterKey.Bytes))
require.Equal(t, data2.Registration, data2.Registration)
}
113 changes: 60 additions & 53 deletions x/registration/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package keeper

import (
"os"
"path/filepath"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
eng "github.com/scrtlabs/SecretNetwork/types"
"github.com/scrtlabs/SecretNetwork/x/registration/internal/types"
ra "github.com/scrtlabs/SecretNetwork/x/registration/remote_attestation"
"github.com/stretchr/testify/require"
)

func init() {
Expand All @@ -13,73 +20,73 @@ func init() {
config.Seal()
}

// func TestNewKeeper(t *testing.T) {
// tempDir, err := os.MkdirTemp("", "reg")
// require.NoError(t, err)
// defer os.RemoveAll(tempDir)
// _, regKeeper := CreateTestInput(t, false, tempDir, true)
// require.NotNil(t, regKeeper)
// }
func TestNewKeeper(t *testing.T) {
tempDir, err := os.MkdirTemp("", "reg")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
_, regKeeper := CreateTestInput(t, false, tempDir, true)
require.NotNil(t, regKeeper)
}

// func TestNewKeeper_Node(t *testing.T) {
// tempDir, err := os.MkdirTemp("", "reg")
// require.NoError(t, err)
// defer os.RemoveAll(tempDir)
func TestNewKeeper_Node(t *testing.T) {
tempDir, err := os.MkdirTemp("", "reg")
require.NoError(t, err)
defer os.RemoveAll(tempDir)

// seedPath := filepath.Join(tempDir, types.SecretNodeCfgFolder, types.SecretNodeSeedNewConfig)
seedPath := filepath.Join(tempDir, types.SecretNodeCfgFolder, types.SecretNodeSeedNewConfig)

// err = os.MkdirAll(filepath.Join(tempDir, types.SecretNodeCfgFolder), 0o700)
// require.NoError(t, err)
err = os.MkdirAll(filepath.Join(tempDir, types.SecretNodeCfgFolder), 0o700)
require.NoError(t, err)

// err = os.WriteFile(seedPath, CreateTestSeedConfig(t), 0o700)
// require.NoError(t, err)
err = os.WriteFile(seedPath, CreateTestSeedConfig(t), 0o700)
require.NoError(t, err)

// _, regKeeper := CreateTestInput(t, false, tempDir, false)
// require.NotNil(t, regKeeper)
// }
_, regKeeper := CreateTestInput(t, false, tempDir, false)
require.NotNil(t, regKeeper)
}

// func TestKeeper_RegisterationStore(t *testing.T) {
// tempDir, err := os.MkdirTemp("", "wasm")
// require.NoError(t, err)
// defer os.RemoveAll(tempDir)
// ctx, regKeeper := CreateTestInput(t, false, tempDir, true)
func TestKeeper_RegisterationStore(t *testing.T) {
tempDir, err := os.MkdirTemp("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, regKeeper := CreateTestInput(t, false, tempDir, true)

// cert, err := os.ReadFile("../../testdata/attestation_cert_sw")
// require.NoError(t, err)
cert, err := os.ReadFile("../../testdata/attestation_cert_sw")
require.NoError(t, err)

// regInfo := types.RegistrationNodeInfo{
// Certificate: cert,
// EncryptedSeed: []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
// }
regInfo := types.RegistrationNodeInfo{
Certificate: cert,
EncryptedSeed: []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
}

// regKeeper.SetRegistrationInfo(ctx, regInfo)
regKeeper.SetRegistrationInfo(ctx, regInfo)

// publicKey, err := ra.VerifyRaCert(regInfo.Certificate)
// if err != nil {
// return
// }
publicKey, err := ra.VerifyRaCert(regInfo.Certificate)
if err != nil {
return
}

// regInfo2 := regKeeper.getRegistrationInfo(ctx, publicKey)
regInfo2 := regKeeper.getRegistrationInfo(ctx, publicKey)

// require.Equal(t, regInfo, *regInfo2)
// }
require.Equal(t, regInfo, *regInfo2)
}

// func TestKeeper_RegisterNode(t *testing.T) {
// tempDir, err := os.MkdirTemp("", "wasm")
// require.NoError(t, err)
// defer os.RemoveAll(tempDir)
// ctx, regKeeper := CreateTestInput(t, false, tempDir, true)
func TestKeeper_RegisterNode(t *testing.T) {
tempDir, err := os.MkdirTemp("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, regKeeper := CreateTestInput(t, false, tempDir, true)

// cert, err := os.ReadFile("../../testdata/attestation_cert_sw")
// require.NoError(t, err)
cert, err := os.ReadFile("../../testdata/attestation_cert_sw")
require.NoError(t, err)

// regInfo := types.RegistrationNodeInfo{
// Certificate: cert,
// EncryptedSeed: []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
// }
regInfo := types.RegistrationNodeInfo{
Certificate: cert,
EncryptedSeed: []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
}

// regKeeper.SetRegistrationInfo(ctx, regInfo)
regKeeper.SetRegistrationInfo(ctx, regInfo)

// _, err = regKeeper.RegisterNode(ctx, cert)
// require.NoError(t, err)
// }
_, err = regKeeper.RegisterNode(ctx, cert)
require.NoError(t, err)
}
25 changes: 20 additions & 5 deletions x/registration/internal/keeper/querier_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package keeper

//
////
// import (
// "encoding/hex"
// "encoding/json"
// "os"
// "testing"

// "cosmossdk.io/api/tendermint/abci"
// sdkErrors "github.com/cosmos/cosmos-sdk/types/errors"
// errorsmod "cosmossdk.io/errors"
// "github.com/scrtlabs/SecretNetwork/x/registration/internal/types"
// "github.com/scrtlabs/SecretNetwork/x/registration"
// ra "github.com/scrtlabs/SecretNetwork/x/registration/remote_attestation"
// "github.com/stretchr/testify/require"
// )

// //
// ////

// func TestNewQuerier(t *testing.T) {
// tempDir, err := os.MkdirTemp("", "wasm")
Expand All @@ -13,7 +28,7 @@ package keeper

// nodeIdValid := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

// querier := NewLegacyQuerier(keeper) // TODO: Should test NewQuerier() as well
// querier := NewQuerier(keeper) // TODO: Should test NewQuerier() as well

// cert, err := os.ReadFile("../../testdata/attestation_cert_sw")
// require.NoError(t, err)
Expand Down Expand Up @@ -42,7 +57,7 @@ package keeper
// // smart queries return raw bytes from contract not []types.Model
// // if this is set, then we just compare - (should be json encoded string)
// // if success and expSmartRes is not set, we parse into []types.Model and compare
// expErr *sdkErrors.Error
// expErr *errorsmod.Error
// result string
// }{
// "query malformed node id": {
Expand Down Expand Up @@ -73,7 +88,7 @@ package keeper

// for msg, spec := range specs {
// t.Run(msg, func(t *testing.T) {
// binResult, err := querier(ctx, spec.srcPath, spec.srcReq)
// binResult, err := querier.EncryptedSeed()(ctx, spec.srcPath, spec.srcReq)
// require.True(t, spec.expErr.Is(err), err)

// if spec.result != "" {
Expand Down
Loading

0 comments on commit f95fa7f

Please sign in to comment.