2022-02-27 15:49:00 +02:00
|
|
|
const std = @import("std");
|
2022-06-29 10:48:37 +03:00
|
|
|
const ascii = std.ascii;
|
|
|
|
|
|
|
|
const ErrCtx = @import("ErrCtx.zig");
|
2022-02-27 15:49:00 +02:00
|
|
|
|
2022-03-22 08:57:57 +02:00
|
|
|
pub fn downCast(comptime T: type, n: u64) error{InvalidRecord}!T {
|
2022-06-05 23:11:13 +03:00
|
|
|
return std.math.cast(T, n) orelse return error.InvalidRecord;
|
2022-02-27 15:49:00 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 08:57:57 +02:00
|
|
|
pub fn utf8(s: []const u8) error{InvalidRecord}!void {
|
2022-02-27 15:49:00 +02:00
|
|
|
if (!std.unicode.utf8ValidateSlice(s)) {
|
|
|
|
return error.InvalidRecord;
|
|
|
|
}
|
|
|
|
}
|
2022-06-29 10:48:37 +03:00
|
|
|
|
|
|
|
// # adduser žmogus
|
|
|
|
// adduser: To avoid problems, the username should consist only of letters,
|
|
|
|
// digits, underscores, periods, at signs and dashes, and not start with a dash
|
|
|
|
// (as defined by IEEE Std 1003.1-2001). For compatibility with Samba machine
|
|
|
|
// accounts $ is also supported at the end of the username
|
|
|
|
pub fn name(s: []const u8, err: *ErrCtx) error{InvalidRecord}!void {
|
|
|
|
if (s.len == 0)
|
|
|
|
return err.returnf("cannot be empty", .{}, error.InvalidRecord);
|
|
|
|
|
|
|
|
const c0 = s[0];
|
|
|
|
if (!(ascii.isAlNum(c0) or c0 == '_' or c0 == '.' or c0 == '@'))
|
|
|
|
return err.returnf("invalid character at position 0", .{}, error.InvalidRecord);
|
|
|
|
|
|
|
|
for (s[1..]) |c, i| {
|
|
|
|
if (!(ascii.isAlNum(c) or c == '_' or c == '.' or c == '@' or c == '-'))
|
|
|
|
return err.returnf(
|
|
|
|
"invalid character at position {d}",
|
|
|
|
.{i + 2},
|
|
|
|
error.InvalidRecord,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 13:36:41 +03:00
|
|
|
// gecos must be utf8 without ':'
|
|
|
|
pub fn gecos(s: []const u8, errc: *ErrCtx) error{InvalidRecord}!void {
|
|
|
|
var utf8view = std.unicode.Utf8View.init(s) catch |err| switch (err) {
|
|
|
|
error.InvalidUtf8 => return errc.returnf("invalid utf8", .{}, error.InvalidRecord),
|
|
|
|
};
|
|
|
|
|
|
|
|
var it = utf8view.iterator();
|
|
|
|
while (it.nextCodepoint()) |codepoint| {
|
|
|
|
if (codepoint == ':')
|
2022-06-29 13:48:08 +03:00
|
|
|
return errc.returnf("colon is not allowed", .{}, error.InvalidRecord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn path(s: []const u8, err: *ErrCtx) error{InvalidRecord}!void {
|
|
|
|
if (s.len == 0)
|
|
|
|
return err.returnf("cannot be empty", .{}, error.InvalidRecord);
|
|
|
|
|
|
|
|
if (s[0] != '/')
|
2022-07-02 09:14:02 +03:00
|
|
|
return err.returnf("must start with /", .{}, error.InvalidRecord);
|
2022-06-29 13:48:08 +03:00
|
|
|
|
|
|
|
for (s[1..]) |c, i| {
|
|
|
|
if (!(ascii.isAlNum(c) or c == '/' or c == '_' or c == '.' or c == '@' or c == '-'))
|
|
|
|
return err.returnf(
|
|
|
|
"invalid character at position {d}",
|
|
|
|
.{i + 2},
|
|
|
|
error.InvalidRecord,
|
|
|
|
);
|
2022-06-29 13:36:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:48:37 +03:00
|
|
|
const testing = std.testing;
|
|
|
|
|
|
|
|
test "validate name" {
|
|
|
|
const examples = [_]struct {
|
|
|
|
name: []const u8,
|
|
|
|
wantErr: ?[]const u8 = null,
|
|
|
|
}{
|
2022-06-29 13:36:41 +03:00
|
|
|
.{ .name = "all-good" },
|
|
|
|
.{ .name = "..." },
|
|
|
|
.{ .name = "", .wantErr = "cannot be empty" },
|
|
|
|
.{ .name = "-no-start-dash", .wantErr = "invalid character at position 0" },
|
|
|
|
.{ .name = "Herbasž", .wantErr = "invalid character at position 7" },
|
|
|
|
};
|
|
|
|
for (examples) |tt| {
|
|
|
|
var err = ErrCtx{};
|
|
|
|
|
|
|
|
const got = name(tt.name, &err);
|
|
|
|
if (tt.wantErr) |wantErr| {
|
|
|
|
try testing.expectError(error.InvalidRecord, got);
|
|
|
|
try testing.expectEqualStrings(wantErr, err.unwrap().constSlice());
|
|
|
|
} else {
|
|
|
|
// TODO: how to assert `got` is a non-error in a single line?
|
|
|
|
if (got) |_| {} else |_| return error.TestUnExpectedError;
|
|
|
|
try testing.expectEqualStrings("", err.unwrap().constSlice());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
test "validate gecos" {
|
|
|
|
const examples = [_]struct {
|
|
|
|
gecos: []const u8,
|
|
|
|
wantErr: ?[]const u8 = null,
|
|
|
|
}{
|
|
|
|
.{ .gecos = "all-good" },
|
|
|
|
.{ .gecos = "..." },
|
|
|
|
.{ .gecos = "" },
|
|
|
|
.{ .gecos = "Vidmantas Kaminskas Ž" },
|
|
|
|
.{ .gecos = "Not\xffUnicode", .wantErr = "invalid utf8" },
|
2022-06-29 13:48:08 +03:00
|
|
|
.{ .gecos = "Has:Colon", .wantErr = "colon is not allowed" },
|
2022-06-29 10:48:37 +03:00
|
|
|
};
|
|
|
|
for (examples) |tt| {
|
|
|
|
var err = ErrCtx{};
|
|
|
|
|
2022-06-29 13:36:41 +03:00
|
|
|
const got = gecos(tt.gecos, &err);
|
2022-06-29 10:48:37 +03:00
|
|
|
if (tt.wantErr) |wantErr| {
|
2022-06-29 13:36:41 +03:00
|
|
|
try testing.expectError(error.InvalidRecord, got);
|
2022-06-29 10:48:37 +03:00
|
|
|
try testing.expectEqualStrings(wantErr, err.unwrap().constSlice());
|
|
|
|
} else {
|
2022-06-29 13:36:41 +03:00
|
|
|
// TODO: how to assert `got` is a non-error in a single line?
|
|
|
|
if (got) |_| {} else |_| return error.TestUnExpectedError;
|
2022-06-29 10:48:37 +03:00
|
|
|
try testing.expectEqualStrings("", err.unwrap().constSlice());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-29 13:48:08 +03:00
|
|
|
|
|
|
|
test "validate path" {
|
|
|
|
const examples = [_]struct {
|
|
|
|
path: []const u8,
|
|
|
|
wantErr: ?[]const u8 = null,
|
|
|
|
}{
|
|
|
|
.{ .path = "/path/ok" },
|
|
|
|
.{ .path = "/" },
|
2022-07-02 09:14:02 +03:00
|
|
|
.{ .path = "foo", .wantErr = "must start with /" },
|
2022-06-29 13:48:08 +03:00
|
|
|
.{ .path = "", .wantErr = "cannot be empty" },
|
|
|
|
.{ .path = "/path:motiejus", .wantErr = "invalid character at position 6" },
|
|
|
|
.{ .path = "/Herbasž", .wantErr = "invalid character at position 8" },
|
|
|
|
};
|
|
|
|
for (examples) |tt| {
|
|
|
|
var err = ErrCtx{};
|
|
|
|
|
|
|
|
const got = path(tt.path, &err);
|
|
|
|
if (tt.wantErr) |wantErr| {
|
|
|
|
try testing.expectError(error.InvalidRecord, got);
|
|
|
|
try testing.expectEqualStrings(wantErr, err.unwrap().constSlice());
|
|
|
|
} else {
|
|
|
|
// TODO: how to assert `got` is a non-error in a single line?
|
|
|
|
if (got) |_| {} else |_| return error.TestUnExpectedError;
|
|
|
|
try testing.expectEqualStrings("", err.unwrap().constSlice());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|