| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.test.protoinputstream; |
| 18 |
|
| 19 |
import android.util.proto.ProtoInputStream; |
| 20 |
import android.util.proto.ProtoParseException; |
| 21 |
import android.util.proto.ProtoStream; |
| 22 |
|
| 23 |
import junit.framework.TestCase; |
| 24 |
|
| 25 |
import java.io.ByteArrayInputStream; |
| 26 |
import java.io.IOException; |
| 27 |
import java.io.InputStream; |
| 28 |
|
| 29 |
public class ProtoInputStreamIncompleteValueTest extends TestCase { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public void testIncompleteVarint() throws IOException { |
| 35 |
final long fieldFlags = ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_INT32; |
| 36 |
|
| 37 |
final long fieldId1 = fieldFlags | ((long) 1 & 0x0ffffffffL); |
| 38 |
|
| 39 |
final byte[] protobuf = new byte[]{ |
| 40 |
|
| 41 |
(byte) 0x08, |
| 42 |
(byte) 0xff, |
| 43 |
}; |
| 44 |
|
| 45 |
InputStream stream = new ByteArrayInputStream(protobuf); |
| 46 |
final ProtoInputStream pi = new ProtoInputStream(stream); |
| 47 |
|
| 48 |
while (pi.nextField() != ProtoInputStream.NO_MORE_FIELDS) { |
| 49 |
try { |
| 50 |
switch (pi.getFieldNumber()) { |
| 51 |
case (int) fieldId1: |
| 52 |
pi.readInt(fieldId1); |
| 53 |
fail("Should have thrown a ProtoParseException"); |
| 54 |
break; |
| 55 |
default: |
| 56 |
fail("Unexpected field id " + pi.getFieldNumber()); |
| 57 |
} |
| 58 |
} catch (ProtoParseException ppe) { |
| 59 |
|
| 60 |
stream.close(); |
| 61 |
return; |
| 62 |
} |
| 63 |
} |
| 64 |
stream.close(); |
| 65 |
fail("Test should not have reached this point..."); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public void testIncompleteFixed64() throws IOException { |
| 72 |
final long fieldFlags = ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_FIXED64; |
| 73 |
|
| 74 |
final long fieldId2 = fieldFlags | ((long) 2 & 0x0ffffffffL); |
| 75 |
|
| 76 |
final byte[] protobuf = new byte[]{ |
| 77 |
|
| 78 |
(byte) 0x11, |
| 79 |
(byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 80 |
(byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 81 |
}; |
| 82 |
|
| 83 |
InputStream stream = new ByteArrayInputStream(protobuf); |
| 84 |
final ProtoInputStream pi = new ProtoInputStream(stream); |
| 85 |
|
| 86 |
while (pi.nextField() != ProtoInputStream.NO_MORE_FIELDS) { |
| 87 |
try { |
| 88 |
switch (pi.getFieldNumber()) { |
| 89 |
case (int) fieldId2: |
| 90 |
pi.readLong(fieldId2); |
| 91 |
fail("Should have thrown a ProtoParseException"); |
| 92 |
break; |
| 93 |
default: |
| 94 |
fail("Unexpected field id " + pi.getFieldNumber()); |
| 95 |
} |
| 96 |
} catch (ProtoParseException ppe) { |
| 97 |
|
| 98 |
stream.close(); |
| 99 |
return; |
| 100 |
} |
| 101 |
} |
| 102 |
stream.close(); |
| 103 |
fail("Test should not have reached this point..."); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public void testIncompleteLengthDelimited() throws IOException { |
| 110 |
final long fieldFlags = ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_BYTES; |
| 111 |
|
| 112 |
final long fieldId5 = fieldFlags | ((long) 5 & 0x0ffffffffL); |
| 113 |
|
| 114 |
final byte[] protobuf = new byte[]{ |
| 115 |
|
| 116 |
(byte) 0x2a, |
| 117 |
(byte) 0x05, |
| 118 |
(byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, |
| 119 |
}; |
| 120 |
|
| 121 |
InputStream stream = new ByteArrayInputStream(protobuf); |
| 122 |
final ProtoInputStream pi = new ProtoInputStream(stream); |
| 123 |
|
| 124 |
while (pi.nextField() != ProtoInputStream.NO_MORE_FIELDS) { |
| 125 |
try { |
| 126 |
switch (pi.getFieldNumber()) { |
| 127 |
case (int) fieldId5: |
| 128 |
pi.readBytes(fieldId5); |
| 129 |
fail("Should have thrown a ProtoParseException"); |
| 130 |
break; |
| 131 |
default: |
| 132 |
fail("Unexpected field id " + pi.getFieldNumber()); |
| 133 |
} |
| 134 |
} catch (ProtoParseException ppe) { |
| 135 |
|
| 136 |
stream.close(); |
| 137 |
return; |
| 138 |
} |
| 139 |
} |
| 140 |
stream.close(); |
| 141 |
fail("Test should not have reached this point..."); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public void testIncompleteFixed32() throws IOException { |
| 148 |
final long fieldFlags = ProtoStream.FIELD_COUNT_SINGLE | ProtoStream.FIELD_TYPE_FIXED32; |
| 149 |
|
| 150 |
final long fieldId2 = fieldFlags | ((long) 2 & 0x0ffffffffL); |
| 151 |
|
| 152 |
final byte[] protobuf = new byte[]{ |
| 153 |
|
| 154 |
(byte) 0x15, |
| 155 |
(byte) 0x01, (byte) 0x00, (byte) 0x00, |
| 156 |
}; |
| 157 |
|
| 158 |
InputStream stream = new ByteArrayInputStream(protobuf); |
| 159 |
final ProtoInputStream pi = new ProtoInputStream(stream); |
| 160 |
|
| 161 |
while (pi.nextField() != ProtoInputStream.NO_MORE_FIELDS) { |
| 162 |
try { |
| 163 |
switch (pi.getFieldNumber()) { |
| 164 |
case (int) fieldId2: |
| 165 |
pi.readInt(fieldId2); |
| 166 |
fail("Should have thrown a ProtoParseException"); |
| 167 |
break; |
| 168 |
default: |
| 169 |
fail("Unexpected field id " + pi.getFieldNumber()); |
| 170 |
} |
| 171 |
} catch (ProtoParseException ppe) { |
| 172 |
|
| 173 |
stream.close(); |
| 174 |
return; |
| 175 |
} |
| 176 |
} |
| 177 |
stream.close(); |
| 178 |
fail("Test should not have reached this point..."); |
| 179 |
} |
| 180 |
} |