slice pointer and unit test fixes
This commit is contained in:
parent
c39f89d995
commit
e6b9d43646
@ -181,8 +181,8 @@ test "users compare function" {
|
|||||||
try testing.expect(!cmpUser({}, b, a));
|
try testing.expect(!cmpUser({}, b, a));
|
||||||
try testing.expect(cmpUser({}, a, bb));
|
try testing.expect(cmpUser({}, a, bb));
|
||||||
try testing.expect(!cmpUser({}, bb, a));
|
try testing.expect(!cmpUser({}, bb, a));
|
||||||
try testing.expect(cmpUser({}, b, bb));
|
try testing.expect(cmpUser({}, bb, b));
|
||||||
try testing.expect(!cmpUser({}, bb, b));
|
try testing.expect(!cmpUser({}, b, bb));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn testCorpus(allocator: Allocator) !Corpus {
|
pub fn testCorpus(allocator: Allocator) !Corpus {
|
||||||
@ -236,7 +236,10 @@ pub fn testCorpus(allocator: Allocator) !Corpus {
|
|||||||
|
|
||||||
const groups = [_]Group{ group0, group1, group2, group3 };
|
const groups = [_]Group{ group0, group1, group2, group3 };
|
||||||
|
|
||||||
return try Corpus.init(allocator, users[0..], groups[0..]);
|
var err_ctx = ErrCtx{};
|
||||||
|
const result = try Corpus.init(allocator, users[0..], groups[0..], &err_ctx);
|
||||||
|
try testing.expectEqualStrings("", err_ctx.unwrap().constSlice());
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
test "test corpus" {
|
test "test corpus" {
|
||||||
|
58
src/DB.zig
58
src/DB.zig
@ -239,14 +239,14 @@ pub fn fromBytes(buf: []align(8) const u8) InvalidHeader!DB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// dumps PackedGroup to []u8 and returns a CGroup.
|
// dumps PackedGroup to []u8 and returns a CGroup.
|
||||||
fn getGroup(self: *const DB, group: PackedGroup, buf: *[]u8) error{OutOfMemory}!CGroup {
|
fn getGroup(self: *const DB, group: PackedGroup, buf: []u8) error{OutOfMemory}!CGroup {
|
||||||
const members_slice = self.groupmembers[group.members_offset..];
|
const members_slice = self.groupmembers[group.members_offset..];
|
||||||
var vit = compress.VarintSliceIteratorMust(members_slice);
|
var vit = compress.VarintSliceIteratorMust(members_slice);
|
||||||
const num_members = vit.remaining;
|
const num_members = vit.remaining;
|
||||||
|
|
||||||
const ptr_end = @sizeOf(?[*:0]const u8) * (num_members + 1);
|
const ptr_end = @sizeOf(?[*:0]const u8) * (num_members + 1);
|
||||||
if (ptr_end > buf.len) return error.OutOfMemory;
|
if (ptr_end > buf.len) return error.OutOfMemory;
|
||||||
var member_ptrs = mem.bytesAsSlice(?[*:0]const u8, buf.*[0..ptr_end]);
|
var member_ptrs = mem.bytesAsSlice(?[*:0]const u8, buf[0..ptr_end]);
|
||||||
member_ptrs[member_ptrs.len - 1] = null;
|
member_ptrs[member_ptrs.len - 1] = null;
|
||||||
var buf_offset: usize = ptr_end;
|
var buf_offset: usize = ptr_end;
|
||||||
|
|
||||||
@ -257,30 +257,30 @@ fn getGroup(self: *const DB, group: PackedGroup, buf: *[]u8) error{OutOfMemory}!
|
|||||||
const start = buf_offset;
|
const start = buf_offset;
|
||||||
const name = entry.user.name();
|
const name = entry.user.name();
|
||||||
if (buf_offset + name.len + 1 > buf.len) return error.OutOfMemory;
|
if (buf_offset + name.len + 1 > buf.len) return error.OutOfMemory;
|
||||||
mem.copy(u8, buf.*[buf_offset..], name);
|
mem.copy(u8, buf[buf_offset..], name);
|
||||||
buf_offset += name.len;
|
buf_offset += name.len;
|
||||||
buf.*[buf_offset] = 0;
|
buf[buf_offset] = 0;
|
||||||
buf_offset += 1;
|
buf_offset += 1;
|
||||||
|
|
||||||
// TODO: arr[i] = buf[...] triggers a bug in zig pre-0.10
|
// TODO: arr[i] = buf[...] triggers a bug in zig pre-0.10
|
||||||
const terminated = buf.*[start .. buf_offset - 1 :0];
|
const terminated = buf[start .. buf_offset - 1 :0];
|
||||||
member_ptrs[i] = terminated;
|
member_ptrs[i] = terminated;
|
||||||
}
|
}
|
||||||
|
|
||||||
const name = group.name();
|
const name = group.name();
|
||||||
if (buf_offset + name.len + 1 > buf.len) return error.OutOfMemory;
|
if (buf_offset + name.len + 1 > buf.len) return error.OutOfMemory;
|
||||||
mem.copy(u8, buf.*[buf_offset..], name);
|
mem.copy(u8, buf[buf_offset..], name);
|
||||||
buf.*[buf_offset + name.len] = 0;
|
buf[buf_offset + name.len] = 0;
|
||||||
|
|
||||||
return CGroup{
|
return CGroup{
|
||||||
.gid = group.gid(),
|
.gid = group.gid(),
|
||||||
.name = buf.*[buf_offset .. buf_offset + name.len].ptr,
|
.name = buf[buf_offset .. buf_offset + name.len].ptr,
|
||||||
.members = member_ptrs.ptr,
|
.members = member_ptrs.ptr,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// get a CGroup entry by name.
|
// get a CGroup entry by name.
|
||||||
fn getgrnam(self: *const DB, name: []const u8, buf: *[]u8) error{OutOfMemory}!?CGroup {
|
fn getgrnam(self: *const DB, name: []const u8, buf: []u8) error{OutOfMemory}!?CGroup {
|
||||||
const idx = bdz.search(self.bdz_groupname, name);
|
const idx = bdz.search(self.bdz_groupname, name);
|
||||||
if (idx >= self.header.num_groups) return null;
|
if (idx >= self.header.num_groups) return null;
|
||||||
const offset = self.idx_groupname2group[idx];
|
const offset = self.idx_groupname2group[idx];
|
||||||
@ -291,7 +291,7 @@ fn getgrnam(self: *const DB, name: []const u8, buf: *[]u8) error{OutOfMemory}!?C
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get a CGroup entry by it's gid.
|
// get a CGroup entry by it's gid.
|
||||||
fn getgrgid(self: *const DB, gid: u32, buf: *[]u8) error{OutOfMemory}!?CGroup {
|
fn getgrgid(self: *const DB, gid: u32, buf: []u8) error{OutOfMemory}!?CGroup {
|
||||||
const idx = bdz.search_u32(self.bdz_gid, gid);
|
const idx = bdz.search_u32(self.bdz_gid, gid);
|
||||||
if (idx >= self.header.num_groups) return null;
|
if (idx >= self.header.num_groups) return null;
|
||||||
const offset = self.idx_gid2group[idx];
|
const offset = self.idx_gid2group[idx];
|
||||||
@ -301,15 +301,15 @@ fn getgrgid(self: *const DB, gid: u32, buf: *[]u8) error{OutOfMemory}!?CGroup {
|
|||||||
return try self.getGroup(group, buf);
|
return try self.getGroup(group, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pushStr(str: []const u8, buf: *[]u8, offset: *usize) [*]const u8 {
|
fn pushStr(str: []const u8, buf: []u8, offset: *usize) [*]const u8 {
|
||||||
const start = offset.*;
|
const start = offset.*;
|
||||||
mem.copy(u8, buf.*[offset.*..], str);
|
mem.copy(u8, buf[offset.*..], str);
|
||||||
buf.*[offset.* + str.len] = 0;
|
buf[offset.* + str.len] = 0;
|
||||||
offset.* += str.len + 1;
|
offset.* += str.len + 1;
|
||||||
return @ptrCast([*]const u8, &buf.*[start]);
|
return @ptrCast([*]const u8, &buf[start]);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getUser(self: *const DB, user: PackedUser, buf: *[]u8) error{OutOfMemory}!CUser {
|
fn getUser(self: *const DB, user: PackedUser, buf: []u8) error{OutOfMemory}!CUser {
|
||||||
const shell_reader = ShellReader{
|
const shell_reader = ShellReader{
|
||||||
.index = self.shell_index,
|
.index = self.shell_index,
|
||||||
.blob = self.shell_blob,
|
.blob = self.shell_blob,
|
||||||
@ -343,7 +343,7 @@ fn getUser(self: *const DB, user: PackedUser, buf: *[]u8) error{OutOfMemory}!CUs
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get a CUser entry by name.
|
// get a CUser entry by name.
|
||||||
pub fn getpwnam(self: *const DB, name: []const u8, buf: *[]u8) error{OutOfMemory}!?CUser {
|
pub fn getpwnam(self: *const DB, name: []const u8, buf: []u8) error{OutOfMemory}!?CUser {
|
||||||
const idx = bdz.search(self.bdz_username, name);
|
const idx = bdz.search(self.bdz_username, name);
|
||||||
// bdz may return a hash that's bigger than the number of users
|
// bdz may return a hash that's bigger than the number of users
|
||||||
if (idx >= self.header.num_users) return null;
|
if (idx >= self.header.num_users) return null;
|
||||||
@ -355,7 +355,7 @@ pub fn getpwnam(self: *const DB, name: []const u8, buf: *[]u8) error{OutOfMemory
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get a CUser entry by uid.
|
// get a CUser entry by uid.
|
||||||
pub fn getpwuid(self: *const DB, uid: u32, buf: *[]u8) error{OutOfMemory}!?CUser {
|
pub fn getpwuid(self: *const DB, uid: u32, buf: []u8) error{OutOfMemory}!?CUser {
|
||||||
const idx = bdz.search_u32(self.bdz_uid, uid);
|
const idx = bdz.search_u32(self.bdz_uid, uid);
|
||||||
if (idx >= self.header.num_users) return null;
|
if (idx >= self.header.num_users) return null;
|
||||||
const offset = self.idx_uid2user[idx];
|
const offset = self.idx_uid2user[idx];
|
||||||
@ -659,8 +659,8 @@ test "getgrnam/getgrgid" {
|
|||||||
defer testing.allocator.free(buf);
|
defer testing.allocator.free(buf);
|
||||||
|
|
||||||
{
|
{
|
||||||
try testing.expectEqual(try db.getgrnam("doesnotexist", &buf), null);
|
try testing.expectEqual(try db.getgrnam("doesnotexist", buf), null);
|
||||||
const all = (try db.getgrnam("all", &buf)).?;
|
const all = (try db.getgrnam("all", buf)).?;
|
||||||
try testing.expectEqual(all.gid, 9999);
|
try testing.expectEqual(all.gid, 9999);
|
||||||
try testing.expectEqualStrings(all.name[0..4], "all\x00");
|
try testing.expectEqualStrings(all.name[0..4], "all\x00");
|
||||||
const members = all.members;
|
const members = all.members;
|
||||||
@ -672,15 +672,15 @@ test "getgrnam/getgrgid" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
try testing.expectEqual(try db.getgrgid(42, &buf), null);
|
try testing.expectEqual(try db.getgrgid(42, buf), null);
|
||||||
const all = (try db.getgrgid(9999, &buf)).?;
|
const all = (try db.getgrgid(9999, buf)).?;
|
||||||
try testing.expectEqual(all.gid, 9999);
|
try testing.expectEqual(all.gid, 9999);
|
||||||
try testing.expectEqualStrings(all.name[0..3], "all");
|
try testing.expectEqualStrings(all.name[0..3], "all");
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = try db.getgrnam("all", &buf);
|
_ = try db.getgrnam("all", buf);
|
||||||
buf.len -= 1;
|
buf.len -= 1;
|
||||||
try testing.expectError(error.OutOfMemory, db.getgrnam("all", &buf));
|
try testing.expectError(error.OutOfMemory, db.getgrnam("all", buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
test "getpwnam/getpwuid" {
|
test "getpwnam/getpwuid" {
|
||||||
@ -692,8 +692,8 @@ test "getpwnam/getpwuid" {
|
|||||||
defer testing.allocator.free(buf);
|
defer testing.allocator.free(buf);
|
||||||
|
|
||||||
{
|
{
|
||||||
try testing.expectEqual(try db.getpwnam("doesnotexist", &buf), null);
|
try testing.expectEqual(try db.getpwnam("doesnotexist", buf), null);
|
||||||
const vidmantas = (try db.getpwnam("vidmantas", &buf)).?;
|
const vidmantas = (try db.getpwnam("vidmantas", buf)).?;
|
||||||
try testing.expectEqual(vidmantas.pw_uid, 128);
|
try testing.expectEqual(vidmantas.pw_uid, 128);
|
||||||
try testing.expectEqual(vidmantas.pw_gid, 128);
|
try testing.expectEqual(vidmantas.pw_gid, 128);
|
||||||
try testing.expectEqualStrings(vidmantas.pw_name[0..10], "vidmantas\x00");
|
try testing.expectEqualStrings(vidmantas.pw_name[0..10], "vidmantas\x00");
|
||||||
@ -702,17 +702,17 @@ test "getpwnam/getpwuid" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
try testing.expectEqual(try db.getpwuid(123456, &buf), null);
|
try testing.expectEqual(try db.getpwuid(123456, buf), null);
|
||||||
const vidmantas = (try db.getpwuid(128, &buf)).?;
|
const vidmantas = (try db.getpwuid(128, buf)).?;
|
||||||
try testing.expectEqual(vidmantas.pw_uid, 128);
|
try testing.expectEqual(vidmantas.pw_uid, 128);
|
||||||
try testing.expectEqual(vidmantas.pw_gid, 128);
|
try testing.expectEqual(vidmantas.pw_gid, 128);
|
||||||
try testing.expectEqualStrings(vidmantas.pw_name[0..10], "vidmantas\x00");
|
try testing.expectEqualStrings(vidmantas.pw_name[0..10], "vidmantas\x00");
|
||||||
}
|
}
|
||||||
|
|
||||||
const long = try db.getpwnam("Name" ** 8, &buf);
|
const long = try db.getpwnam("Name" ** 8, buf);
|
||||||
try testing.expectEqualStrings(long.?.pw_name[0..33], "Name" ** 8 ++ "\x00");
|
try testing.expectEqualStrings(long.?.pw_name[0..33], "Name" ** 8 ++ "\x00");
|
||||||
buf.len -= 1;
|
buf.len -= 1;
|
||||||
try testing.expectError(error.OutOfMemory, db.getpwnam("Name" ** 8, &buf));
|
try testing.expectError(error.OutOfMemory, db.getpwnam("Name" ** 8, buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
test "additionalGids" {
|
test "additionalGids" {
|
||||||
|
@ -112,7 +112,7 @@ export fn _nss_turbo_getpwuid_r(
|
|||||||
errnop.* = @enumToInt(os.E.AGAIN);
|
errnop.* = @enumToInt(os.E.AGAIN);
|
||||||
return c.NSS_STATUS_UNAVAIL;
|
return c.NSS_STATUS_UNAVAIL;
|
||||||
}
|
}
|
||||||
const cuser = state.file.?.db.getpwuid(uid, &buf[0..len]) catch |err| switch (err) {
|
const cuser = state.file.?.db.getpwuid(uid, buf[0..len]) catch |err| switch (err) {
|
||||||
error.OutOfMemory => {
|
error.OutOfMemory => {
|
||||||
errnop.* = @enumToInt(os.E.RANGE);
|
errnop.* = @enumToInt(os.E.RANGE);
|
||||||
return c.NSS_STATUS_TRYAGAIN;
|
return c.NSS_STATUS_TRYAGAIN;
|
||||||
|
@ -121,10 +121,8 @@ fn execute(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn fail(errc: *ErrCtx, stderr: anytype, err: anytype) u8 {
|
fn fail(errc: *ErrCtx, stderr: anytype, err: anytype) u8 {
|
||||||
stderr.print("ERROR {s}", .{@errorName(err)}) catch {};
|
const err_chain = errc.unwrap().constSlice();
|
||||||
var it = errc.rev();
|
stderr.print("ERROR {s}: {s}\n", .{ @errorName(err), err_chain }) catch {};
|
||||||
while (it.next()) |msg| stderr.print(": {s}", .{msg}) catch {};
|
|
||||||
stderr.print("\n", .{}) catch {};
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user