github.com/alanchchen/go-ethereum@v1.6.6-0.20170601190819-6171d01b1195/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/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/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      "ListenAddr": "127.0.0.1",
    84      "Port": "8500",
    85      "PublicKey": "0x045f5cfd26692e48d0017d380349bcf50982488bc11b5145f3ddf88b24924299048450542d43527fbe29a5cb32f38d62755393ac002e6bfdd71b8d7ba725ecd7a3",
    86      "BzzKey": "0xe861964402c0b78e2d44098329b8545726f215afa737d803714a4338552fcb81",
    87      "EnsRoot": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
    88      "NetworkId": 323
    89  }`
    90  )
    91  
    92  func TestConfigWriteRead(t *testing.T) {
    93  	tmp, err := ioutil.TempDir(os.TempDir(), "bzz-test")
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	defer os.RemoveAll(tmp)
    98  
    99  	prvkey, err := crypto.HexToECDSA(hexprvkey)
   100  	if err != nil {
   101  		t.Fatalf("failed to load private key: %v", err)
   102  	}
   103  	orig, err := NewConfig(tmp, common.Address{}, prvkey, 323)
   104  	if err != nil {
   105  		t.Fatalf("expected no error, got %v", err)
   106  	}
   107  	data, err := ioutil.ReadFile(filepath.Join(orig.Path, "config.json"))
   108  	if err != nil {
   109  		t.Fatalf("default config file cannot be read: %v", err)
   110  	}
   111  	exp := strings.Replace(defaultConfig, "TMPDIR", orig.Path, -1)
   112  	exp = strings.Replace(exp, "\\", "\\\\", -1)
   113  	if string(data) != exp {
   114  		t.Fatalf("default config mismatch:\nexpected: %v\ngot: %v", exp, string(data))
   115  	}
   116  
   117  	conf, err := NewConfig(tmp, common.Address{}, prvkey, 323)
   118  	if err != nil {
   119  		t.Fatalf("expected no error, got %v", err)
   120  	}
   121  	if conf.Swap.Beneficiary.Hex() != orig.Swap.Beneficiary.Hex() {
   122  		t.Fatalf("expected beneficiary from loaded config %v to match original %v", conf.Swap.Beneficiary.Hex(), orig.Swap.Beneficiary.Hex())
   123  	}
   124  
   125  }