github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/swarm/api/config_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The go-ethereum library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/atheioschain/go-atheios/common"
    27  	"github.com/atheioschain/go-atheios/crypto"
    28  )
    29  
    30  var (
    31  	hexprvkey     = "65138b2aa745041b372153550584587da326ab440576b2a1191dd95cee30039c"
    32  	defaultConfig = `{
    33      "ChunkDbPath": "` + filepath.Join("TMPDIR", "chunks") + `",
    34      "DbCapacity": 5000000,
    35      "CacheCapacity": 5000,
    36      "Radius": 0,
    37      "Branches": 128,
    38      "Hash": "SHA3",
    39      "CallInterval": 3000000000,
    40      "KadDbPath": "` + filepath.Join("TMPDIR", "bzz-peers.json") + `",
    41      "MaxProx": 8,
    42      "ProxBinSize": 2,
    43      "BucketSize": 4,
    44      "PurgeInterval": 151200000000000,
    45      "InitialRetryInterval": 42000000,
    46      "MaxIdleInterval": 42000000000,
    47      "ConnRetryExp": 2,
    48      "Swap": {
    49          "BuyAt": 20000000000,
    50          "SellAt": 20000000000,
    51          "PayAt": 100,
    52          "DropAt": 10000,
    53          "AutoCashInterval": 300000000000,
    54          "AutoCashThreshold": 50000000000000,
    55          "AutoDepositInterval": 300000000000,
    56          "AutoDepositThreshold": 50000000000000,
    57          "AutoDepositBuffer": 100000000000000,
    58          "PublicKey": "0x045f5cfd26692e48d0017d380349bcf50982488bc11b5145f3ddf88b24924299048450542d43527fbe29a5cb32f38d62755393ac002e6bfdd71b8d7ba725ecd7a3",
    59          "Contract": "0x0000000000000000000000000000000000000000",
    60          "Beneficiary": "0x0d2f62485607cf38d9d795d93682a517661e513e"
    61      },
    62      "RequestDbPath": "` + filepath.Join("TMPDIR", "requests") + `",
    63      "RequestDbBatchSize": 512,
    64      "KeyBufferSize": 1024,
    65      "SyncBatchSize": 128,
    66      "SyncBufferSize": 128,
    67      "SyncCacheSize": 1024,
    68      "SyncPriorities": [
    69          2,
    70          1,
    71          1,
    72          0,
    73          0
    74      ],
    75      "SyncModes": [
    76          true,
    77          true,
    78          true,
    79          true,
    80          false
    81      ],
    82      "Path": "TMPDIR",
    83      "Port": "8500",
    84      "PublicKey": "0x045f5cfd26692e48d0017d380349bcf50982488bc11b5145f3ddf88b24924299048450542d43527fbe29a5cb32f38d62755393ac002e6bfdd71b8d7ba725ecd7a3",
    85      "BzzKey": "0xe861964402c0b78e2d44098329b8545726f215afa737d803714a4338552fcb81",
    86      "EnsRoot": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
    87      "NetworkId": 323
    88  }`
    89  )
    90  
    91  func TestConfigWriteRead(t *testing.T) {
    92  	tmp, err := ioutil.TempDir(os.TempDir(), "bzz-test")
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	defer os.RemoveAll(tmp)
    97  
    98  	prvkey := crypto.ToECDSA(common.Hex2Bytes(hexprvkey))
    99  	orig, err := NewConfig(tmp, common.Address{}, prvkey, 323)
   100  	if err != nil {
   101  		t.Fatalf("expected no error, got %v", err)
   102  	}
   103  	data, err := ioutil.ReadFile(filepath.Join(orig.Path, "config.json"))
   104  	if err != nil {
   105  		t.Fatalf("default config file cannot be read: %v", err)
   106  	}
   107  	exp := strings.Replace(defaultConfig, "TMPDIR", orig.Path, -1)
   108  	exp = strings.Replace(exp, "\\", "\\\\", -1)
   109  	if string(data) != exp {
   110  		t.Fatalf("default config mismatch:\nexpected: %v\ngot: %v", exp, string(data))
   111  	}
   112  
   113  	conf, err := NewConfig(tmp, common.Address{}, prvkey, 323)
   114  	if err != nil {
   115  		t.Fatalf("expected no error, got %v", err)
   116  	}
   117  	if conf.Swap.Beneficiary.Hex() != orig.Swap.Beneficiary.Hex() {
   118  		t.Fatalf("expected beneficiary from loaded config %v to match original %v", conf.Swap.Beneficiary.Hex(), orig.Swap.Beneficiary.Hex())
   119  	}
   120  
   121  }