From fc9b0d24129d5bb878a2799d1903a51ec29e96e8 Mon Sep 17 00:00:00 2001 From: Ian Johnson Date: Fri, 1 Nov 2024 22:19:48 -0400 Subject: [PATCH] test: update fuzzer for writer changes --- fuzz/src/fuzz.zig | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/fuzz/src/fuzz.zig b/fuzz/src/fuzz.zig index 86f553b..b2aa5e0 100644 --- a/fuzz/src/fuzz.zig +++ b/fuzz/src/fuzz.zig @@ -16,11 +16,53 @@ fn fuzz(gpa: Allocator, input: []const u8) !void { var doc = xml.StaticDocument.init(input); var reader = doc.reader(gpa, .{}); defer reader.deinit(); + + var out_bytes = std.ArrayList(u8).init(gpa); + defer out_bytes.deinit(); + const output = xml.streamingOutput(out_bytes.writer()); + var writer = output.writer(gpa, .{}); + defer writer.deinit(); + while (true) { const node = reader.read() catch |err| switch (err) { error.MalformedXml => break, error.OutOfMemory => return error.OutOfMemory, }; - if (node == .eof) break; + switch (node) { + .eof => break, + .xml_declaration => { + try writer.xmlDeclaration(reader.xmlDeclarationEncoding(), reader.xmlDeclarationStandalone()); + }, + .comment => { + // TODO: not implemented yet + }, + .element_start => { + try writer.elementStart(reader.elementName()); + for (0..reader.attributeCount()) |i| { + try writer.attribute(reader.attributeName(i), try reader.attributeValue(i)); + } + }, + .element_end => { + try writer.elementEnd(); + }, + .pi => { + try writer.pi(reader.piTarget(), try reader.piData()); + }, + .text => { + try writer.text(try reader.text()); + }, + .cdata => { + try writer.text(try reader.cdata()); + }, + .character_reference => { + var buf: [4]u8 = undefined; + const len = std.unicode.utf8Encode(reader.characterReferenceChar(), &buf) catch unreachable; + try writer.text(buf[0..len]); + }, + .entity_reference => { + const value = xml.predefined_entities.get(reader.entityReferenceName()) orelse unreachable; + try writer.text(value); + }, + } } }