test and README tiny updates
This commit is contained in:
@@ -8,7 +8,7 @@ const Bom = 0x1234;
|
||||
|
||||
pub const SectionLength = 64;
|
||||
|
||||
const HeaderError = error{
|
||||
const InvalidHeader = error{
|
||||
InvalidMagic,
|
||||
InvalidVersion,
|
||||
InvalidBom,
|
||||
@@ -23,16 +23,16 @@ const Header = packed struct {
|
||||
num_shells: u8,
|
||||
num_users: u32,
|
||||
num_groups: u32,
|
||||
offset_cmph_uid2user: u32,
|
||||
offset_cmph_groupname2group: u32,
|
||||
offset_cmph_username2user: u32,
|
||||
offset_bdz_uid2user: u32,
|
||||
offset_bdz_groupname2group: u32,
|
||||
offset_bdz_username2user: u32,
|
||||
offset_idx: u32,
|
||||
offset_groups: u32,
|
||||
offset_users: u32,
|
||||
offset_groupmembers: u32,
|
||||
offset_additional_gids: u32,
|
||||
|
||||
pub fn init(blob: [HeaderSize]u8) HeaderError!Header {
|
||||
pub fn init(blob: [HeaderSize]u8) InvalidHeader!Header {
|
||||
const self = @bitCast(Header, blob);
|
||||
for (Magic) |item, index| {
|
||||
if (self.magic[index] != item) return error.InvalidMagic;
|
||||
@@ -48,9 +48,9 @@ const Header = packed struct {
|
||||
}
|
||||
|
||||
const offsets = [_]u32{
|
||||
self.offset_cmph_uid2user,
|
||||
self.offset_cmph_groupname2group,
|
||||
self.offset_cmph_username2user,
|
||||
self.offset_bdz_uid2user,
|
||||
self.offset_bdz_groupname2group,
|
||||
self.offset_bdz_username2user,
|
||||
self.offset_idx,
|
||||
self.offset_groups,
|
||||
self.offset_users,
|
||||
@@ -73,12 +73,15 @@ const Header = packed struct {
|
||||
|
||||
const testing = std.testing;
|
||||
|
||||
test "constants and types are reasonable" {
|
||||
test "header is byte-aligned" {
|
||||
try testing.expectEqual(HeaderSize * 8, @bitSizeOf(Header));
|
||||
}
|
||||
|
||||
test "Section length is a power of two" {
|
||||
try testing.expect(std.math.isPowerOfTwo(SectionLength));
|
||||
}
|
||||
|
||||
test "header pack and unpack" {
|
||||
test "header pack, unpack and validation" {
|
||||
const goodHeader = Header{
|
||||
.magic = Magic,
|
||||
.version = Version,
|
||||
@@ -86,9 +89,9 @@ test "header pack and unpack" {
|
||||
.num_shells = 0,
|
||||
.num_users = 0,
|
||||
.num_groups = 0,
|
||||
.offset_cmph_uid2user = 0,
|
||||
.offset_cmph_groupname2group = 0,
|
||||
.offset_cmph_username2user = 0,
|
||||
.offset_bdz_uid2user = 0,
|
||||
.offset_bdz_groupname2group = 0,
|
||||
.offset_bdz_username2user = 0,
|
||||
.offset_idx = 0,
|
||||
.offset_groups = 0,
|
||||
.offset_users = 0,
|
||||
@@ -96,8 +99,7 @@ test "header pack and unpack" {
|
||||
.offset_additional_gids = 0,
|
||||
};
|
||||
|
||||
const blob = goodHeader.asArray();
|
||||
const gotHeader = try Header.init(blob);
|
||||
const gotHeader = try Header.init(goodHeader.asArray());
|
||||
try testing.expectEqual(goodHeader, gotHeader);
|
||||
|
||||
{
|
||||
@@ -120,7 +122,7 @@ test "header pack and unpack" {
|
||||
|
||||
{
|
||||
var header = goodHeader;
|
||||
header.offset_cmph_uid2user = 65;
|
||||
header.offset_bdz_uid2user = 65;
|
||||
try testing.expectError(error.InvalidOffset, Header.init(header.asArray()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user