5 #ifdef HAVE___ATTRIBUTE__
6 #define UNUSED(x) /*@unused@*/ x __attribute__((unused))
12 #define DEFROB(x) memfrob(x, x ## _len)
13 #define ENFROB(x) memfrob(x, x ## _len)
22 #include "passwordMgr.h"
26 void ProcessPendingEvents(void)
36 int debug_default
= DEBUG_INFO
;
39 /* return 1 if fail, 0 if success */
40 int test_backtickExpand(void)
42 const char *tests
[] = { "prefix`echo 1`suffix",
49 const char *solns
[] = { "prefix1suffix",
58 for (i
= 0; tests
[i
] != NULL
; i
++) {
59 char *x
= backtickExpand(NULL
, tests
[i
]);
60 if (strcmp(x
, solns
[i
]) != 0) {
61 printf("test failed: [%s] != [%s]\n", tests
[i
], solns
[i
]);
66 printf("backtick tests complete\n");
70 #define CKSTRING(x,shouldbe) if(strcmp(x,shouldbe)) { \
71 printf("FAILED: expected '" #shouldbe "' but got '%s'\n", x); \
72 return 1; } else { printf("good: '" shouldbe "' == '%s'\n", x); }
74 /* return 1 if fail, 0 if success */
75 int test_passwordMgr(void)
81 /* sh is almost certainly conforming; owned by root, etc. */
82 if (!permissions_ok(NULL
, "/bin/sh")) {
83 printf("FAILURE: permission checker failed on /bin/sh.");
86 /* tmp is definitely bad; and better be og+w */
87 if (permissions_ok(NULL
, "/tmp")) {
88 printf("FAILURE: permission checker failed on /tmp.");
91 /* TODO: also find some user-owned binary that shouldn't be g+w */
92 printf("SUCCESS: permission checker sanity check went well.\n");
95 m
.askpass
= "echo xlnt; #";
97 b
= passwordFor("bill", "ted", &m
, 0);
98 if (strcmp(b
, "xlnt") != 0) {
99 printf("FAILURE: expected 'xlnt' got '%s'\n", b
);
102 printf("SUCCESS: expected 'xlnt' got '%s'\n", b
);
105 m
.askpass
= "should be cached";
106 b
= passwordFor("bill", "ted", &m
, 0);
107 if (strcmp(b
, "xlnt") != 0) {
108 printf("FAILURE: expected 'xlnt' got '%s'\n", b
);
112 printf("SUCCESS: cached 'xlnt' correctly\n");
115 m
.askpass
= "echo abcdefghi1abcdefghi2abcdefghi3a; #";
117 b
= passwordFor("abbot", "costello", &m
, 0);
118 if (strcmp(b
, "abcdefghi1abcdefghi2abcdefghi3a") != 0) {
120 ("FAILURE: expected 'abcdefghi1abcdefghi2abcdefghi3a' got '%s'\n",
125 ("SUCCESS: expected 'abcdefghi1abcdefghi2abcdefghi3a' got '%s'\n",
128 /* try overflowing the buffer */
129 m
.askpass
= "echo abcdefghi1abcdefghi2abcdefghi3ab; #";
130 b
= passwordFor("laverne", "shirley", &m
, 0);
131 /* should come back truncated to fill the buffer */
132 if (strcmp(b
, "abcdefghi1abcdefghi2abcdefghi3a") != 0) {
134 ("FAILURE: expected 'abcdefghi1abcdefghi2abcdefghi3a' got '%s'\n",
139 ("SUCCESS: expected 'abcdefghi1abcdefghi2abcdefghi3a' got '%s'\n",
142 /* make sure we still have the old one */
143 b
= passwordFor("bill", "ted", &m
, 0);
144 if (strcmp(b
, "xlnt") != 0) {
145 printf("FAILURE: expected 'xlnt' got '%s'\n", b
);
148 printf("SUCCESS: expected 'xlnt' got '%s'\n", b
);
150 /* what it's like if ssh-askpass is cancelled - should drop the mailbox */
152 /* will exit on our behalf; not so good for continued testing. */
153 m
.askpass
= "echo -n ; #";
154 b
= passwordFor("abort", "me", &m
, 0);
155 if (strcmp(b
, "") != 0) {
156 printf("FAILURE: expected '' got '%s'\n", b
);
159 printf("SUCCESS: expected '' got '%s'\n", b
);
162 /* what it's like if ssh-askpass is ok'd with an empty password. */
163 m
.askpass
= "echo ; #";
164 b
= passwordFor("try", "again", &m
, 0);
165 if (b
== NULL
|| strcmp(b
, "") != 0) {
166 printf("FAILURE: expected '' got '%s'\n", b
? b
: "(null)");
169 printf("SUCCESS: expected '' got '%s'\n", b
);
171 m
.askpass
= "echo \"rt*m\"; #";
172 b
= passwordFor("faq", "faq", &m
, 0);
173 if (strcmp(b
, "rt*m") != 0) {
174 printf("FAILURE: expected '' got '%s'\n", b
);
177 printf("SUCCESS: expected 'rt*m' got '%s'\n", b
);
181 #define CKINT(x,shouldbe) if(x != shouldbe) { \
182 printf("Failed: expected '" #shouldbe "' but got '%d'\n", x); \
184 int test_imap4creator(void)
188 if (imap4Create(&m
, "imap:foo:@bar/mybox")) {
191 CKSTRING(m
.path
, "mybox");
192 CKSTRING(m
.u
.pop_imap
.serverName
, "bar");
193 CKINT(m
.u
.pop_imap
.serverPort
, 143);
195 if (imap4Create(&m
, "imap:foo:@bar/\"mybox\"")) {
198 CKSTRING(m
.path
, "\"mybox\"");
199 CKSTRING(m
.u
.pop_imap
.serverName
, "bar");
200 CKINT(m
.u
.pop_imap
.serverPort
, 143);
202 if (imap4Create(&m
, "imap:foo:@192.168.1.1/\"mybox\"")) {
204 ("FAILED: to create IMAP box with IP address for servername\n");
207 CKSTRING(m
.path
, "\"mybox\"");
208 CKSTRING(m
.u
.pop_imap
.serverName
, "192.168.1.1");
209 CKINT(m
.u
.pop_imap
.serverPort
, 143);
211 if (imap4Create(&m
, "imap:foo:@bar/\"space box\"")) {
214 CKSTRING(m
.path
, "\"space box\"");
215 CKSTRING(m
.u
.pop_imap
.serverName
, "bar");
216 CKINT(m
.u
.pop_imap
.serverPort
, 143);
218 if (imap4Create(&m
, "imap:user pass bar/\"space box\"")) {
221 CKSTRING(m
.path
, "\"space box\"");
222 CKSTRING(m
.u
.pop_imap
.serverName
, "bar");
223 CKINT(m
.u
.pop_imap
.serverPort
, 143);
225 if (imap4Create(&m
, "imap:star *as* star/\"space box\"")) {
228 printf("mmm %s", (m
.u
.pop_imap
.password
));
229 DEFROB(m
.u
.pop_imap
.password
);
230 CKSTRING(m
.u
.pop_imap
.password
, "*as*");
231 CKINT(m
.u
.pop_imap
.serverPort
, 143);
232 if (imap4Create(&m
, "imap:user:*as*@bar/blah")) {
236 DEFROB(m
.u
.pop_imap
.password
);
237 CKSTRING(m
.u
.pop_imap
.password
, "*as*");
238 CKINT(m
.u
.pop_imap
.serverPort
, 143);
240 if (imap4Create(&m
, "imap:user pass bar/\"space box\" 12")) {
243 CKSTRING(m
.path
, "\"space box\"");
244 CKSTRING(m
.u
.pop_imap
.serverName
, "bar");
245 CKINT(m
.u
.pop_imap
.serverPort
, 12);
247 if (imap4Create(&m
, "imap:foo:@bar/\"mybox\":12")) {
250 CKSTRING(m
.path
, "\"mybox\"");
251 CKSTRING(m
.u
.pop_imap
.serverName
, "bar");
252 CKINT(m
.u
.pop_imap
.serverPort
, 12);
254 if (imap4Create(&m
, "imap:foo:@bar/\"mybox\":12 auth")) {
257 CKSTRING(m
.path
, "\"mybox\"");
258 CKSTRING(m
.u
.pop_imap
.serverName
, "bar");
259 CKINT(m
.u
.pop_imap
.serverPort
, 12);
260 CKSTRING(m
.u
.pop_imap
.authList
, "auth");
262 if (imap4Create(&m
, "imap:foo:@bar/\"mybox\":12 cram-md5 plaintext")) {
265 CKSTRING(m
.u
.pop_imap
.authList
, "cram-md5 plaintext");
267 if (imap4Create(&m
, "imap:foo:@bar/\"mybox\":12 CRAm-md5 plainTEXt")) {
270 CKSTRING(m
.u
.pop_imap
.authList
, "cram-md5 plaintext");
272 /* doesn't really matter, as the # is gobbled by the parser as a comment. */
274 (&m
, "imap:harry:has#pass@bar/\"mybox\":12 CRAm-md5 plainTEXt")) {
277 CKSTRING(m
.u
.pop_imap
.userName
, "harry");
278 DEFROB(m
.u
.pop_imap
.password
);
279 CKSTRING(m
.u
.pop_imap
.password
, "has#pass");
282 if (pop3Create(&m
, "pop3:foo:@bar:12 cram-md5 plaintext")) {
285 CKSTRING(m
.u
.pop_imap
.authList
, "cram-md5 plaintext");
287 /* should not parse this; it is ambiguous. */
288 if (!imap4Create(&m
, "imap:foo:mi@ta@bar/mybox") && !Relax
) {
292 /* should not parse this; it is ambiguous. */
293 if (!imap4Create(&m
, "imap:user pa ss bar/\"space box\" 12") && !Relax
) {
297 /* should not parse this; it is ambiguous. */
298 if (!pop3Create(&m
, "pop3:user pa ss bar 12") && !Relax
) {
306 int test_getline_from_buffer(void)
308 #define LINE_BUF_LEN 256
309 char linebuf
[LINE_BUF_LEN
];
310 char scratchbuf
[LINE_BUF_LEN
];
312 /* try to ensure that even an endless loop terminates */
314 strcpy(scratchbuf
, "\r\n");
315 getline_from_buffer(scratchbuf
, linebuf
, LINE_BUF_LEN
);
316 if (strlen(scratchbuf
) != 0) {
317 printf("FAILURE: scratchbuf not snarfed\n");
320 if (strlen(linebuf
) != 2) {
321 printf("FAILURE: linebuf not populated\n");
324 strcpy(scratchbuf
, "\n");
325 getline_from_buffer(scratchbuf
, linebuf
, LINE_BUF_LEN
);
326 if (strlen(scratchbuf
) != 0) {
327 printf("FAILURE: scratchbuf not snarfed\n");
330 if (strlen(linebuf
) != 1) {
331 printf("FAILURE: linebuf not populated\n");
339 int test_charutil(void)
342 char *v
= strdup("abc#def");
345 if (strcmp(v
, "abc#def") != 0) {
346 printf("FAILURE: comment stripper stripped when it shouldn't\n");
350 v
= strdup("abc #def");
353 if (strcmp(v
, "abc ") != 0) {
354 printf("FAILURE: comment stripper should've stripped\n");
362 #include <sys/types.h>
363 #include <netinet/in.h>
364 #include <sys/socket.h>
365 int test_sock_connect(void)
367 struct sockaddr_in addr
;
368 int s
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
369 socklen_t addrlen
= sizeof(struct sockaddr_in
);
374 addr
.sin_family
= AF_INET
;
375 addr
.sin_addr
.s_addr
= 0;
377 if (bind(s
, (const struct sockaddr
*)&addr
, sizeof(struct sockaddr_in
)) < 0) {
381 getsockname(s
, (struct sockaddr
*)&addr
, &addrlen
);
382 if (listen(s
, 5) < 0) {
386 if (sock_connect("127.0.0.1", htons(addr
.sin_port
)) < 0) {
389 if (sock_connect("localhost", htons(addr
.sin_port
)) < 0) {
396 int print_info(UNUSED(void *state
))
400 const char *certificate_filename
= NULL
;
401 const char *tls
= "NORMAL";
402 int SkipCertificateCheck
= 0;
403 int exists(UNUSED(const char *filename
))
409 // void initialize_blacklist(void) { }
410 // void tlscomm_printf(UNUSED(int x), UNUSED(const char *f), ...) { }
411 // void tlscomm_expect(void) { }
412 // void tlscomm_close() { }
413 // int tlscomm_is_blacklisted(UNUSED(const char *x)) { return 1; }
414 // void initialize_gnutls(void) { }
415 // int sock_connect(UNUSED(const char *n), UNUSED(int p)) { return 1; } /* stdout */
416 // void initialize_unencrypted(void) { }
418 int main(UNUSED(int argc
), UNUSED(char *argv
[]))
421 if (test_backtickExpand() || test_passwordMgr()) {
422 printf("SOME TESTS FAILED!\n");
427 if (test_imap4creator()) {
428 printf("SOME TESTS FAILED!\n");
432 if (test_sock_connect()) {
433 printf("SOME TESTS FAILED!\n");
438 if (test_imap4creator()) {
439 printf("SOME TESTS FAILED!\n");
443 if (test_getline_from_buffer()) {
444 printf("SOME TESTS FAILED!\n");
448 if (test_charutil()) {
449 printf("SOME TESTS FAILED!\n");
453 if (test_sock_connect()) {
454 printf("SOME TESTS FAILED!\n");
458 printf("Success! on all tests.\n");