Skip to content

Commit

Permalink
fix(MongoBinaryDownload::makeMD5check): actually hash the content of …
Browse files Browse the repository at this point in the history
…the file, not the path
  • Loading branch information
hasezoey committed May 8, 2023
1 parent 0e55931 commit c71dbf3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Expand Up @@ -10,7 +10,7 @@ import MongoBinaryDownloadUrl from './MongoBinaryDownloadUrl';
import { HttpsProxyAgent } from 'https-proxy-agent';
import resolveConfig, { envToBool, ResolveConfigVariables } from './resolveConfig';
import debug from 'debug';
import { assertion, mkdir, pathExists, md5 } from './utils';
import { assertion, mkdir, pathExists, md5FromFile } from './utils';
import { DryMongoBinary } from './DryMongoBinary';
import { MongoBinaryOpts } from './MongoBinary';
import { clearLine } from 'readline';
Expand Down Expand Up @@ -156,7 +156,7 @@ export class MongoBinaryDownload {
const signatureContent = (await fspromises.readFile(archiveMD5Path)).toString('utf-8');
const regexMatch = signatureContent.match(/^\s*([\w\d]+)\s*/i);
const md5SigRemote = regexMatch ? regexMatch[1] : null;
const md5SigLocal = md5(mongoDBArchive);
const md5SigLocal = await md5FromFile(mongoDBArchive);
log(`makeMD5check: Local MD5: ${md5SigLocal}, Remote MD5: ${md5SigRemote}`);

if (md5SigRemote !== md5SigLocal) {
Expand Down
Expand Up @@ -130,7 +130,7 @@ describe('MongoBinaryDownload', () => {
const fileWithReferenceMd5 = '/another/path';

jest.spyOn(fspromises, 'readFile').mockResolvedValueOnce(`${someMd5} fileName`);
jest.spyOn(utils, 'md5').mockImplementationOnce(() => someMd5);
jest.spyOn(utils, 'md5FromFile').mockResolvedValueOnce(someMd5);
jest.spyOn(fspromises, 'unlink').mockResolvedValue(void 0);

const du = new MongoBinaryDownload({ downloadDir: '/', checkMD5: true });
Expand All @@ -142,12 +142,12 @@ describe('MongoBinaryDownload', () => {
expect(du.download).toBeCalledWith(urlToMongoDBArchivePath);
expect(fspromises.readFile).toBeCalledWith(fileWithReferenceMd5);
expect(fspromises.unlink).toBeCalledTimes(1);
expect(utils.md5).toBeCalledWith(mongoDBArchivePath);
expect(utils.md5FromFile).toBeCalledWith(mongoDBArchivePath);
});

it('makeMD5check throws an error if md5 of downloaded mongoDBArchive is NOT the same as in the reference result', async () => {
jest.spyOn(fspromises, 'readFile').mockResolvedValueOnce(`someMD5 fileName`);
jest.spyOn(utils, 'md5').mockImplementationOnce(() => 'anotherMD5');
jest.spyOn(utils, 'md5FromFile').mockResolvedValueOnce('anotherMD5');

const du = new MongoBinaryDownload({ downloadDir: '/', checkMD5: true });
jest.spyOn(du, 'download').mockResolvedValue('');
Expand All @@ -163,7 +163,7 @@ describe('MongoBinaryDownload', () => {

it('false value of checkMD5 attribute disables makeMD5check validation', async () => {
jest.spyOn(fspromises, 'readFile').mockResolvedValueOnce(`someMD5 fileName`);
jest.spyOn(utils, 'md5').mockImplementationOnce(() => 'anotherMD5');
jest.spyOn(utils, 'md5FromFile').mockResolvedValueOnce('anotherMD5');

const du = new MongoBinaryDownload({ downloadDir: '/', checkMD5: false });
const result = await du.makeMD5check('', '');
Expand Down
9 changes: 9 additions & 0 deletions packages/mongodb-memory-server-core/src/util/utils.ts
Expand Up @@ -370,3 +370,12 @@ export function uuidv4(): string {
export function md5(content: BinaryLike): string {
return createHash('md5').update(content).digest('hex');
}

/**
* Helper function to have md5 generation and definition in one place for a file
* @param file the location of a file to read for a hash
* @returns a md5 of the input file
*/
export async function md5FromFile(file: string): Promise<string> {
return md5(await fspromises.readFile(file));
}

0 comments on commit c71dbf3

Please sign in to comment.