Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests created for the Ethereum Blockchain node providers #277

Merged
merged 10 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ require (
github.com/otiai10/copy v1.7.0
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.12.1-0.20220712161005-5247643f0235
github.com/stretchr/testify v1.8.0
github.com/stretchr/objx v0.5.1 // indirect
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.10.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,9 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
github.com/stretchr/testify v0.0.0-20180303142811-b89eecf5ca5d/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand All @@ -876,6 +879,9 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs=
github.com/subosito/gotenv v1.4.0 h1:yAzM1+SmVcz5R4tXGsNMu1jUl2aOJXoiWUCEwwnGrvs=
Expand Down
177 changes: 177 additions & 0 deletions internal/blockchain/ethereum/besu/besu_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package besu

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

"github.com/hyperledger/firefly-cli/internal/blockchain/ethereum"

"github.com/hyperledger/firefly-cli/pkg/types"
"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/stretchr/testify/assert"
)

func TestNewBesuProvider(t *testing.T) {
var ctx context.Context
testCases := []struct {
Name string
Ctx context.Context
Stack *types.Stack
}{
{
Name: "testcase1",
Ctx: ctx,
Stack: &types.Stack{
Name: "TestBesuProviderWithEthconnect",
Members: []*types.Organization{{OrgName: "Org1"}},
BlockchainProvider: fftypes.FFEnumValue("BlockchainProvider", "Ethereum"),
BlockchainConnector: fftypes.FFEnumValue("BlockchainConnector", "EthConnect"),
BlockchainNodeProvider: fftypes.FFEnumValue("BlockchainNodeProvider", "besu"),
},
},
{
Name: "testcase2",
Ctx: ctx,
Stack: &types.Stack{
Members: []*types.Organization{{OrgName: "Org2"}, {OrgName: "org4"}},
Name: "TestBesuProviderWithEvmconnect",
BlockchainProvider: fftypes.FFEnumValue("BlockchainProvider", "Ethereum"),
BlockchainConnector: fftypes.FFEnumValue("BlockchainConnector", "EvmConnect"),
BlockchainNodeProvider: fftypes.FFEnumValue("BlockchainNodeProvider", "besu"),
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
besuProvider := NewBesuProvider(tc.Ctx, tc.Stack)
assert.NotNil(t, besuProvider)
})
}
}

func TestParseAccount(t *testing.T) {
input := map[string]interface{}{
"address": "0xAddress1",
"privateKey": "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
}

besuProvider := &BesuProvider{}
result := besuProvider.ParseAccount(input)

// Assert that the result is of type *ethereum.Account
if _, ok := result.(*ethereum.Account); !ok {
t.Errorf("Expected result to be of type *ethereum.Account, but got %T", result)
}
expectedAccount := &ethereum.Account{
Address: "0xAddress1",
PrivateKey: "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
}
assert.Equal(t, expectedAccount, result, "Generated result unmatched")

}

func TestGetContracts(t *testing.T) {
FilePath := "testdata"
testContractFile := filepath.Join(FilePath, "/test_contracts.json")
// Sample contract JSON content for testing
const testContractJSON = `{
"contracts": {
"Contract1": {
"name": "sample_1",
"abi": "sample_abi_1",
"bin": "sample_bin_1"
},
"Contract2": {
"name": "sample_2",
"abi": "sample_abi_2",
"bin": "sample_bin_2"
}
}
}`
p := &BesuProvider{}

err := os.WriteFile(testContractFile, []byte(testContractJSON), 0755)
if err != nil {
t.Log("unable to write file:", err)
}
contracts, err := p.GetContracts(testContractFile, nil)
if err != nil {
t.Log("unable to get contract", err)
}
// Assert that the returned contracts match the expected contract names
expectedContracts := []string{"Contract1", "Contract2"}
if !CompareStringSlices(contracts, expectedContracts) {
t.Errorf("Expected contracts: %v, Got: %v", expectedContracts, contracts)
}
}

func CompareStringSlices(a, b []string) bool {
//compare string lengths
if len(a) != len(b) {
return false
}
//compare values
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}

func TestGetOrgConfig(t *testing.T) {
testCases := []struct {
Name string
Org *types.Organization
Stack *types.Stack
OrgConfig *types.OrgConfig
}{
{
Name: "Testcase1",
Stack: &types.Stack{
Name: "Org-1",
},
Org: &types.Organization{
OrgName: "Org-1",
NodeName: "besu",
Account: &ethereum.Account{
Address: "0x1234567890abcdef0123456789abcdef6789abcd",
PrivateKey: "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff",
},
},
OrgConfig: &types.OrgConfig{
Name: "Org-1",
Key: "0x1234567890abcdef0123456789abcdef6789abcd",
},
},
{
Name: "Testcase2",
Stack: &types.Stack{
Name: "Org-2",
},
Org: &types.Organization{
OrgName: "Org-2",
NodeName: "besu",
Account: &ethereum.Account{
Address: "0x1f2a000000000000000000000000000000000000",
PrivateKey: "9876543210987654321098765432109876543210987654321098765432109876",
},
},
OrgConfig: &types.OrgConfig{
Name: "Org-2",
Key: "0x1f2a000000000000000000000000000000000000",
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
p := &BesuProvider{}

Orgconfig := p.GetOrgConfig(tc.Stack, tc.Org)
assert.NotNil(t, Orgconfig)
assert.Equal(t, tc.OrgConfig, Orgconfig)
})
}
}
187 changes: 187 additions & 0 deletions internal/blockchain/ethereum/besu/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package besu

