aboutsummaryrefslogtreecommitdiff
path: root/icing/scoring/advanced_scoring/advanced-scorer_fuzz_test.cc
blob: ca081cd77cf032d0d35ec36d9d7f52615c27ff88 (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
70
71
72
73
74
75
76
77
78
79
80
81
// 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 <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>

#include "icing/file/filesystem.h"
#include "icing/file/portable-file-backed-proto-log.h"
#include "icing/index/embed/embedding-query-results.h"
#include "icing/schema/schema-store.h"
#include "icing/scoring/advanced_scoring/advanced-scorer.h"
#include "icing/store/document-store.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";
  EmbeddingQueryResults empty_embedding_query_results_;
  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, /*pre_mapping_fbv=*/false,
          /*use_persistent_hash_map=*/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,
                         SearchSpecProto::EmbeddingQueryMetricType::DOT_PRODUCT,
                         document_store.get(), schema_store.get(),
                         fake_clock.GetSystemTimeMilliseconds(),
                         /*join_children_fetcher=*/nullptr,
                         &empty_embedding_query_results_);

  // 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