import test from 'ava'
import * as crypto from 'crypto'

let root
let keyRoot

if ('WEBCRYPTO' in process.env) {
  root = keyRoot = '#dist/webcrypto'
} else if ('CRYPTOKEY' in process.env) {
  root = '#dist'
  keyRoot = '#dist/webcrypto'
} else {
  root = keyRoot = '#dist'
}

const { FlattenedSign, flattenedVerify, FlattenedEncrypt, flattenedDecrypt, base64url } =
  await import(root)
const { generateKeyPair, importJWK, exportPKCS8 } = await import(keyRoot)

function pubjwk(jwk) {
  const { d, p, q, dp, dq, qi, ...publicJwk } = jwk
  return publicJwk
}

test.before((t) => {
  const encode = TextEncoder.prototype.encode.bind(new TextEncoder())
  t.context.payload = encode('It’s a dangerous business, Frodo, going out your door.')
  t.context.rsa2040 = {
    e: 'AQAB',
    n: '4waoB9XUAsGc-bhkfY-v3hKEqmLYF4nS-8nji5R5KoOOeWC2hCkvbMfd2IlKRdMU7EmGXNx2BD2FVIqN9mWyZKJlzR2125lgJ-VxCymGv1A9057LEAFIrXsCUqwjPO07hCzZNv8IAAQzq53pnlAgb3TbfrxW24tamhCtaKHb5upAwo4jhYnfzex2--vD7mPxMoTuikno-eD_hxYmA52Uh1gu3wEWy44KA6aFJBpP7m4G5StuHSCXxiOWDaqMeFsMX1jqrom7SwbGJ7j0sf3ZqWrZR4x1pB3wk5Sixi_lmDfOkXhiizYnvJJ5rzr_f0bVdXeAe2U8vpEJSQeA36T7',
    d: 'T7ZPm2it50XZ-yiOSDQCWSQBZt4L57_hz7ykY6b_IDlO9jlJ_H-FgllvAI-7_2ZNC2YJgmN6IKUFQpjfnas5hvIqcmnDJ9bjlz6NgZDUGipvevVfcUAyJ49wUlzUhpj5c8BXiGLliTPwfIqWs5qIuPm78_TnPnDgoAXJDVr_njr_C6CJFVfWni_6MTeT8iSApGrIJ-tovLlUWSQAyKfWT0QK8dclmREla6-B4YPFwMuBViiSI8dGFpw1O-sEn7D1aMWRepWKr-dgvukuksdL39LxBeGz-iHpI_DMdSB63t5kyjT7GPlbBkD__4ie_Vl4bG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nhi7eJt06XDosMqbhxeh6FIWvl0x9DgtFlE',
    dq: 'UqK8VY0ftJlHLHXwDrV9yHqRZdEFP76c5jAXbFee-epAL_3bX4QW8WYxeAW7P1BMU7SkR_pNDh8d-6CC7Oz04aaxLd49nXhTDLHaDmP4rE4rB2CSZtnyfSIVwk3PBJOy80EtUjePWCTEx8-AkA_5sf7zr7ytkkvc_yd-1CggTdE',
    qi: 'd72eV7EbpvaSA3ZiQdGXpMMr41o0ih1WnV80Bxraugj1vMqxLlhVdDhDCVoF3LEoXVz8n2NEl1F6k2o3Gt9C5pXUDJwRGS41FwYVp8RN-aWviJM43mM0oQndJomZyDzjOKzpTzlNlAkFQQbfoagc4sbg-0JxK9rWdnMDW5AR1BU',
    kty: 'RSA',
  }
})

async function testRSAsig(t, alg) {
  const message = `${alg} requires key modulusLength to be 2048 bits or larger`
  const keyBad = t.context.rsa2040
  const keyOk = t.context.rsa2048
  await t.throwsAsync(
    new FlattenedSign(t.context.payload)
      .setProtectedHeader({ alg })
      .sign(await importJWK(keyBad, alg)),
    { instanceOf: TypeError, message },
  )

  const jws = await new FlattenedSign(t.context.payload)
    .setProtectedHeader({ alg })
    .sign(await importJWK(keyOk, alg))

  await t.throwsAsync(flattenedVerify(jws, await importJWK(pubjwk(keyBad), alg)), {
    instanceOf: TypeError,
    message,
  })
}
testRSAsig.title = (title, alg) => `${alg} requires key modulusLength to be 2048 bits or larger`

async function testRSAenc(t, alg) {
  const message = `${alg} requires key modulusLength to be 2048 bits or larger`
  const keyBad = t.context.rsa2040
  const keyOk = t.context.rsa2048
  await t.throwsAsync(
    new FlattenedEncrypt(t.context.payload)
      .setProtectedHeader({ alg, enc: 'A256GCM' })
      .encrypt(await importJWK(pubjwk(keyBad), alg)),
    { instanceOf: TypeError, message },
  )

  const jwe = await new FlattenedEncrypt(t.context.payload)
    .setProtectedHeader({ alg, enc: 'A256GCM' })
    .encrypt(await importJWK(pubjwk(keyOk), alg))

  await t.throwsAsync(flattenedDecrypt(jwe, await importJWK(keyBad, alg)), {
    instanceOf: TypeError,
    message,
  })
}
testRSAenc.title = (title, alg) => `${alg} requires key modulusLength to be 2048 bits or larger`

async function testECDSASigEncoding(t, alg) {
  let { privateKey, publicKey } = await generateKeyPair(alg, { extractable: true })

  const jws = await new FlattenedSign(t.context.payload)
    .setProtectedHeader({ alg })
    .sign(privateKey)

  const derEncodedSignature = base64url.encode(
    crypto.sign(`sha${alg.slice(2, 5)}`, Buffer.from('foo'), await exportPKCS8(privateKey)),
  )

  await t.throwsAsync(flattenedVerify({ ...jws, signature: derEncodedSignature }, publicKey), {
    message: 'signature verification failed',
    code: 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED',
  })
}
testECDSASigEncoding.title = (title, alg) => `${alg} swallows invalid signature encoding errors`

test(testRSAsig, 'RS256')
test(testRSAsig, 'PS256')
test(testRSAsig, 'RS384')
test(testRSAsig, 'PS384')
test(testRSAsig, 'RS512')
test(testRSAsig, 'PS512')

test(testRSAenc, 'RSA-OAEP')
test(testRSAenc, 'RSA-OAEP-256')
test(testRSAenc, 'RSA-OAEP-384')
test(testRSAenc, 'RSA-OAEP-512')

test(testECDSASigEncoding, 'ES256')
test(testECDSASigEncoding, 'ES384')
test(testECDSASigEncoding, 'ES512')

function conditional({ webcrypto = 1, electron = 1 } = {}) {
  let run = test
  if (!webcrypto && ('WEBCRYPTO' in process.env || 'CRYPTOKEY' in process.env)) {
    run = run.failing
  }

  if (!electron && 'electron' in process.versions) {
    run = run.failing
  }
  return run
}

conditional({ webcrypto: 0 })(testRSAenc, 'RSA1_5')
conditional({ webcrypto: 0, electron: 0 })(testECDSASigEncoding, 'ES256K')
