1 // Copyright 2022 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 // safe_math_unittest.cc: Unit tests for SafeMath
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h> // Must come first
33 #endif
34
35 #include "safe_math.h"
36 #include "breakpad_googletest_includes.h"
37
38 namespace {
39
40 using google_breakpad::AddIgnoringOverflow;
41 using google_breakpad::AddWithOverflowCheck;
42
TEST(SafeMath,AddOverflowWorksAsIntended)43 TEST(SafeMath, AddOverflowWorksAsIntended) {
44 EXPECT_EQ(AddWithOverflowCheck<uint8_t>(0, 0),
45 std::make_pair<uint8_t>(0, false));
46 EXPECT_EQ(AddWithOverflowCheck<uint8_t>(0, 255),
47 std::make_pair<uint8_t>(255, false));
48 EXPECT_EQ(AddWithOverflowCheck<uint8_t>(1, 255),
49 std::make_pair<uint8_t>(0, true));
50
51 EXPECT_EQ(AddWithOverflowCheck<int8_t>(-128, 127),
52 std::make_pair<int8_t>(-1, false));
53 EXPECT_EQ(AddWithOverflowCheck<int8_t>(127, -128),
54 std::make_pair<int8_t>(-1, false));
55 EXPECT_EQ(AddWithOverflowCheck<int8_t>(1, -128),
56 std::make_pair<int8_t>(-127, false));
57 EXPECT_EQ(AddWithOverflowCheck<int8_t>(127, -1),
58 std::make_pair<int8_t>(126, false));
59
60 EXPECT_EQ(AddWithOverflowCheck<int8_t>(-128, -1),
61 std::make_pair<int8_t>(127, true));
62 EXPECT_EQ(AddWithOverflowCheck<int8_t>(-128, -128),
63 std::make_pair<int8_t>(0, true));
64 EXPECT_EQ(AddWithOverflowCheck<int8_t>(127, 1),
65 std::make_pair<int8_t>(-128, true));
66 EXPECT_EQ(AddWithOverflowCheck<int8_t>(127, 127),
67 std::make_pair<int8_t>(-2, true));
68 }
69
TEST(SafeMath,AddIgnoringOverflowWorksAsIntended)70 TEST(SafeMath, AddIgnoringOverflowWorksAsIntended) {
71 EXPECT_EQ(AddIgnoringOverflow<uint8_t>(0, 0), 0);
72 EXPECT_EQ(AddIgnoringOverflow<uint8_t>(0, 255), 255);
73 EXPECT_EQ(AddIgnoringOverflow<uint8_t>(1, 255), 0);
74 }
75
76 } // namespace
77