import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"

"github.com/hyperledger/firefly-cli/internal/blockchain/ethereum"
"github.com/hyperledger/firefly-cli/pkg/types"
"github.com/stretchr/testify/assert"
)

func TestCreateGenesis(t *testing.T) {
//test diferent parameter cases for the CreateGenesis()
testCases := []struct {
Name string
addresses []string
blockPeriod int
chainID int64
}{
{
Name: "testcase1",
addresses: []string{"0xAddress1", "0xAddress2"},
blockPeriod: 10,
chainID: int64(123),
},
{
Name: "testcase2",
addresses: []string{"0xAddress3", "0xAddress4"},
blockPeriod: 7,
chainID: int64(456),
},
{
Name: "testcase3",
addresses: []string{"0xAddress14", "0xAddress29"},
blockPeriod: 22,
chainID: 345,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
genesis := CreateGenesis(tc.addresses, tc.blockPeriod, tc.chainID)
extraData := "0x0000000000000000000000000000000000000000000000000000000000000000"
alloc := make(map[string]*Alloc)
for _, address := range tc.addresses {
alloc[address] = &Alloc{
Balance: "0x200000000000000000000000000000000000000000000000000000000000000",
}
extraData += address
}
extraData = strings.ReplaceAll(fmt.Sprintf("%-236s", extraData), " ", "0")
expectedGenesis := &Genesis{
Config: &GenesisConfig{
ChainId: tc.chainID,
ConstantinopleFixBlock: 0,
Clique: &CliqueConfig{
Blockperiodseconds: tc.blockPeriod,
EpochLength: 30000,
},
},
Coinbase: "0x0000000000000000000000000000000000000000",
Difficulty: "0x1",
ExtraData: extraData,
GasLimit: "0xffffffff",
MixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
Nonce: "0x0",
Timestamp: "0x5c51a607",
Alloc: alloc,
Number: "0x0",
GasUsed: "0x0",
ParentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
}
// Assert that the generated Genesis is equal to the expected Genesis
assert.Equal(t, expectedGenesis, genesis, "Generated Genesis does not match the expected Genesis")
})
}

}

func TestWriteGenesisJSON(t *testing.T) {
filepath := "testdata"

testCases := []struct {
Name string
SampleGenesis Genesis
filename string
}{
{
Name: "TestCase1",
SampleGenesis: Genesis{
Config: &GenesisConfig{
ChainId: int64(456),
ConstantinopleFixBlock: 0,
Clique: &CliqueConfig{
Blockperiodseconds: 20,
EpochLength: 2000,
},
},
},
filename: filepath + "/genesis1_output.json",
},
{
Name: "TestCase2",
SampleGenesis: Genesis{
Config: &GenesisConfig{
ChainId: int64(338),
ConstantinopleFixBlock: 0,
Clique: &CliqueConfig{
Blockperiodseconds: 40,
EpochLength: 4000,
},
},
},
filename: filepath + "/genesis2_output.json",
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
err := tc.SampleGenesis.WriteGenesisJson(tc.filename)
if err != nil {
t.Log("unable to write Genesis JSON", err)
}
// Assert that there is no error
assert.NoError(t, err)

writtenJSONBytes, err := os.ReadFile(tc.filename)
if err != nil {
t.Log("Unable to write JSON Bytes", err)
}
assert.NoError(t, err)
var writtenGenesis Genesis

err = json.Unmarshal(writtenJSONBytes, &writtenGenesis)
if err != nil {
t.Log("unable to unmarshal JSON", err)
}
assert.NoError(t, err)

// Assert that the written Genesis matches the original Genesis
assert.Equal(t, tc.SampleGenesis, writtenGenesis)
})

}

}

func TestGetConnectorExternal(t *testing.T) {
testcase := []struct {
Name string
Org *types.Organization
ExpectedPort string
}{
{
Name: "testcase1",
Org: &types.Organization{
OrgName: "Org-1",
NodeName: "besu",
Account: &ethereum.Account{
Address: "0x1f2a000000000000000000000000000000000000",
PrivateKey: "9876543210987654321098765432109876543210987654321098765432109876",
},
ExposedConnectorPort: 8584,
},
ExpectedPort: "http://127.0.0.1:8584",
},
{
Name: "testcase2",
Org: &types.Organization{
OrgName: "Org-2",
NodeName: "besu",
Account: &ethereum.Account{
Address: "0xabcdeffedcba9876543210abcdeffedc00000000",
PrivateKey: "aabbccddeeff0011223344556677889900112233445566778899aabbccddeeff",
},
ExposedConnectorPort: 8000,
},
ExpectedPort: "http://127.0.0.1:8000",
},
}
for _, tc := range testcase {
p := &BesuProvider{}
result := p.GetConnectorExternalURL(tc.Org)
assert.Equal(t, tc.ExpectedPort, result)
}
}
Loading
Loading