Skip to content

Commit 5fe6dc6

Browse files
Ink Open Sourcecopybara-github
authored andcommitted
Add fuzz test for BrushTipShape constructor
To make sure that it doesn't crash even if the tip modeler sends it large, infinite, or in some cases even NaN tip state parameters. PiperOrigin-RevId: 831487601
1 parent abcb782 commit 5fe6dc6

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

ink/strokes/internal/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ package(
1919
default_visibility = ["//ink:__subpackages__"],
2020
)
2121

22+
cc_library(
23+
name = "fuzz_domains",
24+
testonly = 1,
25+
srcs = ["fuzz_domains.cc"],
26+
hdrs = ["fuzz_domains.h"],
27+
deps = [
28+
":brush_tip_state",
29+
"//ink/geometry:angle",
30+
"//ink/geometry:fuzz_domains",
31+
"@com_google_fuzztest//fuzztest",
32+
],
33+
)
34+
2235
cc_library(
2336
name = "mutable_multi_mesh",
2437
srcs = ["mutable_multi_mesh.cc"],
@@ -365,12 +378,14 @@ cc_test(
365378
":brush_tip_shape",
366379
":brush_tip_state",
367380
":extrusion_points",
381+
":fuzz_domains",
368382
"//ink/geometry:angle",
369383
"//ink/geometry:type_matchers",
370384
"//ink/geometry/internal:circle",
371385
"//ink/geometry/internal:test_matchers",
372386
"@com_google_absl//absl/strings",
373387
"@com_google_absl//absl/types:span",
388+
"@com_google_fuzztest//fuzztest",
374389
"@com_google_googletest//:gtest_main",
375390
],
376391
)

ink/strokes/internal/brush_tip_shape_test.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "gmock/gmock.h"
2020
#include "gtest/gtest.h"
21+
#include "fuzztest/fuzztest.h"
2122
#include "absl/strings/str_cat.h"
2223
#include "absl/types/span.h"
2324
#include "ink/geometry/angle.h"
@@ -26,6 +27,7 @@
2627
#include "ink/geometry/type_matchers.h"
2728
#include "ink/strokes/internal/brush_tip_state.h"
2829
#include "ink/strokes/internal/extrusion_points.h"
30+
#include "ink/strokes/internal/fuzz_domains.h"
2931

3032
namespace ink::strokes_internal {
3133
namespace {
@@ -1477,5 +1479,13 @@ TEST(BrushTipShapeTest, ContainsWithRoundedTriangleEdgeCase) {
14771479
EXPECT_FALSE(rounded_triangle.Contains(circle));
14781480
}
14791481

1482+
void CanConstructFromAnyValidTipState(const BrushTipState& tip_state,
1483+
float min_nonzero_radius_and_separation) {
1484+
BrushTipShape tip_shape(tip_state, min_nonzero_radius_and_separation);
1485+
EXPECT_THAT(tip_shape.Center(), NanSensitivePointEq(tip_state.position));
1486+
}
1487+
FUZZ_TEST(BrushTipShapeTest, CanConstructFromAnyValidTipState)
1488+
.WithDomains(ValidBrushTipState(), fuzztest::NonNegative<float>());
1489+
14801490
} // namespace
14811491
} // namespace ink::strokes_internal
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2025 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+
#include "ink/strokes/internal/fuzz_domains.h"
16+
17+
#include "fuzztest/fuzztest.h"
18+
#include "ink/geometry/angle.h"
19+
#include "ink/geometry/fuzz_domains.h"
20+
#include "ink/strokes/internal/brush_tip_state.h"
21+
22+
namespace ink::strokes_internal {
23+
24+
fuzztest::Domain<BrushTipState> ValidBrushTipState() {
25+
return fuzztest::StructOf<BrushTipState>(
26+
/*position=*/ArbitraryPoint(),
27+
/*width=*/fuzztest::NonNegative<float>(),
28+
/*height=*/fuzztest::NonNegative<float>(),
29+
/*percent_radius=*/fuzztest::InRange<float>(0, 1),
30+
/*rotation=*/NormalizedAngle(),
31+
/*slant=*/AngleInRange(-kHalfTurn, kHalfTurn),
32+
/*pinch=*/fuzztest::InRange<float>(0, 1),
33+
/*texture_animation_progress_offset=*/fuzztest::InRange<float>(0, 1),
34+
/*hue_offset_in_full_turns=*/fuzztest::InRange<float>(0, 1),
35+
/*saturation_multiplier=*/fuzztest::InRange<float>(0, 2),
36+
/*luminosity_shift=*/fuzztest::InRange<float>(-1, 1),
37+
/*opacity_multiplier=*/fuzztest::InRange<float>(0, 2));
38+
}
39+
40+
} // namespace ink::strokes_internal
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2025 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+
#ifndef INK_STROKES_INTERNAL_FUZZ_DOMAINS_H_
16+
#define INK_STROKES_INTERNAL_FUZZ_DOMAINS_H_
17+
18+
#include "fuzztest/fuzztest.h"
19+
#include "ink/strokes/internal/brush_tip_state.h"
20+
21+
namespace ink::strokes_internal {
22+
23+
fuzztest::Domain<BrushTipState> ValidBrushTipState();
24+
25+
} // namespace ink::strokes_internal
26+
27+
#endif // INK_STROKES_INTERNAL_FUZZ_DOMAINS_H_

0 commit comments

Comments
 (0)