Skip to content

Commit 1c39aa8

Browse files
authored
feat: Browser testing (#100)
* Browser testing using Karma * Convert web-crypt-backend test to Karma * add raw-aes-keyring-browser tests * fix raw-aes-keyring-browser providerInfo serialization * add raw-rsa-keyring-browser tests
1 parent 03b8d13 commit 1c39aa8

15 files changed

+785
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package.json.decrypt
1818

1919
# nyc/code coverage
2020
.nyc_output
21+
.karma_output
2122
coverage
2223

2324
# symlink to test vectors

karma.conf.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Karma configuration
2+
process.env.CHROME_BIN = require('puppeteer').executablePath()
3+
const webpack = require('webpack')
4+
5+
module.exports = function (config) {
6+
config.set({
7+
basePath: '',
8+
frameworks: ['mocha', 'chai'],
9+
files: [
10+
'modules/*-browser/test/**/*.ts',
11+
'modules/web-crypto-backend/test/**/*.ts',
12+
],
13+
preprocessors: {
14+
'modules/*-browser/test/**/*.ts': ['webpack', 'credentials'],
15+
'modules/web-crypto-backend/test/**/*.ts': ['webpack', 'credentials'],
16+
},
17+
webpack: {
18+
resolve: {
19+
extensions: [ '.ts', '.js' ]
20+
},
21+
mode: 'development',
22+
module: {
23+
rules: [
24+
{
25+
test: /\.tsx?$/,
26+
use: [
27+
{
28+
loader: 'ts-loader',
29+
options: {
30+
configFile: 'tsconfig.module.json',
31+
compilerOptions: {
32+
rootDir: './'
33+
}
34+
}
35+
}
36+
],
37+
exclude: /node_modules/,
38+
},
39+
{
40+
test: /\.ts$/,
41+
exclude: [ /\/test\// ],
42+
enforce: 'post',
43+
use: {
44+
loader: 'istanbul-instrumenter-loader',
45+
options: { esModules: true }
46+
}
47+
}
48+
]
49+
},
50+
stats: {
51+
colors: true,
52+
modules: true,
53+
reasons: true,
54+
errorDetails: true
55+
},
56+
devtool: 'inline-source-map',
57+
node: {
58+
fs: 'empty'
59+
}
60+
},
61+
coverageIstanbulReporter: {
62+
reports: [ 'json' ],
63+
dir: '.karma_output',
64+
fixWebpackSourcePaths: true
65+
},
66+
plugins: [
67+
'@aws-sdk/karma-credential-loader',
68+
'karma-chrome-launcher',
69+
'karma-mocha',
70+
'karma-chai',
71+
'karma-webpack',
72+
'karma-coverage-istanbul-reporter',
73+
'karma-json-fixtures-preprocessor'
74+
],
75+
reporters: ['progress', 'coverage-istanbul'],
76+
port: 9876,
77+
colors: true,
78+
logLevel: config.LOG_INFO,
79+
autoWatch: false,
80+
browsers: ['ChromeHeadlessDisableCors'],
81+
customLaunchers: {
82+
ChromeHeadlessDisableCors: {
83+
base: 'ChromeHeadless',
84+
flags: ['--disable-web-security', '--no-sandbox']
85+
}
86+
},
87+
singleRun: true,
88+
concurrency: Infinity,
89+
exclude: ['**/*.d.ts']
90+
})
91+
}

lerna.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"command": {
88
"bootstrap": {
99
"nohoist": [
10-
"typedoc",
11-
"karma*"
10+
"typedoc"
1211
]
1312
}
1413
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Karma configuration
2+
process.env.CHROME_BIN = require('puppeteer').executablePath()
3+
4+
module.exports = function (config) {
5+
config.set({
6+
basePath: '',
7+
frameworks: ['mocha', 'chai'],
8+
files: [
9+
'test/**/*.ts'
10+
],
11+
preprocessors: {
12+
'test/**/*.ts': ['webpack', 'credentials']
13+
},
14+
webpack: {
15+
resolve: {
16+
extensions: [ '.ts', '.js' ]
17+
},
18+
mode: 'development',
19+
module: {
20+
rules: [
21+
{
22+
test: /\.tsx?$/,
23+
use: [
24+
{
25+
loader: 'ts-loader',
26+
options: {
27+
configFile: 'tsconfig.module.json',
28+
compilerOptions: {
29+
rootDir: './'
30+
}
31+
}
32+
}
33+
],
34+
exclude: /node_modules/,
35+
},
36+
{
37+
test: /\.ts$/,
38+
exclude: [ /\/test\// ],
39+
enforce: 'post',
40+
use: {
41+
loader: 'istanbul-instrumenter-loader',
42+
options: { esModules: true }
43+
}
44+
}
45+
]
46+
},
47+
stats: {
48+
colors: true,
49+
modules: true,
50+
reasons: true,
51+
errorDetails: true
52+
},
53+
devtool: 'inline-source-map',
54+
node: {
55+
fs: 'empty'
56+
}
57+
},
58+
coverageIstanbulReporter: {
59+
reports: [ 'json' ],
60+
dir: '.karma_output',
61+
fixWebpackSourcePaths: true
62+
},
63+
plugins: [
64+
'@aws-sdk/karma-credential-loader',
65+
'karma-chrome-launcher',
66+
'karma-mocha',
67+
'karma-chai',
68+
'karma-webpack',
69+
'karma-coverage-istanbul-reporter'
70+
],
71+
reporters: ['progress', 'coverage-istanbul'],
72+
port: 9876,
73+
colors: true,
74+
logLevel: config.LOG_INFO,
75+
autoWatch: false,
76+
browsers: ['ChromeHeadlessDisableCors'],
77+
customLaunchers: {
78+
ChromeHeadlessDisableCors: {
79+
base: 'ChromeHeadless',
80+
flags: ['--disable-web-security']
81+
}
82+
},
83+
singleRun: true,
84+
concurrency: Infinity,
85+
exclude: ['**/*.d.ts']
86+
})
87+
}

modules/raw-aes-keyring-browser/package.json

+14-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"prepublishOnly": "npm run build",
77
"build": "tsc -b tsconfig.json && tsc -b tsconfig.module.json",
88
"lint": "standard src/*.ts test/**/*.ts",
9-
"mocha": "mocha --require ts-node/register test/**/*test.ts",
9+
"karma": "karma start karma.conf.js",
1010
"test": "npm run lint && npm run coverage",
11-
"coverage": "nyc -e .ts npm run mocha"
11+
"coverage": "npm run karma && nyc report --exclude-after-remap false -t .karma_output --check-coverage"
1212
},
1313
"author": {
1414
"name": "AWS Crypto Tools Team",
@@ -26,12 +26,21 @@
2626
"tslib": "^1.9.3"
2727
},
2828
"devDependencies": {
29+
"@aws-sdk/karma-credential-loader": "0.1.0-preview.2",
2930
"@types/chai": "^4.1.4",
3031
"@types/mocha": "^5.2.5",
3132
"@types/node": "^11.11.4",
3233
"@typescript-eslint/eslint-plugin": "^1.9.0",
3334
"@typescript-eslint/parser": "^1.9.0",
3435
"chai": "^4.1.2",
36+
"karma": "^4.1.0",
37+
"karma-chai": "^0.1.0",
38+
"karma-chrome-launcher": "^2.2.0",
39+
"karma-mocha": "^1.3.0",
40+
"karma-webpack": "^3.0.5",
41+
"istanbul-instrumenter-loader": "^3.0.1",
42+
"karma-coverage-istanbul-reporter": "^2.0.4",
43+
"ts-loader": "^5.3.3",
3544
"mocha": "^5.2.0",
3645
"nyc": "^14.0.0",
3746
"standard": "^12.0.1",
@@ -42,7 +51,9 @@
4251
"main": "./build/main/index.js",
4352
"module": "./build/module/index.js",
4453
"types": "./build/main/index.d.ts",
45-
"files": ["./build/**/*"],
54+
"files": [
55+
"./build/**/*"
56+
],
4657
"standard": {
4758
"parser": "@typescript-eslint/parser",
4859
"plugins": [

modules/raw-aes-keyring-browser/src/raw_aes_keyring_browser.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class RawAesKeyringWebCrypto extends KeyringWebCrypto {
125125
*/
126126
_onDecrypt = _onDecrypt<WebCryptoAlgorithmSuite, RawAesKeyringWebCrypto>()
127127

128-
static importCryptoKey (masterKey: Uint8Array, wrappingSuite: WrappingSuiteIdentifier) {
128+
static async importCryptoKey (masterKey: Uint8Array, wrappingSuite: WrappingSuiteIdentifier) {
129129
needs(masterKey instanceof Uint8Array, 'Unsupported master key type.')
130130
const material = new WebCryptoRawAesMaterial(wrappingSuite)
131131
/* Precondition: masterKey must correspond to the algorithm suite specification.
@@ -164,8 +164,10 @@ async function aesGcmWrapKey (
164164
const info = new Uint8Array()
165165
const dataKey = material.getUnencryptedDataKey()
166166
const buffer = await kdfGetSubtleEncrypt(info)(iv, aad)(dataKey)
167+
const ciphertext = new Uint8Array(buffer, 0, buffer.byteLength - material.suite.tagLength / 8)
168+
const authTag = new Uint8Array(buffer, buffer.byteLength - material.suite.tagLength / 8)
167169

168-
const edk = rawAesEncryptedDataKey(keyNamespace, keyName, iv, new Uint8Array(buffer), new Uint8Array())
170+
const edk = rawAesEncryptedDataKey(keyNamespace, keyName, iv, ciphertext, authTag)
169171
return material.addEncryptedDataKey(edk, encryptFlags)
170172
}
171173

0 commit comments

Comments
 (0)