test_utils

package
v0.0.0-...-aeb4a1d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2022 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package test_utils contains test utility functions for creating users, printing the ledger, and etc. These functions should only be used in unit tests.

Index

Examples

Constants

View Source
const COMPOSITE_KEY_NAMESPACE = "\x00"

COMPOSITE_KEY_NAMESPACE is the namespace for composite keys. Simple keys should not enter the composite key namespace to avoid simple/composite key collisions.

Variables

This section is empty.

Functions

func AddPHIArgsToTransientMap

func AddPHIArgsToTransientMap(tmap map[string][]byte, params [][]byte, args ...[]byte) (map[string][]byte, [][]byte)

AddPHIArgsToTransientMap adds phi args to the transient map to be used in Invoke

func AssertFalse

func AssertFalse(t *testing.T, assertion bool, message string)

AssertFalse asserts that the given boolean is false.

Example
var t *testing.T // param passed into every go test function
a := 1
b := 2

AssertFalse(t, a == b, "Expected a != b")
Output:

func AssertInLists

func AssertInLists(t *testing.T, expectedValue string, expectedList []string, message string)

AssertInLists asserts that expectedValue is in expectedList.

Example
var t *testing.T // param passed into every go test function
item := "item1"
list := []string{"item1", "item2", "item3"}

AssertInLists(t, item, list, "Expected item1 in list")
Output:

func AssertListsEqual

func AssertListsEqual(t *testing.T, expectedList []string, actualList []string)

AssertListsEqual asserts that two lists are equal.

Example
var t *testing.T // param passed into every go test function
list1 := []string{"item1", "item2", "item3"}
list2 := []string{"item1", "item2", "item3"}

AssertListsEqual(t, list1, list2)
Output:

func AssertMapsEqual

func AssertMapsEqual(t *testing.T, expectedMap interface{}, actualMap interface{}, message string)

AssertMapsEqual assets that two maps are equal.

Example
var t *testing.T // param passed into every go test function
map1 := make(map[string]string)
map1["age"] = "40"
map1["name"] = "Jo"
map2 := make(map[string]string)
map2["name"] = "Jo"
map2["age"] = "40"

AssertMapsEqual(t, map1, map2, "Expected map1 and map2 to be equal")
Output:

func AssertNil

func AssertNil(t *testing.T, actual interface{})

AssertNil asserts that the provided value is nil. Useful for checking there are no errors: AssertNil(t, err).

Example
var t *testing.T // param passed into every go test function
_, err := json.Marshal("data")
AssertNil(t, err)
Output:

func AssertNilError

func AssertNilError(t *testing.T, myError error, message string)

AssertNilError if myError is not nil, prints error details/stack and fails the test

func AssertSetsEqual

func AssertSetsEqual(t *testing.T, expectedList []string, actualList []string)

AssertSetsEqual assets that two sets are equal.

Example
var t *testing.T // param passed into every go test function
set1 := []string{"item1", "item2", "item3"}
set2 := []string{"item2", "item1", "item3"}

AssertListsEqual(t, set1, set2)
Output:

func AssertStringInArray

func AssertStringInArray(t *testing.T, item string, array []string)

AssertStringInArray assets that a given string is in a given array.

Example
var t *testing.T // param passed into every go test function
item := "item1"
array := []string{"item1", "item2", "item3"}

AssertStringInArray(t, item, array)
Output:

func AssertTrue

func AssertTrue(t *testing.T, assertion bool, message string)

AssertTrue asserts that the given boolean is true.

Example
var t *testing.T // param passed into every go test function
a := 1 + 1
b := 2

AssertTrue(t, a == b, "Expected a == b")
Output:

func CreateSymKey

func CreateSymKey(keyId string) data_model.Key

CreateSymKey generates a sym key and returns it as part of a Key object. Key generation functions are only for testing. In production, keys will never be generated from the chaincode.

Example
CreateSymKey("key1")
Output:

func CreateTestAsset

func CreateTestAsset(assetId string) data_model.Asset

CreateTestAsset returns an asset for tests.

Example
CreateTestAsset(asset_mgmt_g.GetAssetId("data_model.Assert", "asset1"))
Output:

func CreateTestAssetData

func CreateTestAssetData(data string) []byte

CreateTestAssetData returns data that can be used as a test asset's public or private data.

Example
asset := data_model.Asset{}

asset.PublicData = CreateTestAssetData("test_public_data")
Output:

func CreateTestGroup

func CreateTestGroup(groupID string) data_model.User

CreateTestGroup creates a test group with random keys.

Example
org := CreateTestGroup("org1")

fmt.Println(org.IsGroup)
fmt.Println(org.Role)
Output:

