wip fromLine tests
This commit is contained in:
parent
f30510f122
commit
b703eeb37c
44
src/User.zig
44
src/User.zig
@ -73,13 +73,13 @@ fn fromLine(allocator: Allocator, err_ctx: *ErrCtx, line: []const u8) error{ Inv
|
|||||||
return err_ctx.returnf("bad uid: {s}", .{gids.?}, error.InvalidRecord);
|
return err_ctx.returnf("bad uid: {s}", .{gids.?}, error.InvalidRecord);
|
||||||
|
|
||||||
validate.utf8(name.?) catch
|
validate.utf8(name.?) catch
|
||||||
return err_ctx.returnf("name is invalid utf8: {s}", .{name.?}, error.InvalidRecord);
|
return err_ctx.returnf("name is invalid utf8: '{s}'", .{name.?}, error.InvalidRecord);
|
||||||
validate.utf8(gecos.?) catch
|
validate.utf8(gecos.?) catch
|
||||||
return err_ctx.returnf("gecos is invalid utf8: {s}", .{gecos.?}, error.InvalidRecord);
|
return err_ctx.returnf("gecos is invalid utf8: '{s}'", .{gecos.?}, error.InvalidRecord);
|
||||||
validate.utf8(home.?) catch
|
validate.utf8(home.?) catch
|
||||||
return err_ctx.returnf("home is invalid utf8: {s}", .{home.?}, error.InvalidRecord);
|
return err_ctx.returnf("home is invalid utf8: '{s}'", .{home.?}, error.InvalidRecord);
|
||||||
validate.utf8(shell.?) catch
|
validate.utf8(shell.?) catch
|
||||||
return err_ctx.returnf("shell is invalid utf8: {s}", .{shell.?}, error.InvalidRecord);
|
return err_ctx.returnf("shell is invalid utf8: '{s}'", .{shell.?}, error.InvalidRecord);
|
||||||
|
|
||||||
const user = User{
|
const user = User{
|
||||||
.uid = uid,
|
.uid = uid,
|
||||||
@ -174,6 +174,42 @@ pub const max_user = User{
|
|||||||
.shell = "She.LllL" ** 32,
|
.shell = "She.LllL" ** 32,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const from_line_examples = [_]struct {
|
||||||
|
line: []const u8,
|
||||||
|
want: error{InvalidUser}!User,
|
||||||
|
}{
|
||||||
|
.{
|
||||||
|
.line = "root:x:0:0:root:/root:/bin/bash",
|
||||||
|
.want = User{
|
||||||
|
.uid = 0,
|
||||||
|
.gid = 0,
|
||||||
|
.name = "root",
|
||||||
|
.gecos = "root",
|
||||||
|
.home = "/root",
|
||||||
|
.shell = "/bin/bash",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
test "User.fromLine" {
|
||||||
|
const allocator = testing.allocator;
|
||||||
|
var err_ctx = ErrCtx{};
|
||||||
|
for (from_line_examples) |tt| {
|
||||||
|
if (tt.want) |want_user| {
|
||||||
|
var got = try fromLine(allocator, &err_ctx, tt.line);
|
||||||
|
defer got.deinit(allocator);
|
||||||
|
try testing.expectEqual(want_user.uid, got.uid);
|
||||||
|
try testing.expectEqual(want_user.gid, got.gid);
|
||||||
|
try testing.expectEqualStrings(want_user.name, got.name);
|
||||||
|
try testing.expectEqualStrings(want_user.gecos, got.gecos);
|
||||||
|
try testing.expectEqualStrings(want_user.home, got.home);
|
||||||
|
try testing.expectEqualStrings(want_user.shell, got.shell);
|
||||||
|
} else |want_err| {
|
||||||
|
try testing.expectError(want_err, fromLine(allocator, &err_ctx, tt.line));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test "max_user and max_str_len are consistent" {
|
test "max_user and max_str_len are consistent" {
|
||||||
const total_len = max_user.name.len +
|
const total_len = max_user.name.len +
|
||||||
max_user.gecos.len +
|
max_user.gecos.len +
|
||||||
|
Loading…
Reference in New Issue
Block a user