]> Sergey Matveev's repositories - public-inbox.git/blob - t/hval.t
update copyrights for 2021
[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 is(PublicInbox::Hval::to_filename('foo bar  '), 'foo-bar',
51         'to_filename has no trailing -');
52
53 is(PublicInbox::Hval::to_filename("foo   bar\nanother line\n"), 'foo-bar',
54         'to_filename has no repeated -, and nothing past LF');
55
56 is(PublicInbox::Hval::to_filename("foo....bar"), 'foo.bar',
57         'to_filename squeezes -');
58
59 is(PublicInbox::Hval::to_filename(''), undef, 'empty string returns undef');
60
61 my $s = "\0\x07\n";
62 PublicInbox::Hval::src_escape($s);
63 is($s, "\\0\\a\n", 'src_escape works as intended');
64
65 foreach my $s ('Hello/World.pm', 'Zcat', 'hello world.c', 'ElĂ©anor', '$at') {
66         my $attr = to_attr($s);
67         is(from_attr($attr), $s, "$s => $attr => $s round trips");
68 }
69
70 {
71         my $bad = to_attr('foo//bar');
72         ok(!$bad, 'double-slash rejected');
73 }
74
75 done_testing();