-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,346 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) 2019 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// you can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
const crypto = require('crypto') | ||
const fs = require('fs') | ||
|
||
module.exports = function CalculateFileChecksum(filePath, algorithm = 'sha256') { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const checksumGenerator = crypto.createHash(algorithm); | ||
const fileStream = fs.createReadStream(filePath) | ||
fileStream.on('error', function (err) { | ||
err.message = `CalculateFileChecksum error in FileStream at path "${filePath}": ${err.message}` | ||
reject(err) | ||
}) | ||
checksumGenerator.once('readable', function () { | ||
const checksum = checksumGenerator.read().toString('hex') | ||
resolve(checksum) | ||
}) | ||
fileStream.pipe(checksumGenerator) | ||
} catch (err) { | ||
err.message = `CalculateFileChecksum error using algorithm "${algorithm}" at path "${filePath}": ${err.message}` | ||
reject(err); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const path = require('path') | ||
const fs = require('fs-extra') | ||
const os = require('os') | ||
const calculateFileChecksum = require('./calculateFileChecksum') | ||
|
||
const dirPrefixTmp = 'brave-browser-test-calculate-file-checksum-' | ||
const testFile1Name = 'file1' | ||
const testFile1InitialContent = 'this is a test' | ||
const encoding = 'utf8' | ||
let testDirPath, testFile1Path | ||
|
||
beforeEach(async function () { | ||
// Test directory | ||
testDirPath = await fs.mkdtemp(path.join(os.tmpdir(), dirPrefixTmp)) | ||
// Initial test file | ||
testFile1Path = path.join(testDirPath, testFile1Name) | ||
await fs.writeFile(testFile1Path, testFile1InitialContent, encoding) | ||
}) | ||
|
||
afterEach(async function () { | ||
// Remove test directory | ||
try { | ||
await fs.remove(testDirPath) | ||
} catch (err) { | ||
console.warn('Test cleanup: could not remove temp directory at ' + testDirPath) | ||
} | ||
}) | ||
|
||
test('generates a checksum', async function () { | ||
const checksum1 = await calculateFileChecksum(testFile1Path) | ||
expect(checksum1).toBeTruthy() | ||
}) | ||
|
||
test('checksum is stable', async function () { | ||
const checksum1 = await calculateFileChecksum(testFile1Path) | ||
const checksum2 = await calculateFileChecksum(testFile1Path) | ||
expect(checksum2).toBe(checksum1) | ||
}) | ||
|
||
test('checksum changes when file contents change', async function () { | ||
const checksum1 = await calculateFileChecksum(testFile1Path) | ||
await fs.writeFile(testFile1Path, testFile1InitialContent + testFile1InitialContent, encoding) | ||
const checksum2 = await calculateFileChecksum(testFile1Path) | ||
expect(checksum2).not.toBe(checksum1) | ||
}) |
Oops, something went wrong.