From fada14523d081f7df14b44186d2a8f1b2f7777ac Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 1 Nov 2017 18:17:57 -0700 Subject: [PATCH] Add presignedPutObject test with content-sha256 --- run/core/aws-sdk-php/quick-tests.php | 87 ++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/run/core/aws-sdk-php/quick-tests.php b/run/core/aws-sdk-php/quick-tests.php index 9cbe62ea..627a3d0c 100644 --- a/run/core/aws-sdk-php/quick-tests.php +++ b/run/core/aws-sdk-php/quick-tests.php @@ -22,12 +22,14 @@ use Aws\Credentials; use Aws\Exception\AwsException; use GuzzleHttp\Psr7; +use GuzzleHttp\Client; // Constants const FILE_10KB = "datafile-10-kB"; const FILE_5_MB = "datafile-5-MB"; const HTTP_OK = "200"; const HTTP_NOCONTENT = "204"; +const HTTP_BADREQUEST = "400"; const HTTP_NOTIMPLEMENTED = "501"; const HTTP_INTERNAL_ERROR = "500"; const TEST_METADATA = ['Param_1' => 'val-1']; @@ -812,6 +814,90 @@ function testAnonDeleteObjects($s3Client, $params) { } } + /** + * testPresignedPut tests presigned PUT S3 API under following conditions + * - Set content-sha256 + * - No content-sha256 + * - Invalid content-sha256 + * + * @param $s3Client AWS\S3\S3Client object + * + * @param $params associative array containing bucket and object name + * + * @return void + */ +function testPresignedPut($s3Client, $params) { + // Test to validate the validate sha256 set to the value of the body. + // ------ 1 ------ + $bucket = $params['Bucket']; + $object = $params['Object']; + $sha256 = "03675ac53ff9cd1535ccc7dfcdfa2c458c5218371f418dc136f2d19ac1fbe8a5"; + + $cmd = $s3Client->getCommand('PutObject', [ + 'Bucket' => $bucket, + 'Key' => $object, + 'ContentSHA256' => $sha256, + ]); + + $request = $s3Client->createPresignedRequest($cmd, '+20 minutes'); + $presignedUrl = (string) $request->getUri(); + + $client = new Client(); + $body = "Hello, World"; + $res = $client->request('PUT', $presignedUrl, ['body' => $body]); + + if ($res->getStatusCode() != HTTP_OK) { + throw new Exception('presignedPutObject API failed for ' . + $bucket, $object); + } + // ------ 1 ------ + + // Test to simple presigned PUT with just body no validation. + // ------ 2 ------ + $bucket = $params['Bucket']; + $object = $params['Object']; + + $cmd = $s3Client->getCommand('PutObject', [ + 'Bucket' => $bucket, + 'Key' => $object, + ]); + + $request = $s3Client->createPresignedRequest($cmd, '+20 minutes'); + $presignedUrl = (string) $request->getUri(); + $res = $client->request('PUT', $presignedUrl, ['body' => $body]); + + if ($res->getStatusCode() != HTTP_OK) { + throw new Exception('presignedPutObject API failed for ' . + $bucket, $object); + } + // ------ 2 ------ + + // Test to presigned PUT with invalid sha256. + // ------ 3 ------ + $bucket = $params['Bucket']; + $object = $params['Object']; + $sha256 = "invalid-sha256"; + + $cmd = $s3Client->getCommand('PutObject', [ + 'Bucket' => $bucket, + 'Key' => $object, + 'ContentSHA256' => $sha256, + ]); + + $request = $s3Client->createPresignedRequest($cmd, '+20 minutes'); + $presignedUrl = (string) $request->getUri(); + + $client = new Client(); + $body = "Hello, World"; + $res = $client->request('PUT', $presignedUrl, ['body' => $body]); + + if ($res->getStatusCode() != HTTP_BADREQUEST) { + throw new Exception('presignedPutObject API failed for ' . + $bucket, $object); + } + // ------ 3 ------ +} + /** * testBucketPolicy tests GET/PUT Bucket policy S3 APIs * @@ -1084,6 +1170,7 @@ public function out($data) { runTest($s3Client, 'testMultipartUpload', "createMultipartUpload ( array \$params = [] )", $testParams); runTest($s3Client, 'testMultipartUploadFailure', "uploadPart ( array \$params = [] )", $testParams); runTest($s3Client, 'testAbortMultipartUpload', "abortMultipartupload ( array \$params = [] )", $testParams); + runTest($s3Client, 'testPresignedPut', "presignedPut ( array \$params = [] )", $testParams); runTest($s3Client, 'testBucketPolicy', "getBucketPolicy ( array \$params = [] )", ['Bucket' => $emptyBucket]); } finally {