|
| 1 | +const assert = require('assert'); |
| 2 | +const async = require('async'); |
| 3 | +const { makeS3Request } = require('../utils/makeRequest'); |
| 4 | +const HttpRequestAuthV4 = require('../utils/HttpRequestAuthV4'); |
| 5 | + |
| 6 | +const bucket = 'testunsupportedchecksumsbucket'; |
| 7 | +const objectKey = 'key'; |
| 8 | +const objData = Buffer.alloc(1024, 'a'); |
| 9 | +// note this is not the correct checksum in objDataWithTrailingChecksum |
| 10 | +const objDataWithTrailingChecksum = '10\r\n0123456789abcdef\r\n0\r\nx-amz-checksum-crc64nvme:YeIDuLa7tU0=\r\n'; |
| 11 | +const objDataWithoutTrailingChecksum = '0123456789abcdef'; |
| 12 | + |
| 13 | +const authCredentials = { |
| 14 | + accessKey: 'accessKey1', |
| 15 | + secretKey: 'verySecretKey1', |
| 16 | +}; |
| 17 | + |
| 18 | +const itSkipIfAWS = process.env.AWS_ON_AIR ? it.skip : it; |
| 19 | + |
| 20 | +describe('trailing checksum requests:', () => { |
| 21 | + before(done => { |
| 22 | + makeS3Request({ |
| 23 | + method: 'PUT', |
| 24 | + authCredentials, |
| 25 | + bucket, |
| 26 | + }, err => { |
| 27 | + assert.ifError(err); |
| 28 | + done(); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + after(done => { |
| 33 | + async.series([ |
| 34 | + next => makeS3Request({ |
| 35 | + method: 'DELETE', |
| 36 | + authCredentials, |
| 37 | + bucket, |
| 38 | + objectKey, |
| 39 | + }, next), |
| 40 | + next => makeS3Request({ |
| 41 | + method: 'DELETE', |
| 42 | + authCredentials, |
| 43 | + bucket, |
| 44 | + }, next), |
| 45 | + ], err => { |
| 46 | + assert.ifError(err); |
| 47 | + done(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + const objDataChunkings = [ |
| 52 | + [objDataWithTrailingChecksum], |
| 53 | + [objDataWithTrailingChecksum.substring(0, 1), objDataWithTrailingChecksum.substring(1)], |
| 54 | + [objDataWithTrailingChecksum.substring(0, 2), objDataWithTrailingChecksum.substring(2)], |
| 55 | + [objDataWithTrailingChecksum.substring(0, 3), objDataWithTrailingChecksum.substring(3)], |
| 56 | + [objDataWithTrailingChecksum.substring(0, 4), objDataWithTrailingChecksum.substring(4)], |
| 57 | + [objDataWithTrailingChecksum.substring(0, 10), objDataWithTrailingChecksum.substring(10)], |
| 58 | + [objDataWithTrailingChecksum.substring(0, 20), objDataWithTrailingChecksum.substring(20)], |
| 59 | + [objDataWithTrailingChecksum.substring(0, 21), objDataWithTrailingChecksum.substring(21)], |
| 60 | + [objDataWithTrailingChecksum.substring(0, 22), objDataWithTrailingChecksum.substring(22)], |
| 61 | + ]; |
| 62 | + |
| 63 | + objDataChunkings.forEach((objDataChunks, index) => { |
| 64 | + it(`should accept unsigned trailing checksum, chunk configuration ${index + 1}`, done => { |
| 65 | + const req = new HttpRequestAuthV4( |
| 66 | + `http://localhost:8000/${bucket}/${objectKey}`, |
| 67 | + Object.assign( |
| 68 | + { |
| 69 | + method: 'PUT', |
| 70 | + headers: { |
| 71 | + 'content-length': objDataWithTrailingChecksum.length, |
| 72 | + 'x-amz-decoded-content-length': objDataWithoutTrailingChecksum.length, |
| 73 | + 'x-amz-content-sha256': 'STREAMING-UNSIGNED-PAYLOAD-TRAILER', |
| 74 | + 'x-amz-trailer': 'x-amz-checksum-crc64nvme', |
| 75 | + }, |
| 76 | + }, |
| 77 | + authCredentials |
| 78 | + ), |
| 79 | + res => { |
| 80 | + assert.strictEqual(res.statusCode, 200); |
| 81 | + res.on('data', () => {}); |
| 82 | + res.on('end', done); |
| 83 | + } |
| 84 | + ); |
| 85 | + |
| 86 | + req.on('error', err => { |
| 87 | + assert.ifError(err); |
| 88 | + }); |
| 89 | + |
| 90 | + // write the data in two chunks |
| 91 | + objDataChunks.forEach(chunk => { |
| 92 | + req.write(chunk); |
| 93 | + } |
| 94 | + ); |
| 95 | + |
| 96 | + req.once('drain', () => { |
| 97 | + req.end(); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should have correct object content for unsigned trailing checksum', done => { |
| 102 | + makeS3Request({ |
| 103 | + method: 'GET', |
| 104 | + authCredentials, |
| 105 | + bucket, |
| 106 | + objectKey, |
| 107 | + }, (err, res) => { |
| 108 | + assert.ifError(err); |
| 109 | + assert.strictEqual(res.statusCode, 200); |
| 110 | + // check that the object data is the input stripped of the trailing checksum |
| 111 | + assert.strictEqual(res.body, objDataWithoutTrailingChecksum); |
| 112 | + return done(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + itSkipIfAWS('should respond with BadRequest for signed trailing checksum', done => { |
| 118 | + const req = new HttpRequestAuthV4( |
| 119 | + `http://localhost:8000/${bucket}/${objectKey}`, |
| 120 | + Object.assign( |
| 121 | + { |
| 122 | + method: 'PUT', |
| 123 | + headers: { |
| 124 | + 'content-length': objData.length, |
| 125 | + 'x-amz-content-sha256': 'STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER', |
| 126 | + 'x-amz-trailer': 'x-amz-checksum-sha256', |
| 127 | + }, |
| 128 | + }, |
| 129 | + authCredentials |
| 130 | + ), |
| 131 | + res => { |
| 132 | + assert.strictEqual(res.statusCode, 400); |
| 133 | + res.on('data', () => {}); |
| 134 | + res.on('end', done); |
| 135 | + } |
| 136 | + ); |
| 137 | + |
| 138 | + req.on('error', err => { |
| 139 | + assert.ifError(err); |
| 140 | + }); |
| 141 | + |
| 142 | + req.write(objData); |
| 143 | + |
| 144 | + req.once('drain', () => { |
| 145 | + req.end(); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments