github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/test/integration/run_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  
     6  	cfg "github.com/bytom/bytom/config"
     7  	"github.com/bytom/bytom/crypto/ed25519/chainkd"
     8  	"github.com/bytom/bytom/util"
     9  )
    10  
    11  // Mock config.
    12  func mockConfig() *cfg.Config {
    13  	var config = cfg.DefaultConfig()
    14  	config.Wallet.Disable = false
    15  	config.ApiAddress = "127.0.0.1:9888"
    16  	return config
    17  }
    18  
    19  // Test net-info call api.
    20  func testNet() bool {
    21  	data, exitCode := util.ClientCall("/net-info")
    22  	if exitCode != util.Success {
    23  		return false
    24  	}
    25  	dataMap, ok := data.(map[string]interface{})
    26  	if ok && dataMap["listening"].(bool) && dataMap["syncing"].(bool) && dataMap["mining"].(bool) {
    27  		return true
    28  	}
    29  	return false
    30  }
    31  
    32  // Test create-key delete-key list-key api and function.
    33  func testKey() bool {
    34  	var key = struct {
    35  		Alias    string `json:"alias"`
    36  		Password string `json:"password"`
    37  	}{Alias: "alice", Password: "123456"}
    38  
    39  	data, exitCode := util.ClientCall("/create-key", &key)
    40  	if exitCode != util.Success {
    41  		return false
    42  	}
    43  	dataMap, ok := data.(map[string]interface{})
    44  	if (ok && dataMap["alias"].(string) == "alice") == false {
    45  		return false
    46  	}
    47  
    48  	_, exitCode1 := util.ClientCall("/list-keys")
    49  	if exitCode1 != util.Success {
    50  		return false
    51  	}
    52  
    53  	fmt.Println("dataMap", dataMap)
    54  	xpub := new(chainkd.XPub)
    55  	if err := xpub.UnmarshalText([]byte(dataMap["xpub"].(string))); err != nil {
    56  		return false
    57  	}
    58  
    59  	var key1 = struct {
    60  		Password string
    61  		XPub     chainkd.XPub `json:"xpubs"`
    62  	}{XPub: *xpub, Password: "123456"}
    63  
    64  	if _, exitCode := util.ClientCall("/delete-key", &key1); exitCode != util.Success {
    65  		return false
    66  	}
    67  
    68  	return true
    69  }
    70  
    71  // Test node running.
    72  /*func TestRunNode(t *testing.T) {
    73  	// Create & start node
    74  	config := mockConfig()
    75  	n := node.NewNodeDefault(config)
    76  	if _, err := n.Start(); err != nil {
    77  		t.Fatalf("Failed to start node: %v", err)
    78  	}
    79  
    80  	go func() {
    81  		time.Sleep(3000 * time.Millisecond)
    82  		if testNet() && testKey() {
    83  			os.RemoveAll("./data")
    84  			os.RemoveAll("./keystore")
    85  			os.Exit(0)
    86  		} else {
    87  			os.RemoveAll("./data")
    88  			os.RemoveAll("./keystore")
    89  			os.Exit(1)
    90  		}
    91  	}()
    92  	// Trap signal, run forever.
    93  	n.RunForever()
    94  }*/