| 1 | /* Copyright (c) 2015, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include "my_config.h" |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <time.h> |
| 28 | |
| 29 | #include "my_inttypes.h" |
| 30 | #include "my_systime.h" |
| 31 | #include "sql/sql_const.h" |
| 32 | |
| 33 | namespace timespec_unittest { |
| 34 | |
| 35 | class TimespecTest : public ::testing::Test { |
| 36 | protected: |
| 37 | /* |
| 38 | Helper function which checks that none of the fields have overflowed. |
| 39 | */ |
| 40 | void verify_timespec() { |
| 41 | EXPECT_GT(ts.tv_sec, 0); |
| 42 | EXPECT_GE(ts.tv_nsec, 0); |
| 43 | } |
| 44 | |
| 45 | struct timespec ts; |
| 46 | }; |
| 47 | |
| 48 | /* Tests for set_timespec_nsec */ |
| 49 | |
| 50 | TEST_F(TimespecTest, TestNsecZero) { |
| 51 | ulonglong nsec = 0; |
| 52 | set_timespec_nsec(&ts, nsec); |
| 53 | verify_timespec(); |
| 54 | } |
| 55 | |
| 56 | TEST_F(TimespecTest, TestNsecMax) { |
| 57 | set_timespec_nsec(&ts, TIMEOUT_INF); |
| 58 | verify_timespec(); |
| 59 | } |
| 60 | |
| 61 | /* Tests for set_timespec (taking a seconds argument) */ |
| 62 | |
| 63 | TEST_F(TimespecTest, TestSecZero) { |
| 64 | ulonglong sec = 0; |
| 65 | set_timespec(&ts, sec); |
| 66 | verify_timespec(); |
| 67 | } |
| 68 | |
| 69 | TEST_F(TimespecTest, TestSec_LONG_TIMEOUT) { |
| 70 | ulonglong sec = LONG_TIMEOUT; |
| 71 | set_timespec(&ts, sec); |
| 72 | verify_timespec(); |
| 73 | } |
| 74 | |
| 75 | TEST_F(TimespecTest, TestSec_INT_MAX32) { |
| 76 | ulonglong sec = INT_MAX32; |
| 77 | set_timespec(&ts, sec); |
| 78 | verify_timespec(); |
| 79 | } |
| 80 | |
| 81 | TEST_F(TimespecTest, TestSec_UINT_MAX32) { |
| 82 | ulonglong sec = UINT_MAX32; |
| 83 | set_timespec(&ts, sec); |
| 84 | verify_timespec(); |
| 85 | } |
| 86 | } // namespace timespec_unittest |