2020-03-29 21:32:01 +08:00
|
|
|
package tester
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-07-14 16:26:42 +08:00
|
|
|
"github.com/Rican7/retry"
|
|
|
|
"github.com/Rican7/retry/strategy"
|
2020-03-29 21:32:01 +08:00
|
|
|
"github.com/meshplus/bitxhub-kit/crypto"
|
|
|
|
"github.com/meshplus/bitxhub-kit/crypto/asym"
|
|
|
|
"github.com/meshplus/bitxhub-kit/types"
|
2020-10-21 18:41:53 +08:00
|
|
|
"github.com/meshplus/bitxhub-model/constant"
|
2020-03-29 21:32:01 +08:00
|
|
|
"github.com/meshplus/bitxhub-model/pb"
|
2020-04-28 17:08:21 +08:00
|
|
|
"github.com/meshplus/bitxhub/internal/coreapi/api"
|
2020-03-29 21:32:01 +08:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
2020-07-14 16:26:42 +08:00
|
|
|
const (
|
|
|
|
value = "value"
|
|
|
|
)
|
|
|
|
|
2020-03-29 21:32:01 +08:00
|
|
|
type API struct {
|
|
|
|
suite.Suite
|
2020-04-28 17:08:21 +08:00
|
|
|
api api.CoreAPI
|
2020-03-29 21:32:01 +08:00
|
|
|
privKey crypto.PrivateKey
|
2020-10-21 19:26:41 +08:00
|
|
|
from *types.Address
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *API) SetupSuite() {
|
2020-08-11 14:07:15 +08:00
|
|
|
privKey, err := asym.GenerateKeyPair(crypto.Secp256k1)
|
2020-03-29 21:32:01 +08:00
|
|
|
suite.Assert().Nil(err)
|
|
|
|
|
|
|
|
from, err := privKey.PublicKey().Address()
|
|
|
|
suite.Assert().Nil(err)
|
|
|
|
|
|
|
|
suite.privKey = privKey
|
|
|
|
suite.from = from
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:26:42 +08:00
|
|
|
func (suite *API) TestSend() {
|
|
|
|
hash := testSendTransaction(suite)
|
|
|
|
if err := retry.Retry(func(attempt uint) error {
|
|
|
|
receipt, err := suite.api.Broker().GetReceipt(hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if receipt.IsSuccess() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, strategy.Wait(2*time.Second)); err != nil {
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
|
|
|
|
2020-07-14 16:26:42 +08:00
|
|
|
testSendView(suite)
|
|
|
|
}
|
2020-03-29 21:32:01 +08:00
|
|
|
|
2020-10-22 14:22:31 +08:00
|
|
|
func testSendTransaction(suite *API) *types.Hash {
|
2020-09-29 15:01:30 +08:00
|
|
|
tx, err := genContractTransaction(pb.TransactionData_BVM, suite.privKey, 1,
|
2020-07-14 16:26:42 +08:00
|
|
|
constant.StoreContractAddr.Address(), "Set", pb.String("key"), pb.String(value))
|
2020-03-29 21:32:01 +08:00
|
|
|
suite.Nil(err)
|
|
|
|
|
2020-07-14 16:26:42 +08:00
|
|
|
suite.Nil(suite.api.Broker().HandleTransaction(tx))
|
|
|
|
return tx.TransactionHash
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSendView(suite *API) {
|
2020-09-29 15:01:30 +08:00
|
|
|
tx, err := genContractTransaction(pb.TransactionData_BVM, suite.privKey, 1,
|
2020-07-14 16:26:42 +08:00
|
|
|
constant.StoreContractAddr.Address(), "Get", pb.String("key"))
|
2020-03-29 21:32:01 +08:00
|
|
|
|
2020-07-16 16:24:10 +08:00
|
|
|
receipt, err := suite.api.Broker().HandleView(tx)
|
2020-04-28 17:08:21 +08:00
|
|
|
suite.Nil(err)
|
2020-07-24 01:07:51 +08:00
|
|
|
suite.Equal(receipt.Status, pb.Receipt_SUCCESS)
|
|
|
|
suite.Equal(value, string(receipt.Ret))
|
2020-03-29 21:32:01 +08:00
|
|
|
}
|
|
|
|
|
2020-12-08 20:18:42 +08:00
|
|
|
func (suite *API) TestDelVPNode() {
|
|
|
|
_, err := suite.api.Network().DelVPNode("")
|
|
|
|
suite.NotNil(err)
|
|
|
|
|
|
|
|
_, err = suite.api.Network().DelVPNode("1234")
|
|
|
|
suite.Nil(err)
|
|
|
|
}
|
|
|
|
|
2020-03-29 21:32:01 +08:00
|
|
|
func TestAPI(t *testing.T) {
|
|
|
|
suite.Run(t, &API{})
|
|
|
|
}
|