export async function _getDecryptTestVectorIterator()

in modules/integration-vectors/src/get_decrypt_test_iterator.ts [57:113]


export async function _getDecryptTestVectorIterator(
  filesMap: Map<string, StreamEntry>
): Promise<IterableIterator<TestVectorInfo>> {
  const { tests, keys } = await parseDecryptionFiles(filesMap)
  return (function* nextTest(): IterableIterator<TestVectorInfo> {
    for (const [name, testInfo] of Object.entries(tests)) {
      const {
        description,
        result,
        ciphertext,
        'master-keys': masterKeys,
        'decryption-method': decryptionMethod,
      } = testInfo
      const cipherInfo = filesMap.get(ciphertext)
      if (!cipherInfo) throw new Error(`no file for ${name}: ${ciphertext}`)
      const cipherStream = cipherInfo.stream

      const keysInfo = masterKeys.map((keyInfo) => {
        if (keyInfo.type === 'aws-kms-mrk-aware-discovery') {
          return [keyInfo] as KeyInfoTuple
        }
        const key = keys[keyInfo.key]
        if (!key) throw new Error(`no key for ${name}`)
        return [keyInfo, key] as KeyInfoTuple
      })

      if (result && 'output' in result) {
        const plainTextInfo = filesMap.get(result.output.plaintext)
        if (!plainTextInfo)
          throw new Error(
            `no plaintext file for ${name}: ${result.output.plaintext}`
          )
        //Yield Positive Decryption Test
        yield {
          name,
          description,
          keysInfo,
          cipherStream,
          decryptionMethod,
          plainTextStream: plainTextInfo.stream,
        }
      } else if (result && 'error' in result) {
        //Yield Negative Decryption Test
        yield {
          name,
          description,
          keysInfo,
          cipherStream,
          decryptionMethod,
          errorDescription: result.error['error-description'],
        }
      } else {
        throw new Error(`Could not parse vector for ${name}`)
      }
    }
  })()
}