permissions: add unit tests

This commit is contained in:
Trung Nguyen 2019-08-19 09:39:18 -04:00
parent 8212be30e7
commit 2b70d2bfbf
No known key found for this signature in database
GPG Key ID: 4636434ED9505EB7
3 changed files with 80 additions and 15 deletions

View File

@ -132,7 +132,7 @@ var (
//
// This configuration is intentionally not using keyed fields to force anyone
// adding flags to the config to also have to set these fields.
AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, nil, false, 32, 32}
AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, nil, false, 32, 50}
// AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
// and accepted by the Ethereum core developers into the Clique consensus.

View File

@ -19,7 +19,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
@ -56,7 +55,7 @@ type PermissionLocalConfig struct {
type PermissionCtrl struct {
node *node.Node
ethClnt *ethclient.Client
ethClnt bind.ContractBackend
eth *eth.Ethereum
key *ecdsa.PrivateKey
dataDir string

View File

@ -4,34 +4,100 @@ import (
"math/big"
"testing"
"github.com/stretchr/testify/assert"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
permission "github.com/ethereum/go-ethereum/permission/bind"
"github.com/stretchr/testify/assert"
pbind "github.com/ethereum/go-ethereum/permission/bind"
)
func TestPermissionCtrl_InitializeService(t *testing.T) {
key, _ := crypto.GenerateKey()
senderOpts := bind.NewKeyedTransactor(key)
guardianKey, _ := crypto.GenerateKey()
guardianAddress := crypto.PubkeyToAddress(guardianKey.PublicKey)
guardianTransactor := bind.NewKeyedTransactor(guardianKey)
genesisAlloc := map[common.Address]core.GenesisAccount{
senderOpts.From: {
guardianAddress: {
Balance: big.NewInt(100000000000000),
},
}
sb := backends.NewSimulatedBackend(genesisAlloc, 100000000000000)
sb := backends.NewSimulatedBackend(genesisAlloc, 10000000)
permUpgrAddress, _, permUpgrInstance, err := pbind.DeployPermUpgr(guardianTransactor, sb, guardianAddress)
if err != nil {
t.Fatal(err)
}
permInterfaceAddress, _, _, err := pbind.DeployPermInterface(guardianTransactor, sb, permUpgrAddress)
if err != nil {
t.Fatal(err)
}
nodeManagerAddress, _, _, err := pbind.DeployNodeManager(guardianTransactor, sb, permUpgrAddress)
if err != nil {
t.Fatal(err)
}
roleManagerAddress, _, _, err := pbind.DeployRoleManager(guardianTransactor, sb, permUpgrAddress)
if err != nil {
t.Fatal(err)
}
accountManagerAddress, _, _, err := pbind.DeployAcctManager(guardianTransactor, sb, permUpgrAddress)
if err != nil {
t.Fatal(err)
}
orgManagerAddress, _, _, err := pbind.DeployOrgManager(guardianTransactor, sb, permUpgrAddress)
if err != nil {
t.Fatal(err)
}
voterManagerAddress, _, _, err := pbind.DeployVoterManager(guardianTransactor, sb, permUpgrAddress)
if err != nil {
t.Fatal(err)
}
permImplAddress, _, _, err := pbind.DeployPermImpl(guardianTransactor, sb, permUpgrAddress, orgManagerAddress, roleManagerAddress, accountManagerAddress, voterManagerAddress, nodeManagerAddress)
if err != nil {
t.Fatal(err)
}
// call init
if _, err := permUpgrInstance.Init(guardianTransactor, permInterfaceAddress, permImplAddress); err != nil {
t.Fatal(err)
}
permUpgrAddress, _, _, err := permission.DeployPermUpgr(senderOpts, sb, senderOpts.From)
sNode, err := node.New(&node.Config{
P2P: p2p.Config{
PrivateKey: guardianKey,
},
})
if err != nil {
t.Fatal(err)
}
testObject, err := NewQuorumPermissionCtrl(sNode, &types.PermissionConfig{
UpgrdAddress: permUpgrAddress,
InterfAddress: permInterfaceAddress,
ImplAddress: permImplAddress,
NodeAddress: nodeManagerAddress,
AccountAddress: accountManagerAddress,
RoleAddress: roleManagerAddress,
VoterAddress: voterManagerAddress,
OrgAddress: orgManagerAddress,
NwAdminOrg: "NETWORK_ADMIN",
NwAdminRole: "NETWORK_ADMIN_ROLE",
OrgAdminRole: "ORG_ADMIN_ROLE",
Accounts: []common.Address{
guardianAddress,
},
SubOrgDepth: *big.NewInt(3),
SubOrgBreadth: *big.NewInt(3),
})
if err != nil {
t.Fatal(err)
}
p, err := permission.NewPermUpgr(permUpgrAddress, sb)
if err != nil {
t.Fatal(err)
}
assert.NotNil(t, p)
err = testObject.InitializeService()
assert.NoError(t, err)
}