Server/Storage/Test.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.4
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Server/PSGI.pm ↑ Summary
1 package Net::Blossom::Server::Storage::Test;
2  
3 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Test::More ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Net::Blossom::BlobDescriptor;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶use Net::Blossom::Server;
Subroutine BEGIN: called CC=1 SCAR=2.6
13 use Net::Blossom::Server::BlobResult;
14 use Net::Blossom::Server::Storage;
15 use Net::Blossom::Server::UploadResult;
16  
17 our @EXPORT_OK = qw(run_storage_contract_tests);
18  
19 my $PUBKEY = '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798';
20 my $OTHER_PUBKEY = '266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5';
21 my $MISSING_SHA256 = 'f' x 64;
22  
23 sub run_storage_contract_tests {
24 my %args = @_;
25 my $name = delete $args{name};
26 my $factory = delete $args{factory};
27 croak "unknown argument(s): " . join(', ', sort keys %args) if keys %args;
28 croak "name is required" unless defined $name && !ref($name) && length $name;
29 croak "factory must be a code reference" unless ref($factory) eq 'CODE';
30  
31 Test::More::subtest $name => sub {
32 _committed_uploads_are_visible($factory);
33 _head_blob_returns_metadata_if_supported($factory);
34 _get_blob_range_returns_requested_bytes_if_supported($factory);
35 _failed_uploads_are_not_visible($factory);
36 _delete_hides_blob($factory);
37 _delete_is_pubkey_scoped($factory);
38 };
39  
40 return 1;
41 }
42  
43 sub _get_blob_range_returns_requested_bytes_if_supported {
44 my ($factory) = @_;
45 my $storage = _fresh_storage($factory);
46  
47 if (!$storage->can('get_blob_range')) {
48 Test::More::pass('optional get_blob_range is not implemented');
49 return;
50 }
51  
52 my $body = "range contract body\n";
53 my $blob = _upload($storage, $body, 1725105921)->descriptor;
54 my $range = $storage->get_blob_range(
55 $blob->sha256,
56 offset => 6,
57 length => 8,
58 );
59 Test::More::is(_body_to_scalar($range), 'contract',
60 'get_blob_range returns only requested bytes');
61 Test::More::is(
62 $storage->get_blob_range($MISSING_SHA256, offset => 0, length => 1),
63 undef,
64 'get_blob_range returns undef for missing blobs',
65 );
66 }
67  
68 sub _committed_uploads_are_visible {
69 my ($factory) = @_;
70 my $storage = _fresh_storage($factory);
71 my $server = Net::Blossom::Server->new(storage => $storage, clock => sub { 1725105921 });
72 my $body = "contract blob one\n";
73 my $sha256 = sha256_hex($body);
74  
75 my $result = $server->receive_blob(
76 $body,
77 type => 'text/plain',
78 expected_sha256 => $sha256,
79 content_length => length($body),
80 pubkey => $PUBKEY,
81 );
82  
83 _is_upload_result($result, 1, 'new upload result');
84 my $blob = $result->descriptor;
85 _is_descriptor($blob, $sha256, length($body), 'text/plain', 1725105921, 'upload descriptor');
86 _is_blob_result($storage->get_blob($sha256), $sha256, length($body), 'text/plain', 1725105921, $body, 'get_blob result');
87  
88 my $second = _upload($storage, "contract blob two\n", 1725105922);
89 my $same_time_a = _upload($storage, "same timestamp a\n", 1725105923);
90 my $same_time_b = _upload($storage, "same timestamp b\n", 1725105923);
91 my @same_time = sort { $a->sha256 cmp $b->sha256 } (
92 $same_time_a->descriptor,
93 $same_time_b->descriptor,
94 );
95 my @ordered = (
96 (map { $_->sha256 } @same_time),
97 $second->descriptor->sha256,
98 $sha256,
99 );
100  
101 my $listed = $storage->list_blobs($PUBKEY, limit => 10);
102 _is_descriptor_list($listed, \@ordered, 'list_blobs returns uploaded-desc sha256-asc order');
103  
104 my $first_page = $storage->list_blobs($PUBKEY, limit => 1);
105 _is_descriptor_list($first_page, [$ordered[0]], 'list_blobs honors limit');
106  
107 my $page = $storage->list_blobs($PUBKEY, cursor => $ordered[0], limit => 2);
108 _is_descriptor_list($page, [@ordered[1, 2]], 'list_blobs starts after cursor and honors limit');
109  
110 my $unknown_cursor = $storage->list_blobs($PUBKEY, cursor => $MISSING_SHA256, limit => 10);
111 _is_descriptor_list($unknown_cursor, [], 'list_blobs returns an empty page for an unknown cursor');
112  
113 my $empty = $storage->list_blobs($OTHER_PUBKEY, limit => 10);
114 _is_descriptor_list($empty, [], 'list_blobs scopes by pubkey');
115  
116 my $duplicate = _upload($storage, $body, 1725105924);
117 _is_upload_result($duplicate, 0, 'duplicate upload result');
118 $listed = $storage->list_blobs($PUBKEY, limit => 10);
119 my @same = grep { $_->sha256 eq $sha256 } @$listed;
120 Test::More::is(scalar @same, 1, 'duplicate upload is visible once for the same pubkey');
121 }
122  
123 sub _head_blob_returns_metadata_if_supported {
124 my ($factory) = @_;
125 my $storage = _fresh_storage($factory);
126  
127 if (!$storage->can('head_blob')) {
128 Test::More::pass('optional head_blob is not implemented');
129 return;
130 }
131  
132 my $body = "head contract blob\n";
133 my $uploaded = 1725105921;
134 my $blob = _upload($storage, $body, $uploaded)->descriptor;
135  
136 my $result = $storage->head_blob($blob->sha256);
137 my $descriptor = _descriptor_from_head_result($result);
138 _is_descriptor(
139 $descriptor,
140 $blob->sha256,
141 length($body),
142 'application/octet-stream',
143 $uploaded,
144 'head_blob descriptor',
145 );
146 Test::More::is($storage->head_blob($MISSING_SHA256), undef, 'head_blob returns undef for missing blobs');
147 }
148  
149 sub _failed_uploads_are_not_visible {
150 my ($factory) = @_;
151 my $storage = _fresh_storage($factory);
152 my $server = Net::Blossom::Server->new(storage => $storage, clock => sub { 1725105921 });
153 my $body = "bad contract blob\n";
154 my $sha256 = sha256_hex($body);
155  
156 my $ok = eval {
157 $server->receive_blob($body, expected_sha256 => '0' x 64, pubkey => $PUBKEY);
158 1;
159 };
160  
161 Test::More::ok(!$ok, 'failed upload croaks');
162 Test::More::like($@, qr/sha256 mismatch/, 'failed upload reports sha mismatch');
163 Test::More::is($storage->get_blob($sha256), undef, 'failed upload is not retrievable');
164 _is_descriptor_list($storage->list_blobs($PUBKEY, limit => 10), [], 'failed upload is not listed');
165 }
166  
167 sub _delete_hides_blob {
168 my ($factory) = @_;
169 my $storage = _fresh_storage($factory);
170 my $blob = _upload($storage, "delete contract blob\n", 1725105921)->descriptor;
171  
172 Test::More::ok($storage->delete_blob($blob->sha256, pubkey => $PUBKEY), 'delete_blob returns true for existing blob');
173 Test::More::is($storage->get_blob($blob->sha256), undef, 'deleted blob is not retrievable');
174 _is_descriptor_list($storage->list_blobs($PUBKEY, limit => 10), [], 'deleted blob is not listed');
175 Test::More::ok(!$storage->delete_blob($blob->sha256, pubkey => $PUBKEY), 'delete_blob returns false for missing blob');
176 }
177  
178 sub _delete_is_pubkey_scoped {
179 my ($factory) = @_;
180 my $storage = _fresh_storage($factory);
181 my $body = "shared contract blob\n";
182 my $first = _upload($storage, $body, 1725105921, $PUBKEY);
183 my $second = _upload($storage, $body, 1725105922, $OTHER_PUBKEY);
184 my $sha256 = $first->descriptor->sha256;
185  
186 _is_upload_result($first, 1, 'first shared upload result');
187 _is_upload_result($second, 0, 'second shared upload result');
188 Test::More::is($second->descriptor->sha256, $sha256, 'same bytes produce same hash for another pubkey');
189 Test::More::ok($storage->delete_blob($sha256, pubkey => $PUBKEY), 'delete_blob removes one owner');
190 Test::More::isa_ok($storage->get_blob($sha256), 'Net::Blossom::Server::BlobResult', 'shared blob remains retrievable');
191 _is_descriptor_list($storage->list_blobs($PUBKEY, limit => 10), [], 'deleted owner no longer lists shared blob');
192 _is_descriptor_list($storage->list_blobs($OTHER_PUBKEY, limit => 10), [$sha256], 'other owner still lists shared blob');
193 Test::More::ok($storage->delete_blob($sha256, pubkey => $OTHER_PUBKEY), 'delete_blob removes final owner');
194 Test::More::is($storage->get_blob($sha256), undef, 'shared blob is removed after final owner delete');
195 }
196  
197 sub _upload {
198 my ($storage, $body, $uploaded, $pubkey) = @_;
199 $pubkey = $PUBKEY unless defined $pubkey;
200 my $server = Net::Blossom::Server->new(storage => $storage, clock => sub { $uploaded });
201 return $server->receive_blob(
202 $body,
203 type => 'application/octet-stream',
204 content_length => length($body),
205 pubkey => $pubkey,
206 );
207 }
208  
209 sub _fresh_storage {
210 my ($factory) = @_;
211 my $storage = $factory->();
212 Net::Blossom::Server::Storage->assert_implements($storage);
213 return $storage;
214 }
215  
216 sub _descriptor_from_head_result {
217 my ($result) = @_;
218 return undef unless defined $result;
219 return $result
220 if blessed($result) && $result->isa('Net::Blossom::BlobDescriptor');
221 return $result->descriptor
222 if blessed($result) && $result->isa('Net::Blossom::Server::BlobResult');
223 Test::More::fail('head_blob returns a descriptor or blob result');
224 return undef;
225 }
226  
227 sub _is_descriptor {
228 my ($descriptor, $sha256, $size, $type, $uploaded, $name) = @_;
229 Test::More::isa_ok($descriptor, 'Net::Blossom::BlobDescriptor', $name);
230 return unless blessed($descriptor) && $descriptor->isa('Net::Blossom::BlobDescriptor');
231 Test::More::is($descriptor->sha256, $sha256, "$name sha256");
232 Test::More::is($descriptor->size, $size, "$name size");
233 Test::More::is($descriptor->type, $type, "$name type");
234 Test::More::is($descriptor->uploaded, $uploaded, "$name uploaded");
235 }
236  
237 sub _is_upload_result {
238 my ($result, $created, $name) = @_;
239 Test::More::isa_ok($result, 'Net::Blossom::Server::UploadResult', $name);
240 return unless blessed($result) && $result->isa('Net::Blossom::Server::UploadResult');
241 Test::More::isa_ok($result->descriptor, 'Net::Blossom::BlobDescriptor', "$name descriptor");
242 Test::More::is($result->created, $created, "$name created");
243 }
244  
245 sub _is_blob_result {
246 my ($result, $sha256, $size, $type, $uploaded, $body, $name) = @_;
247 Test::More::isa_ok($result, 'Net::Blossom::Server::BlobResult', $name);
248 return unless blessed($result) && $result->isa('Net::Blossom::Server::BlobResult');
249 _is_descriptor($result->descriptor, $sha256, $size, $type, $uploaded, "$name descriptor");
250 Test::More::is(_body_to_scalar($result->body), $body, "$name body");
251 }
252  
253 sub _is_descriptor_list {
254 my ($list, $sha256, $name) = @_;
255 Test::More::is(ref($list), 'ARRAY', "$name returns array reference");
256 return unless ref($list) eq 'ARRAY';
257 my @got;
258 for my $descriptor (@$list) {
259 Test::More::isa_ok($descriptor, 'Net::Blossom::BlobDescriptor', "$name item");
260 push @got, blessed($descriptor) && $descriptor->isa('Net::Blossom::BlobDescriptor')
261 ? $descriptor->sha256
262 : undef;
263 }
264 Test::More::is_deeply(\@got, $sha256, $name);
265 }
266  
267 sub _body_to_scalar {
268 my ($body) = @_;
269 return $body unless ref($body);
270  
271 if (ref($body) eq 'ARRAY') {
272 return join '', @$body;
273 }
274  
275 if (blessed($body) && $body->can('read')) {
276 my $content = '';
277 while (1) {
278 my $chunk = '';
279 my $read = $body->read($chunk, 8192);
280 die "body stream read failed" unless defined $read;
281 last if $read == 0;
282 $content .= $chunk;
283 }
284 return $content;
285 }
286  
287 if (blessed($body) && $body->can('getline')) {
288 my $content = '';
289 while (defined(my $chunk = $body->getline)) {
290 $content .= $chunk;
291 }
292 return $content;
293 }
294  
295 Test::More::fail('blob result body is a scalar, array reference, or stream object');
296 return undef;
297 }
298  
299 1;
300  
301 =pod
302  
303 =head1 NAME
304  
305 Net::Blossom::Server::Storage::Test - Conformance tests for Blossom server storage backends
306  
307 =head1 SYNOPSIS
308  
309 use Net::Blossom::Server::Storage::Test qw(run_storage_contract_tests);
310  
311 run_storage_contract_tests(
312 name => 'postgres storage',
313 factory => sub {
314 reset_test_database($dbh);
315 return My::Blossom::Storage::Postgres->new(
316 dbh => $dbh,
317 base_url => 'https://cdn.example.test',
318 );
319 },
320 );
321  
322 =head1 DESCRIPTION
323  
324 C<Net::Blossom::Server::Storage::Test> provides reusable tests for storage
325 backend distributions. A backend can be database-backed, filesystem-backed, or
326 object-storage-backed as long as it satisfies L<Net::Blossom::Server::Storage>.
327  
328 The test helper creates fresh storage instances with a caller-provided factory
329 and verifies observable behavior through the public server and storage APIs.
330  
331 =head1 FUNCTIONS
332  
333 =head2 run_storage_contract_tests
334  
335 run_storage_contract_tests(
336 name => $name,
337 factory => sub { ... },
338 );
339  
340 Runs a C<Test::More> subtest named C<$name>. C<factory> must return a fresh,
341 empty storage object each time it is called. Persistent backends should delete
342 test rows or files before returning the storage object.
343  
344 The tests cover successful uploads, duplicate upload deduplication, retrieval,
345 optional C<head_blob> and C<get_blob_range>, failed upload abort behavior,
346 owner-scoped deletion, shared blobs, list ordering, cursor pagination, unknown
347 cursors, and limit handling.
348  
349 =head1 STORAGE EXPECTATIONS
350  
351 The storage object must implement the runtime contract documented by
352 L<Net::Blossom::Server::Storage>. In particular, C<list_blobs> must return
353 descriptors ordered by C<uploaded> descending and C<sha256> ascending, and
354 C<cursor> must start the next page after the matching descriptor in that order.
355  
356 Missing blobs return C<undef> from C<get_blob> and optional C<head_blob> and
357 C<get_blob_range>.
358 C<delete_blob> returns false when there is no matching blob or owner
359 relationship. Failed uploads and aborted uploads must not become visible through
360 C<get_blob> or C<list_blobs>.
361  
362 =cut
« Server/PSGI.pm ↑ Summary