|
| 1 | +const assert = require('assert'); |
| 2 | +const HttpRequestAuthV4 = require('../utils/HttpRequestAuthV4'); |
| 3 | + |
| 4 | +const bucket = 'xxx'; |
| 5 | +const objectKey = 'key'; |
| 6 | +const objData = Buffer.alloc(1, 'a'); |
| 7 | + |
| 8 | +const authCredentials = { |
| 9 | + accessKey: 'accessKey1', |
| 10 | + secretKey: 'verySecretKey1', |
| 11 | +}; |
| 12 | + |
| 13 | +const itSkipIfAWS = process.env.AWS_ON_AIR ? it.skip : it; |
| 14 | + |
| 15 | +describe('Test x-amz-checksums', () => { |
| 16 | + const algos = [ |
| 17 | + { name: 'CRC32', objDataDigest: '6Le+Qw==' }, |
| 18 | + { name: 'CRC32C', objDataDigest: 'wdBDMA==' }, |
| 19 | + { name: 'CRC64NVME', objDataDigest: 'jC+ERbTL/Dw=' }, |
| 20 | + { name: 'SHA1', objDataDigest: 'hvfkN/qlp/zhXR3cuerq6jd2Z7g=' }, |
| 21 | + { name: 'SHA256', objDataDigest: 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs=' }, |
| 22 | + ]; |
| 23 | + const methods = [ |
| 24 | + { |
| 25 | + Name: 'CompleteMultipartupload', |
| 26 | + Query: 'uploadId=77a4ce46b9bf4ea69d9e0cc3f0bb1aae', |
| 27 | + Key: objectKey, |
| 28 | + HTTPMethod: 'POST', |
| 29 | + }, |
| 30 | + { |
| 31 | + Name: 'DeleteObjects', |
| 32 | + Query: 'delete', |
| 33 | + Key: '', |
| 34 | + HTTPMethod: 'POST', |
| 35 | + }, |
| 36 | + { |
| 37 | + Name: 'PutBucketACL', |
| 38 | + Query: 'acl', |
| 39 | + Key: '', |
| 40 | + HTTPMethod: 'PUT', |
| 41 | + }, |
| 42 | + { |
| 43 | + Name: 'PutBucketACL', |
| 44 | + Query: 'cors', |
| 45 | + Key: '', |
| 46 | + HTTPMethod: 'PUT', |
| 47 | + }, |
| 48 | + { |
| 49 | + Name: 'PutBucketEncryption', |
| 50 | + Query: 'encryption', |
| 51 | + Key: '', |
| 52 | + HTTPMethod: 'PUT', |
| 53 | + }, |
| 54 | + { |
| 55 | + Name: 'PutBucketLifecycke', |
| 56 | + Query: 'lifecycle', |
| 57 | + Key: '', |
| 58 | + HTTPMethod: 'PUT', |
| 59 | + }, |
| 60 | + { |
| 61 | + Name: 'PutBucketLogging', |
| 62 | + Query: 'logging', |
| 63 | + Key: '', |
| 64 | + HTTPMethod: 'PUT', |
| 65 | + }, |
| 66 | + { |
| 67 | + Name: 'PutBucketNotification', |
| 68 | + Query: 'notification', |
| 69 | + Key: '', |
| 70 | + HTTPMethod: 'PUT', |
| 71 | + }, |
| 72 | + { |
| 73 | + Name: 'PutBucketPolicy', |
| 74 | + Query: 'policy', |
| 75 | + Key: '', |
| 76 | + HTTPMethod: 'PUT', |
| 77 | + }, |
| 78 | + { |
| 79 | + Name: 'PutBucketReplication', |
| 80 | + Query: 'replication', |
| 81 | + Key: '', |
| 82 | + HTTPMethod: 'PUT', |
| 83 | + }, |
| 84 | + { |
| 85 | + Name: 'PutBucketTagging', |
| 86 | + Query: 'tagging', |
| 87 | + Key: '', |
| 88 | + HTTPMethod: 'PUT', |
| 89 | + }, |
| 90 | + { |
| 91 | + Name: 'PutBucketVersioning', |
| 92 | + Query: 'versioning', |
| 93 | + Key: '', |
| 94 | + HTTPMethod: 'PUT', |
| 95 | + }, |
| 96 | + { |
| 97 | + Name: 'PutBucketWebsite', |
| 98 | + Query: 'website', |
| 99 | + Key: '', |
| 100 | + HTTPMethod: 'PUT', |
| 101 | + }, |
| 102 | + { |
| 103 | + Name: 'PutObjectACL', |
| 104 | + Query: 'acl', |
| 105 | + Key: objectKey, |
| 106 | + HTTPMethod: 'PUT', |
| 107 | + }, |
| 108 | + { |
| 109 | + Name: 'PutObjectLegalHold', |
| 110 | + Query: 'legal-hold', |
| 111 | + Key: objectKey, |
| 112 | + HTTPMethod: 'PUT', |
| 113 | + }, |
| 114 | + { |
| 115 | + Name: 'PutObjectLockConfiguration', |
| 116 | + Query: 'object-lock', |
| 117 | + Key: '', |
| 118 | + HTTPMethod: 'PUT', |
| 119 | + }, |
| 120 | + { |
| 121 | + Name: 'PutObjectRetention', |
| 122 | + Query: 'retention', |
| 123 | + Key: objectKey, |
| 124 | + HTTPMethod: 'PUT', |
| 125 | + }, |
| 126 | + { |
| 127 | + Name: 'PutObjectTagging', |
| 128 | + Query: 'tagging', |
| 129 | + Key: objectKey, |
| 130 | + HTTPMethod: 'PUT', |
| 131 | + }, |
| 132 | + { |
| 133 | + Name: 'RestoreObject', |
| 134 | + Query: 'restore', |
| 135 | + Key: objectKey, |
| 136 | + HTTPMethod: 'POST', |
| 137 | + }, |
| 138 | + ]; |
| 139 | + |
| 140 | + // TODO: |
| 141 | + // - [ ] <Error><Code>InvalidRequest</Code><Message>Expecting a single x-amz-checksum- header. |
| 142 | + // Multiple checksum Types are not allowed.</Message> |
| 143 | + // - [ ] <Error><Code>InvalidRequest</Code><Message>Value for x-amz-sdk-checksum-algorithm header is invalid. |
| 144 | + // - [ ] <Error><Code>InvalidRequest</Code><Message>x-amz-sdk-checksum-algorithm specified, but no corresponding |
| 145 | + // x-amz-checksum-* or x-amz-trailer headers were found. |
| 146 | + // - [ ] <Error><Code>BadDigest</Code><Message>The SHA1 you specified did not match the calculated checksum. |
| 147 | + // - [ ] <Error><Code>InvalidRequest</Code><Message>Value for x-amz-checksum-sha1 header is invalid. |
| 148 | + // - [ ] <Error><Code>InvalidRequest</Code><Message>The algorithm type you specified in x-amz-checksum- header is |
| 149 | + // invalid. |
| 150 | + |
| 151 | + for (const algo of algos) { |
| 152 | + for (const method of methods) { |
| 153 | + itSkipIfAWS( |
| 154 | + `${method.Name} should respond BadRequest ` + |
| 155 | + `with x-amz-sdk-checksum-algorithm and invalid x-amz-checksum-${algo.name.toLowerCase()}`, done => { |
| 156 | + const url = `http://localhost:8000/${bucket}/${method.Key}?${method.Query}`; |
| 157 | + const req = new HttpRequestAuthV4( |
| 158 | + url, |
| 159 | + Object.assign( |
| 160 | + { |
| 161 | + method: method.HTTPMethod, |
| 162 | + headers: { |
| 163 | + 'x-amz-content-sha256': 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs=', |
| 164 | + 'content-length': objData.length, |
| 165 | + 'x-amz-sdk-checksum-algorithm': algo.name, |
| 166 | + [`x-amz-checksum-${algo.name.toLowerCase()}`]: 'xxxxxxxxxxxxxxxxxxxxx', |
| 167 | + }, |
| 168 | + }, |
| 169 | + authCredentials |
| 170 | + ), |
| 171 | + res => { |
| 172 | + let data = ''; |
| 173 | + res.on('data', chunk => { |
| 174 | + data += chunk; |
| 175 | + }); |
| 176 | + res.on('end', () => { |
| 177 | + // console.log(data); |
| 178 | + assert.strictEqual(res.statusCode, 400); |
| 179 | + assert(data.includes('BadDigest')); |
| 180 | + done(); |
| 181 | + }); |
| 182 | + } |
| 183 | + ); |
| 184 | + |
| 185 | + req.on('error', err => { |
| 186 | + assert.ifError(err); |
| 187 | + }); |
| 188 | + |
| 189 | + |
| 190 | + req.once('drain', () => { |
| 191 | + req.end(); |
| 192 | + }); |
| 193 | + |
| 194 | + req.write(objData, err => { |
| 195 | + assert.ifError(err); |
| 196 | + req.end(); |
| 197 | + }); |
| 198 | + }); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + for (const algo of algos) { |
| 203 | + for (const method of methods) { |
| 204 | + itSkipIfAWS( |
| 205 | + `${method.Name} should not respond BadDigest if ` + |
| 206 | + `x-amz-checksum-${algo.name.toLowerCase()} is correct`, done => { |
| 207 | + const url = `http://localhost:8000/${bucket}/${method.Key}?${method.Query}`; |
| 208 | + const req = new HttpRequestAuthV4( |
| 209 | + url, |
| 210 | + Object.assign( |
| 211 | + { |
| 212 | + method: method.HTTPMethod, |
| 213 | + headers: { |
| 214 | + 'x-amz-content-sha256': 'ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs=', |
| 215 | + 'content-length': objData.length, |
| 216 | + 'x-amz-sdk-checksum-algorithm': algo.name, |
| 217 | + [`x-amz-checksum-${algo.name.toLowerCase()}`]: algo.objDataDigest, |
| 218 | + }, |
| 219 | + }, |
| 220 | + authCredentials |
| 221 | + ), |
| 222 | + res => { |
| 223 | + let data = ''; |
| 224 | + res.on('data', chunk => { |
| 225 | + data += chunk; |
| 226 | + }); |
| 227 | + res.on('end', () => { |
| 228 | + // console.log(data); |
| 229 | + assert(!data.includes('BadDigest')); |
| 230 | + done(); |
| 231 | + }); |
| 232 | + } |
| 233 | + ); |
| 234 | + |
| 235 | + req.on('error', err => { |
| 236 | + assert.ifError(err); |
| 237 | + }); |
| 238 | + |
| 239 | + |
| 240 | + req.once('drain', () => { |
| 241 | + req.end(); |
| 242 | + }); |
| 243 | + |
| 244 | + req.write(objData, err => { |
| 245 | + assert.ifError(err); |
| 246 | + req.end(); |
| 247 | + }); |
| 248 | + }); |
| 249 | + } |
| 250 | + } |
| 251 | +}); |
0 commit comments