From d85b51c4331af4904278801fef1001810937f7da Mon Sep 17 00:00:00 2001 From: Xavier Abad <77491413+xabg2@users.noreply.github.com> Date: Wed, 27 Nov 2024 11:22:57 +0100 Subject: [PATCH] feat: tests for scanDIr file callback --- tests/index.js | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/tests/index.js b/tests/index.js index 0f761bd..8d422ff 100755 --- a/tests/index.js +++ b/tests/index.js @@ -1393,6 +1393,90 @@ describe('scanDir', () => { }).timeout(15000); // TODO: Write tests for file_callback + describe('scanDir with fileCb', () => { + it('should call fileCb for each file when scanRecursively is true', async () => { + let fileCbCallCount = 0; + const fileCb = (err, file, isInfected, viruses) => { + fileCbCallCount += 1; + expect(err).to.be.null; + expect(file).to.be.a('string'); + expect(isInfected).to.be.a('boolean'); + expect(viruses).to.be.an('array'); + }; + + clamscan.settings.scanRecursively = true; + + const { goodFiles, badFiles, viruses } = await clamscan.scanDir(goodScanDir, null, fileCb); + + expect(fileCbCallCount).to.equal(goodFiles.length + badFiles.length); + expect(goodFiles).to.be.an('array').that.is.not.empty; + expect(badFiles).to.be.an('array').that.is.empty; + expect(viruses).to.be.an('array').that.is.empty; + }); + + it('should work with fileCb and detect bad files correctly', (done) => { + eicarGen.writeFile(); + + let fileCbCallCount = 0; + const fileCb = (err, file, isInfected, viruses) => { + fileCbCallCount += 1; + expect(file).to.be.a('string'); + expect(isInfected).to.be.a('boolean'); + if (isInfected) { + expect(viruses).to.include.members(['EICAR_TEST_FILE']); + } + }; + + clamscan.settings.scanRecursively = true; + + clamscan.scanDir( + badScanDir, + (err, goodFiles, badFiles, viruses) => { + check(done, () => { + expect(err).to.be.null; + expect(goodFiles).to.be.an('array').that.is.empty; + expect(badFiles).to.be.an('array').that.includes(badScanFile); + expect(viruses).to.include.members(['EICAR_TEST_FILE']); + + expect(fileCbCallCount).to.equal(goodFiles.length + badFiles.length); + if (fs.existsSync(badScanFile)) fs.unlinkSync(badScanFile); + }); + }, + fileCb + ); + }); + + it('should handle errors in fileCb gracefully', async () => { + const fileCb = (err, file, isInfected, viruses) => { + if (file === badScanFile) { + throw new Error('Intentional error in fileCb'); + } + }; + + eicarGen.writeFile(); + + clamscan.settings.scanRecursively = true; + + try { + await clamscan.scanDir(badScanDir, null, fileCb); + } catch (err) { + expect(err).to.be.instanceOf(Error); + expect(err.message).to.equal('Intentional error in fileCb'); + } finally { + if (fs.existsSync(badScanFile)) fs.unlinkSync(badScanFile); + } + }); + + it('should not fail if fileCb is not provided', async () => { + clamscan.settings.scanRecursively = true; + + const { goodFiles, badFiles, viruses } = await clamscan.scanDir(goodScanDir); + + expect(goodFiles).to.be.an('array').that.is.not.empty; + expect(badFiles).to.be.an('array').that.is.empty; + expect(viruses).to.be.an('array').that.is.empty; + }); + }); describe('edge cases', () => { it('should work when falling back to local scan and there is a file with consecutive spaces in it', async () => {