Coverage Report

Created: 2026-07-25 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/condition_fuzzer.cc
Line
Count
Source
1
// Copyright 2026 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
////////////////////////////////////////////////////////////////////////////////
16
17
#include <stdint.h>
18
#include <stddef.h>
19
#include <string>
20
#include <tuple>
21
#include <vector>
22
23
#include "condition.h"
24
#include "action.pb.h"
25
#include "system_state.pb.h"
26
#include "absl/time/time.h"
27
#include <fuzzer/FuzzedDataProvider.h>
28
29
11.8k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
30
11.8k
  FuzzedDataProvider provider(data, size);
31
32
  // Split the data into two parts for the two protobufs
33
  // We use a 50/50 split of the remaining bytes
34
11.8k
  size_t remaining = provider.remaining_bytes();
35
11.8k
  if (remaining < 2) {
36
1
    return 0;
37
1
  }
38
11.8k
  size_t size1 = provider.ConsumeIntegralInRange<size_t>(0, remaining - 1);
39
11.8k
  std::string proto1_bytes = provider.ConsumeBytesAsString(size1);
40
11.8k
  std::string proto2_bytes = provider.ConsumeRemainingBytesAsString();
41
42
11.8k
  safepower_agent_proto::Condition condition;
43
11.8k
  if (!condition.ParseFromString(proto1_bytes)) {
44
2.03k
    return 0;
45
2.03k
  }
46
47
9.83k
  safepower_agent_proto::SystemState system_state;
48
9.83k
  if (!system_state.ParseFromString(proto2_bytes)) {
49
5.29k
    return 0;
50
5.29k
  }
51
52
  // Generate some timestamps
53
4.54k
  absl::Time start_time = absl::FromUnixSeconds(1719657600); // Static base time
54
4.54k
  absl::Time current_time = start_time + absl::Seconds(provider.ConsumeIntegralInRange<int64_t>(0, 3600 * 24));
55
56
  // Call the target API
57
4.54k
  auto [status, matches] = safepower_agent::Condition::Matches(
58
4.54k
      condition, system_state, start_time, current_time);
59
60
4.54k
  return 0;
61
9.83k
}