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