true
org

func CreateTestUser

func CreateTestUser(userID string) data_model.User

CreateTestUser creates a test user with random keys.

Example
user := CreateTestUser("user1")

fmt.Println(user.IsGroup)
fmt.Println(user.Role)
Output:

false
user

func GeneratePrivateKey

func GeneratePrivateKey() *rsa.PrivateKey

GeneratePrivateKey generates a random 2048-bit RSA Private Key. Key generation functions are only for testing. In production, keys will never be generated from the chaincode.

Example
GeneratePrivateKey()
Output:

func GenerateRandomTxID

func GenerateRandomTxID() string

GenerateRandomTxID is a convenience function that generates a random transaction ID. Used only for testing during development when there is 1 peer. In production, TxID should never be generated from the chaincode even during testing.

Example
txID := GenerateRandomTxID()

fmt.Println(txID)
Output:

func GenerateSymKey

func GenerateSymKey() []byte

GenerateSymKey generates a random 32-byte AES symmetric key. Key generation functions are only for testing. In production, keys will never be generated from the chaincode.

Example
symKey := GenerateSymKey()

fmt.Println(len(symKey))
Output:

32

func GetTransientMapFromUser

func GetTransientMapFromUser(user data_model.User) map[string][]byte

GetTransientMapFromUser returns a transienmap with user information to be used in Invoke

Types

type FixedMockStateRangeQueryIterator

type FixedMockStateRangeQueryIterator struct {
	shim.MockStateRangeQueryIterator
}

FixedMockStateRangeQueryIterator overrides the broken HasNext() and Next() methods of MockStateRangeQueryIterator. Fabric's current implementation doesn't handle one-sided open-ended range queries. Also, if fixes an issue with endKey being inclusive: startKey is inclusive and endKey should be exclusive

func NewFixedMockStateRangeQueryIterator

func NewFixedMockStateRangeQueryIterator(stub *NewMockStub, startKey string, endKey string) *FixedMockStateRangeQueryIterator

NewFixedMockStateRangeQueryIterator returns a range query iterator that supports one-sided and open-ended range queries.

Example
var t *testing.T // param passed into every go test function
stub := CreateNewMockStub(t)
startKey := "a" //start ledger key value
endKey := "b"   //end ledger key valuye

NewFixedMockStateRangeQueryIterator(stub, startKey, endKey)
Output:

func (*FixedMockStateRangeQueryIterator) HasNext

func (iter *FixedMockStateRangeQueryIterator) HasNext() bool

HasNext returns true if the range query iterator contains additional keys and values.

func (*FixedMockStateRangeQueryIterator) Next

Next returns the next key and value in the range query iterator.

type MisbehavingMockStub

type MisbehavingMockStub struct {
	*shim.MockStub
}

MisbehavingMockStub returns errors for GetState, PutState, and DelState.

func CreateMisbehavingMockStub

func CreateMisbehavingMockStub(t *testing.T) *MisbehavingMockStub

CreateMisbehavingMockStub returns a misbehaving mock stub which returns errors for GetState, PutState, and DelState.

Example
var t *testing.T // param passed into every go test function

CreateMisbehavingMockStub(t)
Output:

func (*MisbehavingMockStub) DelState

func (stub *MisbehavingMockStub) DelState(key string) error

DelState deletes a value for a MisbehavingMockStub.

Example
var t *testing.T // param passed into every go test function
misbehavingStub := CreateMisbehavingMockStub(t)

err := misbehavingStub.DelState("ledger_key")

fmt.Println(err)
Output:

Misbehaving stub error!

func (*MisbehavingMockStub) GetState

func (stub *MisbehavingMockStub) GetState(key string) ([]byte, error)

GetState returns a value for a MisbehavingMockStub.

Example
var t *testing.T // param passed into every go test function
misbehavingStub := CreateMisbehavingMockStub(t)

_, err := misbehavingStub.GetState("ledger_key")

fmt.Println(err)
Output:

Misbehaving stub error!

func (*MisbehavingMockStub) GetStateByPartialCompositeKey

func (stub *MisbehavingMockStub) GetStateByPartialCompositeKey(key string, value []string) (shim.StateQueryIteratorInterface, error)

GetStateByPartialCompositeKey returns a partial composite key for a MisbehavingMockStub.

Example
var t *testing.T // param passed into every go test function
misbehavingStub := CreateMisbehavingMockStub(t)

_, err := misbehavingStub.GetStateByPartialCompositeKey("ledger_key", []string{})

fmt.Println(err)
Output:

Misbehaving stub error!

func (*MisbehavingMockStub) GetStateByRange

