Rework tests to work properly.

This commit is contained in:
Kitteh 2021-06-02 20:29:40 +01:00
parent 918cde57c1
commit 09066b3033
5 changed files with 93 additions and 61 deletions

View file

@ -51,10 +51,7 @@ pub const Client = struct {
qvar.pretty_print_variant(.{.QVariantMap = map}, 0);
var size = read.readUInt(s.stream.reader());
std.debug.print("\n\nInitResponse: \n", .{});
var varient = try read.readQVariant(s.stream.reader(), s.allocator);
qvar.pretty_print_variant(varient, 0);
try s.read_quassel_packet();
}
pub fn quassel_login(s: *Client, username: []const u8, password: []const u8) !void {
@ -71,6 +68,14 @@ pub const Client = struct {
try s.stream.writer().writeAll(list.items);
}
pub fn read_quassel_packet(s: *Client) !void {
var size = read.readUInt(s.stream.reader());
std.debug.print("\n\nQuassel Packet: \n", .{});
var varient = try read.readQVariant(s.stream.reader(), s.allocator);
qvar.pretty_print_variant(varient, 0);
qvar.freeQVariant(varient, s.allocator);
}
};
pub fn initClient(allocator: *std.mem.Allocator, stream: *std.net.Stream) Client {

View file

@ -13,4 +13,6 @@ pub fn main() !void {
try client.handshake();
try client.quassel_init_packet();
try client.quassel_login("z", "password");
try client.read_quassel_packet();
}

View file

@ -114,3 +114,40 @@ pub fn pretty_print_variant(variant: QVariant, indentLevel: u64) void {
},
}
}
pub fn freeQVariant(variant: QVariant, allocator: *std.mem.Allocator) void {
switch (variant) {
.Byte, .Int, .UInt, .Short => {
// Static
},
.QVariantMap => |q| {
var qMapIter = q.iterator();
while (qMapIter.next()) |v| {
allocator.free(v.key);
freeQVariant(v.value, allocator);
}
},
.QVariantList => |l| {
for (l) |varient, i| {
freeQVariant(varient, allocator);
}
allocator.free(l);
},
.String => |s| {
allocator.free(s);
},
.QStringList => |l| {
for (l) |varient, i| {
allocator.free(varient);
}
allocator.free(l);
},
.QByteArray => |ba| {
allocator.free(ba);
},
}
}

View file

