OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / config_test.go
1 package main
2
3 import (
4         "io/ioutil"
5         "os"
6         "path/filepath"
7         "regexp"
8         "runtime"
9         "testing"
10 )
11
12 var (
13         rpcuserRegexp = regexp.MustCompile("(?m)^rpcuser=.+$")
14         rpcpassRegexp = regexp.MustCompile("(?m)^rpcpass=.+$")
15 )
16
17 func TestCreateDefaultConfigFile(t *testing.T) {
18         // find out where the sample config lives
19         _, path, _, ok := runtime.Caller(0)
20         if !ok {
21                 t.Fatalf("Failed finding config file path")
22         }
23         sampleConfigFile := filepath.Join(filepath.Dir(path), "sample-btcd.conf")
24
25         // Setup a temporary directory
26         tmpDir, err := ioutil.TempDir("", "btcd")
27         if err != nil {
28                 t.Fatalf("Failed creating a temporary directory: %v", err)
29         }
30         testpath := filepath.Join(tmpDir, "test.conf")
31
32         // copy config file to location of btcd binary
33         data, err := ioutil.ReadFile(sampleConfigFile)
34         if err != nil {
35                 t.Fatalf("Failed reading sample config file: %v", err)
36         }
37         appPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
38         if err != nil {
39                 t.Fatalf("Failed obtaining app path: %v", err)
40         }
41         tmpConfigFile := filepath.Join(appPath, "sample-btcd.conf")
42         err = ioutil.WriteFile(tmpConfigFile, data, 0644)
43         if err != nil {
44                 t.Fatalf("Failed copying sample config file: %v", err)
45         }
46
47         // Clean-up
48         defer func() {
49                 os.Remove(testpath)
50                 os.Remove(tmpConfigFile)
51                 os.Remove(tmpDir)
52         }()
53
54         err = createDefaultConfigFile(testpath)
55
56         if err != nil {
57                 t.Fatalf("Failed to create a default config file: %v", err)
58         }
59
60         content, err := ioutil.ReadFile(testpath)
61         if err != nil {
62                 t.Fatalf("Failed to read generated default config file: %v", err)
63         }
64
65         if !rpcuserRegexp.Match(content) {
66                 t.Error("Could not find rpcuser in generated default config file.")
67         }
68
69         if !rpcpassRegexp.Match(content) {
70                 t.Error("Could not find rpcpass in generated default config file.")
71         }
72 }