func (stub *MisbehavingMockStub) GetStateByRange(startKey, endKey string) (shim.StateQueryIteratorInterface, error)

GetStateByRange returns a range query for a MisbehavingMockStub.

func (*MisbehavingMockStub) PutState

func (stub *MisbehavingMockStub) PutState(key string, value []byte) error

PutState adds a value for a MisbehavingMockStub.

Example
var t *testing.T // param passed into every go test function
misbehavingStub := CreateMisbehavingMockStub(t)

err := misbehavingStub.PutState("ledger_key", []byte{})

fmt.Println(err)
Output:

Misbehaving stub error!

type MockChaincode

type MockChaincode struct {
}

MockChaincode is a mock chaincode.

func (*MockChaincode) Init

Init is mocked for MockChaincode.

func (*MockChaincode) Invoke

Invoke is mocked for MockChaincode.

func (*MockChaincode) Query

Query is mocked for MockChaincode.

type NewMockStub

type NewMockStub struct {
	*shim.MockStub
	// contains filtered or unexported fields
}

NewMockStub is a mock stub.

func CreateExampleMockStub

func CreateExampleMockStub() *NewMockStub

CreateExampleMockStub returns a mock stub without *testing.T object, for use in godoc examples.

Example
CreateExampleMockStub()
Output:

func CreateNewMockStub

func CreateNewMockStub(t *testing.T, options ...interface{}) *NewMockStub

CreateNewMockStub returns a mock stub. options = [name string, cc shim.Chaincode, tmap map[string][]byte]

Example
var t *testing.T // param passed into every go test function

CreateNewMockStub(t)
Output:

func (*NewMockStub) DelState

func (stub *NewMockStub) DelState(key string) error

DelState deletes a value for a mock stub.

func (*NewMockStub) GetArgs

func (stub *NewMockStub) GetArgs() [][]byte

GetArgs returns arguments.

func (*NewMockStub) GetFunctionAndParameters

func (stub *NewMockStub) GetFunctionAndParameters() (function string, params []string)

GetFunctionAndParameters returns function name and parameters.

func (*NewMockStub) GetSignedProposal

func (stub *NewMockStub) GetSignedProposal() (*peer.SignedProposal, error)

GetSignedProposal simulates peer proposal response.

func (*NewMockStub) GetState

func (stub *NewMockStub) GetState(key string) ([]byte, error)

GetState returns an item stored in NewMockStub.

Example
var t *testing.T // param passed into every go test function
stub := CreateNewMockStub(t)

stub.GetState("ledger_key")
Output:

func (*NewMockStub) GetStateByRange

func (stub *NewMockStub) GetStateByRange(startKey, endKey string) (shim.StateQueryIteratorInterface, error)

GetStateByRange returns a NewFixedMockStateRangeQueryIterator.

Example
var t *testing.T // param passed into every go test function
stub := CreateNewMockStub(t)
startKey := "a" //start ledger key value
endKey := "b"   //end ledger key valuye

stub.GetStateByRange(startKey, endKey)
Output:

func (*NewMockStub) GetStringArgs

func (stub *NewMockStub) GetStringArgs() []string

GetStringArgs returns a slice of arguments.

func (*NewMockStub) GetTransient

func (stub *NewMockStub) GetTransient() (map[string][]byte, error)

GetTransient returns transient map of the transaction

func (*NewMockStub) MockInit

func (stub *NewMockStub) MockInit(uuid string, args [][]byte) peer.Response

MockInit initializes this chaincode, also starts and ends a transaction.

func (*NewMockStub) MockInvoke

func (stub *NewMockStub) MockInvoke(uuid string, args [][]byte, tmap map[string][]byte) peer.Response

MockInvoke invokes this chaincode, also starts and ends a transaction.

func (*NewMockStub) MockInvokeWithSignedProposal

func (stub *NewMockStub) MockInvokeWithSignedProposal(uuid string, args [][]byte, tmap map[string][]byte, sp *peer.SignedProposal) peer.Response

MockInvokeWithSignedProposal invokes this chaincode, also starts and ends a transaction.

func (*NewMockStub) MockTransactionEnd

func (stub *NewMockStub) MockTransactionEnd(txid string)

MockTransactionEnd returns a transaction ID.

func (*NewMockStub) MockTransactionStart

func (stub *NewMockStub) MockTransactionStart(txid string)

MockTransactionStart returns a transaction ID.

func (*NewMockStub) PutState

func (stub *NewMockStub) PutState(key string, val []byte) error

PutState adds a value for a mock stub.

type TestAssetData

type TestAssetData struct {
	Data string
}

TestAssetData can be used as a test asset's public or private data.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL