Skip to content

test: Testing cacheing CMM #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ import {
NodeGetEncryptionMaterials, // eslint-disable-line no-unused-vars
NodeGetDecryptMaterials // eslint-disable-line no-unused-vars
} from '@aws-crypto/material-management-node'

import { createHash, randomBytes } from 'crypto'
import { sha512 } from './sha512'
import { randomBytes } from 'crypto'

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

const cacheKeyHelpers = buildCryptographicMaterialsCacheKeyHelpers(fromUtf8, toUtf8, sha512)

Expand Down
21 changes: 21 additions & 0 deletions modules/caching-materials-manager-node/src/sha512.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

import { createHash } from 'crypto'

export const sha512 = async (...data: (Uint8Array|string)[]) => data
.map(item => typeof item === 'string' ? Buffer.from(item) : item)
.reduce((hash, item) => hash.update(item), createHash('sha512'))
.digest()
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

/* eslint-env mocha */

import { expect } from 'chai'
import 'mocha'
import { NodeCachingMaterialsManager } from '../src/index'
import { } from '@aws-crypto/cache-material'
import {
KeyringNode,
NodeDefaultCryptographicMaterialsManager,
NodeEncryptionMaterial, // eslint-disable-line no-unused-vars
NodeDecryptionMaterial // eslint-disable-line no-unused-vars
} from '@aws-crypto/material-management-node'

describe('NodeCachingMaterialsManager', () => {
it('constructor will decorate', () => {
class TestKeyring extends KeyringNode {
async _onEncrypt (): Promise<NodeEncryptionMaterial> {
throw new Error('never')
}
async _onDecrypt (): Promise<NodeDecryptionMaterial> {
throw new Error('never')
}
}

const keyring = new TestKeyring()
const cache = 'cache' as any
const partition = 'partition'
const maxAge = 10
const maxBytesEncrypted = 11
const maxMessagesEncrypted = 12
const test = new NodeCachingMaterialsManager({
backingMaterials: keyring,
cache,
partition,
maxAge,
maxBytesEncrypted,
maxMessagesEncrypted
})

expect(test._backingMaterialsManager).to.be.instanceOf(NodeDefaultCryptographicMaterialsManager)
expect(test._cache).to.equal(cache)
expect(test._partition).to.equal(partition)
expect(test._maxAge).to.equal(maxAge)
expect(test._maxBytesEncrypted).to.equal(maxBytesEncrypted)
expect(test._maxMessagesEncrypted).to.equal(maxMessagesEncrypted)
})
})
42 changes: 42 additions & 0 deletions modules/caching-materials-manager-node/test/sha512.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

/* eslint-env mocha */

import { expect } from 'chai'
import 'mocha'
import { sha512 } from '../src/sha512'

// sha512('asdf')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be base64(sha512('asdf'))?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are not wrong. I left the base64 off because the important part was the 'asdf'

const fixture = Buffer.from('QBsJ6rPAE9TKVJIruAK+yP1TGBkrCnXyAdizcnQpCA+zN1kavT5ERTuVRVW3oIEuEIHDm3QCk/dl6ucx9aZe0Q==', 'base64')

describe('WebCryptoCachingMaterialsManager', () => {
it('can hash a string', async () => {
const test = await sha512('asdf')
expect(test).to.deep.equal(fixture)
})

it('can hash a Uint8Array', async () => {
// the string 'asdf' as utf-8 encoded bytes
const test = await sha512(Buffer.from([ 97, 115, 100, 102 ]))
expect(test).to.deep.equal(fixture)
})

it('can hash a mix of arguments', async () => {
// the string 'asdf' as a mix of strings and binary
const test = await sha512('a', new Uint8Array([115]), 'd', Buffer.from([102]))
expect(test).to.deep.equal(fixture)
})
})