Skip to content

Commit

Permalink
Add new test to check for defs regressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
lapo-luchini committed Apr 19, 2024
1 parent 8400fa7 commit fd1b6d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm test all
- run: npm run testdefs
if: matrix.node-version == 'latest'
- run: npm run lint
if: matrix.node-version == 'latest'
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
"homepage": "https://lapo.it/asn1js/",
"files": [ "asn1.js", "base64.js", "hex.js", "int10.js", "oids.js" ],
"scripts": {
"lint": "npx eslint asn1.js base64.js hex.js int10.js oids.js tags.js context.js index.js parseRFC.js dumpASN1.js",
"lint": "npx eslint asn1.js base64.js hex.js int10.js oids.js tags.js context.js index.js parseRFC.js dumpASN1.js test.js testDefs.js",
"lint-action": "npx @action-validator/cli .github/workflows/node.js.yml",
"serve": "echo 'Connect to http://localhost:3000/' ; npx statik --port 3000 .",
"test": "node test"
"test": "node test",
"testdefs": "node testDefs"
},
"engines": {
"node": ">=12.20.0"
Expand Down Expand Up @@ -81,6 +82,11 @@
"parserOptions": {
"ecmaVersion": 2021
}
}, {
"files": [ "testDefs.js" ],
"parserOptions": {
"ecmaVersion": 2022
}
}
]
}
Expand Down
32 changes: 32 additions & 0 deletions testDefs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node

import { promises as fs } from 'node:fs';
import { ASN1 } from './asn1.js';
import { Base64 } from './base64.js';
import { Defs } from './defs.js';

const tot = [];
for await (const file of await fs.opendir('examples')) {
let content = await fs.readFile('examples/' + file.name);
try {
try { // try PEM first
content = Base64.unarmor(content);
} catch (e) { // try DER/BER then
}
let result = ASN1.decode(content);
content = null;
const types = Defs.commonTypes
.map(type => {
const stats = Defs.match(result, type);
return { type, match: stats.recognized / stats.total };
})
.sort((a, b) => b.match - a.match);
tot.push([ types[0].match, file.name, types[0].type.description ]);
} catch (e) {
tot.push([ 0, file.name, e.message ]);
}
}
for (const f of tot)
console.log(f[0].toFixed(3) + '\t' + f[1] + '\t' + f[2]);
const avg = tot.map(f => f[0]).reduce((sum, val) => sum + val) / tot.length;
console.log('\x1B[1m\x1B[32m' + (avg * 100).toFixed(3) + '\x1B[39m\x1B[22m%\tAVERAGE');

0 comments on commit fd1b6d0

Please sign in to comment.