Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Client coverage #187

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ env/
.DS_Store
tags
*.aux
.coverage
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ install:
- nvm install 6.3
- nvm use 6.3
- npm install jshint
- cd client && npm install && cd ..
- cd realtime && npm install && cd ..
- gem install mdl
script:
- make all
after_success:
- coveralls --merge=client/coverage/coverage.json
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ syntax:
# Syntax check realtime JS
find realtime -iname "*.js"|grep -v '/node_modules/'|xargs jshint
# Syntax check client JS
find client -iname "*.js"|grep -v '/node_modules/'|grep -v '/dist/'|xargs jshint
find client -iname "*.js"|grep -v '/node_modules/'|grep -v '/dist/'|grep -v '/lcov-report/'|xargs jshint
# Syntax check MD files
mdl --rules ~MD036 etc
test:
cd backend && python manage.py test
cd sniffer && python test_sniff.py
cd sniffer && nosetests --with-coverage --cover-package=app,sniffer
cd realtime && npm run test
cd client && npm run test
coverage combine backend/.coverage sniffer/.coverage
10 changes: 10 additions & 0 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_nose'
]

MIDDLEWARE_CLASSES = [
Expand Down Expand Up @@ -162,3 +163,12 @@
}
}
}

# Testing settings

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

NOSE_ARGS = [
'--with-coverage',
'--cover-package=breach'
]
3 changes: 3 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ Django==1.9.2
PyYAML==3.11
argh==0.26.1
argparse==1.2.1
coverage==4.2
coveralls==1.1
django-nose==1.4.4
funcsigs==0.4
mock==1.3.0
pathtools==0.1.2
Expand Down
6 changes: 3 additions & 3 deletions client/breach.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const io = require('socket.io-client'),
req = require('./request.js'),
config = require('./config.js');

var BREACHClient = {
const BREACHClient = {
ONE_REQUEST_TIMEOUT: 5000,
MORE_WORK_TIMEOUT: 10000,
_socket: null,
init() {
var flag = 0;
let flag = 0;
this._socket = io.connect(config.COMMAND_CONTROL_URL);
this._socket.on('connect', () => {
console.log('Connected');
Expand Down Expand Up @@ -45,7 +45,7 @@ var BREACHClient = {
}
},
doWork(work) {
var {url, amount, alignmentalphabet} = work;
const {url, amount, alignmentalphabet} = work;

// TODO: rate limiting
if (typeof url == 'undefined') {
Expand Down
4 changes: 3 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"webpack": "webpack --progress --colors",
"watch": "webpack --progress --colors --watch",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "istanbul cover spec/run.js"
},
"contributors": [
{
Expand Down Expand Up @@ -37,6 +37,8 @@
"esversion": 6
},
"devDependencies": {
"istanbul": "^0.4.4",
"jasmine": "^2.4.1",
"jshint": "^2.9.2"
}
}
19 changes: 9 additions & 10 deletions client/request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Request = {
const Request = {
_img: null,
make(url, callback) {
console.log('Making request to ' + url);
Expand All @@ -12,13 +12,13 @@ var Request = {
}
};

var Collection = {
const Collection = {
_ONE_REQUEST_DEFAULT_TIMEOUT: 5000,
_SENTINEL: '^',
create(url, {amount, alignmentalphabet, oneRequestTimeout}, onOneSuccess, onAllSuccess, onError) {
var requests = [];
var loadingTimeout;
var loadedCount = 0;
const requests = [];
let loadingTimeout,
loadedCount = 0;

if (oneRequestTimeout === 0) {
throw 'oneRequestTimeout should not be zero';
Expand All @@ -32,7 +32,7 @@ var Collection = {
const resetTimeout = () => {
clearTimeout(loadingTimeout);
loadingTimeout = setTimeout(() => {
for (var i = 0; i < amount; ++i) {
for (let i = 0; i < amount; ++i) {
requests[i].cancel();
}
clearTimeout(loadingTimeout);
Expand All @@ -58,11 +58,10 @@ var Collection = {
};

antiBrowserCaching = Math.random() * Number.MAX_SAFE_INTEGER;
var alignmentPadding;

for (var i = 0; i < amount; ++i) {
alignmentPadding = alignmentalphabet.substr(0, i % 16);
var request = Request.make(url + alignmentPadding + this._SENTINEL + '&' + (antiBrowserCaching + i), oneLoaded.bind({}, i));
for (let i = 0; i < amount; ++i) {
let alignmentPadding = alignmentalphabet.substr(0, i % 16);
let request = Request.make(url + alignmentPadding + this._SENTINEL + '&' + (antiBrowserCaching + i), oneLoaded.bind({}, i));
requests.push(request);
}
}
Expand Down
74 changes: 74 additions & 0 deletions client/spec/requestspec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const {Request, Collection} = require('../request.js');

describe('request', () => {
var requestsMade = [],
originalImage;

function createMockImage(requestsMade) {
return function() {
Object.defineProperties(this, {
'src': {
'set': (value) => {
requestsMade.push(value);
this.onerror();
}
}
});
};
}

global.Image = createMockImage(requestsMade);

beforeEach(() => {
originalImage = global.Image;
requestsMade = [];
});

afterEach(() => {
global.Image = originalImage;
});

it('uses an image to perform a cross-origin request', (done) => {
Request.make('https://www.ruptureit.com/', done);
expect(requestsMade).toContain('https://www.ruptureit.com/');
});
});

describe('collection', () => {
it('makes multiple requests', () => {
const callbacks = [],
urls = [];

spyOn(Request, 'make').and.callFake((url, callback) => {
urls.push(url);
callbacks.push(callback);
});

const onOneSuccess = jasmine.createSpy(),
onAllSuccess = jasmine.createSpy(),
onError = jasmine.createSpy();

Collection.create('https://ruptureit.com', {
amount: 3,
alignmentalphabet: 'abc'
}, onOneSuccess, onAllSuccess, onError);

expect(Request.make.calls.count()).toBe(3);
expect(onOneSuccess).not.toHaveBeenCalled();
expect(onAllSuccess).not.toHaveBeenCalled();
expect(onError).not.toHaveBeenCalled();

callbacks[0]();

expect(onOneSuccess).toHaveBeenCalled();
expect(onAllSuccess).not.toHaveBeenCalled();
expect(onError).not.toHaveBeenCalled();

callbacks[1]();
callbacks[2]();

expect(onOneSuccess.calls.count()).toBe(3);
expect(onAllSuccess).toHaveBeenCalled();
expect(onError).not.toHaveBeenCalled();
});
});
5 changes: 5 additions & 0 deletions client/spec/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const Jasmine = require('jasmine');
const jasmine = new Jasmine();

jasmine.loadConfigFile('spec/support/jasmine.json');
jasmine.execute();
11 changes: 11 additions & 0 deletions client/spec/support/jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}
6 changes: 4 additions & 2 deletions client/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ var path = require('path');
var webpack = require('webpack');

module.exports = {
entry: './main.js',
entry: {
main: ['./main.js']
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'main.js'
filename: '[name].js'
},
module: {
loaders: [
Expand Down
1 change: 1 addition & 0 deletions sniffer/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Flask==0.10.1
scapy==2.3.2
tl.testing==0.5
mock==1.3.0
nose==1.3.7