Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipto Pandit (dipandit) committed Jan 28, 2021
1 parent 1d30d58 commit f99f351
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 67 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ Snort3 Test Explorer is a visual studio code extension that lets you run snort3
## Setup

* install the [Snort3 Test Explorer](https://marketplace.visualstudio.com/items?itemName=diptopandit.snort3-test-adapter) extension
* open extension settings
* set sf_prefix_snort3 to snort install path
* set snort_srcpath to snort source code path
* set dependencies to dependencies (libdaq, abcip, cpputest etc.) installation path
* make sure the [Snort3 Build Tools](https://marketplace.visualstudio.com/items?itemName=diptopandit.snort3-build-tools) extention is installed
* configure the [Snort3 Build Tools](https://marketplace.visualstudio.com/items?itemName=diptopandit.snort3-build-tools) extention properly
* open an workspace with snort3 and snort3_test folder in it
* configure and build snort3 for regtest using [Snort3 Build Tools](https://marketplace.visualstudio.com/items?itemName=diptopandit.snort3-build-tools)

You should now see a tree view of all the tests in the side panel:

![The fake example test suite](img/fake-tests.png)
![The snort3 test suite](https://raw.githubusercontent.com/diptopandit/vscode-snort3-test-adapter/master/img/fake-tests.png)

Click on the run test button to start the tests
25 changes: 3 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"icon": "img/snort_test_logo.png",
"author": "Dipto Pandit <[email protected]>",
"publisher": "diptopandit",
"version": "0.0.1",
"version": "1.0.0",
"license": "MIT",
"homepage": "https://github.com/diptopandit/vscode-snort3-test-adapter",
"repository": {
Expand Down Expand Up @@ -49,7 +49,8 @@
"vscode": "^1.23.0"
},
"extensionDependencies": [
"hbenl.vscode-test-explorer"
"hbenl.vscode-test-explorer",
"diptopandit.snort3-build-tools"
],
"activationEvents": [
"workspaceContains:bin/snorttest.py"
Expand All @@ -69,26 +70,6 @@
"description": "write diagnostic logs to the given file",
"type": "string",
"scope": "resource"
},
"snort3TestExplorer.sf_prefix_snort3": {
"description": "absolute path to the snort install directory",
"type": "string",
"scope": "resource"
},
"snort3TestExplorer.snort_srcpath": {
"description": "absolute path to the snort source directory",
"type": "string",
"scope": "resource"
},
"snort3TestExplorer.dependencies": {
"description": "absolute path to the dependencies directory",
"type": "string",
"scope": "resource"
},
"snort3TestExplorer.concurrency": {
"description": "number of concurrent test runs (defaults to number of processors)",
"type": "integer",
"scope": "resource"
}
}
}
Expand Down
Binary file not shown.
59 changes: 25 additions & 34 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import * as fs from 'fs';
import { TestSuiteInfo, TestInfo, TestAdapter, TestLoadStartedEvent, TestLoadFinishedEvent, TestRunStartedEvent, TestRunFinishedEvent, TestSuiteEvent, TestEvent, RetireEvent } from 'vscode-test-adapter-api';
import { Log } from 'vscode-test-adapter-util';
import { loadSnort3Tests, snort3Test, runTest } from './snort3Test';
import {myStatusBarItem} from './main';
import {myStatusBarItem, buildtool} from './main';
import * as path from 'path';
//import PromisePool from 'es6-promise-pool';
import {cpus} from 'os';

class jobQueue {
private jobdata = new Array<TestInfo|TestSuiteInfo>();
Expand Down Expand Up @@ -57,7 +56,6 @@ export class Snort3TestAdapter implements TestAdapter {
private readonly testStatesEmitter = new vscode.EventEmitter<TestRunStartedEvent | TestRunFinishedEvent | TestSuiteEvent | TestEvent>();
private readonly autorunEmitter = new vscode.EventEmitter<void>();
private readonly retireEmitter = new vscode.EventEmitter<RetireEvent>();
private concurrency:number = cpus().length;
private isTestReady:boolean = false;

get tests(): vscode.Event<TestLoadStartedEvent | TestLoadFinishedEvent> { return this.testsEmitter.event; }
Expand All @@ -81,20 +79,9 @@ export class Snort3TestAdapter implements TestAdapter {
.onDidChange((e)=>{ this.handleFileChange(e); });
const watcher2=vscode.workspace.createFileSystemWatcher('**/*expected*',true,false,true)
.onDidChange((e)=>{ this.handleFileChange(e); });
const watcher3=vscode.workspace.onDidChangeConfiguration((change)=>{
if(change.affectsConfiguration('snort3TestExplorer.sf_prefix_snort3')){
this.isTestReady=this.validate_config();
this.load();
}
if(change.affectsConfiguration('snort3TestExplorer.concurrency')){
let newVal = <number>(vscode.workspace.getConfiguration('snort3TestExplorer').get('concurrency'));
if(newVal) this.concurrency=newVal;
}
});

this.disposables.push(watcher1);
this.disposables.push(watcher2);
this.disposables.push(watcher3);
this.isTestReady = this.validate_config();
} else {
this.dispose();
Expand All @@ -110,22 +97,20 @@ export class Snort3TestAdapter implements TestAdapter {
}
}
private validate_config():boolean{
let config = vscode.workspace.getConfiguration('snort3TestExplorer');
let snort_binary = config.get('sf_prefix_snort3')+'/bin/snort';
//let config = vscode.workspace.getConfiguration('snort3TestExplorer');
let snort_binary = buildtool.get_sf_prefix_snort3()+'/bin/snort';
try{
fs.accessSync(this.workspace.uri.path + '/bin/snorttest.py', fs.constants.R_OK);
fs.accessSync(snort_binary, fs.constants.R_OK);
} catch(e)
{
this.log.warn(this.workspace.uri.path+": "+e);
if(e.message.search(snort_binary))
vscode.window.showWarningMessage("Snort binary missing. \
Make sure sf_prefix_snort3 setting is correct and snort binary is present in that path.");
return false;
}
//don't care if this fails due to unavailable file handle
try{
fs.watch(config.get('sf_prefix_snort3')+'/bin/snort',(event)=>{
fs.watch(buildtool.get_sf_prefix_snort3()+'/bin/snort',(event)=>{
if(event == 'change') this.retireEmitter.fire({});
});
} finally {
Expand Down Expand Up @@ -160,17 +145,19 @@ export class Snort3TestAdapter implements TestAdapter {
}

async load(): Promise<void> {
if(!this.isTestReady){
if(!this.validate_config()){
this.isTestReady = false;
this.testsEmitter.fire((<TestLoadFinishedEvent>{ type: 'finished' }));
return Promise.resolve();
return ;
}
this.isTestReady = true;
if(this.loading)
return Promise.resolve();
return;

return new Promise((resolve)=>{
this.loading = true;
this.log.info(this.workspace.uri.path+': Loading snort3 tests...');
myStatusBarItem.text=`$(beaker) $(sync~spin)Loading...`;
myStatusBarItem.text=`$(sync~spin)`;
this.testsEmitter.fire(<TestLoadStartedEvent>{ type: 'started' });
loadSnort3Tests(this.workspace).then((value)=>{
this.log.info(this.workspace.uri.path+': Tests loaded.')
Expand All @@ -180,7 +167,7 @@ export class Snort3TestAdapter implements TestAdapter {
this.log.info(this.workspace.uri.path+': '+err);
this.testsEmitter.fire((<TestLoadFinishedEvent>{ type: 'finished' }));
}).finally(()=>{
myStatusBarItem.text=`$(beaker) Snort3 Tests`;
myStatusBarItem.text=`$(beaker)`;
this.loading = false;
this.retireEmitter.fire({});
resolve();
Expand All @@ -189,6 +176,10 @@ export class Snort3TestAdapter implements TestAdapter {
}

async run(tests: string[]): Promise<void> {
if(!this.validate_config()){
this.isTestReady = false;
return;
}
if(this.cancelling) {
this.log.info("Can't schedule now, wait till cancelling is done.");
return;
Expand All @@ -201,9 +192,9 @@ export class Snort3TestAdapter implements TestAdapter {
}
if(this.running) return;
this.running = true;
var PromisePool = require('es6-promise-pool');
const PromisePool = require('es6-promise-pool');
const self = this;
var testJobProducer = function () {
const testJobProducer = function () {
const node = self.currentJobQ.next();
if(node)
{
Expand All @@ -212,14 +203,14 @@ export class Snort3TestAdapter implements TestAdapter {
}
else return;
}
var test_pool = new PromisePool(testJobProducer, this.concurrency);
myStatusBarItem.text=`$(beaker) $(sync~spin)Running...`;
await test_pool.start();
myStatusBarItem.text=`$(beaker) Snort3 Tests`;
this.running=false;
this.cancelling = false;
this.testStatesEmitter.fire(<TestRunFinishedEvent>{ type: 'finished' });
var test_pool = new PromisePool(testJobProducer, buildtool.get_concurrency());
myStatusBarItem.text=`$(beaker~spin)`;
test_pool.start().then(()=>{
myStatusBarItem.text=`$(beaker)`;
this.running=false;
this.cancelling = false;
this.testStatesEmitter.fire(<TestRunFinishedEvent>{ type: 'finished' });
});
return;
}

Expand Down
10 changes: 7 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TestHub, testExplorerExtensionId } from 'vscode-test-adapter-api';
import { Log, TestAdapterRegistrar } from 'vscode-test-adapter-util';
import { Snort3TestAdapter} from './adapter';
export var myStatusBarItem: vscode.StatusBarItem;
export var buildtool:any;
export async function activate(context: vscode.ExtensionContext) {

const workspaceFolder = (vscode.workspace.workspaceFolders || [])[0];
Expand All @@ -13,11 +14,14 @@ export async function activate(context: vscode.ExtensionContext) {
// get the Test Explorer extension
const testExplorerExtension = vscode.extensions.getExtension<TestHub>(testExplorerExtensionId);
if (log.enabled) log.info(`Snort3 Test Explorer ${testExplorerExtension ? '' : 'not '}found`);
const snort3BuildTools = vscode.extensions.getExtension('diptopandit.snort3-build-tools');
if (log.enabled) log.info(`Snort3 Build tools ${snort3BuildTools ? '' : 'not '}found`);

if (testExplorerExtension) {
if (testExplorerExtension && snort3BuildTools) {
const testHub = testExplorerExtension.exports;
myStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
myStatusBarItem.text=`$(beaker) Snort3 Tests`;
buildtool = snort3BuildTools.exports;
myStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left,buildtool.get_status_priority()+1);
myStatusBarItem.text=`$(beaker)`;
context.subscriptions.push(myStatusBarItem);
myStatusBarItem.show();
// this will register an ExampleTestAdapter for each WorkspaceFolder
Expand Down
7 changes: 4 additions & 3 deletions src/snort3Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode';
import * as fs from 'fs';
import * as convert from 'xml-js';
import * as child_process from 'child_process';
import {buildtool} from './main';
import { TestSuiteInfo, TestInfo, TestRunStartedEvent, TestRunFinishedEvent, TestSuiteEvent, TestEvent } from 'vscode-test-adapter-api';

export class snort3Test {
Expand Down Expand Up @@ -213,8 +214,8 @@ export async function loadSnort3Tests(rootdir:vscode.WorkspaceFolder)
};
const test_env = process.env;
let executor_dir = rootdir.uri.path;
let SF_PREFIX_SNORT3=<string>(vscode.workspace.getConfiguration('snort3TestExplorer').get('sf_prefix_snort3'));
let DEPENDENCY_DIR=<string>(vscode.workspace.getConfiguration('snort3TestExplorer').get('dependencies'));
let SF_PREFIX_SNORT3=<string>(buildtool.get_sf_prefix_snort3());
let DEPENDENCY_DIR=<string>(buildtool.get_dependencies());
let SNORT3_TEST_ROOT=executor_dir;

test_env.SNORT_LUA_PATH=SF_PREFIX_SNORT3+'/etc/snort/';
Expand All @@ -230,7 +231,7 @@ export async function loadSnort3Tests(rootdir:vscode.WorkspaceFolder)
test_env.SNORT_TEST_PLUGIN_PATH=SF_PREFIX_SNORT3+'/lib64';
test_env.SNORT_PLUGIN_PATH=SF_PREFIX_SNORT3+'/lib64';
test_env.SNORT3_TEST_ROOT=SNORT3_TEST_ROOT;
test_env.SNORT_SRCPATH=<string>(vscode.workspace.getConfiguration('snort3TestExplorer').get('snort_srcpath'));
test_env.SNORT_SRCPATH=<string>(buildtool.get_snort3_src_path());

var snort3Tests = new Map<string,snort3Test>();
const getLastItem = (thePath: string) => thePath.substring(thePath.lastIndexOf('/') + 1)
Expand Down

0 comments on commit f99f351

Please sign in to comment.