Skip to content

Commit

Permalink
Add web search proto puzzle.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 608660172
  • Loading branch information
FuzzTest Team authored and copybara-github committed Feb 20, 2024
1 parent ad3dadf commit 3c5fc52
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions e2e_tests/testdata/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ cc_binary(
"@com_google_fuzztest//fuzztest",
"@com_google_fuzztest//fuzztest:fuzztest_gtest_main",
"@com_google_fuzztest//fuzztest:test_protobuf_cc_proto",
"@com_googlesource_code_re2//:re2",
],
)

Expand Down
49 changes: 49 additions & 0 deletions e2e_tests/testdata/fuzz_tests_with_proto_inputs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <unordered_set>
#include <vector>

#include "./fuzztest/fuzztest.h"
#include "./fuzztest/internal/test_protobuf.pb.h"
#include "re2/re2.h"

namespace {

Expand All @@ -27,6 +29,7 @@ using fuzztest::internal::FoodMachineProcedure;
using fuzztest::internal::RoboCourier560Plan;
using fuzztest::internal::SingleBytesField;
using fuzztest::internal::TestProtobuf;
using fuzztest::internal::WebSearchResult;

void BytesSummingToMagicValue(const SingleBytesField& input) {
char sum = 0;
Expand Down Expand Up @@ -434,4 +437,50 @@ void IntegerConditionsOnTestProtobufLevel02(const TestProtobuf& input) {
}
FUZZ_TEST(ProtoPuzzles, IntegerConditionsOnTestProtobufLevel02);

static bool IsValidUrl(const std::string& url) {
const std::string url_pattern =
R"(^(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]$)";
return RE2::FullMatch(url, url_pattern);
}

void ValidateUrls(const WebSearchResult& search_result) {
// Search result must contain at least 1 URL.
if (search_result.count() <= 0) {
return;
}

// Search result must contain at most 10 URLs.
if (search_result.count() > 10) {
return;
}

// Query should be at least 2 letters in size.
if (search_result.query().empty() || search_result.query().size() < 2) {
return;
}

// Search result must contain exactly `count` URLs.
if (search_result.urls_size() != search_result.count()) {
return;
}

int valid_urls = 0;
std::unordered_set<std::string> unique_urls;

for (const auto& url : search_result.urls()) {
// Check if url is a valid url.
if (IsValidUrl(url)) {
valid_urls++;
unique_urls.insert(url);
}
}

// Ensure that all valid URLs are unique.
if (unique_urls.size() != search_result.urls_size()) {
return;
}
std::abort();
}
FUZZ_TEST(ProtoPuzzles, ValidateUrls);

} // namespace
8 changes: 8 additions & 0 deletions fuzztest/internal/test_protobuf.proto
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,11 @@ message RoboCourier560Plan {
// Any non-mail related actions to take at a address.
map<string, ExtraAction> extra_actions = 2;
}

message WebSearchResult {
// Number of URLs in the search result.
optional int64 count = 1;
optional string query = 2;
// A list of size `count` containing valid URLs.
repeated string urls = 3;
}

0 comments on commit 3c5fc52

Please sign in to comment.