]> Sergey Matveev's repositories - public-inbox.git/blob - t/hval.t
xt/mem-imapd-tls: update aliases to DSdeflate subs
[public-inbox.git] / t / hval.t
1 # Copyright (C) 2017-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use_ok 'PublicInbox::Hval', qw(to_attr);
7
8 # reverse the result of to_attr
9 sub from_attr ($) {
10         my ($str) = @_;
11         my $first = '';
12         if ($str =~ s/\AZ([a-f0-9]{2})//ms) {
13                 $first = chr(hex($1));
14         }
15         $str =~ s!::([a-f0-9]{2})!chr(hex($1))!egms;
16         $str =~ tr!:!/!;
17         utf8::decode($str);
18         $first . $str;
19 }
20
21 my $ibx = {
22         -no_obfuscate_re => qr/(?:example\.com)\z/i,
23         -no_obfuscate => {
24                 'meta@public-inbox.org' => 1,
25         }
26 };
27
28 my $html = <<'EOF';
29 hello@example.comm
30 hello@example.com
31 meta@public-inbox.org
32 test@public-inbox.org
33 test@a.b.c.org
34 te.st@example.org
35 EOF
36
37 PublicInbox::Hval::obfuscate_addrs($ibx, $html);
38
39 my $exp = <<'EOF';
40 hello@example&#8226;comm
41 hello@example.com
42 meta@public-inbox.org
43 test@public-inbox&#8226;org
44 test@a&#8226;b.c.org
45 te.st@example&#8226;org
46 EOF
47
48 is($html, $exp, 'only obfuscated relevant addresses');
49
50 $exp = 'https://example.net/foo@example.net';
51 PublicInbox::Hval::obfuscate_addrs($ibx, my $res = $exp);
52 is($res, $exp, 'does not obfuscate URL with Message-ID');
53
54 is(PublicInbox::Hval::to_filename('foo bar  '), 'foo-bar',
55         'to_filename has no trailing -');
56
57 is(PublicInbox::Hval::to_filename("foo   bar\nanother line\n"), 'foo-bar',
58         'to_filename has no repeated -, and nothing past LF');
59
60 is(PublicInbox::Hval::to_filename("foo....bar"), 'foo.bar',
61         'to_filename squeezes -');
62
63 is(PublicInbox::Hval::to_filename(''), undef, 'empty string returns undef');
64
65 my $s = "\0\x07\n";
66 PublicInbox::Hval::src_escape($s);
67 is($s, "\\0\\a\n", 'src_escape works as intended');
68
69 foreach my $s ('Hello/World.pm', 'Zcat', 'hello world.c', 'ElĂ©anor', '$at') {
70         my $attr = to_attr($s);
71         is(from_attr($attr), $s, "$s => $attr => $s round trips");
72 }
73
74 {
75         my $bad = to_attr('foo//bar');
76         ok(!$bad, 'double-slash rejected');
77 }
78
79 done_testing();