- 插件测试
- 参数说明
- 默认测试
- 插件耗时测试
- 自定义测试脚本
- 编写自定义测试脚本
- 编写自定义测试脚本
插件测试
参数说明
我们的插件开发工具 openraspjs 提供了插件测试命令,可以对插件进行代码检查、性能测试、检测能力测试。命令行参数如下,
$ rasp check -hUsage: rasp check <file>Options:-t, --test-dir <dir> specify a custom test directory-h, --help output usage information
默认测试
openraspjs 提供了一些默认的必须执行的测试脚本,包括代码检查和基础能力检测,更多细节请参看代码
$ rasp check plugin.js√ 插件代码检查 代码规范: 188ms√ 插件代码检查 模拟环境: 0ms√ 插件能力测试 sql 安全 DESC wp_users: 0ms√ 插件能力测试 sql 安全 select name, email from users where id = 1002: 0ms√ 插件能力测试 sql 不安全 select name, email from users where id = 1002 and 1=2 union select table_name, table_schema from information_schema.tables: 0ms5 passing (203ms)
当测试结果中出现 failing 和详细错误信息时,表示插件未通过检测
插件耗时测试
每一个测试用例所消耗的时间出现在测试结果行的最末端
若时间被标注为黄色,表示该测试用例消耗时间过长,若时间被标注为红色,表示该测试用例消耗时间超过最大限制。默认超时时间为 20ms
自定义测试脚本
通过 -t 选项指定自定义测试脚本目录,openraspjs 仅载入该目录下以 .test.js 结尾的测试脚本
编写自定义测试脚本
我们使用了测试框架 Mocha 来进行测试
我们在测试环境中增加了全局对象 RASP、CheckPoint*、Context 等对象,可直接使用
e.g 这是一个SQL注入的单元测试脚本,我们期望插件返回拦截操作
const expect = require('chai').expectdescribe('自定义测试', function () { describe('SQL注入测试', function () { it('union 注入', function () { var results = RASP.check('sql', { server: 'mysql', query: 'select name, email from users where id = 1002 and 1=2 union select table_name, table_schema from information_schema.tables' }, new Context()) expect(results).to.be.a('array') results.forEach(result => { expect(result).to.have.property('action').to.equal('block') expect(result).to.have.property('message').to.be.a('string') }) }) })})
原文: https://rasp.baidu.com/doc/dev/test/main.html
