Repository: logging-log4j2 Updated Branches: refs/heads/master 079b1c2ee -> 467a121c2
[LOG4J2-1260] TlsSyslogFrame calculates message length incorrectly. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/467a121c Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/467a121c Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/467a121c Branch: refs/heads/master Commit: 467a121c296d32827c875f9f41cdcbb7c003ad49 Parents: 079b1c2 Author: ggregory <[email protected]> Authored: Thu Jan 21 10:47:33 2016 -0800 Committer: ggregory <[email protected]> Committed: Thu Jan 21 10:47:33 2016 -0800 ---------------------------------------------------------------------- .../log4j/core/appender/TlsSyslogFrame.java | 62 +++++++++++--------- .../log4j/core/appender/TlsSyslogFrameTest.java | 53 +++++------------ src/changes/changes.xml | 3 + 3 files changed, 53 insertions(+), 65 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/467a121c/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/TlsSyslogFrame.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/TlsSyslogFrame.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/TlsSyslogFrame.java index a48876f..1b1a61a 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/TlsSyslogFrame.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/TlsSyslogFrame.java @@ -16,56 +16,62 @@ */ package org.apache.logging.log4j.core.appender; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.apache.logging.log4j.util.Chars; /** - * Wrapper for messages that are formatted according to RFC 5425. + * Wraps messages that are formatted according to RFC 5425. * * @see <a href="https://tools.ietf.org/html/rfc5425">RFC 5425</a> */ public class TlsSyslogFrame { - private String message; - private int messageLengthInBytes; + private final String message; + private final int byteLength; public TlsSyslogFrame(final String message) { - setMessage(message); + this.message = message; + final byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8); + byteLength = messageBytes.length; } public String getMessage() { return this.message; } - public void setMessage(final String message) { - this.message = message; - setLengthInBytes(); - } - - private void setLengthInBytes() { - messageLengthInBytes = message.length(); - } - - public byte[] getBytes() { - final String frame = toString(); - return frame.getBytes(Charset.defaultCharset()); - } - @Override public String toString() { - final String length = Integer.toString(messageLengthInBytes); - return length + Chars.SPACE + message; + return Integer.toString(byteLength) + Chars.SPACE + message; } - public boolean equals(final TlsSyslogFrame frame) { - return isLengthEquals(frame) && isMessageEquals(frame); + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((message == null) ? 0 : message.hashCode()); + return result; } - private boolean isLengthEquals(final TlsSyslogFrame frame) { - return this.messageLengthInBytes == frame.messageLengthInBytes; + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof TlsSyslogFrame)) { + return false; + } + final TlsSyslogFrame other = (TlsSyslogFrame) obj; + if (message == null) { + if (other.message != null) { + return false; + } + } else if (!message.equals(other.message)) { + return false; + } + return true; } - private boolean isMessageEquals(final TlsSyslogFrame frame) { - return this.message.equals(frame.message); - } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/467a121c/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/TlsSyslogFrameTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/TlsSyslogFrameTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/TlsSyslogFrameTest.java index 1c44f6d..887ba99 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/TlsSyslogFrameTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/TlsSyslogFrameTest.java @@ -16,57 +16,36 @@ */ package org.apache.logging.log4j.core.appender; -import java.util.Arrays; +import java.nio.charset.StandardCharsets; import org.apache.logging.log4j.util.Chars; import org.junit.Assert; import org.junit.Test; public class TlsSyslogFrameTest { - private static final String TESTMESSAGE = "The quick brown fox jumps over the lazy dog"; - - @Test - public void messageSetByConstructor() { - final TlsSyslogFrame frame = new TlsSyslogFrame(TESTMESSAGE); - final byte[] representation = frame.getBytes(); - final byte[] expected = getByteRepresentation(TESTMESSAGE); - Assert.assertTrue(Arrays.equals(representation, expected)); - } - - @Test - public void messageSetBySetter() { - final TlsSyslogFrame frame = new TlsSyslogFrame("Some text"); - frame.setMessage(TESTMESSAGE); - final byte[] representation = frame.getBytes(); - final byte[] expected = getByteRepresentation(TESTMESSAGE); - Assert.assertTrue(Arrays.equals(representation, expected)); - } - - @Test - public void checkGetBytes() { - final TlsSyslogFrame frame = new TlsSyslogFrame(TESTMESSAGE); - final byte[] representation = frame.getBytes(); - final byte[] expected = getByteRepresentation(TESTMESSAGE); - Assert.assertTrue(Arrays.equals(representation, expected)); - } - - private byte[] getByteRepresentation(final String message) { - final String frame = message.length() + Character.toString(Chars.SPACE) + message; - final byte[] representation = frame.getBytes(); - return representation; - } + private static final String TEST_MESSAGE = "The quick brown fox jumps over the lazy dog"; @Test public void equals() { - final TlsSyslogFrame first = new TlsSyslogFrame("A message"); - final TlsSyslogFrame second = new TlsSyslogFrame("A message"); - Assert.assertTrue(first.equals(second)); + final TlsSyslogFrame first = new TlsSyslogFrame(TEST_MESSAGE); + final TlsSyslogFrame second = new TlsSyslogFrame(TEST_MESSAGE); + Assert.assertEquals(first, second); + Assert.assertEquals(first.hashCode(), second.hashCode()); } @Test public void notEquals() { final TlsSyslogFrame first = new TlsSyslogFrame("A message"); final TlsSyslogFrame second = new TlsSyslogFrame("B message"); - Assert.assertFalse(first.equals(second)); + Assert.assertNotEquals(first, second); + Assert.assertNotEquals(first.hashCode(), second.hashCode()); + } + + @Test + public void testToString() { + final TlsSyslogFrame frame = new TlsSyslogFrame(TEST_MESSAGE); + final int length = TEST_MESSAGE.getBytes(StandardCharsets.UTF_8).length; + final String expected = Integer.toString(length) + Chars.SPACE + TEST_MESSAGE; + Assert.assertEquals(expected, frame.toString()); } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/467a121c/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 7650945..645bed9 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -44,6 +44,9 @@ <action issue="LOG4J2-1192" dev="ggregory" type="add" due-to="Jörg Bretschneider, Gary Gregory"> Dynamic Subject for SMTP Appender. </action> + <action issue="LOG4J2-1260" dev="ggregory" type="fix" due-to="Blake Day, Gary Gregory"> + TlsSyslogFrame calculates message length incorrectly. + </action> <action issue="LOG4J2-1258" dev="ggregory" type="fix" due-to="Francis Lalonde"> Async DynamicThresholdFilter does not use the log event's context map. </action>
