Skip to content

Commit

Permalink
Unifying fuzz tests; #576
Browse files Browse the repository at this point in the history
  • Loading branch information
the-moisrex committed Dec 24, 2024
1 parent 9d0bd6d commit 31e713a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
31 changes: 3 additions & 28 deletions tests/unicode_fuzz.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@

#include "../webpp/unicode/normalization.hpp"
#include "common/fuzz_common.hpp"

// NOLINTBEGIN(*-pro-type-reinterpret-cast)
void unicode_fuzz(std::string_view data) {
using webpp::unicode::toNFC;
#include "./unicode_fuzz.hpp"

std::string const str{data.data(), data.size()};
std::u8string const str8{reinterpret_cast<char8_t const*>(data.data()), data.size()};
std::u16string const str16{reinterpret_cast<char16_t const*>(data.data()), data.size() / 2};
std::u32string const str32{reinterpret_cast<char32_t const*>(data.data()), data.size() / 4};
auto const res = toNFC<std::string>(str);
auto const res8 = toNFC<std::u8string>(str8);
auto const res16 = toNFC<std::u16string>(str16);
auto const res32 = toNFC<std::u32string>(str32);
if (!str8.empty()) {
ASSERT_NE(str8.size(), 0);
ASSERT_NE(str16.size(), 0);
ASSERT_NE(str32.size(), 0);
ASSERT_NE(str.size(), 0);
ASSERT_NE(res.size(), 0);
ASSERT_NE(res16.size(), 0);
ASSERT_NE(res8.size(), 0);
ASSERT_NE(res32.size(), 0);
}
}

// NOLINTEND(*-pro-type-reinterpret-cast)
#include "common/fuzz_common.hpp"

register_fuzz(unicode_fuzz);
register_fuzz(webpp::tests::unicode_fuzz);
51 changes: 51 additions & 0 deletions tests/unicode_fuzz.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Created by moisrex on 12/23/24.

#ifndef UNICODE_FUZZ_HPP
#define UNICODE_FUZZ_HPP

#include "../webpp/unicode/normalization.hpp"
#include "./common/tests_common_pch.hpp"

namespace webpp::tests {
// NOLINTBEGIN(*-pro-type-reinterpret-cast)
static void unicode_fuzz(std::string_view data) {
using webpp::unicode::toNFC;

auto const length = data.size();
auto const* const ptr = data.data();

std::string const str{ptr, length};
std::u8string const str8{reinterpret_cast<char8_t const*>(ptr), length};
std::u16string str16;
std::u32string str32;
if (length / 2 != 0) {
str16 = std::u16string{reinterpret_cast<char16_t const*>(ptr), length / 2};
}
if (length / 4 != 0) {
str32 = std::u32string{reinterpret_cast<char32_t const*>(ptr), length / 4};
}
auto const res = toNFC<std::string>(str);
auto const res8 = toNFC<std::u8string>(str8);
auto const res16 = toNFC<std::u16string>(str16);
auto const res32 = toNFC<std::u32string>(str32);
if (!str8.empty()) {
ASSERT_NE(str8.size(), 0);
if (length / 2 != 0) {
ASSERT_NE(str16.size(), 0);
ASSERT_NE(res16.size(), 0);
}
if (length / 4 != 0) {
ASSERT_NE(str32.size(), 0);
ASSERT_NE(res32.size(), 0);
}
ASSERT_NE(str.size(), 0);
ASSERT_NE(res.size(), 0);
ASSERT_NE(res8.size(), 0);
}
}
} // namespace webpp::tests

// NOLINTEND(*-pro-type-reinterpret-cast)


#endif // UNICODE_FUZZ_HPP
9 changes: 9 additions & 0 deletions tests/unicode_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../webpp/std/format.hpp"
#include "../webpp/unicode/normalization.hpp"
#include "./unicode_fuzz.hpp"
#include "common/tests_common_pch.hpp"

#include <filesystem>
Expand Down Expand Up @@ -6924,7 +6925,9 @@ TEST(Unicode, CheckedNextCodePoint) {
}

TEST(Unicode, FuzzFixes) {
using webpp::tests::unicode_fuzz;
using webpp::unicode::toNFC;

EXPECT_EQ(u"", toNFC<std::u16string>(u""));
EXPECT_EQ("\xac", toNFC<std::string>("\xac"));
EXPECT_EQ("\x90\xe", toNFC<std::string>("\x90\xe"));
Expand All @@ -6935,6 +6938,12 @@ TEST(Unicode, FuzzFixes) {
EXPECT_EQ("\x10\xf4", toNFC<std::string>("\x10\xf4"));
EXPECT_EQ("\xa\xa\xfc", toNFC<std::string>("\xa\xa\xfc"));
EXPECT_EQ("\x75\xb2\xf5\xf5", toNFC<std::string>("\x75\xb2\xf5\xf5"));

unicode_fuzz("\xa\xa");
unicode_fuzz("\xa\xdf");
unicode_fuzz("\xa\xdd\xdd\xdd\xdd\xdd\xdd\xdd");
unicode_fuzz("\xa\xdf\xff\xff\xff");
unicode_fuzz("\x2e\xdd\xa");
}

// NOLINTEND(*-magic-numbers, *-pro-bounds-pointer-arithmetic)

0 comments on commit 31e713a

Please sign in to comment.