Repository: logging-log4j2 Updated Branches: refs/heads/master 629edaa5a -> debb8ec1c
Test [LOG4J2-1502] CsvParameterLayout is inserting NUL character if data starts with {, (, [ or ". Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/debb8ec1 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/debb8ec1 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/debb8ec1 Branch: refs/heads/master Commit: debb8ec1ca84e3a362c688b796df4aa45d25e1af Parents: 629edaa Author: Gary Gregory <ggreg...@apache.org> Authored: Sun Aug 28 14:31:08 2016 -0700 Committer: Gary Gregory <ggreg...@apache.org> Committed: Sun Aug 28 14:31:08 2016 -0700 ---------------------------------------------------------------------- .../CsvJsonParameterLayoutFileAppenderTest.java | 71 ++++++++++++++++++++ .../test/resources/log4j-cvs-json-parameter.xml | 34 ++++++++++ 2 files changed, 105 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/debb8ec1/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/CsvJsonParameterLayoutFileAppenderTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/CsvJsonParameterLayoutFileAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/CsvJsonParameterLayoutFileAppenderTest.java new file mode 100644 index 0000000..851e692 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/CsvJsonParameterLayoutFileAppenderTest.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package org.apache.logging.log4j.core.appender; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.List; + +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.RuleChain; + +import com.google.common.io.Files; + +public class CsvJsonParameterLayoutFileAppenderTest { + + private static final String FILE_PATH = "target/CsvJsonParameterLayoutFileAppenderTest.log"; + + private static final LoggerContextRule loggerContextRule = new LoggerContextRule("log4j-cvs-json-parameter.xml"); + + @Rule + public RuleChain rule = loggerContextRule.withCleanFilesRule(FILE_PATH); + + @Test + @Ignore("https://issues.apache.org/jira/browse/LOG4J2-1502") + public void testNoNulCharacters() throws IOException { + @SuppressWarnings("resource") + final LoggerContext loggerContext = loggerContextRule.getLoggerContext(); + final Logger logger = loggerContext.getLogger("com.example"); + final String json = "{\"id\":10,\"name\":\"Alice\"}"; + logger.error("log:", json); + loggerContext.stop(); + final File file = new File(FILE_PATH); + final byte[] contents = Files.toByteArray(file); + int count0s = 0; + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < contents.length; i++) { + final byte b = contents[i]; + if (b == 0) { + sb.append(i); + sb.append(", "); + count0s++; + } + } + Assert.assertEquals("File contains at least one 0x00 byte at indices " + sb, 0, count0s); + final List<String> readLines = Files.readLines(file, Charset.defaultCharset()); + final String actual = readLines.get(0); + Assert.assertTrue(actual, actual.contains(json)); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/debb8ec1/log4j-core/src/test/resources/log4j-cvs-json-parameter.xml ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/resources/log4j-cvs-json-parameter.xml b/log4j-core/src/test/resources/log4j-cvs-json-parameter.xml new file mode 100644 index 0000000..f47f69e --- /dev/null +++ b/log4j-core/src/test/resources/log4j-cvs-json-parameter.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<Configuration status="warn" name="CsvJsonParameterLayoutFileAppenderTest"> + <Appenders> + <File name="File" fileName="target/CsvJsonParameterLayoutFileAppenderTest.log" bufferedIO="false"> + <CsvParameterLayout delimiter=","> + </CsvParameterLayout> + </File> + </Appenders> + + <Loggers> + + <Root level="error"> + <AppenderRef ref="File"/> + </Root> + </Loggers> + +</Configuration> \ No newline at end of file