From cfb6b625d709c643e8e227b0cce15e650dacacff Mon Sep 17 00:00:00 2001 From: Ivan Frade Date: Tue, 10 Dec 2024 13:12:20 -0800 Subject: PushCertificateIdentTest: Add test for the timezone parsing PushCertificateIdent translates the hhmm in the certificate ident to minutes and then back to hhmm when a timezone is needed. This accepts invalid timezones like +0061 (which becomes +0101 (!)). Add test covering these peculiar timezone translations. This will help when porting the PushCertificateIdent to the new java.time API Change-Id: I008648d4e0c977eb3b3135e548d577680cf5f39c --- .../jgit/transport/PushCertificateIdentTest.java | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit') diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushCertificateIdentTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushCertificateIdentTest.java index cee023d5a9..6290b7978e 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushCertificateIdentTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/PushCertificateIdentTest.java @@ -138,6 +138,51 @@ public class PushCertificateIdentTest { "Me "); } + @Test + public void timezoneRange_hours() { + int HOUR_TO_MS = 60 * 60 * 1000; + + // java.util.TimeZone: Hours must be between 0 to 23 + PushCertificateIdent hourLimit = PushCertificateIdent + .parse("A U. Thor 1218123387 +2300"); + assertEquals(1380, hourLimit.getTimeZoneOffset()); + assertEquals(23 * HOUR_TO_MS, + hourLimit.getTimeZone().getOffset(1218123387)); + + PushCertificateIdent hourDubious = PushCertificateIdent + .parse("A U. Thor 1218123387 +2400"); + assertEquals(1440, hourDubious.getTimeZoneOffset()); + assertEquals(0, hourDubious.getTimeZone().getOffset(1218123387)); + } + + @Test + public void timezoneRange_minutes() { + PushCertificateIdent hourLimit = PushCertificateIdent + .parse("A U. Thor 1218123387 +0059"); + assertEquals(59, hourLimit.getTimeZoneOffset()); + assertEquals(59 * 60 * 1000, + hourLimit.getTimeZone().getOffset(1218123387)); + + // This becomes one hour and one minute (!) + PushCertificateIdent hourDubious = PushCertificateIdent + .parse("A U. Thor 1218123387 +0061"); + assertEquals(61, hourDubious.getTimeZoneOffset()); + assertEquals(61 * 60 * 1000, + hourDubious.getTimeZone().getOffset(1218123387)); + + PushCertificateIdent weirdCase = PushCertificateIdent + .parse("A U. Thor 1218123387 +0099"); + assertEquals(99, weirdCase.getTimeZoneOffset()); + assertEquals(99 * 60 * 1000, + weirdCase.getTimeZone().getOffset(1218123387)); + + PushCertificateIdent weirdCase2 = PushCertificateIdent + .parse("A U. Thor 1218123387 +0199"); + assertEquals(60 + 99, weirdCase2.getTimeZoneOffset()); + assertEquals((60 + 99) * 60 * 1000, + weirdCase2.getTimeZone().getOffset(1218123387)); + } + private static void assertMatchesPersonIdent(String raw, PersonIdent expectedPersonIdent, String expectedUserId) { PushCertificateIdent certIdent = PushCertificateIdent.parse(raw); -- cgit v1.2.3