]> Sergey Matveev's repositories - public-inbox.git/blob - t/gcf2.t
No ext_urls
[public-inbox.git] / t / gcf2.t
1 #!perl -w
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>
4 use strict;
5 use PublicInbox::TestCommon;
6 use Test::More;
7 use Fcntl qw(:seek);
8 use IO::Handle ();
9 use POSIX qw(_exit);
10 use Cwd qw(abs_path);
11 require_mods('PublicInbox::Gcf2');
12 use_ok 'PublicInbox::Gcf2';
13 use PublicInbox::Import;
14 my ($tmpdir, $for_destroy) = tmpdir();
15
16 my $gcf2 = PublicInbox::Gcf2::new();
17 is(ref($gcf2), 'PublicInbox::Gcf2', '::new works');
18 my $COPYING = 'dba13ed2ddf783ee8118c6a581dbf75305f816a3';
19 open my $agpl, '<', 'COPYING' or BAIL_OUT "AGPL-3 missing: $!";
20 $agpl = do { local $/; <$agpl> };
21
22 PublicInbox::Import::init_bare($tmpdir);
23 my $fi_data = './t/git.fast-import-data';
24 my $rdr = {};
25 open $rdr->{0}, '<', $fi_data or BAIL_OUT $!;
26 xsys([qw(git fast-import --quiet)], { GIT_DIR => $tmpdir }, $rdr);
27 is($?, 0, 'fast-import succeeded');
28 $gcf2->add_alternate("$tmpdir/objects");
29
30 {
31         my ($r, $w);
32         pipe($r, $w) or BAIL_OUT $!;
33         my $tree = 'fdbc43725f21f485051c17463b50185f4c3cf88c';
34         $gcf2->cat_oid(fileno($w), $tree);
35         close $w;
36         is("$tree tree 30\n", <$r>, 'tree header ok');
37         $r = do { local $/; <$r> };
38         is(chop($r), "\n", 'got trailing newline');
39         is(length($r), 30, 'tree length matches');
40 }
41
42 chomp(my $objdir = xqx([qw(git rev-parse --git-path objects)]));
43 if ($objdir =~ /\A--git-path\n/) { # git <2.5
44         chomp($objdir = xqx([qw(git rev-parse --git-dir)]));
45         $objdir .= '/objects';
46 }
47 if ($objdir && -d $objdir) {
48         $objdir = abs_path($objdir);
49         open my $alt, '>>', "$tmpdir/objects/info/alternates" or
50                                                         BAIL_OUT $!;
51         print $alt $objdir, "\n" or BAIL_OUT $!;
52         close $alt or BAIL_OUT $!;
53
54         # calling gcf2->add_alternate on an already-added path won't
55         # cause alternates to be reloaded, so we do
56         # $gcf2->add_alternate($objdir) later on instead of
57         # $gcf2->add_alternate("$tmpdir/objects");
58         # $objdir = "$tmpdir/objects";
59 } else {
60         $objdir = undef
61 }
62
63 my $nr = $ENV{TEST_LEAK_NR};
64 my $cat = $ENV{TEST_LEAK_CAT} // 10;
65 diag "checking for leaks... (TEST_LEAK_NR=$nr TEST_LEAK_CAT=$cat)" if $nr;
66
67 SKIP: {
68         skip 'not in git worktree', 21 unless defined($objdir);
69         $gcf2->add_alternate($objdir);
70         eval { $gcf2->add_alternate($objdir) };
71         ok(!$@, 'no error adding alternate redundantly');
72         if ($nr) {
73                 diag "adding alternate $nr times redundantly";
74                 $gcf2->add_alternate($objdir) for (1..$nr);
75                 diag 'done adding redundant alternates';
76         }
77
78         open my $fh, '+>', undef or BAIL_OUT "open: $!";
79         $fh->autoflush(1);
80
81         ok(!$gcf2->cat_oid(fileno($fh), 'invalid'), 'invalid fails');
82         seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!";
83         is(do { local $/; <$fh> }, '', 'nothing written');
84
85         open $fh, '+>', undef or BAIL_OUT "open: $!";
86         ok(!$gcf2->cat_oid(fileno($fh), '0'x40), 'z40 fails');
87         seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!";
88         is(do { local $/; <$fh> }, '', 'nothing written for z40');
89
90         open $fh, '+>', undef or BAIL_OUT "open: $!";
91         my $ck_copying = sub {
92                 my ($desc) = @_;
93                 seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!";
94                 is(<$fh>, "$COPYING blob 34520\n", "got expected header $desc");
95                 my $buf = do { local $/; <$fh> };
96                 is(chop($buf), "\n", 'got trailing \\n');
97                 is($buf, $agpl, "AGPL matches ($desc)");
98         };
99         ok($gcf2->cat_oid(fileno($fh), $COPYING), 'cat_oid normal');
100         $ck_copying->('regular file');
101
102         $gcf2 = PublicInbox::Gcf2::new();
103         $gcf2->add_alternate("$tmpdir/objects");
104         open $fh, '+>', undef or BAIL_OUT "open: $!";
105         ok($gcf2->cat_oid(fileno($fh), $COPYING), 'cat_oid alternate');
106         $ck_copying->('alternates after reopen');
107
108         $^O eq 'linux' or skip('pipe tests are Linux-only', 14);
109         for my $blk (1, 0) {
110                 my ($r, $w);
111                 pipe($r, $w) or BAIL_OUT $!;
112                 fcntl($w, 1031, 4096) or
113                         skip('Linux too old for F_SETPIPE_SZ', 14);
114                 $w->blocking($blk);
115                 seek($fh, 0, SEEK_SET) or BAIL_OUT "seek: $!";
116                 truncate($fh, 0) or BAIL_OUT "truncate: $!";
117                 my $pid = fork // BAIL_OUT "fork: $!";
118                 if ($pid == 0) {
119                         close $w;
120                         tick; # wait for parent to block on writev
121                         my $buf = do { local $/; <$r> };
122                         print $fh $buf or _exit(1);
123                         _exit(0);
124                 }
125                 ok($gcf2->cat_oid(fileno($w), $COPYING), "cat blocking=$blk");
126                 close $w or BAIL_OUT "close: $!";
127                 is(waitpid($pid, 0), $pid, 'child exited');
128                 is($?, 0, 'no error in child');
129                 $ck_copying->("pipe blocking($blk)");
130
131                 pipe($r, $w) or BAIL_OUT $!;
132                 fcntl($w, 1031, 4096) or BAIL_OUT $!;
133                 $w->blocking($blk);
134                 close $r;
135                 local $SIG{PIPE} = 'IGNORE';
136                 eval { $gcf2->cat_oid(fileno($w), $COPYING) };
137                 like($@, qr/writev error:/, 'got writev error');
138         }
139 }
140
141 if ($nr) {
142         open my $null, '>', '/dev/null' or BAIL_OUT "open /dev/null: $!";
143         my $fd = fileno($null);
144         local $SIG{PIPE} = 'IGNORE';
145         my ($r, $w);
146         pipe($r, $w);
147         close $r;
148         my $broken = fileno($w);
149         for (1..$nr) {
150                 my $obj = PublicInbox::Gcf2::new();
151                 if (defined($objdir)) {
152                         $obj->add_alternate($objdir);
153                         for (1..$cat) {
154                                 $obj->cat_oid($fd, $COPYING);
155                                 eval { $obj->cat_oid($broken, $COPYING) };
156                                 $obj->cat_oid($fd, '0'x40);
157                                 $obj->cat_oid($fd, 'invalid');
158                         }
159                 }
160         }
161 }
162 done_testing;