github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/config/config_test.go (about) 1 package config 2 3 import ( 4 "io/ioutil" 5 "os" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestDefaultConfig(t *testing.T) { 13 assert := assert.New(t) 14 15 // set up some defaults 16 cfg := DefaultConfig() 17 assert.NotNil(cfg.P2P) 18 19 // check the root dir stuff... 20 cfg.SetRoot("/foo") 21 cfg.DBPath = "/opt/data" 22 23 assert.Equal("/opt/data", cfg.DBDir()) 24 25 } 26 27 func TestNodeKey(t *testing.T) { 28 tmpDir, err := ioutil.TempDir(".", "") 29 if err != nil { 30 t.Fatalf("failed to create temporary data folder: %v", err) 31 } 32 defer os.RemoveAll(tmpDir) 33 config := DefaultConfig() 34 config.BaseConfig.RootDir = tmpDir 35 36 config.P2P.PrivateKey = "0fcbd0be11e35c35c41c686b7ca597bbcf8ecb78e320d01a93349c8ce9420ea4f26d0fbe651bb2c248d6727801329b589ed19e384c9e906d1da4ab2360558bc0" 37 privKey, err := config.NodeKey() 38 if err != nil { 39 t.Fatal("test node key error:", err) 40 } 41 42 if strings.Compare(privKey, config.P2P.PrivateKey) != 0 { 43 t.Fatal("test node key error. want:", config.P2P.PrivateKey, "got:", privKey) 44 } 45 46 config.P2P.PrivateKey = "" 47 writePrivKey, err := config.NodeKey() 48 if err != nil { 49 t.Fatal("test node key error:", err) 50 } 51 52 readPrivKey, err := config.NodeKey() 53 if err != nil { 54 t.Fatal("test node key error:", err) 55 } 56 57 if strings.Compare(writePrivKey, readPrivKey) != 0 { 58 t.Fatal("test node key error. write:", writePrivKey, "read:", readPrivKey) 59 } 60 }