/*
 * Copyright (C) Sapphirecode - All Rights Reserved
 * This file is part of crypto-helper which is released under MIT.
 * See file 'LICENSE' for full license details.
 * Created by Timo Hocker <timo@scode.ovh>, May 2020
 */

/* eslint-disable no-magic-numbers */
// @ts-nocheck
'use strict';

const test = require ('ava');
const crypto = require ('../index');

test ('encryption', (t) => {
  const enc = crypto.encrypt_aes ('foo', 'bar');
  t.is (typeof enc, 'string');
});

test ('decryption', (t) => {
  const enc = crypto.encrypt_aes ('foo', 'bar');
  const dec = crypto.decrypt_aes (enc, 'bar');
  t.is (dec, 'foo');
});

test ('encryption 128', (t) => {
  const enc = crypto.encrypt_aes (
    'foo',
    'bar',
    crypto.encryption_mode_cbc_128
  );
  t.is (typeof enc, 'string');
});

test ('decryption 128', (t) => {
  const enc = crypto.encrypt_aes (
    'foo',
    'bar',
    crypto.encryption_mode_cbc_128
  );
  const dec = crypto.decrypt_aes (
    enc,
    'bar',
    crypto.encryption_mode_cbc_128
  );
  t.is (dec, 'foo');
});

test ('encryption 256_quick', (t) => {
  const enc = crypto.encrypt_aes (
    'foo',
    'bar',
    crypto.encryption_mode_cbc_256_quick
  );
  t.is (typeof enc, 'string');
});

test ('decryption 256_quick', (t) => {
  const enc = crypto.encrypt_aes (
    'foo',
    'bar',
    crypto.encryption_mode_cbc_256_quick
  );
  const dec = crypto.decrypt_aes (
    enc,
    'bar',
    crypto.encryption_mode_cbc_256_quick
  );
  t.is (dec, 'foo');
});

test ('fail decryption', (t) => {
  const enc = crypto.encrypt_aes ('foo', 'bar');
  const dec = crypto.decrypt_aes (enc, 'baz');
  t.is (dec, null);
});

test ('rethrow decryption', (t) => {
  const enc = crypto.encrypt_aes ('foo', 'bar');
  t.throws (() => {
    crypto.decrypt_aes (
      enc,
      'baz',
      crypto.encryption_mode_cbc_256,
      true
    );
  });
});

test ('unique crypto strings', (t) => {
  const enc = [
    crypto.encrypt_aes ('foo', 'bar'),
    crypto.encrypt_aes ('foo', 'bar'),
    crypto.encrypt_aes ('foo', 'bar'),
    crypto.encrypt_aes ('foo', 'bar'),
    crypto.encrypt_aes ('foo', 'bar'),
    crypto.encrypt_aes ('foo', 'bar'),
    crypto.encrypt_aes ('foo', 'bar'),
    crypto.encrypt_aes ('foo', 'bar')
  ];
  const unique = enc.filter ((v, i) => enc.indexOf (v) === i).length;
  t.is (unique, 8);
});