feat: add sncrypto client side packages

This commit is contained in:
Karol Sójko
2022-07-06 12:21:21 +02:00
parent 9d1f7043e5
commit 6ec66795d2
71 changed files with 9786 additions and 34 deletions

View File

@@ -0,0 +1,421 @@
/* eslint-disable no-unused-expressions */
/* eslint-disable no-undef */
import './vendor/chai-as-promised-built.js'
import './vendor/buffer@5.6.0.js'
const Buffer = window.buffer.Buffer
chai.use(chaiAsPromised)
const expect = chai.expect
describe('crypto operations', async function () {
let webCrypto = new SNWebCrypto()
after(() => {
webCrypto.deinit()
webCrypto = null
})
it('webcrypto should be defined', function () {
expect(window.crypto).to.not.be.null
})
it('generates valid uuid', async function () {
expect(webCrypto.generateUUID().length).to.equal(36)
})
it('properly encodes base64', async function () {
const source = 'hello world 🌍'
const target = 'aGVsbG8gd29ybGQg8J+MjQ=='
expect(await base64Encode(source)).to.equal(target)
})
it('properly encodes base64 in url safe mode', async function () {
const source = 'hello world 🌍'
const target = 'aGVsbG8gd29ybGQg8J-MjQ'
expect(await base64URLEncode(source)).to.equal(target)
})
it('properly decodes base64', async function () {
const source = 'aGVsbG8gd29ybGQg8J+MjQ=='
const target = 'hello world 🌍'
expect(await base64Decode(source)).to.equal(target)
})
it('generates proper length generic key', async function () {
const length = 256
const wcResult = await webCrypto.generateRandomKey(length)
expect(wcResult.length).to.equal(length / 4)
})
it('compares strings with timing safe comparison', async function () {
const crypto = new SNWebCrypto()
expect(crypto.timingSafeEqual('hello world 🌍', 'hello world 🌍')).to.equal(
true,
)
expect(crypto.timingSafeEqual('helo world 🌍', 'hello world 🌍')).to.equal(
false,
)
expect(crypto.timingSafeEqual('', 'a')).to.equal(false)
expect(crypto.timingSafeEqual('', '')).to.equal(true)
expect(
crypto.timingSafeEqual(
'2e1ee7920bb188a88f94bb912153befd83cc55cd',
'2e1ee7920bb188a88f94bb912153befd83cc55cd',
),
).to.equal(true)
expect(
crypto.timingSafeEqual(
'1e1ee7920bb188a88f94bb912153befd83cc55cd',
'2e1ee7920bb188a88f94bb912153befd83cc55cd',
),
).to.equal(false)
expect(
crypto.timingSafeEqual(
'2e1ee7920bb188a88f94bb912153befd83cc55cc',
'2e1ee7920bb188a88f94bb912153befd83cc55cd',
),
).to.equal(false)
})
it('random key length', async function () {
const key = await webCrypto.generateRandomKey(256)
expect(key.length).to.equal(64)
})
it('pbkdf2 1', async function () {
const password = 'very_secure🔒'
const salt =
'c3feb78823adce65c4ab024dab9c5cdcda5a04cdbd98f65eac0311dfa432d67b'
const expected =
'bbb3d3af19dd1cbb901c958003faa55f193aad6a57fff30e51a62591bdc054d8'
const result = await webCrypto.pbkdf2(password, salt, 100000, 256)
expect(result).to.equal(expected)
})
it('pbkdf2 2', async function () {
const password = 'correct horse battery staple ✅'
const salt = Buffer.from(
'808182838485868788898a8b8c8d8e8f',
'hex',
).toString('utf8')
const expected =
'795d83b18e55d860d3799f85a20f66ee17eb9dcf041df1d7a13fac30af7103d9'
const result = await webCrypto.pbkdf2(password, salt, 100000, 256)
expect(result).to.equal(expected)
})
it('aes cbc', async function () {
const iv = await webCrypto.generateRandomKey(128)
const key = await webCrypto.generateRandomKey(256)
const text = 'hello world 🌍'
const encrypted = await webCrypto.aes256CbcEncrypt(text, iv, key)
const decrypted = await webCrypto.aes256CbcDecrypt(encrypted, iv, key)
expect(decrypted).to.equal(text)
})
it('hmac 256', async function () {
const text = 'hello world 🌍'
const key =
'e802dc953f3f1f7b5db62409b74ac848559d4711c4e0047ecc5e312ad8ab8397'
const hash = await webCrypto.hmac256(text, key)
const expected =
'b63f94ee33a067ffac3ee97c7987dd3171dcdc747a322bb3f3ab890201c8e6f9'
expect(hash).to.equal(expected)
})
it('sha256', async function () {
const text = 'hello world 🌍'
const hash = await webCrypto.sha256(text)
const expected =
'1e71fe32476da1ff115b44dfd74aed5c90d68a1d80a2033065e30cff4335211a'
expect(hash).to.equal(expected)
})
it('hmac 1', async function () {
const text = 'hello world 🌍'
const key = '73756d6d657274696d65'
const hash = await webCrypto.hmac1(text, key)
const expected = '534bc6ff40d4616e9be4fb530093d5f7f87173fa'
expect(hash).to.equal(expected)
})
it('sha1', async function () {
const text = 'hello world 🌍'
const hash = await webCrypto.unsafeSha1(text)
const expected = '0818667aed20ac104ca8f300f8df9753e1937983'
expect(hash).to.equal(expected)
})
it('argon2 predefined salt', async function () {
/** This differs from libsodium.js test matching below in that we include an emoji at the end */
const password = 'correct horse battery staple ✅'
const salt = '808182838485868788898a8b8c8d8e8f'
const bytes = 67108864
const length = 16
const iterations = 2
const result = await webCrypto.argon2(
password,
salt,
iterations,
bytes,
length,
)
const expectedResult = '18dfbc268f251701652c8e38b5273f73'
expect(result).to.equal(expectedResult)
})
it('argon2 generated salt', async function () {
const rawSalt = await webCrypto.sha256(['foo', 'bar'].join(':'))
const truncatedSalt = rawSalt.substring(0, rawSalt.length / 2)
const password = 'foobarfoo🔒'
const bytes = 67108864
const length = 32
const iterations = 5
const result = await webCrypto.argon2(
password,
truncatedSalt,
iterations,
bytes,
length,
)
const expected =
'bb6ec440708c271ce34decd7f997e2444d309b1105992779ccdb47f78a5fda6f'
expect(result).to.equal(expected)
})
it('xchacha20 encrypt/decrypt', async function () {
const key = await webCrypto.generateRandomKey(256)
const nonce = await webCrypto.generateRandomKey(192)
const plaintext = 'hello world 🌍'
const aad = JSON.stringify({ uuid: '123🎤' })
const ciphertext = await webCrypto.xchacha20Encrypt(
plaintext,
nonce,
key,
aad,
)
const decrypted = await webCrypto.xchacha20Decrypt(
ciphertext,
nonce,
key,
aad,
)
expect(decrypted).to.equal(plaintext)
})
it('xchacha20 streaming encrypt/decrypt', async function () {
const key = await webCrypto.generateRandomKey(256)
const bigFile = await fetch(
'http://localhost:9003/test/resources/big_file.md',
)
const bigText = await bigFile.text()
const plaintext = bigText
const plainBuffer = stringToArrayBuffer(plaintext)
const encryptor = webCrypto.xchacha20StreamEncryptInitEncryptor(key)
const header = base64StringToArrayBuffer(encryptor.header)
let encryptedBuffer = Buffer.concat([header])
const pushChunkSize = plainBuffer.length / 200
const pullChunkSize =
pushChunkSize +
SodiumConstant.CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES
for (let i = 0; i < plainBuffer.length; i += pushChunkSize) {
const readUntil =
i + pushChunkSize > plainBuffer.length
? plainBuffer.length
: i + pushChunkSize
const chunk = webCrypto.xchacha20StreamEncryptorPush(
encryptor,
plainBuffer.slice(i, readUntil),
)
encryptedBuffer = Buffer.concat([encryptedBuffer, chunk])
}
const decryptor = webCrypto.xchacha20StreamEncryptInitDecryptor(
header,
key,
)
let decryptedBuffer = Buffer.alloc(0)
for (
let i = header.length;
i < encryptedBuffer.length;
i += pullChunkSize
) {
const readUntil =
i + pullChunkSize > encryptedBuffer.length
? encryptedBuffer.length
: i + pullChunkSize
const chunk = webCrypto.xchacha20StreamDecryptorPush(
decryptor,
encryptedBuffer.slice(i, readUntil),
)
decryptedBuffer = Buffer.concat([decryptedBuffer, chunk.message])
}
const decryptedPlain = arrayBufferToString(decryptedBuffer)
expect(decryptedPlain).to.equal(plaintext)
})
it('xchacha20 should fail with nonmatching aad', async function () {
const key = await webCrypto.generateRandomKey(256)
const nonce = await webCrypto.generateRandomKey(192)
const plaintext = 'hello world 🌍'
const ciphertext = await webCrypto.xchacha20Encrypt(
plaintext,
nonce,
key,
JSON.stringify({ uuid: 'foo🎲' }),
)
const result = await webCrypto.xchacha20Decrypt(
ciphertext,
nonce,
key,
JSON.stringify({ uuid: 'bar🎲' }),
)
expect(result).to.not.be.ok
})
it('xchacha predefined string', async function () {
/** This differs from libsodium.js test matching below in that we include an emoji at the end */
const plaintext =
"Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it.🌞"
const assocData = await hexStringToArrayBuffer('50515253c0c1c2c3c4c5c6c7')
const nonce = '404142434445464748494a4b4c4d4e4f5051525354555657'
const key =
'808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f'
const ciphertext = await webCrypto.xchacha20Encrypt(
plaintext,
nonce,
key,
assocData,
)
const expected =
'vW0XnT6D1DuVdleUk8DpOVcqFwAlK/rMvtKQLCE5bLtzHH8bC0qmRAvzqC9O2n45rmTGcIxUwhbLlrcuEhO0Ui+Mm6QNtdlFsRtpuYLBu54/P6wrw2lIj3ayODVl0//5IflmTJdjfal2iBL2FcaLE7UuOqw6kdl7HV6PKzn0pIOeHH3rkwQ='
expect(ciphertext).to.equal(expected)
const decrypted = await webCrypto.xchacha20Decrypt(
ciphertext,
nonce,
key,
assocData,
)
expect(decrypted).to.equal(plaintext)
})
it('xchacha libsodium.js test matching', async function () {
/* Same values as https://github.com/jedisct1/libsodium.js/blob/master/test/sodium_utils.js */
const plaintext = Buffer.from(
'4c616469657320616e642047656e746c656d656e206f662074686520636c6173' +
'73206f66202739393a204966204920636f756c64206f6666657220796f75206f' +
'6e6c79206f6e652074697020666f7220746865206675747572652c2073756e73' +
'637265656e20776f756c642062652069742e',
'hex',
).toString('utf8')
const assocData = Buffer.from('50515253c0c1c2c3c4c5c6c7', 'hex')
const nonce = '404142434445464748494a4b4c4d4e4f5051525354555657'
const key =
'808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f'
/** Encrypt */
const ciphertextBase64 = await webCrypto.xchacha20Encrypt(
plaintext,
nonce,
key,
assocData,
)
const ciphertextHex = await base64ToHex(ciphertextBase64)
const expected =
'bd6d179d3e83d43b9576579493c0e939572a1700252bfaccbed2902c21396cbb' +
'731c7f1b0b4aa6440bf3a82f4eda7e39ae64c6708c54c216cb96b72e1213b452' +
'2f8c9ba40db5d945b11b69b982c1bb9e3f3fac2bc369488f76b2383565d3fff9' +
'21f9664c97637da9768812f615c68b13b52e' +
'c0875924c1c7987947deafd8780acf49'
expect(ciphertextHex).to.equal(expected)
/** Decrypt */
const decrypted = await webCrypto.xchacha20Decrypt(
ciphertextBase64,
nonce,
key,
assocData,
)
expect(decrypted).to.equal(plaintext)
})
it('argon2 libsodium.js test matching', async function () {
/* Same values as https://github.com/jedisct1/libsodium.js/blob/master/test/sodium_utils.js */
const password = 'correct horse battery staple'
const salt = '808182838485868788898a8b8c8d8e8f'
const bytes = 67108864
const length = 16
const iterations = 2
const result = await webCrypto.argon2(
password,
salt,
iterations,
bytes,
length,
)
const expectedResult = '720f95400220748a811bca9b8cff5d6e'
expect(result).to.equal(expectedResult)
})
it('generates random OTP secret 160 bits long', async function () {
const secret = await webCrypto.generateOtpSecret()
expect(secret).to.have.length(32)
expect(secret).to.not.include('=')
})
it('generates valid HOTP tokens', async function () {
/**
* Test data acquired from RFC4226
* https://datatracker.ietf.org/doc/html/rfc4226#page-32
*/
const encoder = new TextEncoder()
const secret = '12345678901234567890'
const b32Secret = base32Encode(encoder.encode(secret))
const hotpTest = [
'755224',
'287082',
'359152',
'969429',
'338314',
'254676',
'287922',
'162583',
'399871',
'520489',
]
for (let counter = 0; counter < hotpTest.length; counter++) {
const hotp = hotpTest[counter]
const result = await webCrypto.hotpToken(b32Secret, counter)
expect(result).to.equal(hotp)
}
})
it('generates valid TOTP tokens', async function () {
/**
* Test data acquired from RFC6238
* https://datatracker.ietf.org/doc/html/rfc6238#appendix-B
*/
const encoder = new TextEncoder()
const secret = '12345678901234567890'
const b32Secret = base32Encode(encoder.encode(secret))
const tokenLength = 8
const totpTest = [
{ time: 59000, totp: '94287082' },
{ time: 1111111109000, totp: '07081804' },
{ time: 1111111111000, totp: '14050471' },
{ time: 1234567890000, totp: '89005924' },
{ time: 2000000000000, totp: '69279037' },
]
for (let { time, totp } of totpTest) {
const result = await webCrypto.totpToken(b32Secret, time, tokenLength)
expect(result).to.equal(totp)
}
})
})

