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