diff --git a/src/message.js b/src/message.js index a5a31856..a7d6854d 100644 --- a/src/message.js +++ b/src/message.js @@ -243,10 +243,10 @@ Message.prototype.encrypt = function(keys, passwords) { export function encryptSessionKey(sessionKey, symAlgo, publicKeys, passwords) { /** Convert to arrays if necessary */ - if (publicKeys && !Array.prototype.isPrototypeOf(publicKeys)) { + if (publicKeys && !util.isArray(publicKeys)) { publicKeys = [publicKeys]; } - if (passwords && !Array.prototype.isPrototypeOf(passwords)) { + if (passwords && !util.isArray(passwords)) { passwords = [passwords]; } @@ -455,7 +455,7 @@ export function fromText(text, filename) { * @static */ export function fromBinary(bytes, filename) { - if (!Uint8Array.prototype.isPrototypeOf(bytes)) { + if (!util.isUint8Array(bytes)) { throw new Error('Data must be in the form of a Uint8Array'); } diff --git a/src/util.js b/src/util.js index 9cb1eca1..4e429b38 100644 --- a/src/util.js +++ b/src/util.js @@ -26,6 +26,34 @@ import config from './config'; export default { + + isString: function(data) { + return typeof data === 'string' || String.prototype.isPrototypeOf(data); + }, + + isArray: function(data) { + return Array.prototype.isPrototypeOf(data); + }, + + isUint8Array: function(data) { + return Uint8Array.prototype.isPrototypeOf(data); + }, + + isEmailAddress: function(data) { + if (!this.isString(data)) { + return false; + } + const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + return re.test(data); + }, + + isUserId: function(data) { + if (!this.isString(data)) { + return false; + } + return / $/.test(data); + }, + readNumber: function (bytes) { var n = 0; diff --git a/test/general/index.js b/test/general/index.js index a9684c6b..b0b1312c 100644 --- a/test/general/index.js +++ b/test/general/index.js @@ -1,4 +1,5 @@ describe('General', function () { + require('./util.js'); require('./basic.js'); require('./armor.js'); require('./key.js'); diff --git a/test/general/util.js b/test/general/util.js new file mode 100644 index 00000000..75d3354b --- /dev/null +++ b/test/general/util.js @@ -0,0 +1,151 @@ +'use strict'; + +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../../dist/openpgp'); + +var chai = require('chai'), + expect = chai.expect; + +describe('Util unit tests', function() { + + beforeEach(function() {}); + + afterEach(function() {}); + + describe('isString', function() { + it('should return true for type "string"', function() { + var data = 'foo'; + expect(openpgp.util.isString(data)).to.be.true; + }); + it('should return true for type String', function() { + var data = String('foo'); + expect(openpgp.util.isString(data)).to.be.true; + }); + it('should return true for inherited type of String', function() { + function MyString() {} + MyString.prototype = Object.create(String.prototype); + var data = new MyString(); + expect(openpgp.util.isString(data)).to.be.true; + }); + it('should return true for empty string', function() { + var data = ''; + expect(openpgp.util.isString(data)).to.be.true; + }); + it('should return false for undefined', function() { + var data; + expect(openpgp.util.isString(data)).to.be.false; + }); + it('should return false for Object', function() { + var data = {}; + expect(openpgp.util.isString(data)).to.be.false; + }); + }); + + describe('isArray', function() { + it('should return true for []', function() { + var data = []; + expect(openpgp.util.isArray(data)).to.be.true; + }); + it('should return true for type Array', function() { + var data = Array(); + expect(openpgp.util.isArray(data)).to.be.true; + }); + it('should return true for inherited type of Array', function() { + function MyArray() {} + MyArray.prototype = Object.create(Array.prototype); + var data = new MyArray(); + expect(openpgp.util.isArray(data)).to.be.true; + }); + it('should return false for undefined', function() { + var data; + expect(openpgp.util.isArray(data)).to.be.false; + }); + it('should return false for Object', function() { + var data = {}; + expect(openpgp.util.isArray(data)).to.be.false; + }); + }); + + describe('isUint8Array', function() { + it('should return true for type Uint8Array', function() { + var data = new Uint8Array(); + expect(openpgp.util.isUint8Array(data)).to.be.true; + }); + it('should return true for inherited type of Uint8Array', function() { + function MyUint8Array() {} + MyUint8Array.prototype = new Uint8Array(); + var data = new MyUint8Array(); + expect(openpgp.util.isUint8Array(data)).to.be.true; + }); + it('should return false for undefined', function() { + var data; + expect(openpgp.util.isUint8Array(data)).to.be.false; + }); + it('should return false for Object', function() { + var data = {}; + expect(openpgp.util.isUint8Array(data)).to.be.false; + }); + }); + + describe('isEmailAddress', function() { + it('should return true for valid email address', function() { + var data = 'test@example.com'; + expect(openpgp.util.isEmailAddress(data)).to.be.true; + }); + it('should return false for invalid email address', function() { + var data = 'Test User '; + expect(openpgp.util.isEmailAddress(data)).to.be.false; + }); + it('should return false for invalid email address', function() { + var data = 'test@examplecom'; + expect(openpgp.util.isEmailAddress(data)).to.be.false; + }); + it('should return false for invalid email address', function() { + var data = 'testexamplecom'; + expect(openpgp.util.isEmailAddress(data)).to.be.false; + }); + it('should return false for empty string', function() { + var data = ''; + expect(openpgp.util.isEmailAddress(data)).to.be.false; + }); + it('should return false for undefined', function() { + var data; + expect(openpgp.util.isEmailAddress(data)).to.be.false; + }); + it('should return false for Object', function() { + var data = {}; + expect(openpgp.util.isEmailAddress(data)).to.be.false; + }); + }); + + describe('isUserId', function() { + it('should return true for valid user id', function() { + var data = 'Test User '; + expect(openpgp.util.isUserId(data)).to.be.true; + }); + it('should return false for invalid user id', function() { + var data = 'Test User test@example.com>'; + expect(openpgp.util.isUserId(data)).to.be.false; + }); + it('should return false for invalid user id', function() { + var data = 'Test User