Skip to content

Commit

Permalink
test: update fuzzer for writer changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ianprime0509 committed Nov 2, 2024
1 parent 7cd4ce1 commit fc9b0d2
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion fuzz/src/fuzz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
}
}
}

0 comments on commit fc9b0d2

Please sign in to comment.