tests and fixes for binary keys and content

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2018-03-14 15:02:56 +11:00
parent 4dce09c067
commit a77a127da0
3 changed files with 76 additions and 6 deletions

View File

@ -23,7 +23,9 @@ import static org.junit.Assert.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.eclipse.jetty.http.MultiPartParser.State;
import org.eclipse.jetty.util.BufferUtil;
@ -291,8 +293,6 @@ public class MultiPartParserTest
assertThat(data.remaining(),is(0));
assertThat(handler.fields,Matchers.contains("name: value", "<<COMPLETE>>"));
assertThat(handler.content,Matchers.contains("Hello","<<LAST>>"));
}
@ -346,7 +346,40 @@ public class MultiPartParserTest
+ "How now brown cow.\n"
+ "The quick brown fox jumped over the lazy dog.\n","<<LAST>>"));
}
@Test
public void testBinaryPart()
{
byte[] random = new byte[8192];
final ByteBuffer bytes = BufferUtil.allocate(random.length);
ThreadLocalRandom.current().nextBytes(random);
// Arrays.fill(random,(byte)'X');
TestHandler handler = new TestHandler()
{
@Override
public boolean content(ByteBuffer buffer, boolean last)
{
BufferUtil.append(bytes,buffer);
return last;
}
};
MultiPartParser parser = new MultiPartParser(handler,"BOUNDARY");
String preamble = "Blah blah blah\r\n--BOUNDARY\r\n\r\n";
String epilogue = "\r\n--BOUNDARY\r\nBlah blah blah!\r\n";
ByteBuffer data = BufferUtil.allocate(preamble.length()+random.length+epilogue.length());
BufferUtil.append(data,BufferUtil.toBuffer(preamble));
BufferUtil.append(data,ByteBuffer.wrap(random));
BufferUtil.append(data,BufferUtil.toBuffer(epilogue));
parser.parse(data,true);
assertThat(parser.getState(), is(State.DELIMITER));
assertThat(data.remaining(),is(19));
assertThat(bytes.array(),is(random));
}
@Test
public void testEpilogue() {

View File

@ -78,7 +78,7 @@ public class SearchPattern
for(int i = 0; i<table.length; ++i)
table[i] = pattern.length;
for(int i = 0; i<pattern.length-1; ++i)
table[pattern[i]] = pattern.length-1-i;
table[0xff&pattern[i]] = pattern.length-1-i;
}
@ -100,7 +100,7 @@ public class SearchPattern
for(int i = pattern.length-1; data[skip+i] == pattern[i]; i--)
if(i==0) return skip;
skip += table[data[skip + pattern.length - 1]];
skip += table[0xff&data[skip + pattern.length - 1]];
}
return -1;
@ -125,7 +125,7 @@ public class SearchPattern
if(i==0) return(offset+length - skip);
if(skip + pattern.length - 1 < data.length)
skip += table[data[skip + pattern.length - 1]];
skip += table[0xff&data[skip + pattern.length - 1]];
else
skip++;
}
@ -183,5 +183,4 @@ public class SearchPattern
{
return pattern.length;
}
}

View File

@ -19,7 +19,10 @@
package org.eclipse.jetty.util;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.Assert;
import org.junit.Test;
@ -75,7 +78,42 @@ public class SearchPatternTest
Assert.assertEquals(28,sp.match(d, 28, d.length-28));
Assert.assertEquals(-1,sp.match(d, 29, d.length-29));
}
@Test
public void testSearchInBinary()
{
byte[] random = new byte[8192];
ThreadLocalRandom.current().nextBytes(random);
// Arrays.fill(random,(byte)-67);
String preamble = "Blah blah blah";
String epilogue = "The End! Blah Blah Blah";
ByteBuffer data = BufferUtil.allocate(preamble.length()+random.length+epilogue.length());
BufferUtil.append(data,BufferUtil.toBuffer(preamble));
BufferUtil.append(data,ByteBuffer.wrap(random));
BufferUtil.append(data,BufferUtil.toBuffer(epilogue));
SearchPattern sp = SearchPattern.compile("The End!");
Assert.assertEquals(preamble.length()+random.length,sp.match(data.array(),data.arrayOffset()+data.position(),data.remaining()));
}
@Test
public void testSearchBinaryKey()
{
byte[] random = new byte[8192];
ThreadLocalRandom.current().nextBytes(random);
byte[] key = new byte[64];
ThreadLocalRandom.current().nextBytes(key);
ByteBuffer data = BufferUtil.allocate(random.length+key.length);
BufferUtil.append(data,ByteBuffer.wrap(random));
BufferUtil.append(data,ByteBuffer.wrap(key));
SearchPattern sp = SearchPattern.compile(key);
Assert.assertEquals(random.length,sp.match(data.array(),data.arrayOffset()+data.position(),data.remaining()));
}
@Test
public void testAlmostMatch()