forked from decred/dcrdex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dexcctl_test.go
162 lines (151 loc) · 3.77 KB
/
dexcctl_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"os"
"reflect"
"testing"
"decred.org/dcrdex/dex"
)
func TestConfigure(t *testing.T) {
// print version
os.Args = []string{"", "-V"}
_, _, stop, err := configure()
if err != nil {
t.Fatal(err)
}
if stop != true {
t.Fatal("did not stop for version info")
}
// list commands
os.Args = []string{"", "-l"}
_, _, stop, err = configure()
if err != nil {
t.Fatal(err)
}
if stop != true {
t.Fatal("did not stop when listing commands")
}
// show help
os.Args = []string{"", "-h"}
_, _, stop, err = configure()
if err != nil {
t.Fatal(err)
}
if stop != true {
t.Fatal("did not stop when showing help")
}
// parse command line flags
os.Args = []string{"", "-ubob", "--rpcpass=password123", "-C.nofile"}
cfg, _, _, err := configure()
if err != nil {
t.Fatal(err)
}
if cfg.RPCUser != "bob" && cfg.RPCPass != "password123" {
t.Fatal("incorrectly parsed command line args")
}
// parse config file
tmp, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
cfgFile := tmp + "/testconfig"
defer os.Remove(cfgFile)
b := []byte("rpcaddr=1.2.3.4:3000\nproxyuser=jorb\n")
err = os.WriteFile(cfgFile, b, 0644)
if err != nil {
t.Fatal(err)
}
os.Args = []string{"", "-C" + cfgFile}
cfg, _, _, err = configure()
if err != nil {
t.Fatal(err)
}
if cfg.ProxyUser != "jorb" && cfg.RPCAddr != "1.2.3.4:3000" {
t.Fatal("incorrectly parsed file")
}
// parse args
os.Args = []string{"", "-C.nofile", "arg1", "arg2", "-1"}
_, args, _, err := configure()
if err != nil {
t.Fatal(err)
}
if args[0] != "arg1" || args[1] != "arg2" || args[2] != "-1" {
t.Fatal("arguments not parsed correctly")
}
// bad flag
os.Args = []string{"", "-7"}
_, _, _, err = configure()
if err == nil {
t.Fatal("expected failure on bad flag")
}
}
func TestReadTextFile(t *testing.T) {
saveTextToFile := func(text, filePath string) {
path := dex.CleanAndExpandPath(filePath)
file, err := os.Create(path)
if err != nil {
t.Fatalf("create test file error: %v", err)
}
file.WriteString(text)
file.Close()
}
certTxt := "Hi. I'm a TLS certificate."
cfgTxt := "Hi, I'm a config"
tests := []struct {
name, cmd, txtFilePath, txtToSave string
args, want []string
wantErr bool
}{{
name: "ok with cert",
cmd: "getdexconfig",
args: []string{"1.2.3.4:3000", "./cert"},
txtFilePath: "./cert",
txtToSave: certTxt,
want: []string{"1.2.3.4:3000", certTxt},
}, {
name: "ok no cert",
cmd: "getdexconfig",
args: []string{"1.2.3.4:3000"},
want: []string{"1.2.3.4:3000"},
}, {
name: "not a readCerts command",
cmd: "not a real command",
}, {
name: "no file at path",
cmd: "getdexconfig",
args: []string{"1.2.3.4:3000", "./cert"},
wantErr: true,
}, {
name: "newwallet ok, with cfg file",
cmd: "newwallet",
args: []string{"42", "rpc", "./w.conf"},
txtFilePath: "./w.conf",
txtToSave: cfgTxt,
want: []string{"42", "rpc", cfgTxt},
}, {
name: "newwallet ok, no cfg file",
cmd: "newwallet",
args: []string{"42", "rpc"},
want: []string{"42", "rpc"},
}}
for _, test := range tests {
if test.txtFilePath != "" {
saveTextToFile(test.txtToSave, test.txtFilePath)
}
err := readTextFile(test.cmd, test.args)
os.Remove(test.txtFilePath)
if err != nil {
if test.wantErr {
continue
}
t.Fatalf("unexepected error for %s: %v", test.name, err)
} else if test.wantErr {
t.Fatalf("expected error for test %s", test.name)
}
if !reflect.DeepEqual(test.want, test.args) {
t.Fatalf("wanted %v but got %v for test %s", test.want, test.args, test.name)
}
}
}