Skip to content

Fix Log4jFixedFormatter buffer length #1419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,109 +19,96 @@
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.stream.Stream;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Compare FastDateParser with SimpleDateFormat
*
* Copied from Apache Commons Lang 3 on 2016-11-16.
*/
@RunWith(Parameterized.class)
public class FastDateParserSDFTest {

@Parameters(name= "{index}: {0} {1} {2}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object [][]{
static Stream<Arguments> data() {
return Stream.of(
// General Time zone tests
{"z yyyy", "GMT 2010", Locale.UK, true}, // no offset specified, but this is allowed as a TimeZone name
{"z yyyy", "GMT-123 2010", Locale.UK, false},
{"z yyyy", "GMT-1234 2010", Locale.UK, false},
{"z yyyy", "GMT-12:34 2010", Locale.UK, true},
{"z yyyy", "GMT-1:23 2010", Locale.UK, true},
Arguments.of("z yyyy", "GMT 2010", Locale.UK, true), // no offset specified, but this is allowed as a
// TimeZone name
Arguments.of("z yyyy", "GMT-123 2010", Locale.UK, false),
Arguments.of("z yyyy", "GMT-1234 2010", Locale.UK, false),
Arguments.of("z yyyy", "GMT-12:34 2010", Locale.UK, true),
Arguments.of("z yyyy", "GMT-1:23 2010", Locale.UK, true),
// RFC 822 tests
{"z yyyy", "-1234 2010", Locale.UK, true},
{"z yyyy", "-12:34 2010", Locale.UK, false},
{"z yyyy", "-123 2010", Locale.UK, false},
Arguments.of("z yyyy", "-1234 2010", Locale.UK, true),
Arguments.of("z yyyy", "-12:34 2010", Locale.UK, false),
Arguments.of("z yyyy", "-123 2010", Locale.UK, false),
// year tests
{ "MM/dd/yyyy", "01/11/12", Locale.UK, true},
{ "MM/dd/yy", "01/11/12", Locale.UK, true},
Arguments.of("MM/dd/yyyy", "01/11/12", Locale.UK, true),
Arguments.of("MM/dd/yy", "01/11/12", Locale.UK, true),

// LANG-1089
{ "HH", "00", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "00", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "00", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "00", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0

{ "HH", "01", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "01", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "01", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "01", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0

{ "HH", "11", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "11", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "11", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "11", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0

{ "HH", "12", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "12", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "12", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "12", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0

{ "HH", "13", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "13", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "13", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "13", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0

{ "HH", "23", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "23", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "23", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "23", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0

{ "HH", "24", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "24", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "24", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "24", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0

{ "HH", "25", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "25", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "25", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "25", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0

{ "HH", "48", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "48", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "48", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "48", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0
});
Arguments.of("HH", "00", Locale.UK, true), // Hour in day (0-23)
Arguments.of("KK", "00", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of("hh", "00", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of("kk", "00", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0

Arguments.of("HH", "01", Locale.UK, true), // Hour in day (0-23)
Arguments.of("KK", "01", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of("hh", "01", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of("kk", "01", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0

Arguments.of("HH", "11", Locale.UK, true), // Hour in day (0-23)
Arguments.of("KK", "11", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of("hh", "11", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of("kk", "11", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0

Arguments.of("HH", "12", Locale.UK, true), // Hour in day (0-23)
Arguments.of("KK", "12", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of("hh", "12", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of("kk", "12", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0

Arguments.of("HH", "13", Locale.UK, true), // Hour in day (0-23)
Arguments.of("KK", "13", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of("hh", "13", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of("kk", "13", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0

Arguments.of("HH", "23", Locale.UK, true), // Hour in day (0-23)
Arguments.of("KK", "23", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of("hh", "23", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of("kk", "23", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0

Arguments.of("HH", "24", Locale.UK, true), // Hour in day (0-23)
Arguments.of("KK", "24", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of("hh", "24", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of("kk", "24", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0

Arguments.of("HH", "25", Locale.UK, true), // Hour in day (0-23)
Arguments.of("KK", "25", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of("hh", "25", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of("kk", "25", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0

Arguments.of("HH", "48", Locale.UK, true), // Hour in day (0-23)
Arguments.of("KK", "48", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of("hh", "48", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of("kk", "48", Locale.UK, true) // Hour in day (1-24), i.e. midnight is 24, not 0
);
}

private final String format;
private final String input;
private final Locale locale;
private final boolean valid;
private final TimeZone timeZone = TimeZone.getDefault();

public FastDateParserSDFTest(final String format, final String input, final Locale locale, final boolean valid) {
this.format = format;
this.input = input;
this.locale = locale;
this.valid = valid;
}

private void checkParse(final String formattedDate) {
private void checkParse(final String formattedDate, String format, String input, Locale locale, boolean valid) {
final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
sdf.setTimeZone(timeZone);
final DateParser fdf = new FastDateParser(format, timeZone, locale);
Expand Down Expand Up @@ -156,13 +143,14 @@ private void checkParse(final String formattedDate) {
fdfE = e.getClass();
}
if (valid) {
assertEquals(locale.toString()+" "+formattedDate +"\n",expectedTime, actualTime);
assertEquals(expectedTime, actualTime, locale.toString() + " " + formattedDate + "\n");
} else {
assertEquals(locale.toString()+" "+formattedDate + " expected same Exception ", sdfE, fdfE);
assertEquals(sdfE, fdfE, locale.toString() + " " + formattedDate + " expected same Exception ");
}
}

private void checkParsePosition(final String formattedDate) {
private void checkParsePosition(final String formattedDate, String format, String input, Locale locale,
boolean valid) {
final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
sdf.setTimeZone(timeZone);
final DateParser fdf = new FastDateParser(format, timeZone, locale);
Expand All @@ -171,7 +159,7 @@ private void checkParsePosition(final String formattedDate) {
final Date expectedTime = sdf.parse(formattedDate, sdfP);
final int sdferrorIndex = sdfP.getErrorIndex();
if (valid) {
assertEquals("Expected SDF error index -1 ", -1, sdferrorIndex);
assertEquals(-1, sdferrorIndex, "Expected SDF error index -1 ");
final int endIndex = sdfP.getIndex();
final int length = formattedDate.length();
if (endIndex != length) {
Expand All @@ -189,44 +177,51 @@ private void checkParsePosition(final String formattedDate) {
final Date actualTime = fdf.parse(formattedDate, fdfP);
final int fdferrorIndex = fdfP.getErrorIndex();
if (valid) {
assertEquals("Expected FDF error index -1 ", -1, fdferrorIndex);
assertEquals(-1, fdferrorIndex, "Expected FDF error index -1 ");
final int endIndex = fdfP.getIndex();
final int length = formattedDate.length();
assertEquals("Expected FDF to parse full string " + fdfP, length, endIndex);
assertEquals(locale.toString()+" "+formattedDate +"\n", expectedTime, actualTime);
assertEquals(length, endIndex, "Expected FDF to parse full string " + fdfP);
assertEquals(expectedTime, actualTime, locale.toString() + " " + formattedDate + "\n");
} else {
assertNotEquals("Test data error: expected FDF parse to fail, but got " + actualTime, -1, fdferrorIndex);
assertTrue("FDF error index ("+ fdferrorIndex + ") should approxiamate SDF index (" + sdferrorIndex + ")",
sdferrorIndex - fdferrorIndex <= 4);
assertNotEquals(-1, fdferrorIndex, "Test data error: expected FDF parse to fail, but got " + actualTime);
assertTrue(sdferrorIndex - fdferrorIndex <= 4,
"FDF error index (" + fdferrorIndex + ") should approxiamate SDF index (" + sdferrorIndex + ")");
}
}

@Test
public void testLowerCase() throws Exception {
checkParse(input.toLowerCase(locale));
@ParameterizedTest
@MethodSource("data")
public void testLowerCase(String format, String input, Locale locale, boolean valid) throws Exception {
checkParse(input.toLowerCase(locale), format, input, locale, valid);
}

@Test
public void testLowerCasePP() throws Exception {
checkParsePosition(input.toLowerCase(locale));
@ParameterizedTest
@MethodSource("data")
public void testLowerCasePP(String format, String input, Locale locale, boolean valid) throws Exception {
checkParsePosition(input.toLowerCase(locale), format, input, locale, valid);
}

@Test
public void testOriginal() throws Exception {
checkParse(input);
@ParameterizedTest
@MethodSource("data")
public void testOriginal(String format, String input, Locale locale, boolean valid) throws Exception {
checkParse(input, format, input, locale, valid);
}

@Test
public void testOriginalPP() throws Exception {
checkParsePosition(input);
@ParameterizedTest
@MethodSource("data")
public void testOriginalPP(String format, String input, Locale locale, boolean valid) throws Exception {
checkParsePosition(input, format, input, locale, valid);
}

@Test
public void testUpperCase() throws Exception {
checkParse(input.toUpperCase(locale));
@ParameterizedTest
@MethodSource("data")
public void testUpperCase(String format, String input, Locale locale, boolean valid) throws Exception {
checkParse(input.toUpperCase(locale), format, input, locale, valid);
}
@Test
public void testUpperCasePP() throws Exception {
checkParsePosition(input.toUpperCase(locale));

@ParameterizedTest
@MethodSource("data")
public void testUpperCasePP(String format, String input, Locale locale, boolean valid) throws Exception {
checkParsePosition(input.toUpperCase(locale), format, input, locale, valid);
}
}
Loading