aboutsummaryrefslogtreecommitdiff
path: root/icing/scoring/advanced_scoring/advanced-scorer_fuzz_test.cc
blob: 8db592a545e7d27dc26cd6b5ecde2be654beb735 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (C) 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <cstdint>
#include <memory>
#include <string_view>

#include "icing/scoring/advanced_scoring/advanced-scorer.h"
#include "icing/testing/fake-clock.h"
#include "icing/testing/tmp-directory.h"

namespace icing {
namespace lib {

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  FakeClock fake_clock;
  Filesystem filesystem;
  const std::string test_dir = GetTestTempDir() + "/icing";
  const std::string doc_store_dir = test_dir + "/doc_store";
  const std::string schema_store_dir = test_dir + "/schema_store";
  filesystem.DeleteDirectoryRecursively(test_dir.c_str());
  filesystem.CreateDirectoryRecursively(doc_store_dir.c_str());
  filesystem.CreateDirectoryRecursively(schema_store_dir.c_str());

  std::unique_ptr<SchemaStore> schema_store =
      SchemaStore::Create(&filesystem, schema_store_dir, &fake_clock)
          .ValueOrDie();
  std::unique_ptr<DocumentStore> document_store =
      DocumentStore::Create(&filesystem, doc_store_dir, &fake_clock,
                            schema_store.get(),
                            /*force_recovery_and_revalidate_documents=*/false,
                            /*namespace_id_fingerprint=*/false,
                            PortableFileBackedProtoLog<
                                DocumentWrapper>::kDeflateCompressionLevel,
                            /*initialize_stats=*/nullptr)
          .ValueOrDie()
          .document_store;

  std::string_view text(reinterpret_cast<const char*>(data), size);
  ScoringSpecProto scoring_spec;
  scoring_spec.set_rank_by(
      ScoringSpecProto::RankingStrategy::ADVANCED_SCORING_EXPRESSION);
  scoring_spec.set_advanced_scoring_expression(text);

  AdvancedScorer::Create(scoring_spec,
                         /*default_score=*/10, document_store.get(),
                         schema_store.get());

  // Not able to test the GetScore method of AdvancedScorer, since it will only
  // be available after AdvancedScorer is successfully created. However, the
  // text provided by the fuzz test is very random, which means that in most
  // cases, there will be syntax errors or type errors that cause
  // AdvancedScorer::Create to fail.
  return 0;
}

}  // namespace lib
}  // namespace icing