Add browser version of crypto and test with karma

This commit is contained in:
Kagami Hiiragi 2014-12-14 12:24:35 +03:00
parent 600dc6e4bb
commit a402c40d4d
5 changed files with 95 additions and 6 deletions

View File

@ -1,6 +1,8 @@
language: node_js
node_js:
- "0.10"
before_script:
- "sudo apt-get install -y chromium-browser"
notifications:
email:
on_success: never

73
karma.conf.js Normal file
View File

@ -0,0 +1,73 @@
"use strict";
process.env.CHROME_BIN = "chromium-browser";
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: "",
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ["mocha", "browserify"],
// list of files / patterns to load in the browser
files: [
"test.js"
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
"test.js": ["browserify"],
},
// list of files to exclude
exclude: [
],
// test results reporter to use
// possible values: "dots", "progress"
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ["mocha"],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ["chromium_no_sandbox"],
customLaunchers: {
chromium_no_sandbox: {
base: "Chrome",
flags: ["--no-sandbox"],
},
},
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
});
};

View File

@ -2,3 +2,11 @@
* Browser version of the crypto for Bitmessage JS implementation.
* @module bitmessage/lib/crypto.browser
*/
"use strict";
exports.sha512 = function(buf) {
return window.crypto.subtle.digest({name: "SHA-512"}, buf).then(function(arr) {
return new Buffer(new Uint8Array(arr));
});
};

View File

@ -2,12 +2,13 @@
"name": "bitmessage",
"version": "0.0.0",
"description": "JavaScript Bitmessage library",
"main": "./lib/index",
"main": "./lib/index.js",
"browser": {
"./lib/crypto": "./lib/crypto.browser"
"./lib/crypto.js": "./lib/crypto.browser.js"
},
"scripts": {
"test": "mocha && jshint ."
"test": "mocha && xvfb-run -a karma start --single-run && jshint .",
"karma": "xvfb-run -a karma start"
},
"repository": {
"type": "git",
@ -28,6 +29,12 @@
"devDependencies": {
"chai": "*",
"jshint": "*",
"karma": "^0.12.28",
"karma-browserify": "^1.0.1",
"karma-chrome-launcher": "^0.1.7",
"karma-cli": "~0.0.4",
"karma-mocha": "^0.1.10",
"karma-mocha-reporter": "^0.3.1",
"mocha": "*"
},
"dependencies": {

View File

@ -3,10 +3,9 @@ var expect = require("chai").expect;
var bmcrypto = require("./lib/crypto");
describe("Bitmessage crypto", function() {
it("should calculate sha512 hash for both node and browserify", function(done) {
bmcrypto.sha512(new Buffer("test")).then(function(res) {
it("should calculate sha512 hash", function() {
return bmcrypto.sha512(new Buffer("test")).then(function(res) {
expect(res.toString("hex")).to.equal("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff");
done();
});
});
});