Skip to content

Commit

Permalink
Add js fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
invernizzi committed Mar 25, 2024
1 parent c68c7ac commit 28e16a5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@types/jest": "^29.5.12",
"@types/node": "^20.11.25",
"documentation": "^14.0.3",
"fast-check": "^3.17.0",
"jest": "^29.5.0",
"ts-jest": "^29.0.5",
"typescript": "^5.0.2"
Expand Down
23 changes: 22 additions & 1 deletion js/test/magika.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {Readable} from 'stream';
import {finished} from 'stream/promises';
import {ReadableStream} from 'stream/web';
import {MagikaNode as Magika} from '../magika_node';
import * as fc from 'fast-check';



describe('Magika class', () => {

Expand Down Expand Up @@ -71,6 +74,24 @@ describe('Magika class', () => {
expect(Object.values(TfnMock.accessed).reduce((a, b) => a + b, 0)).toBe(1);
});


it('scores should be in the expected range', async () => {
const magika = new Magika();
await magika.load();
fc.assert(
fc.asyncProperty(
fc.array(
fc.integer({min:0, max:255}),
{minLength:0, maxLength:10})
,
async (bytesContent) => {
const output = await magika.identifyBytes(Uint8Array.from(bytesContent));
expect(output.score).toBeGreaterThanOrEqual(0);
expect(output.score).toBeLessThanOrEqual(1);
}
))
});

it('features should result in known value', async () => {
const magika = new Magika();
await magika.load({configPath: workdir.config, modelPath: workdir.model});
Expand All @@ -94,7 +115,7 @@ describe('Magika class', () => {
110, 59, 97, 91, 48, 93, 105, 110, 32, 99, 124, 124, 34, 117, 110, 100, 101, 102, 105, 110, 101, 100, 34, 61, 61, 116, 121, 112, 101, 111,
102, 32, 99, 46, 101, 120, 101, 99, 83, 99, 114, 105, 112, 116, 124, 124, 99, 46, 101, 120, 101, 99, 83, 99, 114, 105, 112, 116, 40, 34,
118, 97, 114, 32, 34, 43, 97, 91, 48, 93, 41, 59, 102, 111, 114, 40, 118, 97, 114, 32, 100, 59, 97, 46, 108, 101, 110, 103, 116, 104,
38, 38, 40, 100, 61, 97, 46, 115, 104, 105, 102, 116, 40, 41, 41, 59, 41, 97, 46, 108, 101, 110, 103, 116, 104, 124, 124, 118, 111, 105,
38, 38, 40, 100, 61, 97, 46, 115, 104, 105, 102, 116, 40, 41, 41, 59, 41, 97, 46, 108, 101, 110, 103, 116, 104, 124, 124, 118, 111, 105,
100, 32, 48, 61, 61, 61, 98, 63, 99, 61, 99, 91, 100, 93, 38, 38, 99, 91, 100, 93, 33, 61, 61, 79, 98, 106, 101, 99, 116, 46,
112, 114, 111, 116, 111, 116, 121, 112, 101, 91, 100, 93, 63, 99, 91, 100, 93, 58, 99, 91, 100, 93, 61, 123, 125, 58, 99, 91, 100, 93,
61, 98, 125, 59, 102, 117, 110, 99, 116, 105, 111, 110, 32, 113, 40, 41, 123, 102, 111, 114, 40, 118, 97, 114, 32, 97, 61, 114, 44, 98,
Expand Down
12 changes: 12 additions & 0 deletions js/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,13 @@ extend@^3.0.0:
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa"
integrity sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==

fast-check@^3.17.0:
version "3.17.0"
resolved "https://registry.yarnpkg.com/fast-check/-/fast-check-3.17.0.tgz#7c0ff6a0e0a0bdc99c8b93d42fa066429c7bd437"
integrity sha512-9A7UiBENXxMBb40qY+xOcIcGdxF5CYbXJOU4b+omh773RIFcIQRejHEjjrzIHQFLIQXWgWQu3dl6FrWR9VpYRQ==
dependencies:
pure-rand "^6.1.0"

[email protected], fast-json-stable-stringify@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
Expand Down Expand Up @@ -3425,6 +3432,11 @@ pure-rand@^6.0.0:
resolved "https://registry.yarnpkg.com/pure-rand/-/pure-rand-6.0.4.tgz#50b737f6a925468679bff00ad20eade53f37d5c7"
integrity sha512-LA0Y9kxMYv47GIPJy6MI84fqTd2HmYZI83W/kM/SkKfDlajnZYfmXFTxkbY+xSBPkLJxltMa9hIkmdc29eguMA==

pure-rand@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/pure-rand/-/pure-rand-6.1.0.tgz#d173cf23258231976ccbdb05247c9787957604f2"
integrity sha512-bVWawvoZoBYpp6yIoQtQXHZjmz35RSVHnUOTefl8Vcjr8snTPY1wnpSPMWekcFwbxI6gtmT7rSYPFvz71ldiOA==

react-is@^18.0.0:
version "18.2.0"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.2.0.tgz#199431eeaaa2e09f86427efbb4f1473edb47609b"
Expand Down

0 comments on commit 28e16a5

Please sign in to comment.