TryListBuilderTest.java revision 5916df99999ae58f707d829792ef3997546628fd
1/* 2 * Copyright 2013, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32package org.jf.dexlib2.writer.util; 33 34import com.google.common.collect.ImmutableList; 35import junit.framework.Assert; 36import org.jf.dexlib2.iface.TryBlock; 37import org.jf.dexlib2.immutable.ImmutableExceptionHandler; 38import org.jf.dexlib2.immutable.ImmutableTryBlock; 39import org.junit.Test; 40 41import java.util.List; 42 43public class TryListBuilderTest { 44 @Test 45 public void testSingleCatchAll_Beginning() { 46 TryListBuilder tlb = new TryListBuilder(); 47 48 tlb.addHandler(null, 0, 10, 5); 49 50 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 51 52 List<? extends TryBlock> expected = ImmutableList.of(new ImmutableTryBlock(0, 10, 53 ImmutableList.of(new ImmutableExceptionHandler(null, 5)))); 54 55 Assert.assertEquals(expected, tryBlocks); 56 } 57 58 @Test 59 public void testSingleCatchAll_Middle() { 60 TryListBuilder tlb = new TryListBuilder(); 61 62 tlb.addHandler(null, 5, 10, 15); 63 64 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 65 66 List<? extends TryBlock> expected = ImmutableList.of(new ImmutableTryBlock(5, 5, 67 ImmutableList.of(new ImmutableExceptionHandler(null, 15)))); 68 69 Assert.assertEquals(expected, tryBlocks); 70 } 71 72 @Test 73 public void testSingleCatch_Beginning() { 74 TryListBuilder tlb = new TryListBuilder(); 75 76 tlb.addHandler("Ljava/lang/Exception;", 0, 10, 5); 77 78 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 79 80 List<? extends TryBlock> expected = ImmutableList.of(new ImmutableTryBlock(0, 10, 81 ImmutableList.of(new ImmutableExceptionHandler("Ljava/lang/Exception;", 5)))); 82 83 Assert.assertEquals(expected, tryBlocks); 84 } 85 86 @Test 87 public void testSingleCatch_Middle() { 88 TryListBuilder tlb = new TryListBuilder(); 89 90 tlb.addHandler("Ljava/lang/Exception;", 5, 10, 15); 91 92 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 93 94 List<? extends TryBlock> expected = ImmutableList.of(new ImmutableTryBlock(5, 5, 95 ImmutableList.of(new ImmutableExceptionHandler("Ljava/lang/Exception;", 15)))); 96 97 Assert.assertEquals(expected, tryBlocks); 98 } 99 100 @Test 101 public void testOverlap_End_After() { 102 TryListBuilder tlb = new TryListBuilder(); 103 104 tlb.addHandler("LException1;", 0, 10, 5); 105 tlb.addHandler("LException2;", 10, 20, 6); 106 107 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 108 109 List<? extends TryBlock> expected = ImmutableList.of( 110 new ImmutableTryBlock(0, 10, 111 ImmutableList.of(new ImmutableExceptionHandler("LException1;", 5))), 112 new ImmutableTryBlock(10, 10, 113 ImmutableList.of(new ImmutableExceptionHandler("LException2;", 6)))); 114 115 Assert.assertEquals(expected, tryBlocks); 116 } 117 118 @Test 119 public void testOverlap_After_After() { 120 TryListBuilder tlb = new TryListBuilder(); 121 122 tlb.addHandler("LException1;", 0, 10, 5); 123 tlb.addHandler("LException2;", 15, 20, 6); 124 125 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 126 127 List<? extends TryBlock> expected = ImmutableList.of( 128 new ImmutableTryBlock(0, 10, 129 ImmutableList.of(new ImmutableExceptionHandler("LException1;", 5))), 130 new ImmutableTryBlock(15, 5, 131 ImmutableList.of(new ImmutableExceptionHandler("LException2;", 6)))); 132 133 Assert.assertEquals(expected, tryBlocks); 134 } 135 136 @Test 137 public void testOverlap_Before_Start() { 138 TryListBuilder tlb = new TryListBuilder(); 139 140 tlb.addHandler("LException1;", 5, 10, 5); 141 tlb.addHandler("LException2;", 0, 5, 6); 142 143 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 144 145 List<? extends TryBlock> expected = ImmutableList.of( 146 new ImmutableTryBlock(0, 5, 147 ImmutableList.of(new ImmutableExceptionHandler("LException2;", 6))), 148 new ImmutableTryBlock(5, 5, 149 ImmutableList.of(new ImmutableExceptionHandler("LException1;", 5)))); 150 151 Assert.assertEquals(expected, tryBlocks); 152 } 153 154 @Test 155 public void testOverlap_Before_Before() { 156 TryListBuilder tlb = new TryListBuilder(); 157 158 tlb.addHandler("LException1;", 5, 10, 5); 159 tlb.addHandler("LException2;", 0, 3, 6); 160 161 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 162 163 List<? extends TryBlock> expected = ImmutableList.of( 164 new ImmutableTryBlock(0, 3, 165 ImmutableList.of(new ImmutableExceptionHandler("LException2;", 6))), 166 new ImmutableTryBlock(5, 5, 167 ImmutableList.of(new ImmutableExceptionHandler("LException1;", 5)))); 168 169 Assert.assertEquals(expected, tryBlocks); 170 } 171 172 @Test 173 public void testOverlap_Start_End() { 174 TryListBuilder tlb = new TryListBuilder(); 175 176 tlb.addHandler("LException1;", 0, 10, 5); 177 tlb.addHandler("LException2;", 0, 10, 6); 178 179 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 180 181 List<? extends TryBlock> expected = ImmutableList.of( 182 new ImmutableTryBlock(0, 10, 183 ImmutableList.of( 184 new ImmutableExceptionHandler("LException1;", 5), 185 new ImmutableExceptionHandler("LException2;", 6)))); 186 187 Assert.assertEquals(expected, tryBlocks); 188 } 189 190 @Test 191 public void testOverlap_Start_Middle() { 192 TryListBuilder tlb = new TryListBuilder(); 193 194 tlb.addHandler("LException1;", 0, 10, 5); 195 tlb.addHandler("LException2;", 0, 5, 6); 196 197 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 198 199 List<? extends TryBlock> expected = ImmutableList.of( 200 new ImmutableTryBlock(0, 5, 201 ImmutableList.of( 202 new ImmutableExceptionHandler("LException1;", 5), 203 new ImmutableExceptionHandler("LException2;", 6))), 204 new ImmutableTryBlock(5, 5, 205 ImmutableList.of( 206 new ImmutableExceptionHandler("LException1;", 5)))); 207 208 Assert.assertEquals(expected, tryBlocks); 209 } 210 211 @Test 212 public void testOverlap_Middle_Middle() { 213 TryListBuilder tlb = new TryListBuilder(); 214 215 tlb.addHandler("LException1;", 0, 10, 5); 216 tlb.addHandler("LException2;", 2, 7, 6); 217 218 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 219 220 List<? extends TryBlock> expected = ImmutableList.of( 221 new ImmutableTryBlock(0, 2, 222 ImmutableList.of( 223 new ImmutableExceptionHandler("LException1;", 5))), 224 new ImmutableTryBlock(2, 5, 225 ImmutableList.of( 226 new ImmutableExceptionHandler("LException1;", 5), 227 new ImmutableExceptionHandler("LException2;", 6))), 228 new ImmutableTryBlock(7, 3, 229 ImmutableList.of( 230 new ImmutableExceptionHandler("LException1;", 5)))); 231 232 Assert.assertEquals(expected, tryBlocks); 233 } 234 235 @Test 236 public void testOverlap_Middle_End() { 237 TryListBuilder tlb = new TryListBuilder(); 238 239 tlb.addHandler("LException1;", 0, 10, 5); 240 tlb.addHandler("LException2;", 5, 10, 6); 241 242 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 243 244 List<? extends TryBlock> expected = ImmutableList.of( 245 new ImmutableTryBlock(0, 5, 246 ImmutableList.of( 247 new ImmutableExceptionHandler("LException1;", 5))), 248 new ImmutableTryBlock(5, 5, 249 ImmutableList.of( 250 new ImmutableExceptionHandler("LException1;", 5), 251 new ImmutableExceptionHandler("LException2;", 6)))); 252 253 Assert.assertEquals(expected, tryBlocks); 254 } 255 256 @Test 257 public void testOverlap_Beginning_After() { 258 TryListBuilder tlb = new TryListBuilder(); 259 260 tlb.addHandler("LException1;", 0, 10, 5); 261 tlb.addHandler("LException2;", 0, 15, 6); 262 263 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 264 265 List<? extends TryBlock> expected = ImmutableList.of( 266 new ImmutableTryBlock(0, 10, 267 ImmutableList.of( 268 new ImmutableExceptionHandler("LException1;", 5), 269 new ImmutableExceptionHandler("LException2;", 6))), 270 new ImmutableTryBlock(10, 5, 271 ImmutableList.of( 272 new ImmutableExceptionHandler("LException2;", 6)))); 273 274 Assert.assertEquals(expected, tryBlocks); 275 } 276 277 @Test 278 public void testOverlap_Middle_After() { 279 TryListBuilder tlb = new TryListBuilder(); 280 281 tlb.addHandler("LException1;", 0, 10, 5); 282 tlb.addHandler("LException2;", 5, 15, 6); 283 284 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 285 286 List<? extends TryBlock> expected = ImmutableList.of( 287 new ImmutableTryBlock(0, 5, 288 ImmutableList.of( 289 new ImmutableExceptionHandler("LException1;", 5))), 290 new ImmutableTryBlock(5, 5, 291 ImmutableList.of( 292 new ImmutableExceptionHandler("LException1;", 5), 293 new ImmutableExceptionHandler("LException2;", 6))), 294 new ImmutableTryBlock(10, 5, 295 ImmutableList.of( 296 new ImmutableExceptionHandler("LException2;", 6)))); 297 298 Assert.assertEquals(expected, tryBlocks); 299 } 300 301 @Test 302 public void testOverlap_Before_End() { 303 TryListBuilder tlb = new TryListBuilder(); 304 305 tlb.addHandler("LException1;", 5, 10, 5); 306 tlb.addHandler("LException2;", 0, 10, 6); 307 308 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 309 310 List<? extends TryBlock> expected = ImmutableList.of( 311 new ImmutableTryBlock(0, 5, 312 ImmutableList.of( 313 new ImmutableExceptionHandler("LException2;", 6))), 314 new ImmutableTryBlock(5, 5, 315 ImmutableList.of( 316 new ImmutableExceptionHandler("LException1;", 5), 317 new ImmutableExceptionHandler("LException2;", 6)))); 318 319 Assert.assertEquals(expected, tryBlocks); 320 } 321 322 @Test 323 public void testOverlap_Before_Middle() { 324 TryListBuilder tlb = new TryListBuilder(); 325 326 tlb.addHandler("LException1;", 5, 10, 5); 327 tlb.addHandler("LException2;", 0, 7, 6); 328 329 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 330 331 List<? extends TryBlock> expected = ImmutableList.of( 332 new ImmutableTryBlock(0, 5, 333 ImmutableList.of( 334 new ImmutableExceptionHandler("LException2;", 6))), 335 new ImmutableTryBlock(5, 2, 336 ImmutableList.of( 337 new ImmutableExceptionHandler("LException1;", 5), 338 new ImmutableExceptionHandler("LException2;", 6))), 339 new ImmutableTryBlock(7, 3, 340 ImmutableList.of( 341 new ImmutableExceptionHandler("LException1;", 5)))); 342 343 Assert.assertEquals(expected, tryBlocks); 344 } 345 346 @Test 347 public void testOverlap_Before_After() { 348 TryListBuilder tlb = new TryListBuilder(); 349 350 tlb.addHandler("LException1;", 5, 10, 5); 351 tlb.addHandler("LException2;", 0, 15, 6); 352 353 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 354 355 List<? extends TryBlock> expected = ImmutableList.of( 356 new ImmutableTryBlock(0, 5, 357 ImmutableList.of( 358 new ImmutableExceptionHandler("LException2;", 6))), 359 new ImmutableTryBlock(5, 5, 360 ImmutableList.of( 361 new ImmutableExceptionHandler("LException1;", 5), 362 new ImmutableExceptionHandler("LException2;", 6))), 363 new ImmutableTryBlock(10, 5, 364 ImmutableList.of( 365 new ImmutableExceptionHandler("LException2;", 6)))); 366 367 Assert.assertEquals(expected, tryBlocks); 368 } 369 370 @Test 371 public void testOverlap_Hole() { 372 TryListBuilder tlb = new TryListBuilder(); 373 374 tlb.addHandler("LException1;", 1, 5, 5); 375 tlb.addHandler("LException1;", 10, 14, 5); 376 tlb.addHandler("LException2;", 0, 15, 6); 377 378 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 379 380 List<? extends TryBlock> expected = ImmutableList.of( 381 new ImmutableTryBlock(0, 1, 382 ImmutableList.of( 383 new ImmutableExceptionHandler("LException2;", 6))), 384 new ImmutableTryBlock(1, 4, 385 ImmutableList.of( 386 new ImmutableExceptionHandler("LException1;", 5), 387 new ImmutableExceptionHandler("LException2;", 6))), 388 new ImmutableTryBlock(5, 5, 389 ImmutableList.of( 390 new ImmutableExceptionHandler("LException2;", 6))), 391 new ImmutableTryBlock(10, 4, 392 ImmutableList.of( 393 new ImmutableExceptionHandler("LException1;", 5), 394 new ImmutableExceptionHandler("LException2;", 6))), 395 new ImmutableTryBlock(14, 1, 396 ImmutableList.of( 397 new ImmutableExceptionHandler("LException2;", 6)))); 398 399 Assert.assertEquals(expected, tryBlocks); 400 } 401 402 @Test 403 public void testHandlerMerge_Same() { 404 TryListBuilder tlb = new TryListBuilder(); 405 406 tlb.addHandler("LException1;", 5, 10, 5); 407 tlb.addHandler("LException1;", 0, 15, 5); 408 409 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 410 411 List<? extends TryBlock> expected = ImmutableList.of( 412 new ImmutableTryBlock(0, 5, 413 ImmutableList.of( 414 new ImmutableExceptionHandler("LException1;", 5))), 415 new ImmutableTryBlock(5, 5, 416 ImmutableList.of( 417 new ImmutableExceptionHandler("LException1;", 5))), 418 new ImmutableTryBlock(10, 5, 419 ImmutableList.of( 420 new ImmutableExceptionHandler("LException1;", 5)))); 421 422 Assert.assertEquals(expected, tryBlocks); 423 } 424 425 @Test 426 public void testHandlerMerge_DifferentType() { 427 TryListBuilder tlb = new TryListBuilder(); 428 429 tlb.addHandler("LException1;", 5, 10, 5); 430 tlb.addHandler("LException2;", 0, 15, 6); 431 432 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 433 434 List<? extends TryBlock> expected = ImmutableList.of( 435 new ImmutableTryBlock(0, 5, 436 ImmutableList.of( 437 new ImmutableExceptionHandler("LException2;", 6))), 438 new ImmutableTryBlock(5, 5, 439 ImmutableList.of( 440 new ImmutableExceptionHandler("LException1;", 5), 441 new ImmutableExceptionHandler("LException2;", 6))), 442 new ImmutableTryBlock(10, 5, 443 ImmutableList.of( 444 new ImmutableExceptionHandler("LException2;", 6)))); 445 446 Assert.assertEquals(expected, tryBlocks); 447 } 448 449 @Test 450 public void testHandlerMerge_DifferentAddress() { 451 TryListBuilder tlb = new TryListBuilder(); 452 453 tlb.addHandler("LException1;", 5, 10, 5); 454 try { 455 tlb.addHandler("LException1;", 0, 15, 6); 456 } catch (TryListBuilder.InvalidTryException ex) { 457 return; 458 } 459 Assert.fail(); 460 } 461 462 @Test 463 public void testHandlerMerge_Exception_Catchall() { 464 TryListBuilder tlb = new TryListBuilder(); 465 466 tlb.addHandler("LException1;", 5, 10, 5); 467 tlb.addHandler(null, 0, 15, 6); 468 469 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 470 471 List<? extends TryBlock> expected = ImmutableList.of( 472 new ImmutableTryBlock(0, 5, 473 ImmutableList.of( 474 new ImmutableExceptionHandler(null, 6))), 475 new ImmutableTryBlock(5, 5, 476 ImmutableList.of( 477 new ImmutableExceptionHandler("LException1;", 5), 478 new ImmutableExceptionHandler(null, 6))), 479 new ImmutableTryBlock(10, 5, 480 ImmutableList.of( 481 new ImmutableExceptionHandler(null, 6)))); 482 483 Assert.assertEquals(expected, tryBlocks); 484 } 485 486 @Test 487 public void testHandlerMerge_Catchall_Exception() { 488 TryListBuilder tlb = new TryListBuilder(); 489 490 tlb.addHandler(null, 5, 10, 5); 491 tlb.addHandler("LException1;", 0, 15, 6); 492 493 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 494 495 List<? extends TryBlock> expected = ImmutableList.of( 496 new ImmutableTryBlock(0, 5, 497 ImmutableList.of( 498 new ImmutableExceptionHandler("LException1;", 6))), 499 new ImmutableTryBlock(5, 5, 500 ImmutableList.of( 501 new ImmutableExceptionHandler(null, 5), 502 new ImmutableExceptionHandler("LException1;", 6))), 503 new ImmutableTryBlock(10, 5, 504 ImmutableList.of( 505 new ImmutableExceptionHandler("LException1;", 6)))); 506 507 Assert.assertEquals(expected, tryBlocks); 508 } 509 510 @Test 511 public void testHandlerMerge_Catchall_Catchall() { 512 TryListBuilder tlb = new TryListBuilder(); 513 514 tlb.addHandler(null, 5, 10, 5); 515 tlb.addHandler(null, 0, 15, 5); 516 517 List<TryBlock> tryBlocks = tlb.getTryBlocks(); 518 519 List<? extends TryBlock> expected = ImmutableList.of( 520 new ImmutableTryBlock(0, 5, 521 ImmutableList.of( 522 new ImmutableExceptionHandler(null, 5))), 523 new ImmutableTryBlock(5, 5, 524 ImmutableList.of( 525 new ImmutableExceptionHandler(null, 5))), 526 new ImmutableTryBlock(10, 5, 527 ImmutableList.of( 528 new ImmutableExceptionHandler(null, 5)))); 529 530 Assert.assertEquals(expected, tryBlocks); 531 } 532 533 @Test 534 public void testHandlerMerge_Catchall_Catchall_DifferentAddress() { 535 TryListBuilder tlb = new TryListBuilder(); 536 537 tlb.addHandler(null, 5, 10, 5); 538 try { 539 tlb.addHandler(null, 0, 15, 6); 540 } catch (TryListBuilder.InvalidTryException ex) { 541 return; 542 } 543 Assert.fail(); 544 } 545} 546