| #include <locale.h> |
| |
| #include "testing/base/public/gunit.h" |
| #include "testing/fuzzing/fuzztest.h" |
| #include "third_party/glibc_locales/glibc_locales.h" |
| #include "third_party/glibc_locales/supported_locales.h" |
| |
| auto AnyUTF8Locale() { return fuzztest::ElementOf<std::string>({UTF_LOCALES}); } |
| |
| auto AnyNonUTF8Locale() { |
| return fuzztest::ElementOf<std::string>({NONUTF_LOCALES}); |
| } |
| |
| // Does google_newlocale() work with any locale-name string? |
| void NewlocaleArbitaryInputTest(std::string name) { |
| google_locale_t locale = google_newlocale(LC_ALL_MASK, name.c_str(), NULL, 0); |
| google_freelocale(locale); |
| } |
| FUZZ_TEST(DefinedBehaviorTests, NewlocaleArbitaryInputTest); |
| |
| // Does google_newlocale() load any supported locale? |
| void test_newlocale(std::string name) { |
| google_locale_t locale = google_newlocale(LC_ALL_MASK, name.c_str(), NULL, 0); |
| EXPECT_TRUE(locale != NULL); |
| google_freelocale(locale); |
| } |
| |
| void NewlocaleSupportedUTF8LocaleTest(std::string name) { |
| test_newlocale(name); |
| } |
| FUZZ_TEST(DefinedBehaviorTests, NewlocaleSupportedUTF8LocaleTest) |
| .WithDomains(AnyUTF8Locale()); |
| |
| void NewlocaleSupportedNonUTF8LocaleTest(std::string name) { |
| test_newlocale(name); |
| } |
| FUZZ_TEST(DefinedBehaviorTests, NewlocaleSupportedNonUTF8LocaleTest) |
| .WithDomains(AnyNonUTF8Locale()); |
| |
| // Does google_strcoll_l work with any pair of strings? |
| // Core test function |
| void test_strcoll(std::string name, std::string s1, std::string s2) { |
| google_locale_t locale = google_newlocale(LC_ALL_MASK, name.c_str(), NULL, 0); |
| EXPECT_TRUE(locale != NULL); |
| google_strcoll_l(s1.c_str(), s2.c_str(), locale); |
| google_freelocale(locale); |
| } |
| |
| // Test UTF-8 locales with arbitrary valid-UTF-8 strings. |
| void StrcollArbitraryUTF8InputTest(std::string name, std::string s1, |
| std::string s2) { |
| test_strcoll(name, s1, s2); |
| } |
| FUZZ_TEST(DefinedBehaviorTests, StrcollArbitraryUTF8InputTest) |
| .WithDomains(AnyUTF8Locale(), fuzztest::Utf8String(), |
| fuzztest::Utf8String()); |
| |
| // Test UTF-8 locales with arbitrary strings. |
| void StrcollArbitraryBadUTF8InputTest(std::string name, std::string s1, |
| std::string s2) { |
| test_strcoll(name, s1, s2); |
| } |
| FUZZ_TEST(DefinedBehaviorTests, StrcollArbitraryBadUTF8InputTest) |
| .WithDomains(AnyUTF8Locale(), fuzztest::Arbitrary<std::string>(), |
| fuzztest::Arbitrary<std::string>()); |
| |
| // Test non-UTF-8 locales with arbitrary strings. |
| void StrcollArbitraryNonUTF8InputTest(std::string name, std::string s1, |
| std::string s2) { |
| test_strcoll(name, s1, s2); |
| } |
| FUZZ_TEST(DefinedBehaviorTests, StrcollArbitraryNonUTF8InputTest) |
| .WithDomains(AnyNonUTF8Locale(), fuzztest::Arbitrary<std::string>(), |
| fuzztest::Arbitrary<std::string>()); |
| |
| // Does google_strxfrm_l work with any strings? |
| // Core test function |
| void test_strxfrm(std::string name, std::string s) { |
| google_locale_t locale = google_newlocale(LC_ALL_MASK, name.c_str(), NULL, 0); |
| EXPECT_TRUE(locale != NULL); |
| int n1 = google_strxfrm_l(NULL, s.c_str(), 0, locale); |
| char* s2 = (char*)malloc(n1); |
| EXPECT_TRUE(s2 != NULL); |
| int n2 = google_strxfrm_l(s2, s.c_str(), n1, locale); |
| EXPECT_TRUE(n1 == n2); |
| free(s2); |
| google_freelocale(locale); |
| } |
| |
| // Test UTF-8 locales with arbitrary valid-UTF-8 strings. |
| void StrxfrmArbitraryUTF8InputTest(std::string name, std::string s) { |
| test_strxfrm(name, s); |
| } |
| FUZZ_TEST(DefinedBehaviorTests, StrxfrmArbitraryUTF8InputTest) |
| .WithDomains(AnyUTF8Locale(), fuzztest::Utf8String()); |
| |
| // Test UTF-8 locales with arbitrary strings. |
| void StrxfrmArbitraryBadUTF8InputTest(std::string name, std::string s) { |
| test_strxfrm(name, s); |
| } |
| FUZZ_TEST(DefinedBehaviorTests, StrxfrmArbitraryBadUTF8InputTest) |
| .WithDomains(AnyUTF8Locale(), fuzztest::Arbitrary<std::string>()); |
| |
| // Test non-UTF-8 locales with arbitrary strings. |
| void StrxfrmArbitraryNonUTF8InputTest(std::string name, std::string s) { |
| test_strxfrm(name, s); |
| } |
| FUZZ_TEST(DefinedBehaviorTests, StrxfrmArbitraryNonUTF8InputTest) |
| .WithDomains(AnyUTF8Locale(), fuzztest::Arbitrary<std::string>()); |