Esempio n. 1
0
        protected void SetupFlexibleChecksumStream(IWebResponseData responseData, Stream responseStream, long contentLength, IRequestContext requestContext)
        {
            var algorithm = ChecksumUtils.SelectChecksumForResponseValidation(requestContext?.OriginalRequest?.ChecksumResponseAlgorithms, responseData);

            if (algorithm == CoreChecksumAlgorithm.NONE)
            {
                return;
            }

            ChecksumAlgorithm = algorithm;
            ExpectedFlexibleChecksumResult = responseData.GetHeaderValue(ChecksumUtils.GetChecksumHeaderKey(algorithm));
            var checksum = Convert.FromBase64String(ExpectedFlexibleChecksumResult);

            switch (algorithm)
            {
            case CoreChecksumAlgorithm.CRC32C:
                FlexibleChecksumStream = new HashStream <HashingWrapperCRC32C>(responseStream, checksum, contentLength);
                break;

            case CoreChecksumAlgorithm.CRC32:
                FlexibleChecksumStream = new HashStream <HashingWrapperCRC32>(responseStream, checksum, contentLength);
                break;

            case CoreChecksumAlgorithm.SHA256:
                FlexibleChecksumStream = new HashStream <HashingWrapperSHA256>(responseStream, checksum, contentLength);
                break;

            case CoreChecksumAlgorithm.SHA1:
                FlexibleChecksumStream = new HashStream <HashingWrapperSHA1>(responseStream, checksum, contentLength);
                break;

            default:
                throw new AmazonClientException($"Unsupported checksum algorithm {algorithm}");
            }
        }
Esempio n. 2
0
        public void ResponseChecksumSelection_NoChecksumsReturned()
        {
            var operationSupportedChecksums = new List <CoreChecksumAlgorithm>
            {
                CoreChecksumAlgorithm.CRC32,
                CoreChecksumAlgorithm.CRC32C
            };
            var responseData = new WebResponseData();

            // No checksum headers on the response, so we cannot choose one to validate
            Assert.AreEqual(CoreChecksumAlgorithm.NONE,
                            ChecksumUtils.SelectChecksumForResponseValidation(operationSupportedChecksums, responseData));
        }
Esempio n. 3
0
        public void TestResponseChecksumSelection_UnsupportedChecksum()
        {
            var operationSupportedChecksums = new List <CoreChecksumAlgorithm>
            {
                CoreChecksumAlgorithm.SHA256,
                CoreChecksumAlgorithm.CRC32
            };
            var responseData = new WebResponseData();

            responseData.Headers = new Dictionary <string, string>
            {
                { "x-amz-checksum-crc64", "checksum" }
            };

            // CRC64 is not currently supported by the SDK
            Assert.AreEqual(CoreChecksumAlgorithm.NONE,
                            ChecksumUtils.SelectChecksumForResponseValidation(operationSupportedChecksums, responseData));
        }
Esempio n. 4
0
        public void ResponseChecksumSelection_SingleChecksum()
        {
            var operationSupportedChecksums = new List <CoreChecksumAlgorithm>
            {
                CoreChecksumAlgorithm.SHA256,
                CoreChecksumAlgorithm.CRC32
            };
            var responseData = new WebResponseData();

            responseData.Headers = new Dictionary <string, string>
            {
                { "x-amz-checksum-sha256", "checksum" }
            };

            // Even though CRC32 is supported and comes first in priority order, only SHA256 is returned from service
            Assert.AreEqual(CoreChecksumAlgorithm.SHA256,
                            ChecksumUtils.SelectChecksumForResponseValidation(operationSupportedChecksums, responseData));
        }
Esempio n. 5
0
        public void TestResponseChecksumSelection_Priority()
        {
            var operationSupportedChecksums = new List <CoreChecksumAlgorithm>
            {
                CoreChecksumAlgorithm.SHA256,
                CoreChecksumAlgorithm.CRC32
            };
            var responseData = new WebResponseData();

            responseData.Headers = new Dictionary <string, string>
            {
                { "x-amz-checksum-sha256", "checksum" },
                { "x-amz-checksum-crc32", "checksum" }
            };

            // CRC32 should take priority over SHA256 when both are returned
            Assert.AreEqual(CoreChecksumAlgorithm.CRC32,
                            ChecksumUtils.SelectChecksumForResponseValidation(operationSupportedChecksums, responseData));
        }