Browse files

adding EOF negative tests

  • Loading branch information...
1 parent d37fdd2 commit 9954db23e06673a19f00fdb931b3364e22cb648a @ropalka ropalka committed Aug 29, 2012
View
36 impl/src/main/java/com/fossnova/json/stream/JsonReaderImpl.java
@@ -83,7 +83,7 @@ public boolean hasNext() throws IOException {
in.unread( nextChar );
return true;
}
- return false;
+ return !analyzer.isFinished();
}
public JsonEvent next() throws IOException {
@@ -168,7 +168,7 @@ public JsonEvent next() throws IOException {
if ( nextCharacter >= 0 ) {
throw new JsonException( "Unexpected JSON stream character: " + nextCharacter ); // TODO: print unicode
} else {
- throw new JsonException( "Unexpected JSON stream EOF" );
+ throw new JsonException( "Unexpected EOF while reading JSON stream" );
}
}
}
@@ -210,11 +210,11 @@ public boolean isBoolean() {
private void readString() throws IOException {
final StringBuilder retVal = new StringBuilder();
- char previousChar = ( char ) -1;
- char currentChar = ( char ) -1;
+ int previousChar = -1;
+ int currentChar = -1;
in.read();
boolean stringEndFound = false;
- while ( ( currentChar = ( char ) in.read() ) != -1 ) {
+ while ( ( currentChar = in.read() ) != -1 ) {
if ( isStringEnd( previousChar, currentChar ) ) {
stringEndFound = true;
break;
@@ -247,35 +247,44 @@ private void readString() throws IOException {
previousChar = ( char ) -1;
}
if ( !stringEndFound ) {
- throw new JsonException( "Unexpected EOF" );
+ throw new JsonException( "Unexpected EOF while reading JSON string" );
}
lastString = retVal.toString();
}
private void readBoolean() throws IOException {
final int firstChar = in.read();
+ int currentChar = -1;
boolean correctBooleanValue = false;
if ( firstChar == 't' ) {
- if ( ( in.read() == 'r' ) && ( in.read() == 'u' ) && ( in.read() == 'e' ) ) {
+ if ( ( ( currentChar = in.read() ) == 'r' ) && ( ( currentChar = in.read() ) == 'u' ) && ( ( currentChar = in.read() ) == 'e' ) ) {
lastBoolean = true;
correctBooleanValue = true;
}
} else {
- if ( ( in.read() == 'a' ) && ( in.read() == 'l' ) && ( in.read() == 's' ) && ( in.read() == 'e' ) ) {
+ if ( ( ( currentChar = in.read() ) == 'a' ) && ( ( currentChar = in.read() ) == 'l' ) && ( ( currentChar = in.read() ) == 's' )
+ && ( ( currentChar = in.read() ) == 'e' ) ) {
lastBoolean = false;
correctBooleanValue = true;
}
}
if ( !correctBooleanValue ) {
- throw new JsonException( "Expected true or false token" );
+ if ( currentChar == -1 ) {
+ throw new JsonException( "Unexpected EOF while reading JSON " + ( firstChar == 't' ) + " token" );
+ }
+ throw new JsonException( "Unexpected char '" + ( char ) currentChar + "' while readding JSON " + ( firstChar == 't' ) + " token" );
}
}
private void readNull() throws IOException {
- if ( ( in.read() == 'n' ) && ( in.read() == 'u' ) && ( in.read() == 'l' ) && ( in.read() == 'l' ) ) {
+ int currentChar = -1;
+ if ( ( ( currentChar = in.read() ) == 'n' ) && ( ( currentChar = in.read() ) == 'u' ) && ( ( currentChar = in.read() ) == 'l' ) && ( ( currentChar = in.read() ) == 'l' ) ) {
return;
}
- throw new JsonException( "Expected null token" );
+ if ( currentChar == -1 ) {
+ throw new JsonException( "Unexpected EOF while reading JSON null token" );
+ }
+ throw new JsonException( "Unexpected char '" + ( char ) currentChar + "' while readding JSON null token" );
}
private void readNumber() throws IOException {
@@ -288,6 +297,9 @@ private void readNumber() throws IOException {
}
retVal.appendCodePoint( currentChar );
}
+ if ( currentChar == -1 ) {
+ throw new JsonException( "Unexpected EOF while reading JSON number" );
+ }
lastNumber = retVal.toString();
}
@@ -301,7 +313,7 @@ private boolean isNumberCharacter( final int character ) {
return false;
}
- private boolean isStringEnd( final char first, final char second ) {
+ private boolean isStringEnd( final int first, final int second ) {
return ( first != BACKSLASH ) && ( second == JsonConstants.QUOTE );
}
View
54 tests/src/test/java/test/fossnova/json/stream/InvalidJsonReaderTestCase.java
@@ -31,7 +31,6 @@
*/
public final class InvalidJsonReaderTestCase extends AbstractJsonTestCase {
- // TODO: implement test - EOF at string, number, boolean, null, array or object structure
// TODO: implement test - illegal char in string, number, boolean, null or inside array or object structure
@Test
public void emptyState() throws IOException {
@@ -408,6 +407,17 @@ public void notEmptyObjectStartState() throws IOException {
read_objectStart_string_colon_objectStart_objectEnd_comma_string_null();
}
+ @Test
+ public void unexpectedEOF() throws IOException {
+ read_arrayStart_string_EOF();
+ read_arrayStart_number_EOF();
+ read_arrayStart_true_EOF();
+ read_arrayStart_false_EOF();
+ read_arrayStart_null_EOF();
+ read_arrayStart_EOF();
+ read_objectStart_EOF();
+ }
+
private void read_colon() throws IOException {
final JsonReader reader = getJsonReader( ":" );
assertJsonException( reader, "Expecting { [" );
@@ -3164,4 +3174,46 @@ private void read_objectStart_string_colon_objectStart_objectEnd_comma_string_nu
assertStringState( reader, "2" );
assertJsonException( reader, "Expecting :" );
}
+
+ private void read_arrayStart_string_EOF() throws IOException {
+ final JsonReader reader = getJsonReader( "[\"0" );
+ assertArrayStartState( reader );
+ assertJsonException( reader, "Unexpected EOF while reading JSON string" );
+ }
+
+ private void read_arrayStart_number_EOF() throws IOException {
+ final JsonReader reader = getJsonReader( "[0" );
+ assertArrayStartState( reader );
+ assertJsonException( reader, "Unexpected EOF while reading JSON number" );
+ }
+
+ private void read_arrayStart_false_EOF() throws IOException {
+ final JsonReader reader = getJsonReader( "[fals" );
+ assertArrayStartState( reader );
+ assertJsonException( reader, "Unexpected EOF while reading JSON false token" );
+ }
+
+ private void read_arrayStart_true_EOF() throws IOException {
+ final JsonReader reader = getJsonReader( "[tru" );
+ assertArrayStartState( reader );
+ assertJsonException( reader, "Unexpected EOF while reading JSON true token" );
+ }
+
+ private void read_arrayStart_null_EOF() throws IOException {
+ final JsonReader reader = getJsonReader( "[nul" );
+ assertArrayStartState( reader );
+ assertJsonException( reader, "Unexpected EOF while reading JSON null token" );
+ }
+
+ private void read_arrayStart_EOF() throws IOException {
+ final JsonReader reader = getJsonReader( "[" );
+ assertArrayStartState( reader );
+ assertJsonException( reader, "Unexpected EOF while reading JSON stream" );
+ }
+
+ private void read_objectStart_EOF() throws IOException {
+ final JsonReader reader = getJsonReader( "{" );
+ assertObjectStartState( reader );
+ assertJsonException( reader, "Unexpected EOF while reading JSON stream" );
+ }
}

0 comments on commit 9954db2

Please sign in to comment.