Catchup #2

Merged
merged 3 commits into from Apr 10, 2012
Jump to file or symbol
Failed to load files and symbols.
+209 −78
Split
@@ -32,7 +32,10 @@
List<Header> headers;
boolean should_keep_alive;
- boolean upgrade;
+ byte[] upgrade;
+ boolean upgrade() {
+ return null != upgrade;
+ }
int http_major;
int http_minor;
@@ -124,8 +127,13 @@ void execute () {
p.execute(s, buf);
+ if (!p.upgrade) {
+ // call execute again, else parser can't know message is done
+ // if no content length is set.
+ p.execute(s, buf);
+ }
if (!s.success) {
- throw new RuntimeException("Test: "+name+"failed");
+ throw new RuntimeException("Test: "+name+" failed");
}
} // execute
@@ -140,21 +148,32 @@ void execute_permutations() {
*/
p(name);
for (int i = 2; i != raw.length; ++i) {
- // p(i);
+ // p(i);
HTTPParser p = new HTTPParser();
TestSettings s = settings();
ByteBuffer buf = ByteBuffer.wrap(raw);
int olimit = buf.limit();
buf.limit(i);
parse(p,s,buf);
+ if (!p.upgrade) {
+ buf.position(i);
+ buf.limit(olimit);
- buf.position(i);
- buf.limit(olimit);
-
- parse(p,s,buf);
- parse(p,s,buf);
-
+ parse(p,s,buf);
+ if (!p.upgrade) {
+ parse(p,s,buf);
+ } else {
+ if (!upgrade()) {
+ throw new RuntimeException("Test:"+name+"parsed as upgrade, is not");
+ }
+ }
+
+ } else {
+ if (!upgrade()) {
+ throw new RuntimeException("Test:"+name+"parsed as upgrade, is not");
+ }
+ }
if (!s.success) {
p(this);
throw new RuntimeException("Test: "+name+" failed");
@@ -1,23 +1,51 @@
package http_parser.lolevel;
import http_parser.HTTPParserUrl;
+import static http_parser.lolevel.Util.*;
public class ParseUrl {
+ public static void test(int i) {
+ HTTPParserUrl u = new HTTPParserUrl();
+ HTTPParser p = new HTTPParser();
+ Url test = Url.URL_TESTS[i];
+// System.out.println(":: " + test.name);
+ int rv = p.parse_url(Util.buffer(test.url),test.is_connect,u);
+ UnitTest.check_equals(rv, test.rv);
+ if(test.rv == 0){
+ UnitTest.check_equals(u, test.u);
+ }
+
+ }
public static void test() {
- Url test;
- int rv;
+ p(ParseUrl.class);
for (int i = 0; i < Url.URL_TESTS.length; i++) {
-
- HTTPParserUrl u = new HTTPParserUrl();
- HTTPParser p = new HTTPParser();
- test = Url.URL_TESTS[i];
- System.out.println(":: " + test.name);
- rv = p.parse_url(Util.buffer(test.url),test.is_connect,u);
- UnitTest.check_equals(rv, test.rv);
- if(test.rv == 0){
- UnitTest.check_equals(u, test.u);
+ test(i);
+ }
+ }
+
+ static void usage() {
+ p("usage: [jre] http_parser.lolevel.ParseUrl [i]");
+ p(" i : optional test case id");
+ p("---------------------------------------------");
+ p("Test Cases:");
+ for (int i =0; i!= Url.URL_TESTS.length; ++i) {
+ p(" "+i+": "+Url.URL_TESTS[i].name);
+ }
+ }
+
+ public static void main (String [] args) {
+ if (0 == args.length) {
+ test();
+ } else {
+ try {
+ int i = Integer.parseInt(args[0]);
+ test(i);
+ } catch (Throwable t) {
+ t.printStackTrace();
+ usage();
}
+
}
}
}
@@ -35,7 +35,7 @@ static void simple_tests() {
}
public static void test () {
-
+ p(Requests.class);
simple_tests();
List<Message> all = TestLoaderNG.load("tests.dumped");
@@ -13,6 +13,7 @@
public static void test () {
+ p(Responses.class);
List<Message> all = TestLoaderNG.load("tests.dumped");
List<Message> responses = new LinkedList<Message>();
for (Message m : all) {
@@ -39,6 +39,7 @@ static ByteBuffer getBytes (http_parser.ParserType type) {
}
public static void test () {
+ p(TestHeaderOverflowError.class);
test(http_parser.ParserType.HTTP_REQUEST);
test(http_parser.ParserType.HTTP_RESPONSE);
}
@@ -99,7 +99,7 @@ else if (key.startsWith("header")) {
}
else if ("should_keep_alive".equals(key))
{curr.should_keep_alive = (1 == Integer.parseInt(value));}
- else if ("upgrade".equals(key)) {curr.upgrade = (1 == Integer.parseInt(value));}
+ else if ("upgrade".equals(key)) { curr.upgrade = toByteArray(value);}
else if ("http_major".equals(key)) {curr.http_major = Integer.parseInt(value);}
else if ("http_minor".equals(key)) {curr.http_minor = Integer.parseInt(value);}
} else {
@@ -50,6 +50,7 @@ static ByteBuffer getBytes (http_parser.ParserType type, int length) {
}
public static void test () {
+ p(TestNoOverflowLongBody.class);
test(http_parser.ParserType.HTTP_REQUEST, 1000);
test(http_parser.ParserType.HTTP_REQUEST, 100000);
test(http_parser.ParserType.HTTP_RESPONSE, 1000);
@@ -110,6 +110,7 @@ static void check_equals(Object supposed2be, Object is) {
public static void test () {
+ p(UnitTest.class);
testErrorFormat();
testErrorCallback();
}
@@ -13,6 +13,7 @@
"Upgrade: WebSocket\r\n\r\n" +
"third key data";
static void test () {
+ p(Upgrade.class);
HTTPParser parser = new HTTPParser(ParserType.HTTP_REQUEST);
ByteBuffer buf = buffer(upgrade);
@@ -41,6 +41,11 @@ static void check(boolean betterBtrue) {
throw new RuntimeException("!");
}
}
+ static void check (int should, int is) {
+ if (should != is) {
+ throw new RuntimeException("should be: "+should+" is:"+is);
+ }
+ }
static void test_message(Message mes) {
int raw_len = mes.raw.length;
@@ -55,7 +60,7 @@ static void test_message(Message mes) {
int read = 0;
if (msg1len !=0) {
read = parser.execute(settings, msg1);
- if (mes.upgrade && parser.upgrade) {
+ if (mes.upgrade() && parser.upgrade) {
// Messages have a settings() that checks itself...
check(1 == mes.num_called);
continue;
@@ -64,17 +69,17 @@ static void test_message(Message mes) {
}
read = parser.execute(settings, msg2);
- if (mes.upgrade && parser.upgrade) {
+ if (mes.upgrade() && parser.upgrade) {
check(1 == mes.num_called);
continue;
}
- check(read == mes.raw.length - msg1len);
+ check( mes.raw.length - msg1len, read);
ByteBuffer empty = Util.empty();
read = parser.execute(settings, empty);
- if (mes.upgrade && parser.upgrade) {
+ if (mes.upgrade() && parser.upgrade) {
check(1 == mes.num_called);
continue;
}
@@ -87,13 +92,13 @@ static void test_message(Message mes) {
static void test_multiple3(Message r1, Message r2, Message r3) {
int message_count = 1;
- if (!r1.upgrade) {
+ if (!r1.upgrade()) {
message_count++;
- if (!r2.upgrade) {
+ if (!r2.upgrade()) {
message_count++;
}
}
- boolean has_upgrade = (message_count < 3 || r3.upgrade);
+ boolean has_upgrade = (message_count < 3 || r3.upgrade());
ByteList blist = new ByteList();
blist.addAll(r1.raw);
@@ -108,6 +113,7 @@ static void test_multiple3(Message r1, Message r2, Message r3) {
int read = parser.execute(settings, buf);
if (has_upgrade && parser.upgrade) {
+ raw = upgrade_message_fix(raw, read, r1,r2,r3);
check(settings.numCalled == message_count);
return;
}
@@ -124,6 +130,71 @@ static void test_multiple3(Message r1, Message r2, Message r3) {
check(0 == read);
check(settings.numCalled == message_count);
}
+
+ /* Given a sequence of bytes and the number of these that we were able to
+ * parse, verify that upgrade bodies are correct.
+ */
+ static byte [] upgrade_message_fix(byte[] body, int nread, Message... msgs) {
+ int off = 0;
+ for (Message m : msgs) {
+ off += m.raw.length;
+ if (m.upgrade()) {
+ off -= m.upgrade.length;
+ // Original C:
+ // Check the portion of the response after its specified upgrade
+ // if (!check_str_eq(m, "upgrade", body + off, body + nread)) {
+ // abort();
+ // }
+ // to me, this seems to be equivalent to comparing off and nread ...
+ check (off, nread);
+
+ // Original C:
+ // Fix up the response so that message_eq() will verify the beginning
+ // of the upgrade */
+ //
+ // *(body + nread + strlen(m->upgrade)) = '\0';
+ // This only shortens body so the strlen check passes.
+ return new byte[off];
+
+ }
+ }
+ return null;
+ }
+//upgrade_message_fix(char *body, const size_t nread, const size_t nmsgs, ...) {
+// va_list ap;
+// size_t i;
+// size_t off = 0;
+//
+// va_start(ap, nmsgs);
+//
+// for (i = 0; i < nmsgs; i++) {
+// struct message *m = va_arg(ap, struct message *);
+//
+// off += strlen(m->raw);
+//
+// if (m->upgrade) {
+// off -= strlen(m->upgrade);
+//
+// /* Check the portion of the response after its specified upgrade */
+// if (!check_str_eq(m, "upgrade", body + off, body + nread)) {
+// abort();
+// }
+//
+// /* Fix up the response so that message_eq() will verify the beginning
+// * of the upgrade */
+// *(body + nread + strlen(m->upgrade)) = '\0';
+// messages[num_messages -1 ].upgrade = body + nread;
+//
+// va_end(ap);
+// return;
+// }
+// }
+//
+// va_end(ap);
+// printf("\n\n*** Error: expected a message with upgrade ***\n");
+//
+// abort();
+//}
static void p (Object o) {
System.out.println(o);
}
@@ -14,6 +14,7 @@
"hello" +
"hello_again";
static void test () {
+ p(WrongContentLength.class);
HTTPParser parser = new HTTPParser(ParserType.HTTP_REQUEST);
ByteBuffer buf = buffer(contentLength);
@@ -43,7 +44,7 @@ public int cb (HTTPParser p) {
this.on_body = new HTTPDataCallback() {
public int cb (HTTPParser p, ByteBuffer b, int pos, int len) {
bodyCount += len;
- p(str(b, pos, len));
+ check ("hello".equals(str(b, pos, len)));
return 0;
}
};
Oops, something went wrong.