BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlt_fuzzutil.h
Go to the documentation of this file.
1/// @file bdlt_fuzzutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_fuzzutil.h -*-C++-*-
8#ifndef INCLUDED_BDLT_FUZZUTIL
9#define INCLUDED_BDLT_FUZZUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_fuzzutil bdlt_fuzzutil
15/// @brief Provide creation of `bdlt` data types from fuzz data.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_fuzzutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_fuzzutil-purpose"> Purpose</a>
25/// * <a href="#bdlt_fuzzutil-classes"> Classes </a>
26/// * <a href="#bdlt_fuzzutil-description"> Description </a>
27/// * <a href="#bdlt_fuzzutil-usage"> Usage </a>
28/// * <a href="#bdlt_fuzzutil-example-1-provide-bdlt-datetime-within-a-range"> Example 1: Provide bdlt::Datetime within a Range </a>
29///
30/// # Purpose {#bdlt_fuzzutil-purpose}
31/// Provide creation of `bdlt` data types from fuzz data.
32///
33/// # Classes {#bdlt_fuzzutil-classes}
34///
35/// - bdlt::FuzzUtil: functions to create `bdlt` data types from fuzz data
36///
37/// @see bslim_fuzzdataview, bslim_fuzzutil
38///
39/// # Description {#bdlt_fuzzutil-description}
40/// This component defines a struct, `bdlt::FuzzUtil`, which serves
41/// as a namespace for functions to create `bdlt` date and time types from
42/// supplied fuzz input data.
43///
44/// Effectively, given a random sequence of bytes provided by a fuzz harness,
45/// like `libFuzzer`, this component can be used to generate arbitrary date and
46/// time values determined by that random sequence. This can be used to fuzz
47/// test an interface using date/time values. See the
48/// [BDE fuzz-testing guide](https://bloomberg.github.io/bde/articles/fuzz_testing.html)
49/// for more detail.
50///
51/// The operations to produce date and time types allow restrictions on ranges
52/// of dates to be produced, since it is common for operations to be valid only
53/// on a certain range of dates. The same is not typically true for time and
54/// time-zone offset, so operations that restrict the range of values produced
55/// for those attributes are not currently provided.
56///
57/// ## Usage {#bdlt_fuzzutil-usage}
58///
59///
60/// This section illustrates intended use of this component.
61///
62/// ### Example 1: Provide bdlt::Datetime within a Range {#bdlt_fuzzutil-example-1-provide-bdlt-datetime-within-a-range}
63///
64///
65/// The provided fuzz data is here represented by an array of bytes:
66/// @code
67/// const uint8_t data[] = {0x8A, 0x19, 0x0D, 0x44, 0x37, 0x0D,
68/// 0x38, 0x5E, 0x9B, 0xAA, 0xF3, 0xDA};
69/// @endcode
70/// First, we default construct a `bslim::FuzzDataView` object, `fdv`:
71/// @code
72/// bslim::FuzzDataView fdv(data, sizeof(data));
73///
74/// assert(12 == fdv.length());
75/// @endcode
76/// Next, we construct `Date` objects to represent the `begin` and `end` of the
77/// time interval in which we wish to construct our new `Date` from the fuzz
78/// data:
79/// @code
80/// bdlt::Date begin(1833, 5, 7);
81/// bdlt::Date end(1897, 4, 3);
82/// @endcode
83/// Finally, we create a `Date` object, `within`, by employing @ref bdlt_fuzzutil :
84/// @code
85/// bdlt::Date within = bdlt::FuzzUtil::consumeDateInRange(&fdv, begin, end);
86///
87/// assert(begin <= within);
88/// assert(within <= end);
89/// @endcode
90/// @}
91/** @} */
92/** @} */
93
94/** @addtogroup bdl
95 * @{
96 */
97/** @addtogroup bdlt
98 * @{
99 */
100/** @addtogroup bdlt_fuzzutil
101 * @{
102 */
103
104#include <bdlscm_version.h>
105
106#include <bdlt_date.h>
107#include <bdlt_datetz.h>
108#include <bdlt_time.h>
109#include <bdlt_timetz.h>
110#include <bdlt_datetime.h>
111#include <bdlt_datetimetz.h>
112
113#include <bslim_fuzzdataview.h>
114#include <bslim_fuzzutil.h>
115
116
117namespace bdlt {
118
119 // ===============
120 // struct FuzzUtil
121 // ===============
122
123/// This utility `struct` provides a namespace for a suite of functions that
124/// produce objects of date and time values from fuzz data.
125///
126/// See @ref bdlt_fuzzutil
127struct FuzzUtil {
128
129 private:
130 // PRIVATE CLASS METHODS
131
132 /// Return the first valid date represented by `Date` objects.
133 static Date firstValidDate();
134
135 /// Return the last valid date represented by `Date` objects.
136 static Date lastValidDate();
137
138 public:
139 // CLASS METHODS
140
141 /// Return a `Date` based on the next bytes from the specified
142 /// `fuzzDataView`, and update `fuzzDataView` to reflect the bytes
143 /// consumed.
144 static bdlt::Date consumeDate(bslim::FuzzDataView *fuzzDataView);
145
146 /// Return a `Date` between the specified `begin` and `end` dates based
147 /// on the next bytes from the specified `fuzzDataView`, and update
148 /// `fuzzDataView` to reflect the bytes consumed.
149 ///
150 /// \pre The behavior is undefined unless `begin <= end`.
152 const bdlt::Date& begin,
153 const bdlt::Date& end);
154
155 /// Return a `DateTz` based on the next bytes from the specified
156 /// `fuzzDataView`, and update `fuzzDataView` to reflect the bytes
157 /// consumed.
159
160 /// Return a `DateTz` whose `localDate()` is between the specified `begin`
161 /// and `end` dates based on the next bytes from the specified
162 /// `fuzzDataView`, and update `fuzzDataView` to reflect the bytes consumed.
163 ///
164 /// \pre The behavior is undefined unless `begin <= end`.
166 const bdlt::Date& begin,
167 const bdlt::Date& end);
168
169 /// Return a `Datetime` based on the next bytes from the specified
170 /// `fuzzDataView`, and update `fuzzDataView` to reflect the bytes
171 /// consumed.
173
174 /// Return a `Datetime` whose `date()` is between the specified `begin` and
175 /// `end` dates based on the next bytes from the specified `fuzzDataView`,
176 /// and update `fuzzDataView` to reflect the bytes consumed.
177 ///
178 /// \pre The behavior is undefined unless `begin <= end`.
180 const bdlt::Date& begin,
181 const bdlt::Date& end);
182
183 /// Return a `DatetimeTz` based on the next bytes from the specified
184 /// `fuzzDataView`, and update `fuzzDataView` to reflect the bytes
185 /// consumed.
187
188 /// Return a `DatetimeTz` whose `dateTz().localDate()` is between the
189 /// specified `begin` and `end` dates based on the next bytes from the
190 /// specified `fuzzDataView`, and update `fuzzDataView` to reflect the bytes consumed.
191 ///
192 /// \pre The behavior is undefined unless `begin <= end`.
194 bslim::FuzzDataView *fuzzDataView,
195 const bdlt::Date& begin,
196 const bdlt::Date& end);
197
198 /// Return a `Time` based on the next bytes from the specified
199 /// `fuzzDataView`, and update `fuzzDataView` to reflect the bytes
200 /// consumed.
201 static Time consumeTime(bslim::FuzzDataView *fuzzDataView);
202
203 /// Return a `TimeTz` based on the next bytes from the specified
204 /// `fuzzDataView`, and update `fuzzDataView` to reflect the bytes
205 /// consumed.
207
208 /// Return a timezone offset in minutes.
209 static int consumeTz(bslim::FuzzDataView *fuzzDataView);
210};
211
212// ============================================================================
213// INLINE DEFINITIONS
214// ============================================================================
215
216 // ---------------
217 // struct FuzzUtil
218 // ---------------
219
220// PRIVATE CLASS METHODS
221inline
222Date FuzzUtil::firstValidDate()
223{
224 return Date();
225}
226
227inline
228Date FuzzUtil::lastValidDate()
229{
230 return Date(9999, 12, 31);
231}
232
233// CLASS METHODS
234inline
236{
237 return consumeDateInRange(fuzzDataView, firstValidDate(), lastValidDate());
238}
239
240inline
242 const bdlt::Date& begin,
243 const bdlt::Date& end)
244{
245 int beginDays = begin - firstValidDate();
246 int endDays = end - firstValidDate();
247
248 int dateBetween = bslim::FuzzUtil::consumeNumberInRange<int>(
249 fuzzDataView, beginDays, endDays);
250
251 return firstValidDate() + dateBetween;
252}
253
254inline
256{
257 const short maxTz = 24 * 60 - 1, // (-24, 24) hours
258 minTz = -maxTz;
259 return bslim::FuzzUtil::consumeNumberInRange<short>(fuzzDataView,
260 minTz,
261 maxTz);
262}
263
264} // close package namespace
265
266
267#endif
268
269// ----------------------------------------------------------------------------
270// Copyright 2021 Bloomberg Finance L.P.
271//
272// Licensed under the Apache License, Version 2.0 (the "License");
273// you may not use this file except in compliance with the License.
274// You may obtain a copy of the License at
275//
276// http://www.apache.org/licenses/LICENSE-2.0
277//
278// Unless required by applicable law or agreed to in writing, software
279// distributed under the License is distributed on an "AS IS" BASIS,
280// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
281// See the License for the specific language governing permissions and
282// limitations under the License.
283// ----------------------------- END-OF-FILE ----------------------------------
284
285/** @} */
286/** @} */
287/** @} */
Definition bdlt_datetz.h:161
Definition bdlt_date.h:294
Definition bdlt_datetimetz.h:308
Definition bdlt_datetime.h:330
Definition bdlt_timetz.h:190
Definition bdlt_time.h:195
Definition bslim_fuzzdataview.h:130
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bbldc_basicisma30360.h:112
Definition bdlt_fuzzutil.h:127
static DatetimeTz consumeDatetimeTzInRange(bslim::FuzzDataView *fuzzDataView, const bdlt::Date &begin, const bdlt::Date &end)
static bdlt::Date consumeDate(bslim::FuzzDataView *fuzzDataView)
Definition bdlt_fuzzutil.h:235
static Time consumeTime(bslim::FuzzDataView *fuzzDataView)
static Datetime consumeDatetimeInRange(bslim::FuzzDataView *fuzzDataView, const bdlt::Date &begin, const bdlt::Date &end)
static DatetimeTz consumeDatetimeTz(bslim::FuzzDataView *fuzzDataView)
static Datetime consumeDatetime(bslim::FuzzDataView *fuzzDataView)
static bdlt::Date consumeDateInRange(bslim::FuzzDataView *fuzzDataView, const bdlt::Date &begin, const bdlt::Date &end)
Definition bdlt_fuzzutil.h:241
static TimeTz consumeTimeTz(bslim::FuzzDataView *fuzzDataView)
static int consumeTz(bslim::FuzzDataView *fuzzDataView)
Return a timezone offset in minutes.
Definition bdlt_fuzzutil.h:255
static DateTz consumeDateTz(bslim::FuzzDataView *fuzzDataView)
static DateTz consumeDateTzInRange(bslim::FuzzDataView *fuzzDataView, const bdlt::Date &begin, const bdlt::Date &end)