github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/cmd/swarm/config_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"io/ioutil"
    23  	"net"
    24  	"os"
    25  	"os/exec"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/ShyftNetwork/go-empyrean/cmd/utils"
    30  	"github.com/ShyftNetwork/go-empyrean/rpc"
    31  	"github.com/ShyftNetwork/go-empyrean/swarm"
    32  	"github.com/ShyftNetwork/go-empyrean/swarm/api"
    33  
    34  	"github.com/docker/docker/pkg/reexec"
    35  )
    36  
    37  func TestConfigDump(t *testing.T) {
    38  	swarm := runSwarm(t, "dumpconfig")
    39  	defaultConf := api.NewConfig()
    40  	out, err := tomlSettings.Marshal(&defaultConf)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	swarm.Expect(string(out))
    45  	swarm.ExpectExit()
    46  }
    47  
    48  func TestConfigFailsSwapEnabledNoSwapApi(t *testing.T) {
    49  	flags := []string{
    50  		fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
    51  		fmt.Sprintf("--%s", SwarmPortFlag.Name), "54545",
    52  		fmt.Sprintf("--%s", SwarmSwapEnabledFlag.Name),
    53  	}
    54  
    55  	swarm := runSwarm(t, flags...)
    56  	swarm.Expect("Fatal: " + SWARM_ERR_SWAP_SET_NO_API + "\n")
    57  	swarm.ExpectExit()
    58  }
    59  
    60  func TestConfigFailsNoBzzAccount(t *testing.T) {
    61  	flags := []string{
    62  		fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
    63  		fmt.Sprintf("--%s", SwarmPortFlag.Name), "54545",
    64  	}
    65  
    66  	swarm := runSwarm(t, flags...)
    67  	swarm.Expect("Fatal: " + SWARM_ERR_NO_BZZACCOUNT + "\n")
    68  	swarm.ExpectExit()
    69  }
    70  
    71  func TestConfigCmdLineOverrides(t *testing.T) {
    72  	dir, err := ioutil.TempDir("", "bzztest")
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	defer os.RemoveAll(dir)
    77  
    78  	conf, account := getTestAccount(t, dir)
    79  	node := &testNode{Dir: dir}
    80  
    81  	// assign ports
    82  	httpPort, err := assignTCPPort()
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	flags := []string{
    88  		fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "42",
    89  		fmt.Sprintf("--%s", SwarmPortFlag.Name), httpPort,
    90  		fmt.Sprintf("--%s", SwarmSyncDisabledFlag.Name),
    91  		fmt.Sprintf("--%s", CorsStringFlag.Name), "*",
    92  		fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
    93  		fmt.Sprintf("--%s", SwarmDeliverySkipCheckFlag.Name),
    94  		fmt.Sprintf("--%s", EnsAPIFlag.Name), "",
    95  		fmt.Sprintf("--%s", utils.DataDirFlag.Name), dir,
    96  		fmt.Sprintf("--%s", utils.IPCPathFlag.Name), conf.IPCPath,
    97  	}
    98  	node.Cmd = runSwarm(t, flags...)
    99  	node.Cmd.InputLine(testPassphrase)
   100  	defer func() {
   101  		if t.Failed() {
   102  			node.Shutdown()
   103  		}
   104  	}()
   105  	// wait for the node to start
   106  	for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
   107  		node.Client, err = rpc.Dial(conf.IPCEndpoint())
   108  		if err == nil {
   109  			break
   110  		}
   111  	}
   112  	if node.Client == nil {
   113  		t.Fatal(err)
   114  	}
   115  
   116  	// load info
   117  	var info swarm.Info
   118  	if err := node.Client.Call(&info, "bzz_info"); err != nil {
   119  		t.Fatal(err)
   120  	}
   121  
   122  	if info.Port != httpPort {
   123  		t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port)
   124  	}
   125  
   126  	if info.NetworkID != 42 {
   127  		t.Fatalf("Expected network ID to be %d, got %d", 42, info.NetworkID)
   128  	}
   129  
   130  	if info.SyncEnabled {
   131  		t.Fatal("Expected Sync to be disabled, but is true")
   132  	}
   133  
   134  	if !info.DeliverySkipCheck {
   135  		t.Fatal("Expected DeliverySkipCheck to be enabled, but it is not")
   136  	}
   137  
   138  	if info.Cors != "*" {
   139  		t.Fatalf("Expected Cors flag to be set to %s, got %s", "*", info.Cors)
   140  	}
   141  
   142  	node.Shutdown()
   143  }
   144  
   145  func TestConfigFileOverrides(t *testing.T) {
   146  
   147  	// assign ports
   148  	httpPort, err := assignTCPPort()
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  
   153  	//create a config file
   154  	//first, create a default conf
   155  	defaultConf := api.NewConfig()
   156  	//change some values in order to test if they have been loaded
   157  	defaultConf.SyncEnabled = false
   158  	defaultConf.DeliverySkipCheck = true
   159  	defaultConf.NetworkID = 54
   160  	defaultConf.Port = httpPort
   161  	defaultConf.DbCapacity = 9000000
   162  	defaultConf.HiveParams.KeepAliveInterval = 6000000000
   163  	defaultConf.Swap.Params.Strategy.AutoCashInterval = 600 * time.Second
   164  	//defaultConf.SyncParams.KeyBufferSize = 512
   165  	//create a TOML string
   166  	out, err := tomlSettings.Marshal(&defaultConf)
   167  	if err != nil {
   168  		t.Fatalf("Error creating TOML file in TestFileOverride: %v", err)
   169  	}
   170  	//create file
   171  	f, err := ioutil.TempFile("", "testconfig.toml")
   172  	if err != nil {
   173  		t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
   174  	}
   175  	//write file
   176  	_, err = f.WriteString(string(out))
   177  	if err != nil {
   178  		t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
   179  	}
   180  	f.Sync()
   181  
   182  	dir, err := ioutil.TempDir("", "bzztest")
   183  	if err != nil {
   184  		t.Fatal(err)
   185  	}
   186  	defer os.RemoveAll(dir)
   187  	conf, account := getTestAccount(t, dir)
   188  	node := &testNode{Dir: dir}
   189  
   190  	flags := []string{
   191  		fmt.Sprintf("--%s", SwarmTomlConfigPathFlag.Name), f.Name(),
   192  		fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
   193  		fmt.Sprintf("--%s", EnsAPIFlag.Name), "",
   194  		fmt.Sprintf("--%s", utils.DataDirFlag.Name), dir,
   195  		fmt.Sprintf("--%s", utils.IPCPathFlag.Name), conf.IPCPath,
   196  	}
   197  	node.Cmd = runSwarm(t, flags...)
   198  	node.Cmd.InputLine(testPassphrase)
   199  	defer func() {
   200  		if t.Failed() {
   201  			node.Shutdown()
   202  		}
   203  	}()
   204  	// wait for the node to start
   205  	for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
   206  		node.Client, err = rpc.Dial(conf.IPCEndpoint())
   207  		if err == nil {
   208  			break
   209  		}
   210  	}
   211  	if node.Client == nil {
   212  		t.Fatal(err)
   213  	}
   214  
   215  	// load info
   216  	var info swarm.Info
   217  	if err := node.Client.Call(&info, "bzz_info"); err != nil {
   218  		t.Fatal(err)
   219  	}
   220  
   221  	if info.Port != httpPort {
   222  		t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port)
   223  	}
   224  
   225  	if info.NetworkID != 54 {
   226  		t.Fatalf("Expected network ID to be %d, got %d", 54, info.NetworkID)
   227  	}
   228  
   229  	if info.SyncEnabled {
   230  		t.Fatal("Expected Sync to be disabled, but is true")
   231  	}
   232  
   233  	if !info.DeliverySkipCheck {
   234  		t.Fatal("Expected DeliverySkipCheck to be enabled, but it is not")
   235  	}
   236  
   237  	if info.DbCapacity != 9000000 {
   238  		t.Fatalf("Expected network ID to be %d, got %d", 54, info.NetworkID)
   239  	}
   240  
   241  	if info.HiveParams.KeepAliveInterval != 6000000000 {
   242  		t.Fatalf("Expected HiveParams KeepAliveInterval to be %d, got %d", uint64(6000000000), uint64(info.HiveParams.KeepAliveInterval))
   243  	}
   244  
   245  	if info.Swap.Params.Strategy.AutoCashInterval != 600*time.Second {
   246  		t.Fatalf("Expected SwapParams AutoCashInterval to be %ds, got %d", 600, info.Swap.Params.Strategy.AutoCashInterval)
   247  	}
   248  
   249  	//	if info.SyncParams.KeyBufferSize != 512 {
   250  	//		t.Fatalf("Expected info.SyncParams.KeyBufferSize to be %d, got %d", 512, info.SyncParams.KeyBufferSize)
   251  	//	}
   252  
   253  	node.Shutdown()
   254  }
   255  
   256  func TestConfigEnvVars(t *testing.T) {
   257  	// assign ports
   258  	httpPort, err := assignTCPPort()
   259  	if err != nil {
   260  		t.Fatal(err)
   261  	}
   262  
   263  	envVars := os.Environ()
   264  	envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmPortFlag.EnvVar, httpPort))
   265  	envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmNetworkIdFlag.EnvVar, "999"))
   266  	envVars = append(envVars, fmt.Sprintf("%s=%s", CorsStringFlag.EnvVar, "*"))
   267  	envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmSyncDisabledFlag.EnvVar, "true"))
   268  	envVars = append(envVars, fmt.Sprintf("%s=%s", SwarmDeliverySkipCheckFlag.EnvVar, "true"))
   269  
   270  	dir, err := ioutil.TempDir("", "bzztest")
   271  	if err != nil {
   272  		t.Fatal(err)
   273  	}
   274  	defer os.RemoveAll(dir)
   275  	conf, account := getTestAccount(t, dir)
   276  	node := &testNode{Dir: dir}
   277  	flags := []string{
   278  		fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
   279  		"--ens-api", "",
   280  		"--datadir", dir,
   281  		"--ipcpath", conf.IPCPath,
   282  	}
   283  
   284  	//node.Cmd = runSwarm(t,flags...)
   285  	//node.Cmd.cmd.Env = envVars
   286  	//the above assignment does not work, so we need a custom Cmd here in order to pass envVars:
   287  	cmd := &exec.Cmd{
   288  		Path:   reexec.Self(),
   289  		Args:   append([]string{"swarm-test"}, flags...),
   290  		Stderr: os.Stderr,
   291  		Stdout: os.Stdout,
   292  	}
   293  	cmd.Env = envVars
   294  	//stdout, err := cmd.StdoutPipe()
   295  	//if err != nil {
   296  	//	t.Fatal(err)
   297  	//}
   298  	//stdout = bufio.NewReader(stdout)
   299  	var stdin io.WriteCloser
   300  	if stdin, err = cmd.StdinPipe(); err != nil {
   301  		t.Fatal(err)
   302  	}
   303  	if err := cmd.Start(); err != nil {
   304  		t.Fatal(err)
   305  	}
   306  
   307  	//cmd.InputLine(testPassphrase)
   308  	io.WriteString(stdin, testPassphrase+"\n")
   309  	defer func() {
   310  		if t.Failed() {
   311  			node.Shutdown()
   312  			cmd.Process.Kill()
   313  		}
   314  	}()
   315  	// wait for the node to start
   316  	for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
   317  		node.Client, err = rpc.Dial(conf.IPCEndpoint())
   318  		if err == nil {
   319  			break
   320  		}
   321  	}
   322  
   323  	if node.Client == nil {
   324  		t.Fatal(err)
   325  	}
   326  
   327  	// load info
   328  	var info swarm.Info
   329  	if err := node.Client.Call(&info, "bzz_info"); err != nil {
   330  		t.Fatal(err)
   331  	}
   332  
   333  	if info.Port != httpPort {
   334  		t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port)
   335  	}
   336  
   337  	if info.NetworkID != 999 {
   338  		t.Fatalf("Expected network ID to be %d, got %d", 999, info.NetworkID)
   339  	}
   340  
   341  	if info.Cors != "*" {
   342  		t.Fatalf("Expected Cors flag to be set to %s, got %s", "*", info.Cors)
   343  	}
   344  
   345  	if info.SyncEnabled {
   346  		t.Fatal("Expected Sync to be disabled, but is true")
   347  	}
   348  
   349  	if !info.DeliverySkipCheck {
   350  		t.Fatal("Expected DeliverySkipCheck to be enabled, but it is not")
   351  	}
   352  
   353  	node.Shutdown()
   354  	cmd.Process.Kill()
   355  }
   356  
   357  func TestConfigCmdLineOverridesFile(t *testing.T) {
   358  
   359  	// assign ports
   360  	httpPort, err := assignTCPPort()
   361  	if err != nil {
   362  		t.Fatal(err)
   363  	}
   364  
   365  	//create a config file
   366  	//first, create a default conf
   367  	defaultConf := api.NewConfig()
   368  	//change some values in order to test if they have been loaded
   369  	defaultConf.SyncEnabled = true
   370  	defaultConf.NetworkID = 54
   371  	defaultConf.Port = "8588"
   372  	defaultConf.DbCapacity = 9000000
   373  	defaultConf.HiveParams.KeepAliveInterval = 6000000000
   374  	defaultConf.Swap.Params.Strategy.AutoCashInterval = 600 * time.Second
   375  	//defaultConf.SyncParams.KeyBufferSize = 512
   376  	//create a TOML file
   377  	out, err := tomlSettings.Marshal(&defaultConf)
   378  	if err != nil {
   379  		t.Fatalf("Error creating TOML file in TestFileOverride: %v", err)
   380  	}
   381  	//write file
   382  	fname := "testconfig.toml"
   383  	f, err := ioutil.TempFile("", fname)
   384  	if err != nil {
   385  		t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
   386  	}
   387  	defer os.Remove(fname)
   388  	//write file
   389  	_, err = f.WriteString(string(out))
   390  	if err != nil {
   391  		t.Fatalf("Error writing TOML file in TestFileOverride: %v", err)
   392  	}
   393  	f.Sync()
   394  
   395  	dir, err := ioutil.TempDir("", "bzztest")
   396  	if err != nil {
   397  		t.Fatal(err)
   398  	}
   399  	defer os.RemoveAll(dir)
   400  	conf, account := getTestAccount(t, dir)
   401  	node := &testNode{Dir: dir}
   402  
   403  	expectNetworkId := uint64(77)
   404  
   405  	flags := []string{
   406  		fmt.Sprintf("--%s", SwarmNetworkIdFlag.Name), "77",
   407  		fmt.Sprintf("--%s", SwarmPortFlag.Name), httpPort,
   408  		fmt.Sprintf("--%s", SwarmSyncDisabledFlag.Name),
   409  		fmt.Sprintf("--%s", SwarmTomlConfigPathFlag.Name), f.Name(),
   410  		fmt.Sprintf("--%s", SwarmAccountFlag.Name), account.Address.String(),
   411  		fmt.Sprintf("--%s", EnsAPIFlag.Name), "",
   412  		fmt.Sprintf("--%s", utils.DataDirFlag.Name), dir,
   413  		fmt.Sprintf("--%s", utils.IPCPathFlag.Name), conf.IPCPath,
   414  	}
   415  	node.Cmd = runSwarm(t, flags...)
   416  	node.Cmd.InputLine(testPassphrase)
   417  	defer func() {
   418  		if t.Failed() {
   419  			node.Shutdown()
   420  		}
   421  	}()
   422  	// wait for the node to start
   423  	for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) {
   424  		node.Client, err = rpc.Dial(conf.IPCEndpoint())
   425  		if err == nil {
   426  			break
   427  		}
   428  	}
   429  	if node.Client == nil {
   430  		t.Fatal(err)
   431  	}
   432  
   433  	// load info
   434  	var info swarm.Info
   435  	if err := node.Client.Call(&info, "bzz_info"); err != nil {
   436  		t.Fatal(err)
   437  	}
   438  
   439  	if info.Port != httpPort {
   440  		t.Fatalf("Expected port to be %s, got %s", httpPort, info.Port)
   441  	}
   442  
   443  	if info.NetworkID != expectNetworkId {
   444  		t.Fatalf("Expected network ID to be %d, got %d", expectNetworkId, info.NetworkID)
   445  	}
   446  
   447  	if info.SyncEnabled {
   448  		t.Fatal("Expected Sync to be disabled, but is true")
   449  	}
   450  
   451  	if info.LocalStoreParams.DbCapacity != 9000000 {
   452  		t.Fatalf("Expected Capacity to be %d, got %d", 9000000, info.LocalStoreParams.DbCapacity)
   453  	}
   454  
   455  	if info.HiveParams.KeepAliveInterval != 6000000000 {
   456  		t.Fatalf("Expected HiveParams KeepAliveInterval to be %d, got %d", uint64(6000000000), uint64(info.HiveParams.KeepAliveInterval))
   457  	}
   458  
   459  	if info.Swap.Params.Strategy.AutoCashInterval != 600*time.Second {
   460  		t.Fatalf("Expected SwapParams AutoCashInterval to be %ds, got %d", 600, info.Swap.Params.Strategy.AutoCashInterval)
   461  	}
   462  
   463  	//	if info.SyncParams.KeyBufferSize != 512 {
   464  	//		t.Fatalf("Expected info.SyncParams.KeyBufferSize to be %d, got %d", 512, info.SyncParams.KeyBufferSize)
   465  	//	}
   466  
   467  	node.Shutdown()
   468  }
   469  
   470  func TestConfigValidate(t *testing.T) {
   471  	for _, c := range []struct {
   472  		cfg *api.Config
   473  		err string
   474  	}{
   475  		{
   476  			cfg: &api.Config{EnsAPIs: []string{
   477  				"/data/testnet/geth.ipc",
   478  			}},
   479  		},
   480  		{
   481  			cfg: &api.Config{EnsAPIs: []string{
   482  				"http://127.0.0.1:1234",
   483  			}},
   484  		},
   485  		{
   486  			cfg: &api.Config{EnsAPIs: []string{
   487  				"ws://127.0.0.1:1234",
   488  			}},
   489  		},
   490  		{
   491  			cfg: &api.Config{EnsAPIs: []string{
   492  				"test:/data/testnet/geth.ipc",
   493  			}},
   494  		},
   495  		{
   496  			cfg: &api.Config{EnsAPIs: []string{
   497  				"test:ws://127.0.0.1:1234",
   498  			}},
   499  		},
   500  		{
   501  			cfg: &api.Config{EnsAPIs: []string{
   502  				"314159265dD8dbb310642f98f50C066173C1259b@/data/testnet/geth.ipc",
   503  			}},
   504  		},
   505  		{
   506  			cfg: &api.Config{EnsAPIs: []string{
   507  				"314159265dD8dbb310642f98f50C066173C1259b@http://127.0.0.1:1234",
   508  			}},
   509  		},
   510  		{
   511  			cfg: &api.Config{EnsAPIs: []string{
   512  				"314159265dD8dbb310642f98f50C066173C1259b@ws://127.0.0.1:1234",
   513  			}},
   514  		},
   515  		{
   516  			cfg: &api.Config{EnsAPIs: []string{
   517  				"test:314159265dD8dbb310642f98f50C066173C1259b@/data/testnet/geth.ipc",
   518  			}},
   519  		},
   520  		{
   521  			cfg: &api.Config{EnsAPIs: []string{
   522  				"eth:314159265dD8dbb310642f98f50C066173C1259b@http://127.0.0.1:1234",
   523  			}},
   524  		},
   525  		{
   526  			cfg: &api.Config{EnsAPIs: []string{
   527  				"eth:314159265dD8dbb310642f98f50C066173C1259b@ws://127.0.0.1:12344",
   528  			}},
   529  		},
   530  		{
   531  			cfg: &api.Config{EnsAPIs: []string{
   532  				"eth:",
   533  			}},
   534  			err: "invalid format [tld:][contract-addr@]url for ENS API endpoint configuration \"eth:\": missing url",
   535  		},
   536  		{
   537  			cfg: &api.Config{EnsAPIs: []string{
   538  				"314159265dD8dbb310642f98f50C066173C1259b@",
   539  			}},
   540  			err: "invalid format [tld:][contract-addr@]url for ENS API endpoint configuration \"314159265dD8dbb310642f98f50C066173C1259b@\": missing url",
   541  		},
   542  		{
   543  			cfg: &api.Config{EnsAPIs: []string{
   544  				":314159265dD8dbb310642f98f50C066173C1259",
   545  			}},
   546  			err: "invalid format [tld:][contract-addr@]url for ENS API endpoint configuration \":314159265dD8dbb310642f98f50C066173C1259\": missing tld",
   547  		},
   548  		{
   549  			cfg: &api.Config{EnsAPIs: []string{
   550  				"@/data/testnet/geth.ipc",
   551  			}},
   552  			err: "invalid format [tld:][contract-addr@]url for ENS API endpoint configuration \"@/data/testnet/geth.ipc\": missing contract address",
   553  		},
   554  	} {
   555  		err := validateConfig(c.cfg)
   556  		if c.err != "" && err.Error() != c.err {
   557  			t.Errorf("expected error %q, got %q", c.err, err)
   558  		}
   559  		if c.err == "" && err != nil {
   560  			t.Errorf("unexpected error %q", err)
   561  		}
   562  	}
   563  }
   564  
   565  func assignTCPPort() (string, error) {
   566  	l, err := net.Listen("tcp", "127.0.0.1:0")
   567  	if err != nil {
   568  		return "", err
   569  	}
   570  	l.Close()
   571  	_, port, err := net.SplitHostPort(l.Addr().String())
   572  	if err != nil {
   573  		return "", err
   574  	}
   575  	return port, nil
   576  }