Skip to content

Commit 7c36c71

Browse files
committed
Escape regex metacharacters in date patterns, harden rolling tests
- escape regex metacharacters in %d{...} date patterns converted to regex; repeated dots now yield \.{n} - RollingFileAppender: warn instead of failing start() when the file/fileNamePattern collision regex is invalid - test archive removal with "(%i)" in fileNamePattern (#933) - MultiThreadedRollingTest: verify output in Java instead of bash, and set a 10 ms check increment so that size based rollover occurs - remove a redundant 100 ms sleep in TimeBasedRollingWithArchiveRemoval_Test - surefire: JUnit module flags for parameterized tests in IDEs; drop parallel/threadCount, which the JUnit Platform provider ignores Signed-off-by: ceki <[email protected]>
1 parent d268774 commit 7c36c71

8 files changed

Lines changed: 175 additions & 118 deletions

File tree

‎logback-core/pom.xml‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,15 @@
136136
<argLine>
137137
--add-opens ch.qos.logback.core/ch.qos.logback.core.testUtil=java.naming
138138
--add-opens ch.qos.logback.core/ch.qos.logback.core.rolling.helper=org.junit.platform.commons
139-
--add-reads ch.qos.logback.core=ALL-UNNAMED,org.junit.jupiter.api
139+
--add-reads ch.qos.logback.core=ALL-UNNAMED,org.junit.jupiter.api,org.junit.jupiter.params
140+
--add-exports org.junit.platform.commons/org.junit.platform.commons.util=ALL-UNNAMED
140141
</argLine>
141142
</configuration>
142143

143144
<executions>
144145
<execution>
145146
<id>default-test</id>
146147
<configuration>
147-
<parallel>classes</parallel>
148-
<threadCount>20</threadCount>
149148
<!--<useUnlimitedThreads>false</useUnlimitedThreads>-->
150149
<forkCount>5</forkCount>
151150
<reuseForks>true</reuseForks>

‎logback-core/src/main/java/ch/qos/logback/core/rolling/RollingFileAppender.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map.Entry;
2121
import java.util.concurrent.locks.Lock;
2222
import java.util.concurrent.locks.ReentrantLock;
23+
import java.util.regex.PatternSyntaxException;
2324

2425
import ch.qos.logback.core.CoreConstants;
2526
import ch.qos.logback.core.FileAppender;
@@ -113,7 +114,14 @@ private boolean checkForFileAndPatternCollisions() {
113114
// no use checking if either fileName or fileNamePattern are null
114115
if (fileNamePattern != null && fileName != null) {
115116
String regex = fileNamePattern.toRegex();
116-
return fileName.matches(regex);
117+
try {
118+
return fileName.matches(regex);
119+
} catch (PatternSyntaxException e) {
120+
// the check is a safeguard and should not prevent the appender from starting
121+
addWarn("Could not check for collisions between file property [" + fileName
122+
+ "] and fileNamePattern [" + fileNamePattern + "] using regex [" + regex + "]", e);
123+
return false;
124+
}
117125
}
118126
}
119127
return false;

‎logback-core/src/main/java/ch/qos/logback/core/util/CharSequenceToRegexMapper.java‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
*/
2727
class CharSequenceToRegexMapper {
2828

29+
// characters with a special meaning in regular expressions outside of character classes
30+
static final String REGEX_META_CHARACTERS = "\\.[]{}()*+?^$|";
31+
2932
final Locale locale;
3033
final DateFormatSymbols symbols;
3134

@@ -89,8 +92,6 @@ else if (occurrences == 3)
8992
return getRegexForAmPms();
9093
case 'Z':
9194
return "(\\+|-)\\d{4}";
92-
case '.':
93-
return "\\.";
9495
case '\\':
9596
throw new IllegalStateException("Forward slashes are not allowed");
9697
case '\'':
@@ -99,14 +100,27 @@ else if (occurrences == 3)
99100
}
100101
throw new IllegalStateException("Too many single quotes");
101102
default:
103+
String literal = escapeRegexMetaCharacter(c);
102104
if (occurrences == 1) {
103-
return "" + c;
105+
return literal;
104106
} else {
105-
return c + "{" + occurrences + "}";
107+
return literal + "{" + occurrences + "}";
106108
}
107109
}
108110
}
109111

