Skip to content

Commit 4a49902

Browse files
authored
Adds a time zone fuzz test (google#326)
Based on https://github.com/google/oss-fuzz/blob/master/projects/cctz/fuzz_cctz.cc but uses the Google FuzzTest framework and runs directly in the CCTZ project using Bazel
1 parent d85cf7b commit 4a49902

File tree

9 files changed

+693
-990
lines changed

9 files changed

+693
-990
lines changed

.github/workflows/ci.yaml

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
- name: Tests
2020
run: >
2121
bazel test ...
22+
--build_tag_filters=-fuzztest
2223
--copt=-DGTEST_REMOVE_LEGACY_TEST_CASEAPI_=1
2324
--copt=-Werror
2425
--cxxopt=-Wall
@@ -43,7 +44,7 @@ jobs:
4344
--show_timestamps
4445
--test_env="TZDIR=${GITHUB_WORKSPACE}/testdata/zoneinfo"
4546
--test_output=errors
46-
--test_tag_filters=-benchmark
47+
--test_tag_filters=-benchmark,-fuzztest
4748
4849
Linux-Clang:
4950
runs-on: ubuntu-latest
@@ -57,6 +58,7 @@ jobs:
5758
run: >
5859
bazel test ...
5960
--action_env=CC=clang
61+
--build_tag_filters=-fuzztest
6062
--copt=-DGTEST_REMOVE_LEGACY_TEST_CASEAPI_=1
6163
--copt=-Werror
6264
--cxxopt=-Wall
@@ -97,7 +99,33 @@ jobs:
9799
--show_timestamps
98100
--test_env="TZDIR=${GITHUB_WORKSPACE}/testdata/zoneinfo"
99101
--test_output=errors
100-
--test_tag_filters=-benchmark
102+
--test_tag_filters=-benchmark,-fuzztest
103+
104+
Linux-FuzzTest:
105+
runs-on: ubuntu-latest
106+
steps:
107+
108+
- uses: actions/checkout@v4
109+
with:
110+
fetch-depth: 0
111+
112+
- name: Configure Fuzztest
113+
run: bazel run @fuzztest//bazel:setup_configs > fuzztest.bazelrc
114+
- name: Fuzz
115+
run: >
116+
bazel --bazelrc=fuzztest.bazelrc test ...
117+
--action_env=CC=clang
118+
--build_tag_filters=fuzztest
119+
--config=fuzztest
120+
--copt=-DGTEST_REMOVE_LEGACY_TEST_CASEAPI_=1
121+
--define=absl=1
122+
--features=external_include_paths
123+
--keep_going
124+
--show_timestamps
125+
--test_arg=--fuzz_for=30s
126+
--test_env="TZDIR=${GITHUB_WORKSPACE}/testdata/zoneinfo"
127+
--test_output=errors
128+
--test_tag_filters=fuzztest
101129
102130
macOS:
103131
runs-on: macos-latest
@@ -110,13 +138,14 @@ jobs:
110138
- name: Tests
111139
run: >
112140
bazel test ...
141+
--build_tag_filters=-fuzztest
113142
--cxxopt="-std=c++17"
114143
--features=external_include_paths
115144
--keep_going
116145
--show_timestamps
117146
--test_env="TZDIR=${GITHUB_WORKSPACE}/testdata/zoneinfo"
118147
--test_output=errors
119-
--test_tag_filters=-benchmark
148+
--test_tag_filters=-benchmark,-fuzztest
120149
121150
Windows:
122151
runs-on: windows-latest
@@ -129,8 +158,9 @@ jobs:
129158
- name: Tests
130159
run: >
131160
bazel test ...
161+
--build_tag_filters="-fuzztest"
132162
--keep_going
133163
--show_timestamps
134164
--test_env="TZDIR=$env:GITHUB_WORKSPACE\testdata\zoneinfo"
135165
--test_output=errors
136-
--test_tag_filters=-benchmark
166+
--test_tag_filters="-benchmark,-fuzztest"

BUILD

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ cc_library(
7575

7676
### tests
7777

78-
test_suite(
79-
name = "all_tests",
80-
visibility = ["//visibility:public"],
78+
cc_library(
79+
name = "test_time_zone_names",
80+
testonly = True,
81+
srcs = ["src/test_time_zone_names.cc"],
82+
hdrs = ["src/test_time_zone_names.h"],
83+
visibility = ["//visibility:private"],
8184
)
8285

8386
cc_test(
@@ -109,12 +112,35 @@ cc_test(
109112
srcs = ["src/time_zone_lookup_test.cc"],
110113
deps = [
111114
":civil_time",
115+
":test_time_zone_names",
112116
":time_zone",
113117
"@googletest//:gtest",
114118
"@googletest//:gtest_main",
115119
],
116120
)
117121

122+
cc_test(
123+
name = "time_zone_fuzz_test",
124+
srcs = [
125+
"src/time_zone_fuzz_test.cc",
126+
"src/time_zone_if.h",
127+
"src/time_zone_impl.h",
128+
"src/time_zone_info.h",
129+
"src/tzfile.h",
130+
],
131+
tags = [
132+
"fuzztest",
133+
],
134+
deps = [
135+
":civil_time",
136+
":test_time_zone_names",
137+
":time_zone",
138+
"@fuzztest//fuzztest",
139+
"@fuzztest//fuzztest:fuzztest_gtest_main",
140+
"@googletest//:gtest",
141+
],
142+
)
143+
118144
### benchmarks
119145

120146
cc_test(
@@ -130,6 +156,7 @@ cc_test(
130156
tags = ["benchmark"],
131157
deps = [
132158
":civil_time",
159+
":test_time_zone_names",
133160
":time_zone",
134161
"@google_benchmark//:benchmark_main",
135162
],

CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ if (BUILD_EXAMPLES)
111111
endif()
112112

113113
if (BUILD_TESTING)
114+
add_library(test_time_zone_names
115+
"src/test_time_zone_names.cc"
116+
"src/test_time_zone_names.h"
117+
)
118+
114119
add_executable(civil_time_test src/civil_time_test.cc)
115120
cctz_target_set_cxx_standard(civil_time_test)
116121
target_include_directories(civil_time_test PRIVATE ${GTEST_INCLUDE_DIRS})
@@ -126,6 +131,7 @@ if (BUILD_TESTING)
126131
target_include_directories(time_zone_lookup_test PRIVATE ${GTEST_INCLUDE_DIRS})
127132
target_link_libraries(time_zone_lookup_test
128133
cctz::cctz
134+
test_time_zone_names
129135
${GTEST_BOTH_LIBRARIES}
130136
${CMAKE_THREAD_LIBS_INIT}
131137
)
@@ -153,7 +159,11 @@ if (BUILD_TESTING)
153159
if (BUILD_BENCHMARK)
154160
add_executable(cctz_benchmark src/cctz_benchmark.cc)
155161
cctz_target_set_cxx_standard(cctz_benchmark)
156-
target_link_libraries(cctz_benchmark cctz::cctz benchmark::benchmark_main)
162+
target_link_libraries(cctz_benchmark
163+
cctz::cctz
164+
test_time_zone_names
165+
benchmark::benchmark_main
166+
)
157167
endif()
158168
endif()
159169

MODULE.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ module(
2222

2323
# Only direct dependencies need to be listed below.
2424

25+
bazel_dep(name = "fuzztest",
26+
version = "20250728.0",
27+
dev_dependency = True)
28+
2529
bazel_dep(name = "google_benchmark",
2630
version = "1.9.4",
2731
dev_dependency = True)

0 commit comments

Comments
 (0)