|
40 | 40 | import com.tencent.cos.xml.listener.CosXmlResultListener;
|
41 | 41 | import com.tencent.cos.xml.model.CosXmlRequest;
|
42 | 42 | import com.tencent.cos.xml.model.CosXmlResult;
|
| 43 | +import com.tencent.cos.xml.model.object.GetObjectBytesRequest; |
43 | 44 | import com.tencent.cos.xml.model.object.GetObjectRequest;
|
44 | 45 | import com.tencent.qcloud.core.http.HttpTaskMetrics;
|
45 | 46 | import com.tencent.qcloud.core.logger.QCloudLogger;
|
@@ -476,4 +477,195 @@ public void onFail(CosXmlRequest request, @Nullable CosXmlClientException client
|
476 | 477 | }
|
477 | 478 | Assert.assertTrue(true);
|
478 | 479 | }
|
| 480 | + |
| 481 | + @Test public void testDownloadPath() { |
| 482 | + testDownloadBtPath("/", true, true); |
| 483 | + testDownloadBtPath("///////", true, true); |
| 484 | + testDownloadBtPath("/abc/../", true, true); |
| 485 | + testDownloadBtPath("/./", true, true); |
| 486 | + testDownloadBtPath("///abc/.//def//../../", true, true); |
| 487 | + testDownloadBtPath("/././///abc/.//def//../../", true, true); |
| 488 | + testDownloadBtPath(TestConst.PERSIST_BUCKET_ROOT_FILE_PATH, false, true); |
| 489 | + testDownloadBtPath(TestConst.PERSIST_BUCKET_PIC_PATH, false, true); |
| 490 | + testDownloadBtPath("/"+TestConst.PERSIST_BUCKET_PIC_PATH, false, true); |
| 491 | + testDownloadBtPath("do_not_remove/", false, true); |
| 492 | + testDownloadBtPath("/do_not_remove/", false, true); |
| 493 | + |
| 494 | + testDownloadBtPath("/", true, false); |
| 495 | + testDownloadBtPath("///////", true, false); |
| 496 | + testDownloadBtPath("/abc/../", true, false); |
| 497 | + testDownloadBtPath("/./", true, false); |
| 498 | + testDownloadBtPath("///abc/.//def//../../", true, false); |
| 499 | + testDownloadBtPath("/././///abc/.//def//../../", true, false); |
| 500 | + testDownloadBtPath(TestConst.PERSIST_BUCKET_ROOT_FILE_PATH, false, false); |
| 501 | + testDownloadBtPath(TestConst.PERSIST_BUCKET_PIC_PATH, false, false); |
| 502 | + testDownloadBtPath("/"+TestConst.PERSIST_BUCKET_PIC_PATH, false, false); |
| 503 | + testDownloadBtPath("do_not_remove/", false, false); |
| 504 | + testDownloadBtPath("/do_not_remove/", false, false); |
| 505 | + |
| 506 | + // 为了覆盖getCanonicalPath()的IOException |
| 507 | + testDownloadBtPath("\u0000", true, true); |
| 508 | + |
| 509 | + Assert.assertTrue(true); |
| 510 | + } |
| 511 | + |
| 512 | + private void testDownloadBtPath(String cosPath, boolean isFail, boolean isObjectKeySimplifyCheck){ |
| 513 | + try { |
| 514 | + TransferManager transferManager = ServiceFactory.INSTANCE.newDefaultTransferManager(); |
| 515 | + |
| 516 | + GetObjectRequest getObjectRequest = new GetObjectRequest(TestConst.PERSIST_BUCKET, |
| 517 | + cosPath, |
| 518 | + TestUtils.localParentPath(), "test"); |
| 519 | + getObjectRequest.setObjectKeySimplifyCheck(isObjectKeySimplifyCheck); |
| 520 | + |
| 521 | + COSXMLDownloadTask downloadTask = transferManager.download(TestUtils.getContext(), |
| 522 | + getObjectRequest); |
| 523 | + final TestLocker testLocker = new TestLocker(); |
| 524 | + downloadTask.setCosXmlResultListener(new CosXmlResultListener() { |
| 525 | + @Override |
| 526 | + public void onSuccess(CosXmlRequest request, CosXmlResult result) { |
| 527 | + testLocker.release(); |
| 528 | + } |
| 529 | + |
| 530 | + @Override |
| 531 | + public void onFail(CosXmlRequest request, CosXmlClientException clientException, CosXmlServiceException serviceException) { |
| 532 | + String error = TestUtils.getCosExceptionMessage(clientException, serviceException); |
| 533 | + TestUtils.printError(error); |
| 534 | + if(serviceException != null){ |
| 535 | + // 如果开启校验且应该校验失败 就不应该走到服务端,其他情况不关注后端错误 |
| 536 | + if(isObjectKeySimplifyCheck && isFail){ |
| 537 | + Assert.fail(error); |
| 538 | + } |
| 539 | + } |
| 540 | + if(clientException != null){ |
| 541 | + if(isFail) { |
| 542 | + if (!"The key in the getobject is illegal".equals(clientException.getMessage())) { |
| 543 | + Assert.fail(clientException.getMessage()); |
| 544 | + } else { |
| 545 | + if(!isObjectKeySimplifyCheck){ |
| 546 | + Assert.fail(clientException.getMessage()); |
| 547 | + } |
| 548 | + } |
| 549 | + } else { |
| 550 | + Assert.fail(clientException.getMessage()); |
| 551 | + } |
| 552 | + } |
| 553 | + testLocker.release(); |
| 554 | + } |
| 555 | + }); |
| 556 | + testLocker.lock(); |
| 557 | + } catch (Exception e){ |
| 558 | + e.printStackTrace(); |
| 559 | + } |
| 560 | + } |
| 561 | + |
| 562 | + @Test public void testGetObjectPath() { |
| 563 | + getObjectByPath("/", true, true); |
| 564 | + getObjectByPath("///////", true, true); |
| 565 | + getObjectByPath("/abc/../", true, true); |
| 566 | + getObjectByPath("/./", true, true); |
| 567 | + getObjectByPath("///abc/.//def//../../", true, true); |
| 568 | + getObjectByPath("/././///abc/.//def//../../", true, true); |
| 569 | + getObjectByPath(TestConst.PERSIST_BUCKET_ROOT_FILE_PATH, false, true); |
| 570 | + getObjectByPath(TestConst.PERSIST_BUCKET_PIC_PATH, false, true); |
| 571 | + getObjectByPath("/"+TestConst.PERSIST_BUCKET_PIC_PATH, false, true); |
| 572 | + getObjectByPath("do_not_remove/", false, true); |
| 573 | + getObjectByPath("/do_not_remove/", false, true); |
| 574 | + |
| 575 | + getObjectByPath("/", true, false); |
| 576 | + getObjectByPath("///////", true, false); |
| 577 | + getObjectByPath("/abc/../", true, false); |
| 578 | + getObjectByPath("/./", true, false); |
| 579 | + getObjectByPath("///abc/.//def//../../", true, false); |
| 580 | + getObjectByPath("/././///abc/.//def//../../", true, false); |
| 581 | + getObjectByPath(TestConst.PERSIST_BUCKET_ROOT_FILE_PATH, false, false); |
| 582 | + getObjectByPath(TestConst.PERSIST_BUCKET_PIC_PATH, false, false); |
| 583 | + getObjectByPath("/"+TestConst.PERSIST_BUCKET_PIC_PATH, false, false); |
| 584 | + getObjectByPath("do_not_remove/", false, false); |
| 585 | + getObjectByPath("/do_not_remove/", false, false); |
| 586 | + |
| 587 | + Assert.assertTrue(true); |
| 588 | + } |
| 589 | + private void getObjectByPath(String cosPath, boolean isFail, boolean isObjectKeySimplifyCheck) { |
| 590 | + CosXmlSimpleService cosXmlSimpleService = ServiceFactory.INSTANCE.newDefaultService(); |
| 591 | + GetObjectRequest getObjectRequest = new GetObjectRequest(TestConst.PERSIST_BUCKET, |
| 592 | + cosPath, |
| 593 | + TestUtils.localParentPath(), "test"); |
| 594 | + getObjectRequest.setObjectKeySimplifyCheck(isObjectKeySimplifyCheck); |
| 595 | + try { |
| 596 | + cosXmlSimpleService.getObject(getObjectRequest); |
| 597 | + } catch (CosXmlClientException e) { |
| 598 | + TestUtils.printError(e.getMessage()); |
| 599 | + if(isFail) { |
| 600 | + if (!"The key in the getobject is illegal".equals(e.getMessage())) { |
| 601 | + Assert.fail(e.getMessage()); |
| 602 | + } else { |
| 603 | + if(!isObjectKeySimplifyCheck){ |
| 604 | + Assert.fail(e.getMessage()); |
| 605 | + } |
| 606 | + } |
| 607 | + } else { |
| 608 | + Assert.fail(e.getMessage()); |
| 609 | + } |
| 610 | + } catch (CosXmlServiceException e) { |
| 611 | + // 如果开启校验且应该校验失败 就不应该走到服务端,其他情况不关注后端错误 |
| 612 | + if(isObjectKeySimplifyCheck && isFail){ |
| 613 | + Assert.fail(e.getMessage()); |
| 614 | + } |
| 615 | + } |
| 616 | + } |
| 617 | + |
| 618 | + @Test public void testGetObjectBytesPath() { |
| 619 | + getObjectBytesByPath("/", true, true); |
| 620 | + getObjectBytesByPath("///////", true, true); |
| 621 | + getObjectBytesByPath("/abc/../", true, true); |
| 622 | + getObjectBytesByPath("/./", true, true); |
| 623 | + getObjectBytesByPath("///abc/.//def//../../", true, true); |
| 624 | + getObjectBytesByPath("/././///abc/.//def//../../", true, true); |
| 625 | + getObjectBytesByPath(TestConst.PERSIST_BUCKET_ROOT_FILE_PATH, false, true); |
| 626 | + getObjectBytesByPath(TestConst.PERSIST_BUCKET_PIC_PATH, false, true); |
| 627 | + getObjectBytesByPath("/"+TestConst.PERSIST_BUCKET_PIC_PATH, false, true); |
| 628 | + getObjectBytesByPath("do_not_remove/", false, true); |
| 629 | + getObjectBytesByPath("/do_not_remove/", false, true); |
| 630 | + |
| 631 | + getObjectBytesByPath("/", true, false); |
| 632 | + getObjectBytesByPath("///////", true, false); |
| 633 | + getObjectBytesByPath("/abc/../", true, false); |
| 634 | + getObjectBytesByPath("/./", true, false); |
| 635 | + getObjectBytesByPath("///abc/.//def//../../", true, false); |
| 636 | + getObjectBytesByPath("/././///abc/.//def//../../", true, false); |
| 637 | + getObjectBytesByPath(TestConst.PERSIST_BUCKET_ROOT_FILE_PATH, false, false); |
| 638 | + getObjectBytesByPath(TestConst.PERSIST_BUCKET_PIC_PATH, false, false); |
| 639 | + getObjectBytesByPath("/"+TestConst.PERSIST_BUCKET_PIC_PATH, false, false); |
| 640 | + getObjectBytesByPath("do_not_remove/", false, false); |
| 641 | + getObjectBytesByPath("/do_not_remove/", false, false); |
| 642 | + |
| 643 | + Assert.assertTrue(true); |
| 644 | + } |
| 645 | + private void getObjectBytesByPath(String cosPath, boolean isFail, boolean isObjectKeySimplifyCheck) { |
| 646 | + CosXmlSimpleService cosXmlSimpleService = ServiceFactory.INSTANCE.newDefaultService(); |
| 647 | + GetObjectBytesRequest getObjectBytesRequest = new GetObjectBytesRequest(TestConst.PERSIST_BUCKET, cosPath); |
| 648 | + getObjectBytesRequest.setObjectKeySimplifyCheck(isObjectKeySimplifyCheck); |
| 649 | + try { |
| 650 | + cosXmlSimpleService.getObject(getObjectBytesRequest); |
| 651 | + } catch (CosXmlClientException e) { |
| 652 | + TestUtils.printError(e.getMessage()); |
| 653 | + if(isFail) { |
| 654 | + if (!"The key in the getobject is illegal".equals(e.getMessage())) { |
| 655 | + Assert.fail(e.getMessage()); |
| 656 | + } else { |
| 657 | + if(!isObjectKeySimplifyCheck){ |
| 658 | + Assert.fail(e.getMessage()); |
| 659 | + } |
| 660 | + } |
| 661 | + } else { |
| 662 | + Assert.fail(e.getMessage()); |
| 663 | + } |
| 664 | + } catch (CosXmlServiceException e) { |
| 665 | + // 如果开启校验且应该校验失败 就不应该走到服务端,其他情况不关注后端错误 |
| 666 | + if(isObjectKeySimplifyCheck && isFail){ |
| 667 | + Assert.fail(e.getMessage()); |
| 668 | + } |
| 669 | + } |
| 670 | + } |
479 | 671 | }
|
0 commit comments