Skip to content

Commit a2adc90

Browse files
committed
msgpack#100: Add test cases to increase the code coverage from 83% to 95%
1 parent 0e30c43 commit a2adc90

File tree

1 file changed

+116
-3
lines changed

1 file changed

+116
-3
lines changed

msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,31 @@ class MessagePackTest extends MessagePackSpec with PropertyChecks {
110110
}
111111
}
112112

113+
def checkException[A](v: A, pack: MessagePacker => Unit, unpack: MessageUnpacker => A) {
114+
var b: Array[Byte] = null
115+
val bs = new ByteArrayOutputStream()
116+
val packer = new MessagePacker(bs)
117+
pack(packer)
118+
packer.close()
119+
120+
b = bs.toByteArray
121+
122+
val unpacker = new MessageUnpacker(b)
123+
val ret = unpack(unpacker)
124+
125+
fail("cannot not reach here")
126+
}
127+
128+
def checkOverflow[A](v: A, pack: MessagePacker => Unit, unpack: MessageUnpacker => A) {
129+
try {
130+
checkException[A](v, pack, unpack)
131+
}
132+
catch {
133+
case e:MessageIntegerOverflowException => // OK
134+
}
135+
}
136+
137+
113138

114139

115140
"pack/unpack primitive values" taggedAs("prim") in {
@@ -123,11 +148,59 @@ class MessagePackTest extends MessagePackSpec with PropertyChecks {
123148
check(null, _.packNil, _.unpackNil())
124149
}
125150

126-
"pack/unpack BigInteger" in {
151+
"pack/unpack integer values" taggedAs("int") in {
152+
val sampleData = Seq[Long](Int.MinValue.toLong - 10, -65535, -8191, -1024, -255, -127, -63, -31, -15, -7, -3, -1, 0, 2, 4, 8, 16, 32, 64, 128, 256, 1024, 8192, 65536, Int.MaxValue.toLong + 10)
153+
for(v <- sampleData) {
154+
check(v, _.packLong(v), _.unpackLong)
155+
156+
if(v.isValidInt) {
157+
val vi = v.toInt
158+
check(vi, _.packInt(vi), _.unpackInt)
159+
}
160+
else {
161+
checkOverflow(v, _.packLong(v), _.unpackInt)
162+
}
163+
164+
if(v.isValidShort) {
165+
val vi = v.toShort
166+
check(vi, _.packShort(vi), _.unpackShort)
167+
}
168+
else {
169+
checkOverflow(v, _.packLong(v), _.unpackShort)
170+
}
171+
172+
if(v.isValidByte) {
173+
val vi = v.toByte
174+
check(vi, _.packByte(vi), _.unpackByte)
175+
}
176+
else {
177+
checkOverflow(v, _.packLong(v), _.unpackByte)
178+
}
179+
180+
}
181+
182+
}
183+
184+
"pack/unpack BigInteger" taggedAs("bi") in {
127185
forAll { (a: Long) =>
128186
val v = BigInteger.valueOf(a)
129187
check(v, _.packBigInteger(v), _.unpackBigInteger)
130188
}
189+
190+
for(bi <- Seq(BigInteger.valueOf(Long.MaxValue).add(BigInteger.valueOf(1)))) {
191+
check(bi, _.packBigInteger(bi), _.unpackBigInteger())
192+
}
193+
194+
for(bi <- Seq(BigInteger.valueOf(Long.MaxValue).shiftLeft(10))) {
195+
try {
196+
checkException(bi, _.packBigInteger(bi), _.unpackBigInteger())
197+
fail("cannot reach here")
198+
}
199+
catch {
200+
case e:IllegalArgumentException => // OK
201+
}
202+
}
203+
131204
}
132205

133206
"pack/unpack strings" taggedAs ("string") in {
@@ -204,8 +277,24 @@ class MessagePackTest extends MessagePackSpec with PropertyChecks {
204277
}
205278
)
206279
}
280+
281+
val len = Seq(1000, 2000, 10000, 50000, 100000, 500000)
282+
for(l <- len) {
283+
val v = new Array[Byte](l)
284+
Random.nextBytes(v)
285+
check(v, { packer => packer.packBinaryHeader(v.length); packer.writePayload(v)}, { unpacker =>
286+
val len = unpacker.unpackBinaryHeader()
287+
val out = new Array[Byte](len)
288+
unpacker.readPayload(out, 0, len)
289+
out
290+
}
291+
)
292+
}
207293
}
208294

295+
val testHeaderLength = Seq(1, 2, 4, 8, 16, 1000, 2000, 10000, 50000, 100000, 500000)
296+
297+
209298
"pack/unpack arrays" taggedAs ("array") in {
210299
forAll { (v: Array[Int]) =>
211300
check(v, { packer =>
@@ -220,6 +309,18 @@ class MessagePackTest extends MessagePackSpec with PropertyChecks {
220309
}
221310
)
222311
}
312+
313+
for(l <- testHeaderLength) {
314+
check(l, _.packArrayHeader(l), _.unpackArrayHeader())
315+
}
316+
317+
try {
318+
checkException(0, _.packArrayHeader(-1), _.unpackArrayHeader)
319+
}
320+
catch {
321+
case e: IllegalArgumentException => // OK
322+
}
323+
223324
}
224325

225326
"pack/unpack maps" taggedAs ("map") in {
@@ -242,6 +343,19 @@ class MessagePackTest extends MessagePackSpec with PropertyChecks {
242343
}
243344
)
244345
}
346+
347+
for(l <- testHeaderLength) {
348+
check(l, _.packMapHeader(l), _.unpackMapHeader())
349+
}
350+
351+
try {
352+
checkException(0, _.packMapHeader(-1), _.unpackMapHeader)
353+
}
354+
catch {
355+
case e: IllegalArgumentException => // OK
356+
}
357+
358+
245359
}
246360

247361
"pack/unpack extended types" taggedAs("ext") in {
@@ -254,8 +368,7 @@ class MessagePackTest extends MessagePackSpec with PropertyChecks {
254368
}
255369
}
256370

257-
val extLen = Seq(1, 2, 4, 8, 16, 1000, 2000, 10000, 50000, 100000, 500000)
258-
for(l <- extLen) {
371+
for(l <- testHeaderLength) {
259372
val ext = new ExtendedTypeHeader(l, Random.nextInt(128))
260373
check(ext, _.packExtendedTypeHeader(ext.getType, ext.getLength), _.unpackExtendedTypeHeader())
261374
}

0 commit comments

Comments
 (0)