make corpus smoke test pass
This commit is contained in:
parent
3c4455efeb
commit
e5870a95fe
|
@ -44,11 +44,11 @@ const Corpus = struct {
|
|||
|
||||
var users = try allocator.alloc(User, usersConst.len);
|
||||
var groups = try allocator.alloc(Group, groupsConst.len);
|
||||
for (usersConst) |user, i| {
|
||||
users[i] = try user.clone(allocator);
|
||||
for (usersConst) |_, i| {
|
||||
users[i] = try usersConst[i].clone(allocator);
|
||||
}
|
||||
for (groupsConst) |group, i| {
|
||||
groups[i] = try group.clone(allocator);
|
||||
for (groupsConst) |_, i| {
|
||||
groups[i] = try groupsConst[i].clone(allocator);
|
||||
}
|
||||
|
||||
sort.sort(User, users, {}, cmpUser);
|
||||
|
@ -58,36 +58,37 @@ const Corpus = struct {
|
|||
var uid2user = AutoHashMap(u32, *const User).init(allocator);
|
||||
var name2group = StringHashMap(*const Group).init(allocator);
|
||||
var gid2group = AutoHashMap(u32, *const Group).init(allocator);
|
||||
for (users) |user| {
|
||||
for (users) |*user| {
|
||||
var res1 = try name2user.getOrPut(user.name);
|
||||
if (res1.found_existing) {
|
||||
return error.Duplicate;
|
||||
}
|
||||
res1.value_ptr.* = &user;
|
||||
res1.value_ptr.* = user;
|
||||
|
||||
var res2 = try uid2user.getOrPut(user.uid);
|
||||
if (res2.found_existing) {
|
||||
return error.Duplicate;
|
||||
}
|
||||
res2.value_ptr.* = &user;
|
||||
|
||||
res2.value_ptr.* = user;
|
||||
}
|
||||
|
||||
for (groups) |group| {
|
||||
for (groups) |*group| {
|
||||
var res1 = try name2group.getOrPut(group.name);
|
||||
if (res1.found_existing) {
|
||||
return error.Duplicate;
|
||||
}
|
||||
res1.value_ptr.* = &group;
|
||||
res1.value_ptr.* = group;
|
||||
var res2 = try gid2group.getOrPut(group.gid);
|
||||
if (res2.found_existing) {
|
||||
return error.Duplicate;
|
||||
}
|
||||
res2.value_ptr.* = &group;
|
||||
res2.value_ptr.* = group;
|
||||
}
|
||||
|
||||
var groupname2users = StringHashMap(ArrayList(*const User)).init(allocator);
|
||||
var username2groups = StringHashMap(ArrayList(*const Group)).init(allocator);
|
||||
for (groups) |group| {
|
||||
for (groups) |*group| {
|
||||
var members = try ArrayList(*const User).initCapacity(
|
||||
allocator,
|
||||
group.members.count(),
|
||||
|
@ -105,7 +106,7 @@ const Corpus = struct {
|
|||
if (!groupsOfMember.found_existing) {
|
||||
groupsOfMember.value_ptr.* = ArrayList(*const Group).init(allocator);
|
||||
}
|
||||
try groupsOfMember.value_ptr.*.append(&group);
|
||||
try groupsOfMember.value_ptr.*.append(group);
|
||||
}
|
||||
|
||||
var result = try groupname2users.getOrPut(group.name);
|
||||
|
@ -161,7 +162,7 @@ const testing = std.testing;
|
|||
test "test corpus" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var users = [_]User{ User{
|
||||
const users = [_]User{ User{
|
||||
.uid = 1000,
|
||||
.gid = 1000,
|
||||
.name = "vidmantas",
|
||||
|
@ -192,7 +193,7 @@ test "test corpus" {
|
|||
|
||||
var members2 = try groupImport.someMembers(
|
||||
allocator,
|
||||
&[_][]const u8{ "svc-bar", "Name" ** 8 },
|
||||
&[_][]const u8{ "svc-bar", "vidmantas" },
|
||||
);
|
||||
defer members2.deinit();
|
||||
|
||||
|
@ -202,18 +203,18 @@ test "test corpus" {
|
|||
);
|
||||
defer members3.deinit();
|
||||
|
||||
var groups = [_]Group{ Group{
|
||||
const groups = [_]Group{ Group{
|
||||
.gid = 1000,
|
||||
.name = "vidmantas",
|
||||
.members = members1,
|
||||
}, Group{
|
||||
.gid = 1234,
|
||||
.name = "service-account",
|
||||
.members = members2,
|
||||
}, Group{
|
||||
.gid = 9999,
|
||||
.name = "all",
|
||||
.members = members3,
|
||||
}, Group{
|
||||
.gid = 1234,
|
||||
.name = "service-account",
|
||||
.members = members2,
|
||||
} };
|
||||
|
||||
var corpus = try Corpus.init(allocator, users[0..], groups[0..]);
|
||||
|
@ -222,16 +223,25 @@ test "test corpus" {
|
|||
try testing.expectEqualStrings(corpus.users[1].name, "svc-bar");
|
||||
try testing.expectEqualStrings(corpus.users[2].name, "vidmantas");
|
||||
|
||||
//if (corpus.uid2user.get(1000)) |user| {
|
||||
// std.debug.print("user: {s}\n", .{user});
|
||||
// std.debug.print("user.*.name: {d}\n", .{user.*.name});
|
||||
// try testing.expectEqualStrings(user.*.name, "vidmantas");
|
||||
//} else {
|
||||
// try testing.expect(false);
|
||||
//}
|
||||
//try testing.expectEqualStrings(corpus.uid2user.get(1000).?.name, "vidmantas");
|
||||
//try testing.expectEqualStrings(corpus.uid2user.get(0).?.name, "Name" ** 8);
|
||||
//try testing.expectEqualStrings(corpus.uid2user.get(1002).?.name, "svc-bar");
|
||||
try testing.expectEqual(corpus.name2user.get("404"), null);
|
||||
try testing.expectEqual(corpus.name2user.get("vidmantas").?.uid, 1000);
|
||||
try testing.expectEqual(corpus.uid2user.get(42), null);
|
||||
try testing.expectEqual(corpus.uid2user.get(1000).?.gid, 1000);
|
||||
try testing.expectEqual(corpus.name2group.get("404"), null);
|
||||
try testing.expectEqual(corpus.name2group.get("vidmantas").?.gid, 1000);
|
||||
try testing.expectEqual(corpus.gid2group.get(42), null);
|
||||
try testing.expectEqual(corpus.gid2group.get(1000).?.gid, 1000);
|
||||
|
||||
// TODO: enable once sorted.
|
||||
//const membersOfAll = corpus.groupname2users.get("all").?.items;
|
||||
//try testing.expectEqualStrings(membersOfAll[0].name, "Name" ** 8);
|
||||
//try testing.expectEqualStrings(membersOfAll[1].name, "svc-bar");
|
||||
//try testing.expectEqualStrings(membersOfAll[2].name, "vidmantas");
|
||||
|
||||
const groupsOfVidmantas = corpus.username2groups.get("vidmantas").?.items;
|
||||
try testing.expectEqual(groupsOfVidmantas[0].gid, 1000);
|
||||
try testing.expectEqual(groupsOfVidmantas[1].gid, 1234);
|
||||
try testing.expectEqual(groupsOfVidmantas[2].gid, 9999);
|
||||
}
|
||||
|
||||
fn testUser(name: []const u8) User {
|
||||
|
|
18
src/user.zig
18
src/user.zig
|
@ -24,8 +24,7 @@ pub const User = struct {
|
|||
|
||||
// deep-clones a User record with a given Allocator.
|
||||
pub fn clone(self: *const User, allocator: Allocator) Allocator.Error!User {
|
||||
const stringdata = try allocator.alloc(u8, self.stringdata_len());
|
||||
//std.debug.print("\nptr to stringdata: {d}\n", .{@ptrToInt(stringdata.ptr)});
|
||||
const stringdata = try allocator.alloc(u8, self.strlen());
|
||||
const gecos_start = self.name.len;
|
||||
const home_start = gecos_start + self.gecos.len;
|
||||
const shell_start = home_start + self.shell.len;
|
||||
|
@ -34,7 +33,7 @@ pub const User = struct {
|
|||
mem.copy(u8, stringdata[home_start..], self.home);
|
||||
mem.copy(u8, stringdata[shell_start..], self.shell);
|
||||
|
||||
const u = User{
|
||||
return User{
|
||||
.uid = self.uid,
|
||||
.gid = self.gid,
|
||||
.name = stringdata[0..self.name.len],
|
||||
|
@ -42,22 +41,19 @@ pub const User = struct {
|
|||
.home = stringdata[home_start .. home_start + self.home.len],
|
||||
.shell = stringdata[shell_start .. shell_start + self.shell.len],
|
||||
};
|
||||
|
||||
return u;
|
||||
}
|
||||
|
||||
fn stringdata_len(self: *const User) usize {
|
||||
fn strlen(self: *const User) usize {
|
||||
return self.name.len +
|
||||
self.gecos.len +
|
||||
self.home.len +
|
||||
self.shell.len;
|
||||
}
|
||||
|
||||
pub fn deinit(self: *const User, allocator: Allocator) void {
|
||||
//allocator.free(self.stringdata);
|
||||
//self.* = undefined;
|
||||
const slice = self.home.ptr[0..self.stringdata_len()];
|
||||
pub fn deinit(self: *User, allocator: Allocator) void {
|
||||
const slice = self.home.ptr[0..self.strlen()];
|
||||
allocator.free(slice);
|
||||
self.* = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -439,8 +435,6 @@ test "User.clone" {
|
|||
.shell = "/bin/bash",
|
||||
};
|
||||
var user2 = try user.clone(allocator);
|
||||
user2.shell = "/bin/zsh";
|
||||
|
||||
defer user2.deinit(allocator);
|
||||
|
||||
try testing.expectEqualStrings(user.shell, "/bin/bash");
|
||||
|
|
Loading…
Reference in New Issue