diff --git a/src/User.zig b/src/User.zig index 372843a..f4ab232 100644 --- a/src/User.zig +++ b/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); 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 - 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 - 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 - 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{ .uid = uid, @@ -174,6 +174,42 @@ pub const max_user = User{ .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" { const total_len = max_user.name.len + max_user.gecos.len +