This commit is contained in:
Motiejus Jakštys 2022-04-19 13:50:29 +03:00 committed by Motiejus Jakštys
parent 7d4623411f
commit bc7a7a0350

View File

@ -343,6 +343,16 @@ fn getpwnam(self: *const DB, name: []const u8, buf: *[]u8) error{OutOfMemory}!?C
return try self.getUser(user, buf);
}
// get a CUser entry by uid.
fn getpwuid(self: *const DB, uid: u32, buf: *[]u8) error{OutOfMemory}!?CUser {
const idx = bdz.search_u32(self.bdz_uid, uid);
const offset = self.idx_uid2user[idx];
const nbits = PackedUser.alignment_bits;
const user = PackedUser.fromBytes(self.users[offset << nbits ..]).user;
if (uid != user.uid()) return null;
return try self.getUser(user, buf);
}
fn shellSections(
allocator: Allocator,
corpus: *const Corpus,
@ -633,8 +643,7 @@ test "getgrnam/getgrgid" {
defer testing.allocator.free(buf);
{
const doesnotexist = try db.getgrnam("doesnotexist", &buf);
try testing.expectEqual(doesnotexist, null);
try testing.expectEqual(try db.getgrnam("doesnotexist", &buf), null);
const all = (try db.getgrnam("all", &buf)).?;
try testing.expectEqual(all.gid, 9999);
try testing.expectEqualStrings(all.name[0..3], "all");
@ -648,8 +657,7 @@ test "getgrnam/getgrgid" {
}
{
const doesnotexist = try db.getgrgid(42, &buf);
try testing.expectEqual(doesnotexist, null);
try testing.expectEqual(try db.getgrgid(42, &buf), null);
const all = (try db.getgrgid(9999, &buf)).?;
try testing.expectEqual(all.gid, 9999);
try testing.expectEqualStrings(all.name[0..3], "all");
@ -661,18 +669,34 @@ test "getgrnam/getgrgid" {
}
test "getpwnam/getpwuid" {
var arena = ArenaAllocator.init(testing.allocator);
defer arena.deinit();
var corpus = try Corpus.testCorpus(arena.allocator());
var db = try DB.fromCorpus(arena.allocator(), &corpus);
var buf = try arena.allocator().alloc(u8, db.header.getpw_bufsize);
var corpus = try Corpus.testCorpus(testing.allocator);
defer corpus.deinit();
var db = try DB.fromCorpus(testing.allocator, &corpus);
defer db.deinit(testing.allocator);
var buf = try testing.allocator.alloc(u8, db.header.getpw_bufsize);
defer testing.allocator.free(buf);
const vidmantas = (try db.getpwnam("vidmantas", &buf)).?;
try testing.expectEqual(vidmantas.pw_uid, 128);
try testing.expectEqual(vidmantas.pw_gid, 128);
try testing.expectEqualStrings(vidmantas.pw_name[0..9], "vidmantas");
try testing.expectEqualStrings(vidmantas.pw_gecos[0..19], "Vidmantas Kaminskas");
try testing.expectEqualStrings(vidmantas.pw_dir[0..15], "/home/vidmantas");
{
try testing.expectEqual(try db.getpwnam("doesnotexist", &buf), null);
const vidmantas = (try db.getpwnam("vidmantas", &buf)).?;
try testing.expectEqual(vidmantas.pw_uid, 128);
try testing.expectEqual(vidmantas.pw_gid, 128);
try testing.expectEqualStrings(vidmantas.pw_name[0..9], "vidmantas");
try testing.expectEqualStrings(vidmantas.pw_gecos[0..19], "Vidmantas Kaminskas");
try testing.expectEqualStrings(vidmantas.pw_dir[0..15], "/home/vidmantas");
}
{
try testing.expectEqual(try db.getpwuid(123456, &buf), null);
const vidmantas = (try db.getpwuid(128, &buf)).?;
try testing.expectEqual(vidmantas.pw_uid, 128);
try testing.expectEqual(vidmantas.pw_gid, 128);
try testing.expectEqualStrings(vidmantas.pw_name[0..9], "vidmantas");
}
_ = try db.getpwnam("Name" ** 8, &buf);
buf.len -= 1;
try testing.expectError(error.OutOfMemory, db.getpwnam("Name" ** 8, &buf));
}
test "additionalGids" {