/*
 * Copyright 2018 Scytl Secure Electronic Voting SA
 *
 * All rights reserved
 *
 * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
 */
/* jshint node:true */
'use strict';

var keyStore = require('../lib/index');

describe('The key store module that should be able to ...', function() {
  var PASSWORD = 'PM6XtkOTbefJmhHGFgbz';
  var PRIVATE_KEY_ALIAS = 'root_rsa_private_key';
  var PKCS12_DER_B64 =
      '[encoded data, 8884 characters]';

  var keyStoreService_;
  var pkcs12KeyStore_;
  var nonUint8Array_;
  var nonString_;
  var emptyString_;
  var wrongPkcs12DerB64_;
  var wrongPassword_;
  var wrongAlias_;
  var wrongSubjectCn_;

  beforeAll(function() {
    keyStoreService_ = keyStore.newService();
    pkcs12KeyStore_ =
        keyStoreService_.newPkcs12KeyStore(PKCS12_DER_B64, PASSWORD);
    nonUint8Array_ = [1, 2, 3];
    nonString_ = 0;
    emptyString_ = '';
    wrongPkcs12DerB64_ = 'Wrong PKCS12';
    wrongPassword_ = 'Wrong password';
    wrongAlias_ = 'Wrong alias';
    wrongSubjectCn_ = 'Wrong subject common name';
  });

  describe('create a key store service that should be able to ..', function() {
    it('throw an error when loading a PKCS12 key store, using an invalid store',
       function() {
         expect(function() {
           keyStoreService_.newPkcs12KeyStore(undefined, PASSWORD);
         }).toThrow();

         expect(function() {
           keyStoreService_.newPkcs12KeyStore(null, PASSWORD);
         }).toThrow();

         expect(function() {
           keyStoreService_.newPkcs12KeyStore(nonUint8Array_, PASSWORD);
         }).toThrow();

         expect(function() {
           keyStoreService_.newPkcs12KeyStore(emptyString_, PASSWORD);
         }).toThrow();
       });

    it('throw an error when loading a PKCS12 key store, using an invalid password',
       function() {
         expect(function() {
           keyStoreService_.newPkcs12KeyStore(PKCS12_DER_B64);
         }).toThrow();

         expect(function() {
           keyStoreService_.newPkcs12KeyStore(PKCS12_DER_B64, undefined);
         }).toThrow();

         expect(function() {
           keyStoreService_.newPkcs12KeyStore(PKCS12_DER_B64, null);
         }).toThrow();

         expect(function() {
           keyStoreService_.newPkcs12KeyStore(PKCS12_DER_B64, nonString_);
         }).toThrow();

         expect(function() {
           keyStoreService_.newPkcs12KeyStore(PKCS12_DER_B64, wrongPassword_);
         }).toThrow();
       });

    describe('load a PKCS12 key store that should be able to', function() {
      it('throw an error when retrieving a private key, using an invalid alias',
         function() {
           expect(function() {
             pkcs12KeyStore_.getPrivateKey(undefined, PASSWORD);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getPrivateKey(null, PASSWORD);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getPrivateKey('', PASSWORD);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getPrivateKey(nonString_, PASSWORD);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getPrivateKey(wrongAlias_, PASSWORD);
           }).toThrow();
         });

      it('throw an error when retrieving a private key, using an invalid password',
         function() {
           expect(function() {
             pkcs12KeyStore_.getPrivateKey(PRIVATE_KEY_ALIAS);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getPrivateKey(PRIVATE_KEY_ALIAS, undefined);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getPrivateKey(PRIVATE_KEY_ALIAS, null);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getPrivateKey(PRIVATE_KEY_ALIAS, nonString_);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getPrivateKey(PRIVATE_KEY_ALIAS, wrongPassword_);
           }).toThrow();
         });

      it('throw an error when retrieving a certificate, using invalid input data',
         function() {
           expect(function() {
             pkcs12KeyStore_.getCertificate();
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificate(undefined);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificate(null);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificate('');
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificate(nonString_);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificate(wrongAlias_);
           }).toThrow();
         });

      it('throw an error when retrieving a certificate by subject, using invalid input data',
         function() {
           expect(function() {
             pkcs12KeyStore_.getCertificateBySubject();
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificateBySubject(undefined);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificateBySubject(null);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificateBySubject('');
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificateBySubject(nonString_);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificateBySubject(wrongSubjectCn_);
           }).toThrow();
         });

      it('throw and error when retrieving a certificate chain, using invalid input data',
         function() {
           expect(function() {
             pkcs12KeyStore_.getCertificateChain();
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificateChain(undefined);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificateChain(null);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificateChain('');
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificateChain(nonString_);
           }).toThrow();

           expect(function() {
             pkcs12KeyStore_.getCertificateChain(wrongAlias_);
           }).toThrow();
         });
    });
  });
});