112+
/**
113+
* Escapes a literal character of the date pattern if it has a special meaning in
114+
* regular expressions, e.g. '(' in "yyyy-MM-dd_(HH)".
115+
*/
116+
static String escapeRegexMetaCharacter(char c) {
117+
if (REGEX_META_CHARACTERS.indexOf(c) != -1) {
118+
return "\\" + c;
119+
} else {
120+
return String.valueOf(c);
121+
}
122+
}
123+
110124
private String number(int occurrences) {
111125
return "\\d{" + occurrences + "}";
112126
}

‎logback-core/src/test/java/ch/qos/logback/core/rolling/MultiThreadedRollingTest.java‎

Lines changed: 44 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@
1313
*/
1414
package ch.qos.logback.core.rolling;
1515

16-
import static ch.qos.logback.core.testUtil.CoreTestConstants.FAILURE_EXIT_CODE;
17-
import static ch.qos.logback.core.testUtil.CoreTestConstants.SUCCESSFUL_EXIT_CODE;
18-
1916
import java.io.File;
20-
import java.io.FileOutputStream;
2117
import java.io.FilenameFilter;
2218
import java.io.IOException;
23-
import java.io.OutputStream;
19+
import java.nio.file.Files;
20+
import java.util.ArrayList;
21+
import java.util.Arrays;
22+
import java.util.Comparator;
23+
import java.util.List;
2424

2525
import ch.qos.logback.core.testUtil.CoreTestConstants;
26-
import ch.qos.logback.core.testUtil.EnvUtilForTests;
2726

2827
import org.junit.jupiter.api.AfterEach;
2928
import org.junit.jupiter.api.Assertions;
@@ -38,6 +37,7 @@
3837
import ch.qos.logback.core.encoder.Encoder;
3938
import ch.qos.logback.core.testUtil.RandomUtil;
4039
import ch.qos.logback.core.status.testUtil.StatusChecker;
40+
import ch.qos.logback.core.util.Duration;
4141
import ch.qos.logback.core.util.FileSize;
4242
import ch.qos.logback.core.util.StatusPrinter;
4343

