Skip to content

Commit 19c8848

Browse files
ignaskHomulvas
ignask
authored andcommitted
starter testsuite for std.tar
1 parent 7285eed commit 19c8848

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

lib/std/tar.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,7 @@ test stripComponents {
196196

197197
const std = @import("std.zig");
198198
const assert = std.debug.assert;
199+
200+
test {
201+
_ = @import("tar/test.zig");
202+
}

lib/std/tar/test.zig

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const std = @import("../std.zig");
2+
3+
const readTestScenario = struct {
4+
data: []const u8,
5+
expect: std.StringHashMap(metadata),
6+
};
7+
8+
const metadata = struct {
9+
md5: []const u8,
10+
mode: usize,
11+
size: u64,
12+
mtime: i128,
13+
};
14+
15+
test "gnu tar" {
16+
var expect = std.StringHashMap(metadata).init(std.testing.allocator);
17+
defer expect.deinit();
18+
try expect.put("small.txt", metadata{
19+
.md5 = "e38b27eaccb4391bdec553a7f3ae6b2f",
20+
.mode = 0o640,
21+
.size = 5,
22+
.mtime = 1244428340 * std.time.ns_per_s,
23+
});
24+
try expect.put("small2.txt", metadata{
25+
.md5 = "c65bd2e50a56a2138bf1716f2fd56fe9",
26+
.mode = 0o640,
27+
.size = 11,
28+
.mtime = 1244436044 * std.time.ns_per_s,
29+
});
30+
31+
readTest(.{
32+
.data = @embedFile("testdata/gnu.tar"),
33+
.expect = expect,
34+
}) catch {
35+
return error.SkipZigTest;
36+
};
37+
}
38+
39+
fn readTest(scenario: readTestScenario) !void {
40+
var file_stream = std.io.fixedBufferStream(scenario.data);
41+
42+
var tmp = std.testing.tmpIterableDir(.{});
43+
defer tmp.cleanup();
44+
45+
try std.tar.pipeToFileSystem(
46+
tmp.iterable_dir.dir,
47+
file_stream.reader(),
48+
.{ .mode_mode = .ignore },
49+
);
50+
51+
var iter = tmp.iterable_dir.iterate();
52+
53+
var elements: u8 = 0;
54+
while (try iter.next()) |entry| {
55+
var expectFile = scenario.expect.get(entry.name) orelse return error.TestFailure;
56+
var f = try tmp.iterable_dir.dir.openFile(
57+
entry.name,
58+
.{ .mode = .read_only },
59+
);
60+
defer f.close();
61+
62+
const content = try f.readToEndAlloc(std.testing.allocator, std.math.maxInt(u32));
63+
defer std.testing.allocator.free(content);
64+
65+
var md5Hash: [std.crypto.hash.Md5.digest_length]u8 = undefined;
66+
std.crypto.hash.Md5.hash(content, &md5Hash, .{});
67+
var b: [2 * std.crypto.hash.Md5.digest_length]u8 = undefined;
68+
_ = try std.fmt.bufPrint(&b, "{s}", .{std.fmt.fmtSliceHexLower(&md5Hash)});
69+
try std.testing.expectEqualStrings(expectFile.md5, &b);
70+
elements += 1;
71+
72+
var stat = try f.stat();
73+
try std.testing.expectEqual(expectFile.size, stat.size);
74+
// TODO: fix possibly broken behavior.
75+
try std.testing.expectEqual(expectFile.mtime, stat.mtime);
76+
try std.testing.expectEqual(expectFile.mode, stat.mode);
77+
}
78+
79+
try std.testing.expectEqual(scenario.expect.count(), elements);
80+
}

lib/std/tar/testdata/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2009 The Go Authors. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
* Neither the name of Google Inc. nor the names of its
14+
contributors may be used to endorse or promote products derived from
15+
this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

lib/std/tar/testdata/gnu.tar

3 KB
Binary file not shown.

0 commit comments

Comments
 (0)