1
Fork 0

wip: full smoke test

main
Motiejus Jakštys 2022-06-07 13:14:50 +03:00
parent e2d20bb805
commit 12561d9f3a
3 changed files with 72 additions and 8 deletions

View File

@ -96,6 +96,19 @@ pub fn fromReader(allocator: Allocator, reader: anytype) FromReaderError![]Group
return groups.toOwnedSlice();
}
pub fn writeTo(self: *const Group, writer: anytype) os.WriteError!void {
try writer.print("{s}:x:{d}:", .{ self.name, self.gid });
if (self.members.len == 0)
return;
try writer.writeAll(self.members[0]);
for (self.members[1..]) |member|
try writer.print(",{s}", .{member});
try writer.writeByte('\n');
}
// suggested buffer size in bytes if all strings were zero-terminated
// (for CGroup).
pub fn strlenZ(self: *const Group) usize {

View File

@ -94,16 +94,17 @@ const max_line_len = fmt.count(line_fmt, .{
max_user.shell,
});
// toPasswdLine formats the user in /etc/passwd format, including the EOL
fn toLine(self: *const User) BoundedArray(u8, max_line_len) {
var result = BoundedArray(u8, max_line_len);
fmt.bufPrint(result.slice(), line_fmt, .{
// toLine formats the user in /etc/passwd format, including the EOL
pub fn toLine(self: *const User) BoundedArray(u8, max_line_len) {
var result = BoundedArray(u8, max_line_len).init(max_line_len) catch unreachable;
_ = fmt.bufPrint(result.slice(), line_fmt, .{
self.name,
self.uid,
self.gid,
self.gecos,
self.home,
self.shell,
});
}) catch unreachable;
return result;
}

View File

@ -65,6 +65,7 @@ fn execute(
const groupFname = result.argFlag("--group") orelse "./group";
const outFile = result.argFlag("--output") orelse "./db.turbo";
std.debug.print("passwd file name: {s}\n", .{passwdFname});
var passwdFile = try fs.cwd().openFile(passwdFname, .{ .mode = .read_only });
defer passwdFile.close();
var groupFile = try fs.cwd().openFile(groupFname, .{ .mode = .read_only });
@ -80,7 +81,7 @@ fn execute(
var db = try DB.fromCorpus(allocator, &corpus);
defer db.deinit(allocator);
const fd = try os.open(outFile, os.O.WRONLY | os.O.TRUNC, 0644);
const fd = try os.open(outFile, os.O.WRONLY | os.O.TRUNC | os.O.CREAT, 0644);
errdefer os.close(fd);
const len = try os.writev(fd, db.iov().constSlice());
@ -114,8 +115,57 @@ test "invalid argument" {
test "smoke test" {
const allocator = testing.allocator;
const args = &[_][*:0]const u8{"--invalid-argument"};
var stderr = ArrayList(u8).init(allocator);
defer stderr.deinit();
_ = args;
var corpus = try Corpus.testCorpus(allocator);
defer corpus.deinit();
var tmp = testing.tmpDir(.{});
//errdefer tmp.cleanup();
const tmp_path = blk: {
const relative_path = try fs.path.join(allocator, &[_][]const u8{
"zig-cache",
"tmp",
tmp.sub_path[0..],
});
const real_path = try fs.realpathAlloc(allocator, relative_path);
allocator.free(relative_path);
break :blk real_path;
};
defer allocator.free(tmp_path);
const passwdPath = try fs.path.joinZ(allocator, &[_][]const u8{ tmp_path, "passwd" });
defer allocator.free(passwdPath);
const groupPath = try fs.path.joinZ(allocator, &[_][]const u8{ tmp_path, "group" });
defer allocator.free(groupPath);
const outPath = try fs.path.joinZ(allocator, &[_][]const u8{ tmp_path, "tmp.turbo" });
defer allocator.free(outPath);
const passwd_fd = try os.open(passwdPath, os.O.CREAT | os.O.WRONLY, 0o644);
const group_fd = try os.open(groupPath, os.O.CREAT | os.O.WRONLY, 0o644);
var i: usize = 0;
while (i < corpus.users.len) : (i += 1) {
const user = corpus.users.get(i);
const line = user.toLine();
_ = try os.write(passwd_fd, line.constSlice());
}
os.close(passwd_fd);
var group_writer = (fs.File{ .handle = group_fd }).writer();
i = 0;
while (i < corpus.groups.len) : (i += 1)
try corpus.groups.get(i).writeTo(group_writer);
os.close(group_fd);
const args = &[_][*:0]const u8{
"--passwd", passwdPath,
"--group", groupPath,
"--output", outPath,
};
const exit_code = try execute(allocator, stderr.writer(), args);
try testing.expectEqual(@as(u8, 0), exit_code);
}