Skip to content

Commit

Permalink
jenkinsci#185 Calculate hash based on line content. Test
Browse files Browse the repository at this point in the history
  • Loading branch information
tszmytka committed Jul 27, 2020
1 parent 1f25f07 commit 28c81f9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import static java.nio.charset.StandardCharsets.UTF_8;

public class LineIdentifier implements Serializable {
private static final String ALGORITHM = "SHA-256";
private MessageDigest messageDigest;
private static final long serialVersionUID = 1;
private transient MessageDigest messageDigest;

private MessageDigest getMessageDigest() {
if (messageDigest == null) {
Expand All @@ -22,7 +25,7 @@ private MessageDigest getMessageDigest() {

public String hash(String lineContent, long lineNo) {
final String key = String.join("|", lineContent, String.valueOf(lineNo));
return Base64.getEncoder().encodeToString(getMessageDigest().digest(key.getBytes()));
return Base64.getEncoder().encodeToString(getMessageDigest().digest(key.getBytes(UTF_8)));
}

public boolean isEqual(String lineContent, long lineNo, String other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
Expand All @@ -17,11 +18,13 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static java.nio.charset.StandardCharsets.UTF_8;

public class ShortlogActionCreator {
private static final Logger LOGGER = Logger.getLogger(ShortlogActionCreator.class.getName());
private static final int CONSOLE_TAIL_DEFAULT = 150;
private static final int BUFFER_SIZE = 16 * 1024;
private static final byte[] EOL = System.lineSeparator().getBytes();
private static final byte[] EOL = System.lineSeparator().getBytes(UTF_8);

private final LineIdentifier lineIdentifier;

Expand All @@ -44,7 +47,7 @@ private ActionContext findStartActionAt(File logFile, Collection<String> seriali
int read;
int totalRead = 0;
String currentStartAction = "";
byte[] partialLine = new byte[]{};
String partialLine = "";
while ((read = inputStream.read(buf)) != -1) {
final String newAction = findActionInBuffer(serializedActions, buf);
if (!newAction.isEmpty()) {
Expand All @@ -54,10 +57,10 @@ private ActionContext findStartActionAt(File logFile, Collection<String> seriali
final int startInBuff = shortlogStart > totalRead ? (int) (shortlogStart - totalRead) : 0;
final int eolPos = indexOfEol(buf, startInBuff);
if (eolPos != -1) {
return new ActionContext(currentStartAction, new String(partialLine) + new String(buf, startInBuff, eolPos - startInBuff + EOL.length));
return new ActionContext(currentStartAction, partialLine + new String(buf, startInBuff, eolPos - startInBuff + EOL.length, UTF_8));
} else {
// line extends to the next buffer
partialLine = Arrays.copyOfRange(buf, startInBuff, buf.length - 1);
partialLine = new String(Arrays.copyOfRange(buf, startInBuff, buf.length - 1), UTF_8);
}
}
totalRead += read;
Expand All @@ -72,7 +75,7 @@ private String findActionInBuffer(Collection<String> serializedActions, byte[] b
int preamblePos = 0;
while (preamblePos < buf.length && (preamblePos = ConsoleNote.findPreamble(buf, preamblePos, buf.length - preamblePos)) != -1) {
final int begin = preamblePos;
final Optional<String> startAction = serializedActions.stream().filter(sa -> buf.length - begin > sa.length() && sa.equals(new String(buf, begin, sa.length()))).findFirst();
final Optional<String> startAction = serializedActions.stream().filter(sa -> buf.length - begin > sa.length() && sa.equals(new String(buf, begin, sa.length(), UTF_8))).findFirst();
if (startAction.isPresent()) {
return startAction.get();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package hudson.plugins.ansicolor.action;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class LineIdentifierTest {

private LineIdentifier lineIdentifier;

@Before
public void setUp() throws Exception {
lineIdentifier = new LineIdentifier();
}

@Test
public void canHashLine() {
assertEquals("ojq32twB56Mha38FSpsOvwxZDdkOKa/SveGHDC4tgHY=", lineIdentifier.hash("test line 123", 735));
}

@Test
public void canDetermineIsEqualPositive() {
assertTrue(lineIdentifier.isEqual("\u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m", 67, "1mo5/lFK3s3+qaz1vK3o62k+EAJkd8Q0j3dMRH8Wkh4="));
}

@Test
public void canDetermineIsEqualNegative() {
assertFalse(lineIdentifier.isEqual("\u001B[92m\u001B[1mlightgreen bold \u001B[92m\u001B[22mlightgreen normal\u001B[0m", 67, "bogus"));
}
}

0 comments on commit 28c81f9

Please sign in to comment.