@@ -50,16 +50,11 @@ public class MultiThreadedRollingTest {
5050
Encoder<Object> encoder;
5151
Context context = new ContextBase();
5252

53-
static String VERIFY_SH = "verify.sh";
54-
5553
int diff = RandomUtil.getPositiveInt();
5654
String outputDirStr = CoreTestConstants.OUTPUT_DIR_PREFIX + "multi-" + diff + "/";
5755

5856
RollingFileAppender<Object> rfa = new RollingFileAppender<Object>();
5957

60-
String pathToBash = EnvUtilForTests.getPathToBash();
61-
OutputStream scriptOS;
62-
6358
@BeforeEach
6459
public void setUp() throws Exception {
6560
encoder = new EchoEncoder<Object>();
@@ -70,24 +65,13 @@ public void setUp() throws Exception {
7065
}
7166
System.out.println("Output dir [" + outputDirStr + "]");
7267

73-
scriptOS = openScript();
74-
7568
rfa.setName("rolling");
7669
rfa.setEncoder(encoder);
7770
rfa.setContext(context);
7871
rfa.setFile(outputDirStr + "output.log");
7972

8073
}
8174

82-
void close(OutputStream os) {
83-
if (os != null) {
84-
try {
85-
os.close();
86-
} catch (IOException e) {
87-
}
88-
}
89-
}
90-
9175
@AfterEach
9276
public void tearDown() throws Exception {
9377
rfa.stop();
@@ -109,6 +93,8 @@ public void setUpSizeBasedTriggeringPolicy(RollingFileAppender<Object> rfa) {
10993
SizeBasedTriggeringPolicy<Object> zbtp = new SizeBasedTriggeringPolicy<>();
11094
zbtp.setContext(context);
11195
zbtp.setMaxFileSize(FileSize.valueOf("100KB"));
96+
// the default check increment of 60 seconds would prevent any rollover during the test
97+
zbtp.setCheckIncrement(Duration.buildByMilliseconds(10));
11298

11399
zbtp.start();
114100
rfa.setTriggeringPolicy(zbtp);
@@ -132,16 +118,11 @@ RunnableWithCounterAndDone[] buildRunnableArray(boolean withDelay) {
132118
return runnableArray;
133119
}
134120

135-
OutputStream openScript() throws IOException {
136-
return new FileOutputStream(outputDirStr + VERIFY_SH);
137-
}
138-
139121
@Test
140122
public void multiThreadedTimedBased() throws InterruptedException, IOException {
141123
setUpTimeBasedTriggeringPolicy(rfa);
142124
executeHarness(TOTAL_DURATION, false);
143-
printScriptForTimeBased();
144-
verify();
125+
verify(timeBasedFilesInOrder());
145126
}
146127

147128
int testFileCount() {
@@ -158,21 +139,41 @@ public boolean accept(File dir, String name) {
158139
return files.length;
159140
}
160141

161-
void verify() throws IOException, InterruptedException {
162-
close(scriptOS);
163-
// no point in this test if we don't have bash
164-
if (pathToBash == null) {
165-
return;
166-
}
167-
ProcessBuilder pb = new ProcessBuilder();
168-
pb.command(pathToBash, VERIFY_SH);
169-
pb.directory(new File(outputDirStr));
170-
Process process = pb.start();
171-
process.waitFor();
172-
int exitCode = process.exitValue();
142+
// archives are named after their date, so sorting by name gives the order of writing
143+
List<File> timeBasedFilesInOrder() {
144+
File[] archives = new File(outputDirStr).listFiles((dir, name) -> name.startsWith("test-"));
145+
Arrays.sort(archives, Comparator.comparing(File::getName));
146+
List<File> result = new ArrayList<>(Arrays.asList(archives));
147+
result.add(new File(outputDirStr + "output.log"));
148+
return result;
149+
}
173150

174-
Assertions.assertEquals(SUCCESSFUL_EXIT_CODE, exitCode);
175-
System.out.println("External script based verification returned with exit code " + exitCode);
151+
// the highest index is the oldest archive
152+
List<File> sizeBasedFilesInOrder(int numFiles) {
153+
List<File> result = new ArrayList<>();
154+
for (int i = numFiles - 1; i >= 0; i--) {
155+
result.add(new File(outputDirStr + "test-" + i + ".log"));
156+
}
157+
result.add(new File(outputDirStr + "output.log"));
158+
return result;
159+
}
160+
161+
// each line is "<threadId> <counter>"; the counters of each thread must appear
162+
// in order, without gaps, up to the final value of the thread's counter
163+
void verify(List<File> filesInOrder) throws IOException {
164+
long[] lastCounter = new long[NUM_THREADS];
165+
for (File file : filesInOrder) {
166+
for (String line : Files.readAllLines(file.toPath())) {
167+
int space = line.indexOf(' ');
168+
int id = Integer.parseInt(line.substring(0, space));
169+
long counter = Long.parseLong(line.substring(space + 1));
170+
Assertions.assertEquals(lastCounter[id] + 1, counter, "thread " + id + " in " + file.getName());
171+
lastCounter[id] = counter;
172+
}
173+
}
174+
for (int i = 0; i < NUM_THREADS; i++) {
175+
Assertions.assertEquals(runnableArray[i].getCounter(), lastCounter[i], "final counter of thread " + i);
176+
}
176177
}
177178

178179
@Test
@@ -182,66 +183,7 @@ public void multiThreadedSizeBased() throws InterruptedException, IOException {
182183
// long the MAX_WINDOW_SIZE is reached, resulting in data loss which
183184
// we cannot test for.
184185
executeHarness(TOTAL_DURATION, true);
185-
int numFiles = testFileCount();
186-
printScriptForSizeBased(numFiles);
187-
verify();
188-
}
189-
190-
private void printScriptHeader(String type) throws IOException {
191-
out("# ====================================================");
192-
out("# A script to check the exactness of the output ");
193-
out("# produced by " + type + " test");
194-
out("# ====================================================");
195-
out("# ");
196-
}
197-
198-
private void printCommonScriptCore() throws IOException {
199-
out("");
200-
out("for t in $(seq 0 1 " + (NUM_THREADS - 1) + ")");
201-
out("do");
202-
out(" echo \"Testing results of thread $t\"");
203-
out(" grep \"$t \" aggregated | cut -d ' ' -f 2 > ${t}-sample");
204-
out(" for j in $(seq 1 1 ${end[$t]}); do echo $j; done > ${t}-witness");
205-
out(" diff -q -w ${t}-sample ${t}-witness;");
206-
out(" res=$?");
207-
out(" if [ $res != \"0\" ]; then");
208-
out(" echo \"FAILED for $t\"");
209-
out(" exit " + FAILURE_EXIT_CODE);
210-
out(" fi");
211-
out("done");
212-
out("");
213-
out("exit " + SUCCESSFUL_EXIT_CODE);
214-
}
215-
216-
private void printScriptForTimeBased() throws IOException {
217-
printScriptHeader("TimeBased");
218-
for (int i = 0; i < NUM_THREADS; i++) {
219-
out("end[" + i + "]=" + this.runnableArray[i].getCounter());
220-
}
221-
out("");
222-
out("rm aggregated");
223-
out("cat test* output.log >> aggregated");
224-
printCommonScriptCore();
225-
226-
}
227-
228-
private void printScriptForSizeBased(int numfiles) throws IOException {
229-
printScriptHeader("SizeBased");
230-
231-
for (int i = 0; i < NUM_THREADS; i++) {
232-
out("end[" + i + "]=" + this.runnableArray[i].getCounter());
233-
}
234-
out("");
235-
out("rm aggregated");
236-
out("for i in $(seq " + (numfiles - 1) + " -1 0); do cat test-$i.log >> aggregated; done");
237-
out("cat output.log >> aggregated");
238-
out("");
239-
printCommonScriptCore();
240-
}
241-
242-
private void out(String msg) throws IOException {
243-
scriptOS.write(msg.getBytes());
244-
scriptOS.write("\n".getBytes());
186+
verify(sizeBasedFilesInOrder(testFileCount()));
245187
}
246188

247189
private void executeHarness(int duration, boolean withDelay) throws InterruptedException {

‎logback-core/src/test/java/ch/qos/logback/core/rolling/RollingFileAppenderTest.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import ch.qos.logback.core.Context;
2727
import ch.qos.logback.core.ContextBase;
2828
import ch.qos.logback.core.appender.AbstractAppenderTest;
29+
import ch.qos.logback.core.rolling.helper.FileNamePattern;
2930
import ch.qos.logback.core.testUtil.DummyEncoder;
3031
import ch.qos.logback.core.status.Status;
3132
import ch.qos.logback.core.testUtil.CoreTestConstants;
@@ -164,6 +165,28 @@ public void testFileNameWithUnbalancedParenthesis() {
164165
rfa.doAppend("hello");
165166
}
166167

168+
@Test
169+
public void invalidCollisionRegexDoesNotPreventStart() {
170+
rfa.setContext(context);
171+
rfa.setFile(randomOutputDir + "toto.log");
172+
tbrp.setFileNamePattern(randomOutputDir + "toto-%d.log");
173+
tbrp.start();
174+
// simulate a regex which escaping failed to anticipate
175+
tbrp.fileNamePattern = new FileNamePattern(randomOutputDir + "toto-%d.log", context) {
176+
@Override
177+
public String toRegex() {
178+
return "toto(";
179+
}
180+
};
181+
rfa.setRollingPolicy(tbrp);
182+
rfa.start();
183+
184+
Assertions.assertTrue(rfa.isStarted());
185+
StatusChecker checker = new StatusChecker(context);
186+
checker.assertContainsMatch(Status.WARN, "Could not check for collisions");
187+
rfa.doAppend("hello");
188+
}
189+
167190
@Test
168191
public void stopTimeBasedRollingPolicy() {
169192
rfa.setContext(context);

0 commit comments

Comments
 (0)