Skip to content

Commit 3468cb5

Browse files
committed
test pass
1 parent b4cca21 commit 3468cb5

File tree

4 files changed

+35
-79
lines changed

4 files changed

+35
-79
lines changed

modules/cache-material/src/caching_cryptographic_materials_decorators.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,32 @@ export function getEncryptionMaterials<S extends SupportedAlgorithmSuites> (
8787
this._cache.del(cacheKey)
8888
}
8989

90-
const response = await this
90+
const material = await this
9191
._backingMaterialsManager
9292
/* Strip any information about the plaintext from the backing request,
9393
* because the resulting response may be used to encrypt multiple plaintexts.
9494
*/
9595
.getEncryptionMaterials({ suite, encryptionContext, frameLength })
9696

9797
/* Check for early return (Postcondition): If I can not cache the EncryptionResponse, just return it. */
98-
if (!response.suite.cacheSafe) return response
98+
if (!material.suite.cacheSafe) return material
9999

100100
/* It is possible for an entry to exceed limits immediately.
101101
* The simplest case is to need to encrypt more than then maxBytesEncrypted.
102102
* In this case, I return the response to encrypt the data,
103103
* but do not put a know invalid item into the cache.
104104
*/
105105
const testEntry = {
106-
response,
106+
response: material,
107107
now: Date.now(),
108108
messagesEncrypted: 1,
109109
bytesEncrypted: plaintextLength
110110
}
111111
if (!this._cacheEntryHasExceededLimits(testEntry)) {
112-
this._cache.putEncryptionResponse(cacheKey, response, plaintextLength, this._maxAge)
112+
this._cache.putEncryptionResponse(cacheKey, material, plaintextLength, this._maxAge)
113113
}
114114

115-
return cloneResponse(response)
115+
return cloneResponse(material)
116116
}
117117
}
118118

@@ -140,12 +140,12 @@ export function decryptMaterials<S extends SupportedAlgorithmSuites> (
140140
this._cache.del(cacheKey)
141141
}
142142

143-
const response = await this
143+
const material = await this
144144
._backingMaterialsManager
145145
.decryptMaterials(request)
146146

147-
this._cache.putDecryptionResponse(cacheKey, response, this._maxAge)
148-
return cloneResponse(response)
147+
this._cache.putDecryptionResponse(cacheKey, material, this._maxAge)
148+
return cloneResponse(material)
149149
}
150150
}
151151

modules/cache-material/test/caching_cryptographic_materials_decorators.test.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -216,25 +216,17 @@ describe('Cryptographic Material Functions', () => {
216216
.setUnencryptedDataKey(udk128, trace)
217217

218218
const context = {}
219-
const encryptionResponse = {
220-
material: encryptionMaterial,
221-
context
222-
}
223-
const decryptionResponse = {
224-
material: decryptionMaterial,
225-
context
226-
}
227219

228220
const _maxAge = 10
229221
const _maxBytesEncrypted = 10
230222
const _maxMessagesEncrypted = 10
231223
const _cache = getLocalCryptographicMaterialsCache(100)
232224
const _backingMaterialsManager = {
233225
getEncryptionMaterials () {
234-
return encryptionResponse
226+
return encryptionMaterial
235227
},
236228
decryptMaterials () {
237-
return decryptionResponse
229+
return decryptionMaterial
238230
}
239231
} as any
240232
const _partition = 'partition'
@@ -268,11 +260,9 @@ describe('Cryptographic Material Functions', () => {
268260
plaintextLength: 10
269261
})
270262
// The response must be cloned... i.e. not the same.
271-
expect(test === encryptionResponse).to.equal(false)
272-
// the material must be cloned... because after use it will be zeroed
273-
expect(test.material === encryptionResponse.material).to.equal(false)
274-
expect(test.context === encryptionResponse.context).to.equal(true)
275-
expect(test.material.getUnencryptedDataKey()).to.deep.equal(encryptionMaterial.getUnencryptedDataKey())
263+
expect(test === encryptionMaterial).to.equal(false)
264+
expect(test.encryptionContext).to.deep.equal(encryptionMaterial.encryptionContext)
265+
expect(test.getUnencryptedDataKey()).to.deep.equal(encryptionMaterial.getUnencryptedDataKey())
276266
})
277267
})
278268

