Serenity Operating System
1/*
2 * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibTest/TestCase.h>
8#include <LibXML/Parser/Parser.h>
9
10TEST_CASE(char_data_ending)
11{
12 EXPECT_NO_CRASH("parsing character data ending by itself should not crash", [] {
13 // After seeing `<C>`, the parser will start parsing the content of the element. The content parser will then parse any character data it sees.
14 // The character parser would see the first two `]]` and consume them. Then, it would see the `>` and set the state machine to say we have seen this,
15 // but it did _not_ consume it and would instead tell GenericLexer that it should stop consuming characters. Therefore, we only consumed 2 characters.
16 // Then, it would see that we are in the state where we've seen the full `]]>` and try to take off three characters from the end of the consumed
17 // input when we only have 2 characters, causing an assertion failure as we are asking to take off more characters than there really is.
18 XML::Parser parser("<C>]]>"sv);
19 (void)parser.parse();
20 return Test::Crash::Failure::DidNotCrash;
21 });
22}
23
24TEST_CASE(character_reference_integer_overflow)
25{
26 EXPECT_NO_CRASH("parsing character references that do not fit in 32 bits should not crash", [] {
27 XML::Parser parser("<G>�"sv);
28 (void)parser.parse();
29 return Test::Crash::Failure::DidNotCrash;
30 });
31}