From 0275d6766961259eba9acaceddaba7279840c152 Mon Sep 17 00:00:00 2001 From: Moritz Gmeiner Date: Mon, 31 Mar 2025 00:02:39 +0200 Subject: [PATCH] added deserialiser tests, fixed some bugs in deserialised --- build.zig | 17 +++++-- build.zig.zon | 4 +- src/deserialise.zig | 89 +----------------------------------- tests/deserialise/binary.zig | 83 +++++++++++++++++++++++++++++++++ tests/deserialise/int.zig | 38 +++++++++++++++ tests/deserialise/root.zig | 40 ++++++++++++++++ tests/deserialise/string.zig | 84 ++++++++++++++++++++++++++++++++++ tests/root.zig | 5 ++ 8 files changed, 267 insertions(+), 93 deletions(-) create mode 100644 tests/deserialise/binary.zig create mode 100644 tests/deserialise/int.zig create mode 100644 tests/deserialise/root.zig create mode 100644 tests/deserialise/string.zig create mode 100644 tests/root.zig diff --git a/build.zig b/build.zig index 6c22072..0c04aaf 100644 --- a/build.zig +++ b/build.zig @@ -33,7 +33,7 @@ pub fn build(b: *std.Build) void { // for actually invoking the compiler. const lib = b.addLibrary(.{ .linkage = .static, - .name = "msgpack_zig", + .name = "msgpack", .root_module = lib_mod, }); @@ -47,12 +47,23 @@ pub fn build(b: *std.Build) void { const lib_unit_tests = b.addTest(.{ .root_module = lib_mod, }); - const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); + const tests_tests = b.addTest(.{ + .root_source_file = b.path("tests/root.zig"), + .target = target, + .optimize = optimize, + }); + + tests_tests.root_module.addImport("msgpack", lib.root_module); + + const run_tests_tests = b.addRunArtifact(tests_tests); + // Similar to creating the run step earlier, this exposes a `test` step to // the `zig build --help` menu, providing a way for the user to request // running the unit tests. - const test_step = b.step("test", "Run unit tests"); + const test_step = b.step("test", "Run tests"); + test_step.dependOn(&run_lib_unit_tests.step); + test_step.dependOn(&run_tests_tests.step); } diff --git a/build.zig.zon b/build.zig.zon index aae74b6..f2d0cba 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -6,7 +6,7 @@ // // It is redundant to include "zig" in this name because it is already // within the Zig package namespace. - .name = .msgpack_zig, + .name = .msgpack, // This is a [Semantic Version](https://semver.org/). // In a future version of Zig it will be used for package deduplication. @@ -24,7 +24,7 @@ // original project's identity. Thus it is recommended to leave the comment // on the following line intact, so that it shows up in code reviews that // modify the field. - .fingerprint = 0x9949832bc6a8b19c, // Changing this has security and trust implications. + .fingerprint = 0x6733a7561ee01213, // Changing this has security and trust implications. // Tracks the earliest Zig version that the package considers to be a // supported use case. diff --git a/src/deserialise.zig b/src/deserialise.zig index 9fbc739..bd7c8b1 100644 --- a/src/deserialise.zig +++ b/src/deserialise.zig @@ -115,7 +115,7 @@ fn deserialise_int(bytes: []const u8) DeserialiseError!ObjectLen { const payload = bytes[1..]; - std.debug.assert(tag >> 2 == 110100); + std.debug.assert(tag >> 2 == 0b110100); switch (@as(u2, @intCast(tag & 0b11))) { 0b00 => { @@ -578,90 +578,3 @@ test "neg fixint" { try std.testing.expectEqual(Object{ .integer = -1 }, obj); } - -test "raw" { - const alloc = std.testing.allocator; - - { - const bytes = [_]u8{ 0xc4, 0x03, 'A', 'B', 'C' }; - - const obj = try deserialise(alloc, &bytes); - defer obj.deinit(alloc); - - switch (obj) { - .raw => |raw| { - switch (raw) { - .binary => |s| try std.testing.expectEqualStrings("ABC", s), - .string => return error.TestExpectedEqual, - } - }, - else => return error.TestExpectedEqual, - } - } - - { - const bytes = [_]u8{ 0xc4, 0x00 }; - - const obj = try deserialise(alloc, &bytes); - defer obj.deinit(alloc); - - switch (obj) { - .raw => |raw| { - switch (raw) { - .binary => |s| try std.testing.expectEqualStrings("", s), - .string => return error.TestExpectedEqual, - } - }, - else => return error.TestExpectedEqual, - } - } - - { - var bytes: [2 + 255]u8 = undefined; - - bytes[0] = 0xc4; - bytes[1] = 0xff; - - for (bytes[2..]) |*c| { - c.* = 'A'; - } - - const obj = try deserialise(alloc, &bytes); - defer obj.deinit(alloc); - - switch (obj) { - .raw => |raw| { - switch (raw) { - .binary => |s| try std.testing.expectEqualStrings("A" ** 255, s), - .string => return error.TestExpectedEqual, - } - }, - else => return error.TestExpectedEqual, - } - } - - { - var bytes: [3 + 256]u8 = undefined; - - bytes[0] = 0xc5; - bytes[1] = 0x01; - bytes[2] = 0x00; - - for (bytes[3..]) |*c| { - c.* = 'A'; - } - - const obj = try deserialise(alloc, &bytes); - defer obj.deinit(alloc); - - switch (obj) { - .raw => |raw| { - switch (raw) { - .binary => |s| try std.testing.expectEqualStrings("A" ** 256, s), - .string => return error.TestExpectedEqual, - } - }, - else => return error.TestExpectedEqual, - } - } -} diff --git a/tests/deserialise/binary.zig b/tests/deserialise/binary.zig new file mode 100644 index 0000000..9d3eb7d --- /dev/null +++ b/tests/deserialise/binary.zig @@ -0,0 +1,83 @@ +const std = @import("std"); + +const msgpack = @import("msgpack"); + +const deserialise = msgpack.deserialise.deserialise; + +fn test_binary(bytes: []const u8, expected: []const u8) !void { + const alloc = std.testing.allocator; + + const obj = try deserialise(alloc, bytes); + defer obj.deinit(alloc); + + try std.testing.expectEqualStrings(expected, obj.raw.binary); +} + +test "binary 1" { + const bytes = [_]u8{ 0xc4, 0x03, 'A', 'B', 'C' }; + + try test_binary(&bytes, "ABC"); +} + +test "binary 2" { + const bytes = [_]u8{ 0xc4, 0x00 }; + + try test_binary(&bytes, ""); +} + +test "binary 3" { + var bytes: [2 + 255]u8 = undefined; + + bytes[0] = 0xc4; + bytes[1] = 0xff; + + for (bytes[2..]) |*c| { + c.* = 'A'; + } + + try test_binary(&bytes, "A" ** 255); +} + +test "binary 4" { + var bytes: [3 + 256]u8 = undefined; + + bytes[0] = 0xc5; + bytes[1] = 0x01; + bytes[2] = 0x00; + + for (bytes[3..]) |*c| { + c.* = 'A'; + } + + try test_binary(&bytes, "A" ** 256); +} + +test "binary 5" { + var bytes: [3 + 65535]u8 = undefined; + + bytes[0] = 0xc5; + bytes[1] = 0xff; + bytes[2] = 0xff; + + for (bytes[3..]) |*c| { + c.* = 'A'; + } + + try test_binary(&bytes, "A" ** 65535); +} + +test "binary 6" { + var bytes: [5 + 65536]u8 = undefined; + + bytes[0] = 0xc6; + bytes[1] = 0x00; + bytes[2] = 0x01; + bytes[3] = 0x00; + bytes[4] = 0x00; + + for (bytes[5..]) |*c| { + c.* = 'A'; + } + + try test_binary(&bytes, "A" ** 65536); +} diff --git a/tests/deserialise/int.zig b/tests/deserialise/int.zig new file mode 100644 index 0000000..28fab32 --- /dev/null +++ b/tests/deserialise/int.zig @@ -0,0 +1,38 @@ +const std = @import("std"); + +pub const msgpack = @import("msgpack"); + +const deserialise = msgpack.deserialise.deserialise; + +fn test_int(bytes: []const u8, expected: i64) !void { + const alloc = std.testing.allocator; + + const obj = try deserialise(alloc, bytes); + defer obj.deinit(alloc); + + try std.testing.expectEqual(expected, obj.integer); +} + +test "int i8" { + try test_int(&[_]u8{ 0xd0, 0x80 }, std.math.minInt(i8)); + + try test_int(&[_]u8{0x7f}, std.math.maxInt(i8)); +} + +test "int i16" { + try test_int(&[_]u8{ 0xd1, 0x80, 0x00 }, std.math.minInt(i16)); + + try test_int(&[_]u8{ 0xcd, 0x7f, 0xff }, std.math.maxInt(i16)); +} + +test "int i32" { + try test_int(&[_]u8{ 0xd2, 0x80, 0x00, 0x00, 0x00 }, std.math.minInt(i32)); + + try test_int(&[_]u8{ 0xce, 0x7f, 0xff, 0xff, 0xff }, std.math.maxInt(i32)); +} + +test "int i64" { + try test_int(&[_]u8{ 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, std.math.minInt(i64)); + + try test_int(&[_]u8{ 0xcf, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, std.math.maxInt(i64)); +} diff --git a/tests/deserialise/root.zig b/tests/deserialise/root.zig new file mode 100644 index 0000000..432324d --- /dev/null +++ b/tests/deserialise/root.zig @@ -0,0 +1,40 @@ +const std = @import("std"); + +pub const binary = @import("binary.zig"); +pub const string = @import("string.zig"); +pub const int = @import("int.zig"); + +pub const msgpack = @import("msgpack"); + +const deserialise = msgpack.deserialise.deserialise; + +test { + @import("std").testing.refAllDecls(@This()); +} + +test "nil" { + const alloc = std.testing.allocator; + + const obj = try deserialise(alloc, &[_]u8{0xc0}); + defer obj.deinit(alloc); + + try std.testing.expectEqualDeep(msgpack.Object.nil, obj); +} + +test "bool" { + const alloc = std.testing.allocator; + + { + const obj = try deserialise(alloc, &[_]u8{0xc2}); + defer obj.deinit(alloc); + + try std.testing.expectEqualDeep(msgpack.Object{ .bool = false }, obj); + } + + { + const obj = try deserialise(alloc, &[_]u8{0xc3}); + defer obj.deinit(alloc); + + try std.testing.expectEqualDeep(msgpack.Object{ .bool = true }, obj); + } +} diff --git a/tests/deserialise/string.zig b/tests/deserialise/string.zig new file mode 100644 index 0000000..bbc87bc --- /dev/null +++ b/tests/deserialise/string.zig @@ -0,0 +1,84 @@ +const std = @import("std"); + +const msgpack = @import("msgpack"); + +const deserialise = msgpack.deserialise.deserialise; + +fn test_string(bytes: []const u8, expected: []const u8) !void { + const alloc = std.testing.allocator; + + const obj = try deserialise(alloc, bytes); + defer obj.deinit(alloc); + + try std.testing.expectEqualStrings(expected, obj.raw.string); +} + +test "string 1" { + const bytes = [_]u8{ 0b101_00000 | 0x03, 'A', 'B', 'C' }; + + try test_string(&bytes, "ABC"); +} + +test "string 2" { + const bytes = [_]u8{ 0xd9, 0x03, 'A', 'B', 'C' }; + + try test_string(&bytes, "ABC"); +} +test "string 3" { + const bytes = [_]u8{ 0xd9, 0x00 }; + + try test_string(&bytes, ""); +} +test "string 4" { + var bytes: [2 + 255]u8 = undefined; + + bytes[0] = 0xd9; + bytes[1] = 0xff; + + for (bytes[2..]) |*c| { + c.* = 'A'; + } + + try test_string(&bytes, "A" ** 255); +} +test "string 5" { + var bytes: [3 + 256]u8 = undefined; + + bytes[0] = 0xda; + bytes[1] = 0x01; + bytes[2] = 0x00; + + for (bytes[3..]) |*c| { + c.* = 'A'; + } + + try test_string(&bytes, "A" ** 256); +} +test "string 6" { + var bytes: [3 + 65535]u8 = undefined; + + bytes[0] = 0xda; + bytes[1] = 0xff; + bytes[2] = 0xff; + + for (bytes[3..]) |*c| { + c.* = 'A'; + } + + try test_string(&bytes, "A" ** 65535); +} +test "string 7" { + var bytes: [5 + 65536]u8 = undefined; + + bytes[0] = 0xdb; + bytes[1] = 0x00; + bytes[2] = 0x01; + bytes[3] = 0x00; + bytes[4] = 0x00; + + for (bytes[5..]) |*c| { + c.* = 'A'; + } + + try test_string(&bytes, "A" ** 65536); +} diff --git a/tests/root.zig b/tests/root.zig new file mode 100644 index 0000000..6dcea11 --- /dev/null +++ b/tests/root.zig @@ -0,0 +1,5 @@ +pub const deserialise = @import("deserialise/root.zig"); + +test { + @import("std").testing.refAllDecls(@This()); +}