@@ -284,11 +274,9 @@ describe('Cryptographic Material Functions', () => {
284274
encryptedDataKeys: [edk1]
285275
})
286276
// The response must be cloned... i.e. not the same.
287-
expect(test === decryptionResponse).to.equal(false)
288-
// the material must be cloned... because after use it will be zeroed
289-
expect(test.material === decryptionResponse.material).to.equal(false)
290-
expect(test.context === decryptionResponse.context).to.equal(true)
291-
expect(test.material.getUnencryptedDataKey()).to.deep.equal(decryptionMaterial.getUnencryptedDataKey())
277+
expect(test === decryptionMaterial).to.equal(false)
278+
expect(test.encryptionContext).to.deep.equal(decryptionMaterial.encryptionContext)
279+
expect(test.getUnencryptedDataKey()).to.deep.equal(decryptionMaterial.getUnencryptedDataKey())
292280
})
293281
})
294282
})

modules/cache-material/test/get_local_cryptographic_materials_cache.test.ts

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ describe('getLocalCryptographicMaterialsCache', () => {
4040

4141
it('putEncryptionResponse', () => {
4242
const key = 'some encryption key'
43-
const response: any = {
44-
material: encryptionMaterial
45-
}
43+
const response: any = encryptionMaterial
4644

4745
putEncryptionResponse(key, response, 1)
4846
const test = getEncryptionResponse(key, 1)
@@ -54,9 +52,7 @@ describe('getLocalCryptographicMaterialsCache', () => {
5452
})
5553

5654
it('Precondition: putEncryptionResponse plaintextLength can not be negative.', () => {
57-
const response: any = {
58-
material: encryptionMaterial
59-
}
55+
const response: any = encryptionMaterial
6056
const u: any = undefined
6157
const s: any = 'not-number'
6258
const n = -1
@@ -67,9 +63,7 @@ describe('getLocalCryptographicMaterialsCache', () => {
6763

6864
it('Postcondition: Only return EncryptionMaterial.', () => {
6965
const key = 'some decryption key'
70-
const response: any = {
71-
material: decryptionMaterial
72-
}
66+
const response: any = decryptionMaterial
7367

7468
putDecryptionResponse(key, response)
7569
expect(() => getEncryptionResponse(key, 1)).to.throw()
@@ -78,18 +72,14 @@ describe('getLocalCryptographicMaterialsCache', () => {
7872
it('Precondition: Only cache EncryptionMaterial that is cacheSafe.', () => {
7973
const key = 'some encryption key'
8074
const suite = new NodeAlgorithmSuite(AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16)
81-
const response: any = {
82-
material: new NodeEncryptionMaterial(suite, {})
83-
}
75+
const response: any = new NodeEncryptionMaterial(suite, {})
8476

8577
expect(() => putEncryptionResponse(key, response, 1)).to.throw()
8678
})
8779

8880
it('putDecryptionResponse', () => {
8981
const key = 'some decryption key'
90-
const response: any = {
91-
material: decryptionMaterial
92-
}
82+
const response: any = decryptionMaterial
9383

9484
putDecryptionResponse(key, response)
9585
const test = getDecryptionResponse(key)
@@ -102,19 +92,15 @@ describe('getLocalCryptographicMaterialsCache', () => {
10292

10393
it('Precondition: Only cache DecryptionMaterial.', () => {
10494
const key = 'some decryption key'
105-
const response: any = {
106-
material: 'not material'
107-
}
95+
const response: any = 'not material'
10896

10997
expect(() => putDecryptionResponse(key, response)).to.throw()
11098
})
11199

112100
it('Precondition: Only cache DecryptionMaterial that is cacheSafe.', () => {
113101
const key = 'some decryption key'
114102
const suite = new NodeAlgorithmSuite(AlgorithmSuiteIdentifier.ALG_AES128_GCM_IV12_TAG16)
115-
const response: any = {
116-
material: new NodeEncryptionMaterial(suite, {})
117-
}
103+
const response: any = new NodeEncryptionMaterial(suite, {})
118104

119105
expect(() => putDecryptionResponse(key, response)).to.throw()
120106
})
@@ -135,9 +121,7 @@ describe('getLocalCryptographicMaterialsCache', () => {
135121

136122
it('Precondition: Only cache EncryptionMaterial.', () => {
137123
const key = 'some encryption key'
138-
const response: any = {
139-
material: 'not material'
140-
}
124+
const response: any = 'not material'
141125

142126
expect(() => putEncryptionResponse(key, response, 1)).to.throw()
143127
})
@@ -149,9 +133,7 @@ describe('getLocalCryptographicMaterialsCache', () => {
149133

150134
it('Postcondition: Only return DecryptionMaterial.', () => {
151135
const key = 'some encryption key'
152-
const response: any = {
153-
material: encryptionMaterial
154-
}
136+
const response: any = encryptionMaterial
155137

156138
putEncryptionResponse(key, response, 1)
157139
expect(() => getDecryptionResponse(key))
@@ -163,9 +145,7 @@ describe('getLocalCryptographicMaterialsCache', () => {
163145

164146
it('zero is an acceptable plaintextLength', () => {
165147
const key = 'some encryption key'
166-
const response: any = {
167-
material: encryptionMaterial
168-
}
148+
const response: any = encryptionMaterial
169149

170150
putEncryptionResponse(key, response, 0)
171151
const test = getEncryptionResponse(key, 0)
@@ -186,9 +166,7 @@ describe('cache eviction', () => {
186166

187167
const key1 = 'key lost'
188168
const key2 = 'key replace'
189-
const response: any = {
190-
material: decryptionMaterial
191-
}
169+
const response: any = decryptionMaterial
192170

193171
putDecryptionResponse(key1, response)
194172
putDecryptionResponse(key2, response)
@@ -206,9 +184,7 @@ describe('cache eviction', () => {
206184
} = getLocalCryptographicMaterialsCache(1)
207185

208186
const key = 'key deleted'
209-
const response: any = {
210-
material: decryptionMaterial
211-
}
187+
const response: any = decryptionMaterial
212188

213189
putDecryptionResponse(key, response)
214190
del(key)
@@ -223,9 +199,7 @@ describe('cache eviction', () => {
223199
} = getLocalCryptographicMaterialsCache(1, 10)
224200

225201
const key = 'key lost'
226-
const response: any = {
227-
material: decryptionMaterial
228-
}
202+
const response: any = decryptionMaterial
229203

230204
putDecryptionResponse(key, response, 1)
231205
await new Promise(resolve => setTimeout(resolve, 20))
@@ -241,9 +215,7 @@ describe('cache eviction', () => {
241215

242216
const key1 = 'key lost'
243217
const key2 = 'key replace'
244-
const response: any = {
245-
material: encryptionMaterial
246-
}
218+
const response: any = encryptionMaterial
247219

248220
putEncryptionResponse(key1, response, 0)
249221
putEncryptionResponse(key2, response, 0)
@@ -261,9 +233,7 @@ describe('cache eviction', () => {
261233
} = getLocalCryptographicMaterialsCache(1, 10)
262234

263235
const key = 'key lost'
264-
const response: any = {
265-
material: encryptionMaterial
266-
}
236+
const response: any = encryptionMaterial
267237

268238
putEncryptionResponse(key, response, 1, 1)
269239
del(key)
@@ -278,9 +248,7 @@ describe('cache eviction', () => {
278248
} = getLocalCryptographicMaterialsCache(1, 10)
279249

280250
const key = 'key lost'
281-
const response: any = {
282-
material: encryptionMaterial
283-
}
251+
const response: any = encryptionMaterial
284252

285253
putEncryptionResponse(key, response, 1, 1)
286254
await new Promise(resolve => setTimeout(resolve, 20))

modules/material-management-browser/test/browser_cryptographic_materials_manager.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('WebCryptoDefaultCryptographicMaterialsManager', () => {
103103
expect(encryptionContext).to.have.haveOwnProperty('some').and.to.equal('context')
104104
})
105105

106-
it('set a verificationKey from context', async () => {
106+
it('set a verificationKey from encryption context', async () => {
107107
class TestKeyring extends KeyringWebCrypto {
108108
async _onEncrypt (): Promise<WebCryptoEncryptionMaterial> {
109109
throw new Error('I should never see this error')
@@ -179,7 +179,7 @@ describe('WebCryptoDefaultCryptographicMaterialsManager', () => {
179179
expect(cmm._initializeDecryptionMaterial(suite, context)).to.rejectedWith(Error)
180180
})
181181

182-
it('can return an encryption response', async () => {
182+
it('can return a encryption material', async () => {
183183
class TestKeyring extends KeyringWebCrypto {
184184
async _onEncrypt (material: WebCryptoEncryptionMaterial): Promise<WebCryptoEncryptionMaterial> {
185185
const udk = synchronousRandomValues(suite.keyLengthBytes)
@@ -288,7 +288,7 @@ describe('WebCryptoDefaultCryptographicMaterialsManager', () => {
288288
expect(cmm.getEncryptionMaterials({ encryptionContext })).to.rejectedWith(Error)
289289
})
290290

291-
it('can return a decryption response', async () => {
291+
it('can return decryption material', async () => {
292292
class TestKeyring extends KeyringWebCrypto {
293293
async _onEncrypt (): Promise<WebCryptoEncryptionMaterial> {
294294
throw new Error('I should never see this error')

0 commit comments

Comments
 (0)