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