Coverage Report

Created: 2026-02-11 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libphonenumber/cpp/test/phonenumbers/fuzz_shortnumberinfo.cc
Line
Count
Source
1
/* Copyright 2025 Google Inc.
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
#include "phonenumbers/phonenumbermatcher.h"
16
#include <string>
17
#include <vector>
18
#include <limits>
19
#include <unicode/unistr.h>
20
21
#include "phonenumbers/base/basictypes.h"
22
#include "phonenumbers/base/memory/scoped_ptr.h"
23
#include "phonenumbers/base/memory/singleton.h"
24
#include "phonenumbers/default_logger.h"
25
#include "phonenumbers/phonenumber.h"
26
#include "phonenumbers/phonenumbermatch.h"
27
#include "phonenumbers/phonenumberutil.h"
28
#include "phonenumbers/stringutil.h"
29
#include "phonenumbers/asyoutypeformatter.h"
30
#include "phonenumbers/shortnumberinfo.h"
31
#include <fuzzer/FuzzedDataProvider.h>
32
33
// returns a short number cost based on the data we got from libfuzzer
34
i18n::phonenumbers::ShortNumberInfo::ShortNumberCost ConsumeShortNumberCost(
35
2.99k
    FuzzedDataProvider& fuzzed_data) {
36
2.99k
  switch (fuzzed_data.ConsumeIntegralInRange(0, 4)) {
37
2.92k
    case 0: return i18n::phonenumbers::ShortNumberInfo::TOLL_FREE;
38
10
    case 1: return i18n::phonenumbers::ShortNumberInfo::STANDARD_RATE;
39
30
    case 2: return i18n::phonenumbers::ShortNumberInfo::PREMIUM_RATE;
40
25
    default: return i18n::phonenumbers::ShortNumberInfo::UNKNOWN_COST;
41
2.99k
  }
42
2.99k
}
43
44
2.99k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
45
  // setup the data provider and util
46
2.99k
  FuzzedDataProvider fuzzed_data(data, size);
47
2.99k
  i18n::phonenumbers::PhoneNumberUtil* phone_util = 
48
2.99k
      i18n::phonenumbers::PhoneNumberUtil::GetInstance();
49
50
  // setup all the data we need to pass to the target methods
51
2.99k
  i18n::phonenumbers::PhoneNumber phone_number;
52
2.99k
  std::string number = fuzzed_data.ConsumeRandomLengthString(32);
53
2.99k
  bool region_is_2_bytes = fuzzed_data.ConsumeBool();
54
2.99k
  std::string region = fuzzed_data.ConsumeBytesAsString(region_is_2_bytes ? 2 : 3);
55
2.99k
  if (fuzzed_data.ConsumeBool()) {
56
52
    phone_util->ParseAndKeepRawInput(number, region, &phone_number);
57
2.93k
  } else {
58
2.93k
    phone_util->Parse(number, region, &phone_number);
59
2.93k
  }
60
61
  // fuzz the public methods
62
2.99k
  i18n::phonenumbers::ShortNumberInfo short_info;
63
2.99k
  short_info.IsPossibleShortNumberForRegion(phone_number, region);
64
2.99k
  short_info.IsPossibleShortNumber(phone_number);
65
2.99k
  short_info.IsValidShortNumber(phone_number);
66
2.99k
  short_info.GetExpectedCostForRegion(phone_number, region);
67
2.99k
  short_info.GetExpectedCost(phone_number);
68
2.99k
  short_info.GetExampleShortNumber(region);
69
2.99k
  i18n::phonenumbers::ShortNumberInfo::ShortNumberCost cost = 
70
2.99k
      ConsumeShortNumberCost(fuzzed_data);
71
2.99k
  short_info.GetExampleShortNumberForCost(region, cost);
72
2.99k
  short_info.ConnectsToEmergencyNumber(number, region);
73
2.99k
  short_info.IsEmergencyNumber(number, region);
74
2.99k
  short_info.IsCarrierSpecific(phone_number);
75
2.99k
  short_info.IsCarrierSpecificForRegion(phone_number, region);
76
2.99k
  short_info.IsSmsServiceForRegion(phone_number, region);
77
78
2.99k
  return 0;
79
2.99k
}