2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 require_mods 'URI::Split';
8 use_ok 'PublicInbox::URIimap';
10 is(PublicInbox::URIimap->new('https://example.com/'), undef,
11 'invalid scheme ignored');
13 my $uri = PublicInbox::URIimap->new('imaps://EXAMPLE.com/');
14 is($uri->host, 'EXAMPLE.com', 'host ok');
15 is($uri->canonical->host, 'example.com', 'host canonicalized');
16 is($uri->canonical->as_string, 'imaps://example.com/', 'URI canonicalized');
17 is($uri->port, 993, 'imaps port');
18 is($uri->auth, undef);
19 is($uri->user, undef);
21 $uri = PublicInbox::URIimap->new('imaps://foo@0/');
22 is("$uri", $uri->as_string, '"" overload works');
23 is($uri->host, '0', 'numeric host');
24 is($uri->user, 'foo', 'user extracted');
26 $uri = PublicInbox::URIimap->new('imap://0/INBOX.sub#frag')->canonical;
27 is($uri->as_string, 'imap://0/INBOX.sub', 'no fragment');
28 is($uri->scheme, 'imap');
30 $uri = PublicInbox::URIimap->new('imaps://;AUTH=ANONYMOUS@0/');
31 is($uri->auth, 'ANONYMOUS', 'AUTH=ANONYMOUS accepted');
33 $uri = PublicInbox::URIimap->new('imaps://bar%40example.com;AUTH=99%25@0/');
34 is($uri->auth, '99%', 'decoded AUTH');
35 is($uri->user, 'bar@example.com', 'decoded user');
36 is($uri->mailbox, undef, 'mailbox is undef');
38 $uri = PublicInbox::URIimap->new('imaps://ipv6@[::1]');
39 is($uri->host, '::1', 'IPv6 host');
40 is($uri->mailbox, undef, 'mailbox is undef');
42 $uri = PublicInbox::URIimap->new('imaps://0:666/INBOX');
43 is($uri->port, 666, 'port read');
44 is($uri->mailbox, 'INBOX');
45 $uri = PublicInbox::URIimap->new('imaps://0/INBOX.sub');
46 is($uri->mailbox, 'INBOX.sub');
47 is($uri->scheme, 'imaps');
49 is(PublicInbox::URIimap->new('imap://0:143/')->canonical->as_string,
51 is(PublicInbox::URIimap->new('imaps://0:993/')->canonical->as_string,
54 $uri = PublicInbox::URIimap->new('imap://NSA:Hunter2@0/INBOX');
55 is($uri->user, 'NSA');
56 is($uri->password, 'Hunter2');
57 is($uri->uidvalidity, undef, 'no UIDVALIDITY');
59 $uri = PublicInbox::URIimap->new('imap://0/%');
60 is($uri->mailbox, '%', "RFC 2192 '%' supported");
61 $uri = PublicInbox::URIimap->new('imap://0/%25');
62 $uri = PublicInbox::URIimap->new('imap://0/*');
63 is($uri->mailbox, '*', "RFC 2192 '*' supported");
65 $uri = PublicInbox::URIimap->new('imap://0/mmm;UIDVALIDITY=1');
66 is($uri->mailbox, 'mmm', 'mailbox works with UIDVALIDITY');
67 is($uri->uidvalidity, 1, 'single-digit UIDVALIDITY');
68 $uri = PublicInbox::URIimap->new('imap://0/mmm;UIDVALIDITY=21');
69 is($uri->uidvalidity, 21, 'multi-digit UIDVALIDITY');
70 $uri = PublicInbox::URIimap->new('imap://0/mmm;UIDVALIDITY=bogus');
71 is($uri->uidvalidity, undef, 'bogus UIDVALIDITY');
72 is($uri->uidvalidity(2), 2, 'uid set');
73 is($$uri, 'imap://0/mmm;UIDVALIDITY=2', 'bogus uidvalidity replaced');
74 is($uri->uidvalidity(13), 13, 'uid set');
75 is($$uri, 'imap://0/mmm;UIDVALIDITY=13', 'valid uidvalidity replaced');
77 $uri = PublicInbox::URIimap->new('imap://0/mmm');
78 is($uri->uidvalidity(2), 2, 'uid set');
79 is($$uri, 'imap://0/mmm;UIDVALIDITY=2', 'uidvalidity appended');
80 is($uri->uid, undef, 'no uid');
82 is(PublicInbox::URIimap->new('imap://0/x;uidvalidity=1')->canonical->as_string,
83 'imap://0/x;UIDVALIDITY=1', 'capitalized UIDVALIDITY');
85 $uri = PublicInbox::URIimap->new('imap://0/mmm/;uid=8');
86 is($uri->canonical->as_string, 'imap://0/mmm/;UID=8', 'canonicalized UID');
87 is($uri->mailbox, 'mmm', 'mailbox works with uid');
88 is($uri->uid, 8, 'uid extracted');
89 is($uri->uid(9), 9, 'uid set');
90 is($$uri, 'imap://0/mmm/;UID=9', 'correct uid when stringified');
91 is($uri->uidvalidity(1), 1, 'set uidvalidity with uid');
92 is($$uri, 'imap://0/mmm;UIDVALIDITY=1/;UID=9',
93 'uidvalidity added with uid');
94 is($uri->uidvalidity(4), 4, 'set uidvalidity with uid');
95 is($$uri, 'imap://0/mmm;UIDVALIDITY=4/;UID=9',
96 'uidvalidity replaced with uid');
97 is($uri->uid(3), 3, 'uid set with uidvalidity');
98 is($$uri, 'imap://0/mmm;UIDVALIDITY=4/;UID=3', 'uid replaced properly');
101 is(PublicInbox::URIimap->new($lc)->canonical->as_string, "$$uri",
102 'canonical uppercased both params');
104 is($uri->uid(undef), undef, 'uid can be clobbered');
105 is($$uri, 'imap://0/mmm;UIDVALIDITY=4', 'uid dropped');
107 $uri->auth('ANONYMOUS');
108 is($$uri, 'imap://;AUTH=ANONYMOUS@0/mmm;UIDVALIDITY=4', 'AUTH= set');
109 is($uri->user, undef, 'user is undef w/ AUTH=');
110 is($uri->password, undef, 'password is undef w/ AUTH=');
113 is($$uri, 'imap://foo;AUTH=ANONYMOUS@0/mmm;UIDVALIDITY=4', 'user set w/AUTH');
114 is($uri->password, undef, 'password is undef w/ AUTH= & user');
116 is($$uri, 'imap://foo@0/mmm;UIDVALIDITY=4', 'user remains set w/o auth');
117 is($uri->password, undef, 'password is undef w/ user only');
120 is($$uri, 'imap://bar@0/mmm;UIDVALIDITY=4', 'user set w/o AUTH');
122 is($$uri, 'imap://bar;AUTH=NTML@0/mmm;UIDVALIDITY=4', 'auth set w/user');
125 is($$uri, 'imap://0/mmm;UIDVALIDITY=4', 'auth and user both cleared');
126 is($uri->user, undef, 'user is undef');
127 is($uri->auth, undef, 'auth is undef');
128 is($uri->password, undef, 'password is undef');
129 $uri = PublicInbox::URIimap->new('imap://[::1]:36281/');
130 my $cred = bless { username => $uri->user, password => $uri->password };
131 is($cred->{username}, undef, 'user is undef in array context');
132 is($cred->{password}, undef, 'password is undef in array context');