View File

@@ -0,0 +1,17 @@
/* eslint-disable no-unused-expressions */
/* eslint-disable no-undef */
import './vendor/chai-as-promised-built.js'
chai.use(chaiAsPromised)
/**
* Simple empty test page to create and deinit empty page
* Then check browser Memory tool to make sure there are no leaks.
*/
describe('memory', async function () {
it('cleanup', function () {
this.webCrypto = new SNWebCrypto()
this.webCrypto.deinit()
this.webCrypto = null
})
})

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,28 @@
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
<script src="https://unpkg.com/chai/chai.js"></script>
<script src="https://unpkg.com/mocha/mocha.js"></script>
<script src="../../../node_modules/regenerator-runtime/runtime.js"></script>
<script src="../dist/sncrypto-web.js"></script>
<script>
Object.assign(window, SNCrypto);
mocha.setup('bdd');
</script>
<script type="module" src="utils.test.js"></script>
<script type="module" src="memory.test.js"></script>
<script type="module" src="crypto.test.js"></script>
<script type="module">
mocha.checkLeaks();
mocha.run();
</script>
</head>
<body>
<div id="mocha"></div>
</body>
</html>

View File

@@ -0,0 +1,110 @@
/* eslint-disable no-unused-expressions */
/* eslint-disable no-undef */
import './vendor/chai-as-promised-built.js'
chai.use(chaiAsPromised)
const expect = chai.expect
describe('utils', async function () {
it('stringToArrayBuffer', async function () {
const str = 'hello world'
const buffer = await stringToArrayBuffer(str)
expect(buffer.byteLength).to.equal(11)
expect(await arrayBufferToString(buffer)).to.equal(str)
})
it('arrayBufferToString', async function () {
const str = 'hello world'
const buffer = await stringToArrayBuffer(str)
const result = await arrayBufferToString(buffer)
expect(result).to.equal(str)
expect(await stringToArrayBuffer(result)).to.eql(buffer)
})
it('arrayBufferToHexString', async function () {
const str = 'hello world'
const buffer = await stringToArrayBuffer(str)
const hex = await arrayBufferToHexString(buffer)
expect(hex).to.equal('68656c6c6f20776f726c64')
})
it('hexStringToArrayBuffer', async function () {
const hex = '68656c6c6f20776f726c64'
const buffer = await hexStringToArrayBuffer(hex)
expect(buffer.byteLength).to.equal(11)
})
it('base64Encode', async function () {
const str = 'hello world'
const b64 = await base64Encode(str)
expect(b64).to.equal('aGVsbG8gd29ybGQ=')
})
it('base64URLEncode', async function () {
const str = 'hello world'
const b64 = await base64Encode(str)
expect(b64).to.equal('aGVsbG8gd29ybGQ')
})
it('base64Decode', async function () {
const b64 = 'aGVsbG8gd29ybGQ='
const str = await base64Decode(b64)
expect(str).to.equal('hello world')
})
it('base64ToArrayBuffer', async function () {
const b64 = 'aGVsbG8gd29ybGQ='
const buffer = await base64ToArrayBuffer(b64)
expect(buffer.byteLength).to.equal(11)
})
it('arrayBufferToBase64', async function () {
const b64 = 'aGVsbG8gd29ybGQ='
const buffer = await base64ToArrayBuffer(b64)
const result = await arrayBufferToBase64(buffer)
expect(result).to.equal(b64)
})
it('hexToBase64', async function () {
const hex = '68656c6c6f20776f726c64'
const result = await hexToBase64(hex)
expect(result).to.equal('aGVsbG8gd29ybGQ=')
})
it('base64ToHex', async function () {
const b64 = 'aGVsbG8gd29ybGQ='
const result = await base64ToHex(b64)
expect(result).to.equal('68656c6c6f20776f726c64')
})
/**
* Table of test values for base32 from RFC4648
* https://datatracker.ietf.org/doc/html/rfc4648#section-10
*/
const base32TestPair = [
{ text: '', base32: '' },
{ text: 'f', base32: 'MY======' },
{ text: 'fo', base32: 'MZXQ====' },
{ text: 'foo', base32: 'MZXW6===' },
{ text: 'foob', base32: 'MZXW6YQ=' },
{ text: 'fooba', base32: 'MZXW6YTB' },
{ text: 'foobar', base32: 'MZXW6YTBOI======' },
]
it('base32Encode', async function () {
const encoder = new TextEncoder()
for (let pair of base32TestPair) {
const result = base32Encode(encoder.encode(pair.text).buffer)
expect(result).to.equal(pair.base32)
}
})
it('base32Decode', async function () {
const decoder = new TextDecoder()
for (let pair of base32TestPair) {
const bufferResult = base32Decode(pair.base32)
const result = decoder.decode(bufferResult)
expect(result).to.equal(pair.text)
}
})
})

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,539 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.chaiAsPromised = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";
/* eslint-disable no-invalid-this */
let checkError = require("check-error");
module.exports = (chai, utils) => {
const Assertion = chai.Assertion;
const assert = chai.assert;
const proxify = utils.proxify;
// If we are using a version of Chai that has checkError on it,
// we want to use that version to be consistent. Otherwise, we use
// what was passed to the factory.
if (utils.checkError) {
checkError = utils.checkError;
}
function isLegacyJQueryPromise(thenable) {
// jQuery promises are Promises/A+-compatible since 3.0.0. jQuery 3.0.0 is also the first version
// to define the catch method.
return typeof thenable.catch !== "function" &&
typeof thenable.always === "function" &&
typeof thenable.done === "function" &&
typeof thenable.fail === "function" &&
typeof thenable.pipe === "function" &&
typeof thenable.progress === "function" &&
typeof thenable.state === "function";
}
function assertIsAboutPromise(assertion) {
if (typeof assertion._obj.then !== "function") {
throw new TypeError(utils.inspect(assertion._obj) + " is not a thenable.");
}
if (isLegacyJQueryPromise(assertion._obj)) {
throw new TypeError("Chai as Promised is incompatible with thenables of jQuery<3.0.0, sorry! Please " +
"upgrade jQuery or use another Promises/A+ compatible library (see " +
"http://promisesaplus.com/).");
}
}
function proxifyIfSupported(assertion) {
return proxify === undefined ? assertion : proxify(assertion);
}
function method(name, asserter) {
utils.addMethod(Assertion.prototype, name, function () {
assertIsAboutPromise(this);
return asserter.apply(this, arguments);
});
}
function property(name, asserter) {
utils.addProperty(Assertion.prototype, name, function () {
assertIsAboutPromise(this);
return proxifyIfSupported(asserter.apply(this, arguments));
});
}
function doNotify(promise, done) {
promise.then(() => done(), done);
}
// These are for clarity and to bypass Chai refusing to allow `undefined` as actual when used with `assert`.
function assertIfNegated(assertion, message, extra) {
assertion.assert(true, null, message, extra.expected, extra.actual);
}
function assertIfNotNegated(assertion, message, extra) {
assertion.assert(false, message, null, extra.expected, extra.actual);
}
function getBasePromise(assertion) {
// We need to chain subsequent asserters on top of ones in the chain already (consider
// `eventually.have.property("foo").that.equals("bar")`), only running them after the existing ones pass.
// So the first base-promise is `assertion._obj`, but after that we use the assertions themselves, i.e.
// previously derived promises, to chain off of.
return typeof assertion.then === "function" ? assertion : assertion._obj;
}
function getReasonName(reason) {
return reason instanceof Error ? reason.toString() : checkError.getConstructorName(reason);
}
// Grab these first, before we modify `Assertion.prototype`.
const propertyNames = Object.getOwnPropertyNames(Assertion.prototype);
const propertyDescs = {};
for (const name of propertyNames) {
propertyDescs[name] = Object.getOwnPropertyDescriptor(Assertion.prototype, name);
}
property("fulfilled", function () {
const derivedPromise = getBasePromise(this).then(
value => {
assertIfNegated(this,
"expected promise not to be fulfilled but it was fulfilled with #{act}",
{ actual: value });
return value;
},
reason => {
assertIfNotNegated(this,
"expected promise to be fulfilled but it was rejected with #{act}",
{ actual: getReasonName(reason) });
return reason;
}
);
module.exports.transferPromiseness(this, derivedPromise);
return this;
});
property("rejected", function () {
const derivedPromise = getBasePromise(this).then(
value => {
assertIfNotNegated(this,
"expected promise to be rejected but it was fulfilled with #{act}",
{ actual: value });
return value;
},
reason => {
assertIfNegated(this,
"expected promise not to be rejected but it was rejected with #{act}",
{ actual: getReasonName(reason) });
// Return the reason, transforming this into a fulfillment, to allow further assertions, e.g.
// `promise.should.be.rejected.and.eventually.equal("reason")`.
return reason;
}
);
module.exports.transferPromiseness(this, derivedPromise);
return this;
});
method("rejectedWith", function (errorLike, errMsgMatcher, message) {
let errorLikeName = null;
const negate = utils.flag(this, "negate") || false;
// rejectedWith with that is called without arguments is
// the same as a plain ".rejected" use.
if (errorLike === undefined && errMsgMatcher === undefined &&
message === undefined) {
/* eslint-disable no-unused-expressions */
return this.rejected;
/* eslint-enable no-unused-expressions */
}
if (message !== undefined) {
utils.flag(this, "message", message);
}
if (errorLike instanceof RegExp || typeof errorLike === "string") {
errMsgMatcher = errorLike;
errorLike = null;
} else if (errorLike && errorLike instanceof Error) {
errorLikeName = errorLike.toString();
} else if (typeof errorLike === "function") {
errorLikeName = checkError.getConstructorName(errorLike);
} else {
errorLike = null;
}
const everyArgIsDefined = Boolean(errorLike && errMsgMatcher);
let matcherRelation = "including";
if (errMsgMatcher instanceof RegExp) {
matcherRelation = "matching";
}
const derivedPromise = getBasePromise(this).then(
value => {
let assertionMessage = null;
let expected = null;
if (errorLike) {
assertionMessage = "expected promise to be rejected with #{exp} but it was fulfilled with #{act}";
expected = errorLikeName;
} else if (errMsgMatcher) {
assertionMessage = `expected promise to be rejected with an error ${matcherRelation} #{exp} but ` +
`it was fulfilled with #{act}`;
expected = errMsgMatcher;
}
assertIfNotNegated(this, assertionMessage, { expected, actual: value });
return value;
},
reason => {
const errorLikeCompatible = errorLike && (errorLike instanceof Error ?
checkError.compatibleInstance(reason, errorLike) :
checkError.compatibleConstructor(reason, errorLike));
const errMsgMatcherCompatible = errMsgMatcher && checkError.compatibleMessage(reason, errMsgMatcher);
const reasonName = getReasonName(reason);
if (negate && everyArgIsDefined) {
if (errorLikeCompatible && errMsgMatcherCompatible) {
this.assert(true,
null,
"expected promise not to be rejected with #{exp} but it was rejected " +
"with #{act}",
errorLikeName,
reasonName);
}
} else {
if (errorLike) {
this.assert(errorLikeCompatible,
"expected promise to be rejected with #{exp} but it was rejected with #{act}",
"expected promise not to be rejected with #{exp} but it was rejected " +
"with #{act}",
errorLikeName,
reasonName);
}
if (errMsgMatcher) {
this.assert(errMsgMatcherCompatible,
`expected promise to be rejected with an error ${matcherRelation} #{exp} but got ` +
`#{act}`,
`expected promise not to be rejected with an error ${matcherRelation} #{exp}`,
errMsgMatcher,
checkError.getMessage(reason));
}
}
return reason;
}
);
module.exports.transferPromiseness(this, derivedPromise);
return this;
});
property("eventually", function () {
utils.flag(this, "eventually", true);
return this;
});
method("notify", function (done) {
doNotify(getBasePromise(this), done);
return this;
});
method("become", function (value, message) {
return this.eventually.deep.equal(value, message);
});
// ### `eventually`
// We need to be careful not to trigger any getters, thus `Object.getOwnPropertyDescriptor` usage.
const methodNames = propertyNames.filter(name => {
return name !== "assert" && typeof propertyDescs[name].value === "function";
});
methodNames.forEach(methodName => {
Assertion.overwriteMethod(methodName, originalMethod => function () {
return doAsserterAsyncAndAddThen(originalMethod, this, arguments);
});
});
const getterNames = propertyNames.filter(name => {
return name !== "_obj" && typeof propertyDescs[name].get === "function";
});
getterNames.forEach(getterName => {
// Chainable methods are things like `an`, which can work both for `.should.be.an.instanceOf` and as
// `should.be.an("object")`. We need to handle those specially.
const isChainableMethod = Assertion.prototype.__methods.hasOwnProperty(getterName);
if (isChainableMethod) {
Assertion.overwriteChainableMethod(
getterName,
originalMethod => function () {
return doAsserterAsyncAndAddThen(originalMethod, this, arguments);
},
originalGetter => function () {
return doAsserterAsyncAndAddThen(originalGetter, this);
}
);
} else {
Assertion.overwriteProperty(getterName, originalGetter => function () {
return proxifyIfSupported(doAsserterAsyncAndAddThen(originalGetter, this));
});
}
});
function doAsserterAsyncAndAddThen(asserter, assertion, args) {
// Since we're intercepting all methods/properties, we need to just pass through if they don't want
// `eventually`, or if we've already fulfilled the promise (see below).
if (!utils.flag(assertion, "eventually")) {
asserter.apply(assertion, args);
return assertion;
}
const derivedPromise = getBasePromise(assertion).then(value => {
// Set up the environment for the asserter to actually run: `_obj` should be the fulfillment value, and
// now that we have the value, we're no longer in "eventually" mode, so we won't run any of this code,
// just the base Chai code that we get to via the short-circuit above.
assertion._obj = value;
utils.flag(assertion, "eventually", false);
return args ? module.exports.transformAsserterArgs(args) : args;
}).then(newArgs => {
asserter.apply(assertion, newArgs);
// Because asserters, for example `property`, can change the value of `_obj` (i.e. change the "object"
// flag), we need to communicate this value change to subsequent chained asserters. Since we build a
// promise chain paralleling the asserter chain, we can use it to communicate such changes.
return assertion._obj;
});
module.exports.transferPromiseness(assertion, derivedPromise);
return assertion;
}
// ### Now use the `Assertion` framework to build an `assert` interface.
const originalAssertMethods = Object.getOwnPropertyNames(assert).filter(propName => {
return typeof assert[propName] === "function";
});
assert.isFulfilled = (promise, message) => (new Assertion(promise, message)).to.be.fulfilled;
assert.isRejected = (promise, errorLike, errMsgMatcher, message) => {
const assertion = new Assertion(promise, message);
return assertion.to.be.rejectedWith(errorLike, errMsgMatcher, message);
};
assert.becomes = (promise, value, message) => assert.eventually.deepEqual(promise, value, message);
assert.doesNotBecome = (promise, value, message) => assert.eventually.notDeepEqual(promise, value, message);
assert.eventually = {};
originalAssertMethods.forEach(assertMethodName => {
assert.eventually[assertMethodName] = function (promise) {
const otherArgs = Array.prototype.slice.call(arguments, 1);
let customRejectionHandler;
const message = arguments[assert[assertMethodName].length - 1];
if (typeof message === "string") {
customRejectionHandler = reason => {
throw new chai.AssertionError(`${message}\n\nOriginal reason: ${utils.inspect(reason)}`);
};
}
const returnedPromise = promise.then(
fulfillmentValue => assert[assertMethodName].apply(assert, [fulfillmentValue].concat(otherArgs)),
customRejectionHandler
);
returnedPromise.notify = done => {
doNotify(returnedPromise, done);
};
return returnedPromise;
};
});
};
module.exports.transferPromiseness = (assertion, promise) => {
assertion.then = promise.then.bind(promise);
};
module.exports.transformAsserterArgs = values => values;
},{"check-error":2}],2:[function(require,module,exports){
'use strict';
/* !
* Chai - checkError utility
* Copyright(c) 2012-2016 Jake Luer <jake@alogicalparadox.com>
* MIT Licensed
*/
/**
* ### .checkError
*
* Checks that an error conforms to a given set of criteria and/or retrieves information about it.
*
* @api public
*/
/**
* ### .compatibleInstance(thrown, errorLike)
*
* Checks if two instances are compatible (strict equal).
* Returns false if errorLike is not an instance of Error, because instances
* can only be compatible if they're both error instances.
*
* @name compatibleInstance
* @param {Error} thrown error
* @param {Error|ErrorConstructor} errorLike object to compare against
* @namespace Utils
* @api public
*/
function compatibleInstance(thrown, errorLike) {
return errorLike instanceof Error && thrown === errorLike;
}
/**
* ### .compatibleConstructor(thrown, errorLike)
*
* Checks if two constructors are compatible.
* This function can receive either an error constructor or
* an error instance as the `errorLike` argument.
* Constructors are compatible if they're the same or if one is
* an instance of another.
*
* @name compatibleConstructor
* @param {Error} thrown error
* @param {Error|ErrorConstructor} errorLike object to compare against
* @namespace Utils
* @api public
*/
function compatibleConstructor(thrown, errorLike) {
if (errorLike instanceof Error) {
// If `errorLike` is an instance of any error we compare their constructors
return thrown.constructor === errorLike.constructor || thrown instanceof errorLike.constructor;
} else if (errorLike.prototype instanceof Error || errorLike === Error) {
// If `errorLike` is a constructor that inherits from Error, we compare `thrown` to `errorLike` directly
return thrown.constructor === errorLike || thrown instanceof errorLike;
}
return false;
}
/**
* ### .compatibleMessage(thrown, errMatcher)
*
* Checks if an error's message is compatible with a matcher (String or RegExp).
* If the message contains the String or passes the RegExp test,
* it is considered compatible.
*
* @name compatibleMessage
* @param {Error} thrown error
* @param {String|RegExp} errMatcher to look for into the message
* @namespace Utils
* @api public
*/
function compatibleMessage(thrown, errMatcher) {
var comparisonString = typeof thrown === 'string' ? thrown : thrown.message;
if (errMatcher instanceof RegExp) {
return errMatcher.test(comparisonString);
} else if (typeof errMatcher === 'string') {
return comparisonString.indexOf(errMatcher) !== -1; // eslint-disable-line no-magic-numbers
}
return false;
}
/**
* ### .getFunctionName(constructorFn)
*
* Returns the name of a function.
* This also includes a polyfill function if `constructorFn.name` is not defined.
*
* @name getFunctionName
* @param {Function} constructorFn
* @namespace Utils
* @api private
*/
var functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*\/)]+\*\/\s*)*([^\(\/]+)/;
function getFunctionName(constructorFn) {
var name = '';
if (typeof constructorFn.name === 'undefined') {
// Here we run a polyfill if constructorFn.name is not defined
var match = String(constructorFn).match(functionNameMatch);
if (match) {
name = match[1];
}
} else {
name = constructorFn.name;
}
return name;
}
/**
* ### .getConstructorName(errorLike)
*
* Gets the constructor name for an Error instance or constructor itself.
*
* @name getConstructorName
* @param {Error|ErrorConstructor} errorLike
* @namespace Utils
* @api public
*/
function getConstructorName(errorLike) {
var constructorName = errorLike;
if (errorLike instanceof Error) {
constructorName = getFunctionName(errorLike.constructor);
} else if (typeof errorLike === 'function') {
// If `err` is not an instance of Error it is an error constructor itself or another function.
// If we've got a common function we get its name, otherwise we may need to create a new instance
// of the error just in case it's a poorly-constructed error. Please see chaijs/chai/issues/45 to know more.
constructorName = getFunctionName(errorLike).trim() ||
getFunctionName(new errorLike()); // eslint-disable-line new-cap
}
return constructorName;
}
/**
* ### .getMessage(errorLike)
*
* Gets the error message from an error.
* If `err` is a String itself, we return it.
* If the error has no message, we return an empty string.
*
* @name getMessage
* @param {Error|String} errorLike
* @namespace Utils
* @api public
*/
function getMessage(errorLike) {
var msg = '';
if (errorLike && errorLike.message) {
msg = errorLike.message;
} else if (typeof errorLike === 'string') {
msg = errorLike;
}
return msg;
}
module.exports = {
compatibleInstance: compatibleInstance,
compatibleConstructor: compatibleConstructor,
compatibleMessage: compatibleMessage,
getMessage: getMessage,
getConstructorName: getConstructorName,
};
},{}]},{},[1])(1)
});