mongo/jstests/core/filemd5.js
Spencer Jackson 05009bb976 SERVER-119317 Improve object lifecycle of MD5 hash state (#47996)
GitOrigin-RevId: 28927c60881a488fcbc5fd4d925b410f33258827
2026-03-17 21:21:14 +00:00

44 lines
1.7 KiB
JavaScript

// Tests the basics of the filemd5 command.
// @tags: [
// # Cannot implicitly shard accessed collections because of following error: GridFS fs.chunks
// # collection must be sharded on either {files_id:1} or {files_id:1, n:1}
// assumes_unsharded_collection,
//
// ]
db.fs.chunks.drop();
assert.commandWorked(db.fs.chunks.insert({files_id: 1, n: 0, data: new BinData(0, "test")}));
assert.commandFailedWithCode(db.runCommand({filemd5: 1, root: "fs"}),
ErrorCodes.NoQueryExecutionPlans);
db.fs.chunks.createIndex({files_id: 1, n: 1});
assert.commandWorked(db.runCommand({filemd5: 1, root: "fs"}));
assert.commandFailedWithCode(db.runCommand({filemd5: 1, root: "fs", partialOk: 1, md5state: 5}),
50847);
assert.commandWorked(db.fs.chunks.insert({files_id: 2, n: 0}));
assert.commandFailedWithCode(db.runCommand({filemd5: 2, root: "fs"}), 50848);
assert.commandWorked(db.fs.chunks.update({files_id: 2, n: 0}, {$set: {data: 5}}));
assert.commandFailedWithCode(db.runCommand({filemd5: 2, root: "fs"}), 50849);
{
const result = assert.commandWorked(
db.runCommand({
filemd5: ObjectId("000000000000000000000000"),
root: "fs",
partialOk: true,
}),
);
assert(result.md5state !== undefined, "Expected md5state in filemd5 response with partialOk: true");
const hex = result.md5state.hex();
const totalBytes = hex.length / 2;
assert.gt(totalBytes, 72, "md5state expected to be larger than 72 bytes");
const checkFromByte = 72;
const paddingHex = hex.substring(checkFromByte * 2);
const allZeros = "0".repeat(paddingHex.length);
assert.eq(paddingHex, allZeros, "md5state expected to be zero padded");
}