@ -6,58 +6,58 @@ const read = @import("./read.zig");
const write = @import("./write.zig");
const qvar = @import("./qvariant.zig");
test "read/write int" {
test "UInt serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.add_int(byteList.writer(), 4242);
try write.writeUInt(byteList.writer(), 4242);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_int(fBS.reader());
var val = try read.readUInt(fBS.reader());
try expect(val == 4242);
}
test "read/write short" {
test "Short serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.add_short(byteList.writer(), 6969);
try write.writeShort(byteList.writer(), 6969);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_short(fBS.reader());
var val = try read.readShort(fBS.reader());
try expect(val == 6969);
}
test "read/write byte" {
test "Byte serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.add_byte(byteList.writer(), 'a');
try write.writeByte(byteList.writer(), 'a');
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_byte(fBS.reader());
var val = try read.readByte(fBS.reader());
try expect(val == 'a');
}
test "read/write string" {
test "String serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
var arr = "Hello World!".*;
try write.add_string(byteList.writer(), global_allocator, &arr);
try write.writeString(byteList.writer(), global_allocator, &arr);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_string(fBS.reader(), global_allocator);
var val = try read.readString(fBS.reader(), global_allocator);
defer global_allocator.free(val);
try std.testing.expectEqualStrings("Hello World!", val);
}
test "read/write bytearray" {
test "QByteArray serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
@ -66,19 +66,19 @@ test "read/write bytearray" {
try arr.append(13);
try arr.append(12);
try write.add_bytearray(byteList.writer(), arr);
try write.writeQByteArray(byteList.writer(), arr.items);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_bytearray(fBS.reader(), global_allocator);
defer val.deinit();
var val = try read.readQByteArray(fBS.reader(), global_allocator);
defer global_allocator.free(val);
try expect(arr.items.len == val.items.len);
try expect(arr.items.len == val.len);
for (arr.items) |item, index| {
try std.testing.expect(item == val.items[index]);
try std.testing.expect(item == val[index]);
}
}
test "read/write strlist" {
test "QStringList serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
@ -87,70 +87,58 @@ test "read/write strlist" {
try arr.append("Hewwo");
try arr.append("World");
try write.add_stringlist(byteList.writer(), global_allocator, arr);
try write.writeQStringList(byteList.writer(), global_allocator, arr.items);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_stringlist(fBS.reader(), global_allocator);
var val = try read.readQStringList(fBS.reader(), global_allocator);
defer {
for (val.items) |str| {
for (val) |str| {
global_allocator.free(str);
}
val.deinit();
global_allocator.free(val);
}
try expect(arr.items.len == val.items.len);
try expect(arr.items.len == val.len);
for (arr.items) |str, index| {
try std.testing.expectEqualStrings(str, val.items[index]);
try std.testing.expectEqualStrings(str, val[index]);
}
}
test "read/write byte variant" {
test "QVariant UInt serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.add_qvariant_with_id(byteList.writer(), @enumToInt(qvar.QVariantTypes.byte));
try write.add_byte(byteList.writer(), 'a');
// ACAB
try write.writeQVariant(byteList.writer(), global_allocator, .{ .UInt = 1312 });
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_variant(fBS.reader(), global_allocator);
try expect(val.byte == 'a');
var variant = try read.readQVariant(fBS.reader(), global_allocator);
try expect(variant.UInt == 1312);
}
test "read/write int variant" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
try write.add_qvariant_with_id(byteList.writer(), @enumToInt(qvar.QVariantTypes.int_2));
try write.add_int(byteList.writer(), 1312);
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_variant(fBS.reader(), global_allocator);
try expect(val.int == 1312);
}
test "read/write qvariantmap" {
test "QVariant QVariantMap serialization/deserialization" {
var byteList = std.ArrayList(u8).init(global_allocator);
defer byteList.deinit();
var map = std.StringHashMap(qvar.QVariant).init(global_allocator);
defer map.deinit();
try map.put("testkey", .{ .int = 1337 });
try write.add_qvariantmap(byteList.writer(), global_allocator, map);
try map.put("testkey", .{ .UInt = 1337 });
try write.writeQVariant(byteList.writer(), global_allocator, .{
.QVariantMap = map,
});
var fBS = std.io.fixedBufferStream(byteList.items);
var val = try read.get_variant(fBS.reader(), global_allocator);
var qmap = val.qmap;
var testKey = qmap.get("testkey");
var varient = try read.readQVariant(fBS.reader(), global_allocator);
defer qvar.freeQVariant(varient, global_allocator);
var qVariantMap = varient.QVariantMap;
var testKey = qVariantMap.get("testkey");
if (testKey) |key| {
try expect(key.int == 1337);
var qMapIter = qmap.iterator();
while (qMapIter.next()) |a| {
global_allocator.free(a.key);
}
qmap.deinit();
try expect(key.UInt == 1337);
} else {
try expect(!true);
}

View file

@ -19,7 +19,7 @@ pub fn writeByte(writer: anytype, byte: u8) !void {
try writer.writeByte(byte);
}
pub fn writeByteArray(writer: anytype, array: []u8) !void {
pub fn writeQByteArray(writer: anytype, array: []u8) !void {
try writeUInt(writer, @intCast(u32, array.len));
try writer.writeAll(array);
}
@ -96,7 +96,7 @@ pub fn writeQVariant(writer: anytype, allocator: *std.mem.Allocator, variant: qv
try writeQStringList(writer, allocator, out);
},
.QByteArray => |out| {
try writeByteArray(writer, out);
try writeQByteArray(writer, out);
},
.Short => |out| {
try writeShort(writer, out);