public void testInvalidTag() throws Exception { // Any tag number which corresponds to field number zero is invalid and // should throw InvalidProtocolBufferException. for (int i = 0; i < 8; i++) { try { CodedInputStream.newInstance(bytes(i)).readTag(); fail("Should have thrown an exception."); } catch (InvalidProtocolBufferException e) { assertEquals(InvalidProtocolBufferException.invalidTag().getMessage(), e.getMessage()); } } }
/** * Attempt to read a field tag, returning zero if we have reached EOF. Protocol message parsers * use this to read tags, since a protocol message may legally end wherever a tag occurs, and zero * is not a valid tag number. */ public int readTag() throws IOException { if (isAtEnd()) { lastTag = 0; return 0; } lastTag = readRawVarint32(); if (WireFormat.getTagFieldNumber(lastTag) == 0) { // If we actually read zero (or any tag number corresponding to field // number zero), that's not a valid tag. throw InvalidProtocolBufferException.invalidTag(); } return lastTag; }