comparison cmd/sievemgr/internal/config/config_test.go @ 5:4dff4c3f0fbb

Introduce configuration file where account information is specified Introduce a configuration file where account information must be specified instead of passing it with each invocation on the command line. Each account has a name by which it can be selected and one may be specified as the default account. This is intended to improve usability for productive usage. Enforce strict permissions since passwords may be specified for non-interactive usage. Remove command-line flags for passing account information.
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Tue, 03 Nov 2020 23:44:45 +0100
parents
children 854167f55839
comparison
equal deleted inserted replaced
4:f925f15d8ce5 5:4dff4c3f0fbb
1 // Copyright (C) 2020 Guido Berhoerster <guido+sievemgr@berhoerster.name>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 package config_test
23
24 import (
25 "testing"
26
27 "go.guido-berhoerster.org/sievemgr/cmd/sievemgr/internal/config"
28 )
29
30 const basicConfig = "\ufeff" + `account "foo"
31 host "imap.example.net"
32 user "bar@example.net" pass "53cRe7"
33 default
34 account "local"
35 host "localhost"
36 port 2000
37 insecure
38 user "foo"
39 pass "s3cR3Et"
40 account "bar" host "imap.example.com" user "bar@example.com" pass "53cRe7"
41 `
42
43 func TestBasicFunctionality(t *testing.T) {
44 var conf config.Configuration
45 if err := config.Parse([]byte(basicConfig), &conf); err != nil {
46 t.Fatalf("failed to parse basic config file: %s", err)
47 }
48 if n := len(conf.Accounts); n != 3 {
49 t.Fatalf("invalid number of parsed accounts, expected 2, got %d", n)
50 }
51 if conf.Accounts[0].Name != "foo" ||
52 conf.Accounts[0].Host != "imap.example.net" ||
53 conf.Accounts[0].Port != "4190" ||
54 conf.Accounts[0].User != "bar@example.net" ||
55 conf.Accounts[0].Password != "53cRe7" ||
56 conf.Accounts[0].Insecure {
57 t.Fatalf(`failed to parse account, expected &main.Account{Name:"foo", Host:"imap.example.net", Port:"4190", User:"bar@example.net", Password:"53cRe7", Insecure:false}, got %#+v`, conf.Accounts[0])
58 }
59 if conf.Default == nil {
60 t.Fatalf("default account not found")
61 }
62 if conf.Default != conf.Accounts[0] {
63 t.Fatalf("wrong default account, expected \"default\", got %q", conf.Default.Name)
64 }
65 }
66
67 const invalidBOMConfig = "\ufeff\ufeff" + `account "foo" host "imap.example.net" user "bar@example.net" pass "53cRe7" default`
68
69 func TestInvalidBOM(t *testing.T) {
70 var conf config.Configuration
71 err := config.Parse([]byte(invalidBOMConfig), &conf)
72 if err == nil {
73 t.Fatalf("expected error due to BOM not at the beginning but succeeded")
74 }
75 if _, ok := err.(*config.ParserError); !ok {
76 t.Fatalf("expected config.ParserError, got %T (%q)", err,
77 err)
78 }
79 t.Logf("reported error: %s", err)
80 }
81
82 const invalidUTF8Config = `account "foo"` + "\xff" + ` host "imap.example.net" user "bar@example.net" pass "53cRe7" default`
83
84 func TestInvalidUTF8(t *testing.T) {
85 var conf config.Configuration
86 err := config.Parse([]byte(invalidUTF8Config), &conf)
87 if err == nil {
88 t.Fatalf("expected error due to invalid UTF-8 but succeeded")
89 }
90 if _, ok := err.(*config.ParserError); !ok {
91 t.Fatalf("expected config.ParserError, got %T (%q)", err,
92 err)
93 }
94 t.Logf("reported error: %s", err)
95 }
96
97 const nulByteConfig = `account "foo` + "\x00" + `" host "imap.example.net" user "bar@example.net" pass "53cRe7" default`
98
99 func TestNulByte(t *testing.T) {
100 var conf config.Configuration
101 err := config.Parse([]byte(nulByteConfig), &conf)
102 if err == nil {
103 t.Fatalf("expected error due to nul byte but succeeded")
104 }
105 if _, ok := err.(*config.ParserError); !ok {
106 t.Fatalf("expected config.ParserError, got %T (%q)", err,
107 err)
108 }
109 t.Logf("reported error: %s", err)
110 }
111
112 const unexpectedRuneConfig = `account "foo" host "imap.example.net" user "bar@example.net" pass "53cRe7" _in_valid default`
113
114 func TestInvalidIdentifier(t *testing.T) {
115 var conf config.Configuration
116 err := config.Parse([]byte(unexpectedRuneConfig), &conf)
117 if err == nil {
118 t.Fatalf("expected error due to unexpected rune but succeeded")
119 }
120 if _, ok := err.(*config.ParserError); !ok {
121 t.Fatalf("expected config.ParserError, got %T (%q)", err,
122 err)
123 }
124 t.Logf("reported error: %s", err)
125 }
126
127 const unknownIdentifierConfig = `account "foo" host "imap.example.net" user "bar@example.net" pass "53cRe7" invalid default`
128
129 func TestUnknownIdentifier(t *testing.T) {
130 var conf config.Configuration
131 err := config.Parse([]byte(unknownIdentifierConfig), &conf)
132 if err == nil {
133 t.Fatalf("expected error due to unknown identifier but succeeded")
134 }
135 if _, ok := err.(*config.ParserError); !ok {
136 t.Fatalf("expected config.ParserError, got %T (%q)", err,
137 err)
138 }
139 t.Logf("reported error: %s", err)
140 }
141
142 const missingWhitespaceConfig = `account "foo" host "imap.example.net" port 2000default user "bar@example.net" pass "53cRe7"`
143
144 func TestMissingSpace(t *testing.T) {
145 var conf config.Configuration
146 err := config.Parse([]byte(missingWhitespaceConfig), &conf)
147 if err == nil {
148 t.Fatalf("expected error due to missing whitespace between tokens but succeeded")
149 }
150 if _, ok := err.(*config.ParserError); !ok {
151 t.Fatalf("expected config.ParserError, got %T (%q)", err,
152 err)
153 }
154 t.Logf("reported error: %s", err)
155 }
156
157 const missingNameConfig = `account host "imap.example.net" user "bar@example.net" pass "53cRe7" default`
158
159 func TestMissingName(t *testing.T) {
160 var conf config.Configuration
161 err := config.Parse([]byte(missingNameConfig), &conf)
162 if err == nil {
163 t.Fatalf("expected error due to missing account name but succeeded")
164 }
165 if _, ok := err.(*config.ParserError); !ok {
166 t.Fatalf("expected config.ParserError, got %T (%q)", err,
167 err)
168 }
169 t.Logf("reported error: %s", err)
170 }
171
172 const invalidTypeConfig = `account 1234 host "imap.example.net" user "bar@example.net" pass "53cRe7"`
173
174 func TestInvalidType(t *testing.T) {
175 var conf config.Configuration
176 err := config.Parse([]byte(invalidTypeConfig), &conf)
177 if err == nil {
178 t.Fatalf("expected error due to invalid type but succeeded")
179 }
180 if _, ok := err.(*config.ParserError); !ok {
181 t.Fatalf("expected config.ParserError, got %T (%q)", err,
182 err)
183 }
184 t.Logf("reported error: %s", err)
185 }
186
187 const unterminatedStringConfig = `account "foo" host "imap.example.net" user "bar@example.net" pass "53cRe7
188 account "bar" host "imap.example.com" user "bar@example.com" pass "53cRe7"
189 `
190
191 func TestUnterminatedString(t *testing.T) {
192 var conf config.Configuration
193 err := config.Parse([]byte(unterminatedStringConfig), &conf)
194 if err == nil {
195 t.Fatalf("expected error due to an unterminated string but succeeded")
196 }
197 if _, ok := err.(*config.ParserError); !ok {
198 t.Fatalf("expected config.ParserError, got %T (%q)", err,
199 err)
200 }
201 t.Logf("reported error: %s", err)
202 }
203
204 const unexpectedEOFConfig = `account "foo" host "imap.example.net" user "bar@example.net" pass `
205
206 func TestUnexpectedEOF(t *testing.T) {
207 var conf config.Configuration
208 err := config.Parse([]byte(unexpectedEOFConfig), &conf)
209 if err == nil {
210 t.Fatalf("expected error due to an unterminated string but succeeded")
211 }
212 if _, ok := err.(*config.ParserError); !ok {
213 t.Fatalf("expected config.ParserError, got %T (%q)", err,
214 err)
215 }
216 t.Logf("reported error: %s", err)
217 }
218
219 const missingHostConfig = `account "foo" user "bar@example.net" pass "53cRe7"`
220
221 func TestMissingHost(t *testing.T) {
222 var conf config.Configuration
223 err := config.Parse([]byte(missingHostConfig), &conf)
224 if err == nil {
225 t.Fatalf("expected error due to missing host but succeeded")
226 }
227 if _, ok := err.(*config.ParserError); !ok {
228 t.Fatalf("expected config.ParserError, got %T (%q)", err,
229 err)
230 }
231 t.Logf("reported error: %s", err)
232 }
233
234 const missingUserConfig = `account "foo" host "imap.example.net" user "" pass "53cRe7"`
235
236 func TestMissingUser(t *testing.T) {
237 var conf config.Configuration
238 err := config.Parse([]byte(missingUserConfig), &conf)
239 if err == nil {
240 t.Fatalf("expected error due to missing user but succeeded")
241 }
242 if _, ok := err.(*config.ParserError); !ok {
243 t.Fatalf("expected config.ParserError, got %T (%q)", err,
244 err)
245 }
246 t.Logf("reported error: %s", err)
247 }