Skip to content

Commit

Permalink
[JENKINS-29804] Add clearer error message when invalid memory length …
Browse files Browse the repository at this point in the history
…is parsed.
  • Loading branch information
MadsNielsen committed Aug 10, 2015
1 parent abb0de1 commit 1647bd4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
@@ -1,5 +1,6 @@
package net.praqma.jenkins.memorymap.parser.gcc;

import hudson.AbortException;
import hudson.Extension;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -52,18 +53,26 @@ public static CharSequence stripComments(CharSequence seq) {
}

/**
* Parses the MEMORY section of the GCC file. Throws an abort exception which will be shown in the Jenkins console log.
*
* @param seq The content of the map file
* @return a list of the defined MEMORY in the map file
*/
public MemoryMapConfigMemory getMemory(CharSequence seq) {
* @throws hudson.AbortException
**/
public MemoryMapConfigMemory getMemory(CharSequence seq) throws AbortException {

Pattern allMemory = Pattern.compile(".*?^(\\s+\\S+).*?[ORIGIN|org|o].*?=([^,]*).*?[LENGTH|len|l]\\s\\=\\s*([^\\s]*).*$", Pattern.MULTILINE);
Matcher match = allMemory.matcher(seq);
MemoryMapConfigMemory memory = new MemoryMapConfigMemory();
while (match.find()) {
String hexLength = new HexUtils.HexifiableString(match.group(3)).toValidHexString().rawString;
MemoryMapConfigMemoryItem item = new MemoryMapConfigMemoryItem(match.group(1), match.group(2), hexLength);
memory.add(item);
try {
String hexLength = new HexUtils.HexifiableString(match.group(3)).toValidHexString().rawString;
MemoryMapConfigMemoryItem item = new MemoryMapConfigMemoryItem(match.group(1), match.group(2), hexLength);
memory.add(item);
} catch (Throwable ex) {
logger.log(Level.SEVERE, "Unable to convert %s to a valid hex string.", ex);
throw new AbortException( String.format( "Unable to convert %s to a valid hex string.", match.group(3) ) );
}
}
return memory;
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/net/praqma/jenkins/unit/HexUtilsTest.java
Expand Up @@ -30,12 +30,17 @@
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

/**
*
* @author jes
*/
public class HexUtilsTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

public HexUtilsTest() {
}
Expand Down Expand Up @@ -143,4 +148,5 @@ public void testValidHexStrings() {
HexUtils.HexifiableString metricToHex = new HexUtils.HexifiableString("2m");
HexUtils.HexifiableString hexified = metricToHex.toValidHexString();
}

}
33 changes: 33 additions & 0 deletions src/test/java/net/praqma/jenkins/unit/MemoryMapGccParserTest.java
Expand Up @@ -22,6 +22,7 @@
* THE SOFTWARE.
*/
package net.praqma.jenkins.unit;
import hudson.AbortException;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
Expand All @@ -30,12 +31,17 @@
import net.praqma.jenkins.memorymap.parser.gcc.GccMemoryMapParser;
import net.praqma.jenkins.memorymap.result.MemoryMapConfigMemory;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
*
* @author mads
*/
public class MemoryMapGccParserTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testParsingOfMemorySegmentInLinkerCommandFile() throws IOException {
Expand Down Expand Up @@ -115,4 +121,31 @@ public void testStrippingLotsOfCrazyComments() {
String result = GccMemoryMapParser.stripComments(testDataWithLotsOfCrazyComments).toString();
assertEquals("Testing successful stripping of lots of block comments", expectedResult, result);
}



@Test
public void testAssertCorrectExceptionThrown() throws Exception {
String illegaAmountOfMemory =
"MEMORY/*" +
"" +
"" +
"*/\n" +
" {\n" +
" /* start comment */\n" +
" /* start line comment */ reserved1 (!A) : ORIGIN = 0, LENGTH = 0xÅP\n" +
" application (rx) : ORIGIN = 0x100000, LENGTH = 2M\n" +
" reserved2 (!A) : ORI/**/GIN = 0x03/* ***** */00000, LENGTH = 0x04ÅN /* COMMENT */\n" +
" ram (w) : ORIGIN = 0x0800000, LENGTH = 2M /* 0x04FFFFF */\n" +
" /* more " +
"comment */\n" +
" }";


thrown.expect(AbortException.class);
thrown.expectMessage("Unable to convert 0xÅP to a valid hex string.");

GccMemoryMapParser parser = new GccMemoryMapParser();
parser.getMemory(illegaAmountOfMemory);
}
}

0 comments on commit 1647bd4

Please sign in to comment.