From: Andrew Hamilton <adhamilt@gmail.com>
To: grub-devel@gnu.org
Cc: daniel.kiper@oracle.com, masayuki.moriyama@miraclelinux.com,
andrea.biardi@viavisolutions.com, phcoder@gmail.com,
Andrew Hamilton <adhamilt@gmail.com>
Subject: [PATCH v4 2/2] date_unit_test: test dates outside of 32-bit unix range
Date: Mon, 25 Aug 2025 22:17:38 -0500 [thread overview]
Message-ID: <20250826031738.86977-3-adhamilt@gmail.com> (raw)
In-Reply-To: <20250826031738.86977-1-adhamilt@gmail.com>
Add tests outside the date range possible with 32-bit time
calculation.
Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
Signed-off-by: Andrew Hamilton <adhamilt@gmail.com>
---
tests/date_unit_test.c | 44 +++++++++++++++++++++++++++++++++---------
1 file changed, 35 insertions(+), 9 deletions(-)
diff --git a/tests/date_unit_test.c b/tests/date_unit_test.c
index 99774f199..aec3dc3e4 100644
--- a/tests/date_unit_test.c
+++ b/tests/date_unit_test.c
@@ -25,12 +25,13 @@
#include <grub/test.h>
static void
-date_test (grub_int32_t v)
+date_test (grub_int64_t v)
{
struct grub_datetime dt;
time_t t = v;
struct tm *g;
int w;
+ grub_int64_t back = 0;
g = gmtime (&t);
@@ -38,28 +39,41 @@ date_test (grub_int32_t v)
w = grub_get_weekday (&dt);
- grub_test_assert (g->tm_sec == dt.second, "time %d bad second: %d vs %d", v,
+ grub_datetime2unixtime (&dt, &back);
+
+ grub_test_assert (g->tm_sec == dt.second, "time %lld bad second: %d vs %d", (long long) v,
g->tm_sec, dt.second);
- grub_test_assert (g->tm_min == dt.minute, "time %d bad minute: %d vs %d", v,
+ grub_test_assert (g->tm_min == dt.minute, "time %lld bad minute: %d vs %d", (long long) v,
g->tm_min, dt.minute);
- grub_test_assert (g->tm_hour == dt.hour, "time %d bad hour: %d vs %d", v,
+ grub_test_assert (g->tm_hour == dt.hour, "time %lld bad hour: %d vs %d", (long long) v,
g->tm_hour, dt.hour);
- grub_test_assert (g->tm_mday == dt.day, "time %d bad day: %d vs %d", v,
+ grub_test_assert (g->tm_mday == dt.day, "time %lld bad day: %d vs %d", (long long) v,
g->tm_mday, dt.day);
- grub_test_assert (g->tm_mon + 1 == dt.month, "time %d bad month: %d vs %d", v,
+ grub_test_assert (g->tm_mon + 1 == dt.month, "time %lld bad month: %d vs %d",(long long) v,
g->tm_mon + 1, dt.month);
grub_test_assert (g->tm_year + 1900 == dt.year,
- "time %d bad year: %d vs %d", v,
+ "time %lld bad year: %d vs %d", (long long) v,
g->tm_year + 1900, dt.year);
- grub_test_assert (g->tm_wday == w, "time %d bad week day: %d vs %d", v,
+ grub_test_assert (g->tm_wday == w, "time %lld bad week day: %d vs %d", (long long) v,
g->tm_wday, w);
+ grub_test_assert (back == v, "time %lld bad back transform: %lld", (long long) v,
+ (long long) back);
}
static void
date_test_iter (void)
{
- grub_int32_t tests[] = { -1, 0, +1, -2133156255, GRUB_INT32_MIN,
+ grub_int32_t tests[] = { -1, 0, +1, 978224552, -2133156255, -2110094321, GRUB_INT32_MIN,
GRUB_INT32_MAX };
+ /*
+ * Test several known UNIX timestamps outside 32-bit time:
+ * 1. 5774965200: 2152-12-31 21:00:00 - Leap year
+ * 2. 4108700725: 2100-03-14 09:45:25 - Not a leap year
+ * 3. -5029179792: 1810-08-19 21:36:48 - Not a leap year
+ */
+ grub_int64_t tests64[] = { (grub_int64_t) 5774965200,
+ (grub_int64_t) 4108700725,
+ (grub_int64_t) -5029179792 };
unsigned i;
for (i = 0; i < ARRAY_SIZE (tests); i++)
@@ -71,6 +85,18 @@ date_test_iter (void)
date_test (x);
date_test (-x);
}
+
+ if (sizeof (time_t) > 4)
+ {
+ for (i = 0; i < ARRAY_SIZE (tests64); i++)
+ date_test (tests64[i]);
+ for (i = 0; i < 10000000; i++)
+ {
+ /* Ranges from 0064 to 6869. */
+ grub_int64_t x = rand () + (rand () % 100 - 28) * (grub_uint64_t) GRUB_INT32_MAX;
+ date_test (x);
+ }
+ }
}
GRUB_UNIT_TEST ("date_unit_test", date_test_iter);
--
2.39.5
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel
next prev parent reply other threads:[~2025-08-26 3:19 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-26 3:17 [PATCH v4 0/2] Support dates outside of 1901..2038 range Andrew Hamilton
2025-08-26 3:17 ` [PATCH v4 1/2] datetime: " Andrew Hamilton
2025-08-26 15:06 ` Daniel Kiper via Grub-devel
2025-08-26 15:28 ` Andrew Hamilton
2025-08-26 16:53 ` Daniel Kiper via Grub-devel
2025-08-26 17:00 ` Andrew Hamilton
2025-08-26 3:17 ` Andrew Hamilton [this message]
2025-08-26 15:25 ` [PATCH v4 2/2] date_unit_test: test dates outside of 32-bit unix range Daniel Kiper via Grub-devel
2025-08-26 15:35 ` Andrew Hamilton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250826031738.86977-3-adhamilt@gmail.com \
--to=adhamilt@gmail.com \
--cc=andrea.biardi@viavisolutions.com \
--cc=daniel.kiper@oracle.com \
--cc=grub-devel@gnu.org \
--cc=masayuki.moriyama@miraclelinux.com \
--cc=phcoder@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).