Skip to content

Commit 159feee

Browse files
authored
test: Testing cacheing CMM (#163)
Simple refactor to better test the node caching CMM
1 parent 970dbfa commit 159feee

File tree

4 files changed

+127
-6
lines changed

4 files changed

+127
-6
lines changed

modules/caching-materials-manager-node/src/caching_materials_manager_node.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,11 @@ import {
3131
NodeGetEncryptionMaterials, // eslint-disable-line no-unused-vars
3232
NodeGetDecryptMaterials // eslint-disable-line no-unused-vars
3333
} from '@aws-crypto/material-management-node'
34-
35-
import { createHash, randomBytes } from 'crypto'
34+
import { sha512 } from './sha512'
35+
import { randomBytes } from 'crypto'
3636

3737
const fromUtf8 = (input: string) => Buffer.from(input, 'utf8')
3838
const toUtf8 = (input: Uint8Array) => Buffer.from(input).toString('utf8')
39-
const sha512 = async (...data: (Uint8Array|string)[]) => data
40-
.map(item => typeof item === 'string' ? Buffer.from(item, 'hex') : item)
41-
.reduce((hash, item) => hash.update(item), createHash('sha512'))
42-
.digest()
4339

4440
const cacheKeyHelpers = buildCryptographicMaterialsCacheKeyHelpers(fromUtf8, toUtf8, sha512)
4541

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is
6+
* located at
7+
*
8+
* http://aws.amazon.com/apache2.0/
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
* implied. See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import { createHash } from 'crypto'
17+
18+
export const sha512 = async (...data: (Uint8Array|string)[]) => data
19+
.map(item => typeof item === 'string' ? Buffer.from(item) : item)
20+
.reduce((hash, item) => hash.update(item), createHash('sha512'))
21+
.digest()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is
6+
* located at
7+
*
8+
* http://aws.amazon.com/apache2.0/
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
* implied. See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
/* eslint-env mocha */
17+
18+
import { expect } from 'chai'
19+
import 'mocha'
20+
import { NodeCachingMaterialsManager } from '../src/index'
21+
import { } from '@aws-crypto/cache-material'
22+
import {
23+
KeyringNode,
24+
NodeDefaultCryptographicMaterialsManager,
25+
NodeEncryptionMaterial, // eslint-disable-line no-unused-vars
26+
NodeDecryptionMaterial // eslint-disable-line no-unused-vars
27+
} from '@aws-crypto/material-management-node'
28+
29+
describe('NodeCachingMaterialsManager', () => {
30+
it('constructor will decorate', () => {
31+
class TestKeyring extends KeyringNode {
32+
async _onEncrypt (): Promise<NodeEncryptionMaterial> {
33+
throw new Error('never')
34+
}
35+
async _onDecrypt (): Promise<NodeDecryptionMaterial> {
36+
throw new Error('never')
37+
}
38+
}
39+
40+
const keyring = new TestKeyring()
41+
const cache = 'cache' as any
42+
const partition = 'partition'
43+
const maxAge = 10
44+
const maxBytesEncrypted = 11
45+
const maxMessagesEncrypted = 12
46+
const test = new NodeCachingMaterialsManager({
47+
backingMaterials: keyring,
48+
cache,
49+
partition,
50+
maxAge,
51+
maxBytesEncrypted,
52+
maxMessagesEncrypted
53+
})
54+
55+
expect(test._backingMaterialsManager).to.be.instanceOf(NodeDefaultCryptographicMaterialsManager)
56+
expect(test._cache).to.equal(cache)
57+
expect(test._partition).to.equal(partition)
58+
expect(test._maxAge).to.equal(maxAge)
59+
expect(test._maxBytesEncrypted).to.equal(maxBytesEncrypted)
60+
expect(test._maxMessagesEncrypted).to.equal(maxMessagesEncrypted)
61+
})
62+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is
6+
* located at
7+
*
8+
* http://aws.amazon.com/apache2.0/
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
* implied. See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
/* eslint-env mocha */
17+
18+
import { expect } from 'chai'
19+
import 'mocha'
20+
import { sha512 } from '../src/sha512'
21+
22+
// sha512('asdf')
23+
const fixture = Buffer.from('QBsJ6rPAE9TKVJIruAK+yP1TGBkrCnXyAdizcnQpCA+zN1kavT5ERTuVRVW3oIEuEIHDm3QCk/dl6ucx9aZe0Q==', 'base64')
24+
25+
describe('WebCryptoCachingMaterialsManager', () => {
26+
it('can hash a string', async () => {
27+
const test = await sha512('asdf')
28+
expect(test).to.deep.equal(fixture)
29+
})
30+
31+
it('can hash a Uint8Array', async () => {
32+
// the string 'asdf' as utf-8 encoded bytes
33+
const test = await sha512(Buffer.from([ 97, 115, 100, 102 ]))
34+
expect(test).to.deep.equal(fixture)
35+
})
36+
37+
it('can hash a mix of arguments', async () => {
38+
// the string 'asdf' as a mix of strings and binary
39+
const test = await sha512('a', new Uint8Array([115]), 'd', Buffer.from([102]))
40+
expect(test).to.deep.equal(fixture)
41+
})
42+
})

0 commit comments

Comments
 (0)