UNPKG

boho

Version:

Secure Lightweight Encryption & Authentication Library for Node.js, Browsers and Arduino.

154 lines (122 loc) 4.58 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Mocha Tests</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="./mocha.css" /> </head> <body> <div id="mocha"></div> <script src="./chai.js"></script> <script src="./mocha.js"></script> <script class="mocha-init"> mocha.setup('bdd'); mocha.checkLeaks(); </script> <script src="../dist/boho.min.js"></script> <script> let assert = chai.assert; const RAND = Boho.RAND const MBP = Boho.MBP const MetaSize = Boho.MetaSize const BohoMsg = Boho.BohoMsg const Meta = Boho.Meta const Buffer = Boho.Buffer; describe('RAND browser', function () { describe('for number', function () { const num = 16 it('should return Uint8Array', function () { assert.ok(RAND(num) instanceof Uint8Array) }) it('should return number-bytes size of Uint8Array', function () { assert.ok(RAND(num).byteLength === num) }) }) }) describe('ENC_PACK', function () { describe('encryption and decryption', function () { const plainData = 'aaaaa' const key = 'key' it('should decryptPack.data property has same buffer of origin.', function () { const boho = new Boho() boho.set_key(key) const encData = boho.encryptPack(plainData) const decObj = boho.decryptPack(encData) const srcBuffer = MBP.B8(plainData) const resultBuffer = MBP.B8(decObj.data) assert.ok(MBP.equal(srcBuffer, resultBuffer)) }) }) }) describe('AUTH process', function () { const id = 'id' const key = 'key' // client side const c = new Boho() c.set_id8(id) c.set_key(key) // server side const s = new Boho() const auth_nonce_buffer = s.server_time_nonce() const auth_req_buffer = c.auth_req(auth_nonce_buffer) const unpack = MBP.unpack(auth_req_buffer, Meta.AUTH_REQ) // Find hashID8 from DATABASE then set the id and key. s.set_id8(id) s.set_key('wrong-key') // if key is wrong. const verify_auth_req_fail = s.verify_auth_req(unpack) s.set_key(key) // if correct. const auth_res_buffer = s.verify_auth_req(unpack) const auth_res_buffer_with_incorrect_hmac = Boho.Buffer.alloc(9) const isCorrectSeverHMAC = c.verify_auth_res(auth_res_buffer) auth_res_buffer.copy(auth_res_buffer_with_incorrect_hmac) auth_res_buffer_with_incorrect_hmac[2] ^= 0x55 // change hmac const wrongSeverHMACResult = c.verify_auth_res(auth_res_buffer_with_incorrect_hmac) describe('1. server send SERVER_TIME_NONCE', function () { it('should return static size buffer', function () { assert.equal(auth_nonce_buffer.byteLength, MetaSize.SERVER_TIME_NONCE) }) it('should pack[0] is header type ', function () { assert.ok(auth_nonce_buffer[0], BohoMsg.SERVER_TIME_NONCE) }) }) describe('2. client send AUTH_REQ', function () { it('should return static size buffer', function () { assert.equal(auth_req_buffer.byteLength, MetaSize.AUTH_REQ) }) it('should pack[0] is header type ', function () { assert.ok(auth_req_buffer[0], BohoMsg.AUTH_REQ) }) }) describe('3. server verify AUTH_REQ pack', function () { it('3.1. should success unpack', function () { assert.ok(typeof unpack === 'object') }) describe('3.2. verify_auth_req(obj) ', function () { // wrong key of id. it('3.2.1. should return false when wrong key', function () { assert.ok(!verify_auth_req_fail) }) // correct id and key it('3-2.2. should return AUTH_RES buffer pack when correct key', function () { assert.equal(auth_res_buffer[0], BohoMsg.AUTH_RES) }) }) }) describe('4. client process', function () { describe('4.1 verify_auth_res()', function () { it('should return true when correct AUTH_RES ', function () { assert.ok(isCorrectSeverHMAC) }) it('should return false when AUTH_RES has wrong hmac', function () { assert.ok(!wrongSeverHMACResult) }) }) }) }) </script> <script class="mocha-exec"> mocha.run(); </script> </body> </html>