Commit 48eea2aa authored by Frauke's avatar Frauke
Browse files

Updated fileUtils.

Added tests for fileUtils and server routes.
parent b9878201
......@@ -14,9 +14,9 @@ app.post('/register', (req, res) => {
const publicKey = Buffer.from(JSON.stringify(req.body.publicKey))
const privateKey = Buffer.from(JSON.stringify(req.body.privateKey))
const writeSalt = fileUtils.writeFile('./server/files/salt.json', salt)
const writePublicKey = fileUtils.writeFile('./server/files/publickey.json', publicKey)
const writePrivateKey = fileUtils.writeFile('./server/files/privatekey.json', privateKey)
const writeSalt = fileUtils.writeFile('salt.json', salt, './server/files')
const writePublicKey = fileUtils.writeFile('publickey.json', publicKey, './server/files')
const writePrivateKey = fileUtils.writeFile('privatekey.json', privateKey, './server/files')
Promise.all([writeSalt, writePublicKey, writePrivateKey]).then(() => {
res.status(200).end()
......@@ -46,5 +46,9 @@ app.get('/privatekey', (req, res) => {
})
})
console.log('Server started at port 8080')
app.listen(8080)
console.log('Server started on port 8080')
\ No newline at end of file
module.exports = {
app
}
const expect = require('expect')
const request = require('supertest')
const {app} = require('../index')
const fileUtils = require('../utils/fileUtils')
const testSalt = {'0': 176, '1': 79, '2': 86, '3': 167, '4': 163, '5': 109, '6': 202, '7': 122, '8': 232, '9': 230, '10': 35, '11': 68, '12': 243, '13': 228, '14': 217, '15': 165}
const publicKey = {'0': 4, '1': 20, '2': 174, '3': 134, '4': 168, '5': 217, '6': 59, '7': 186, '8': 122, '9': 230, '10': 204, '11': 76, '12': 72, '13': 178, '14': 36, '15': 129, '16': 66, '17': 0, '18': 156, '19': 125, '20': 122, '21': 194, '22': 250, '23': 16, '24': 38, '25': 161, '26': 3, '27': 90, '28': 231, '29': 236, '30': 126, '31': 75}
const privateKey = {'0': 135, '1': 68, '2': 230, '3': 58, '4': 5, '5': 77, '6': 223, '7': 36, '8': 216, '9': 109, '10': 129, '11': 174, '12': 34, '13': 203, '14': 239, '15': 42}
describe('POST /register', () => {
it('should create a new salt, publickey and privatekey file', (done) => {
const keys = {
salt: {'0': 135, '1': 68, '2': 230, '3': 58, '4': 5, '5': 77, '6': 223, '7': 36, '8': 216, '9': 109, '10': 129, '11': 174, '12': 34, '13': 203, '14': 239, '15': 42},
publicKey: {'0': 4, '1': 20, '2': 174, '3': 134, '4': 168, '5': 217, '6': 59, '7': 186, '8': 122, '9': 230, '10': 204, '11': 76, '12': 72, '13': 178, '14': 36, '15': 129, '16': 66, '17': 0, '18': 156, '19': 125, '20': 122, '21': 194, '22': 250, '23': 16, '24': 38, '25': 161, '26': 3, '27': 90, '28': 231, '29': 236, '30': 126, '31': 75},
privateKey: {'0': 135, '1': 68, '2': 230, '3': 58, '4': 5, '5': 77, '6': 223, '7': 36, '8': 216, '9': 109, '10': 129, '11': 174, '12': 34, '13': 203, '14': 239, '15': 42}
}
request(app)
.post('/register')
.send(keys)
.expect(200)
.end((err, res) => {
if (err) {
return done(err)
}
const readSalt = fileUtils.readFile('./server/files/salt.json')
const readPublicKey = fileUtils.readFile('./server/files/publickey.json')
const readPrivateKey = fileUtils.readFile('./server/files/privatekey.json')
Promise.all([readSalt, readPublicKey, readPrivateKey]).then((result) => {
expect(JSON.parse(result[0])).toEqual(keys.salt)
expect(JSON.parse(result[2])).toEqual(keys.privateKey)
expect(JSON.parse(result[1])).toEqual(keys.publicKey)
done()
}).catch((err) => done(err))
})
})
})
describe('GET /publickey', () => {
beforeEach(() => {
fileUtils.writeFile('publickey.json', Buffer.from(JSON.stringify(publicKey)), './server/files').catch((err) => console.log(err))
})
it('should return the publickey value', (done) => {
request(app)
.get('/publickey')
.expect(200)
.expect((res) => {
expect(res.body).toEqual(publicKey)
})
.end(done)
})
})
describe('GET /privatekey', () => {
beforeEach(() => {
fileUtils.writeFile('privatekey.json', Buffer.from(JSON.stringify(privateKey)), './server/files').catch((err) => console.log(err))
})
it('should return the privatekey value', (done) => {
request(app)
.get('/privatekey')
.expect(200)
.expect((res) => {
expect(res.body).toEqual(privateKey)
})
.end(done)
})
})
......@@ -5,12 +5,11 @@ const path = require('path')
function writeFile (fileName, value, pathName) {
return new Promise((resolve, reject) => {
makeDir(pathName).then(() => {
fs.writeFile(path.join(pathName, fileName), value, (err) => {
if (err) {
reject(err)
}
resolve()
})
let wstream = fs.createWriteStream(path.join(pathName, fileName))
wstream.write(value)
wstream.end()
wstream.on('finish', () => { resolve() })
wstream.on('error', (err) => { reject(err) })
}).catch((err) => reject(err))
})
}
......@@ -34,11 +33,11 @@ function makeDir (pathName) {
// Reads a file from the filesystem.
function readFile (fileName) {
return new Promise((resolve, reject) => {
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) {
reject(err)
}
resolve(data)
let readStream = fs.createReadStream(fileName)
readStream.on('error', (err) => { reject(err) })
readStream.on('data', (chunk) => {
resolve(chunk)
})
})
}
......
......@@ -2,7 +2,7 @@ const expect = require('expect')
const fileUtils = require('../fileUtils')
const fs = require('fs')
const testString = 'This is a unit test string'
const testString = Buffer.from('This is a unit test string')
describe('File utils tests', () => {
......@@ -33,7 +33,7 @@ describe('File utils tests', () => {
it('should read a file from the filesystem', (done) => {
fileUtils.readFile('./server/test/test.txt').then((file) => {
expect(file).toBe(testString)
expect(file).toEqual(testString)
done()
}).catch((err) => done(err))
})
......
......@@ -6,17 +6,6 @@ const indexedDB = require('./indexedDB')
const form = document.getElementById('register')
function loadedKeyPair () {
indexedDB.callOnStore('testkeystore', 'keyStore', (store) => {
const getData = store.get(1)
getData.onsuccess = (event) => {
const keys = getData.result.keys
}
})
}
loadedKeyPair()
form.addEventListener('submit', (e) => {
e.preventDefault()
const password = e.target.password.value
......@@ -24,8 +13,8 @@ form.addEventListener('submit', (e) => {
const keyPair = sodium.crypto_box_keypair()
const keyLength = sodium.crypto_pwhash_BYTES_MIN
const opslimit = sodium.crypto_pwhash_OPSLIMIT_MAX
const memlimit = sodium.crypto_pwhash_MEMLIMIT_MAX
const opslimit = sodium.crypto_pwhash_OPSLIMIT_MODERATE
const memlimit = 10777215
const algorithm = sodium.crypto_pwhash_ALG_DEFAULT
const encryptPassword = sodium.crypto_pwhash(keyLength, password, salt, opslimit, memlimit, algorithm)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment