public void TestNoSecretKeyToDecrypt()
        {
            var testFilepath = Path.Combine(_testDir, "test_pgp.txt");

            DeleteFile(testFilepath);

            string[] lines = { "The purpose of this file is to test 'PgpHelper.EncryptFile and PgpHelper.DecryptFile' methods",
                               "Second Line",
                               "Third Line",
                               "!@#$%^&*()_+-=[]{}/><;':~" };

            File.WriteAllLines(testFilepath, lines);

            //Encrypt
            var encryptedFilePath = testFilepath.Replace(".txt", ".asc");

            DeleteFile(encryptedFilePath);
            PgpHelper.EncryptFile(testFilepath, encryptedFilePath, _vantivPublicKeyId);

            var decryptedFilePath = Path.Combine(_testDir, "test_pgp_decrypted.txt");

            try
            {
                PgpHelper.DecryptFile(encryptedFilePath, decryptedFilePath, _passphrase);
                Assert.Fail("CnpOnline exception expected but was not thrown");
            }
            catch (CnpOnlineException e)
            {
                Assert.True(e.Message.Contains("Please make sure that your merchant secret key is added to your gpg keyring."));
            }
        }
        public void TestInvalidPublicKeyId()
        {
            var testFilepath = Path.Combine(_testDir, "test_pgp.txt");

            DeleteFile(testFilepath);

            string[] lines = { "The purpose of this file is to test 'PgpHelper.EncryptFile and PgpHelper.DecryptFile' methods",
                               "Second Line",
                               "Third Line",
                               "!@#$%^&*()_+-=[]{}/><;':~" };

            File.WriteAllLines(testFilepath, lines);

            var encryptedFilePath = testFilepath.Replace(".txt", ".asc");

            DeleteFile(encryptedFilePath);
            try
            {
                PgpHelper.EncryptFile(testFilepath, encryptedFilePath, "BadPublicKeyId");
                Assert.Fail("CnpOnline exception expected but was not thrown");
            }
            catch (CnpOnlineException e)
            {
                Assert.True(e.Message.Contains("Please make sure that the recipient Key ID is correct and is added to your gpg keyring"));
            }
        }
        public void TestInvalidPassphrase()
        {
            var testFilepath = Path.Combine(_testDir, "test_pgp.txt");

            DeleteFile(testFilepath);

            string[] lines = { "The purpose of this file is to test 'PgpHelper.EncryptFile and PgpHelper.DecryptFile' methods",
                               "Second Line",
                               "Third Line",
                               "!@#$%^&*()_+-=[]{}/><;':~" };

            File.WriteAllLines(testFilepath, lines);

            //Encrypt
            var encryptedFilePath = testFilepath.Replace(".txt", ".asc");

            DeleteFile(encryptedFilePath);
            PgpHelper.EncryptFile(testFilepath, encryptedFilePath, _merchantPublickeyId);

            var decryptedFilePath = Path.Combine(_testDir, "test_pgp_decrypted.txt");

            try
            {
                PgpHelper.DecryptFile(encryptedFilePath, decryptedFilePath, "bad_passphrase");
                Assert.Fail("CnpOnline exception expected but was not thrown");
            }
            catch (CnpOnlineException e)
            {
                Console.WriteLine(e.Message);
                Assert.True(e.Message.Contains("Please make sure that the passphrase is correct."));
            }
        }
        public void TestNonExistantFileToEncrypt()
        {
            var testFilepath      = "bad_file_path";
            var encryptedFilePath = Path.Combine(_testDir, "test_pgp.asc");

            try
            {
                PgpHelper.EncryptFile(testFilepath, encryptedFilePath, _merchantPublickeyId);
                Assert.Fail("CnpOnline exception expected but was not thrown");
            }
            catch (CnpOnlineException e)
            {
                Assert.True(e.Message.Contains("Please make sure the input file exists and has read permission."));
            }
        }
        public void TestNonExistentFileToEncrypt()
        {
            var testFilepath      = "bad_file_path";
            var encryptedFilePath = Path.Combine(this.testDir, "test_pgp.asc");

            // Assert that encrypting a file throws the correct exception.
            try
            {
                PgpHelper.EncryptFile(testFilepath, encryptedFilePath, this.merchantPublickeyId);
                Assert.Fail("CnpOnline exception expected but was not thrown");
            }
            catch (CnpOnlineException e)
            {
                Assert.True(e.Message.Contains("Please make sure the input file exists and has read permission."), "Actual error message: " + e.Message);
            }
        }
        public void TestEncryptionDecryption()
        {
            // Delete the existing file.
            var testFilepath = Path.Combine(this.testDir, "test_pgp.txt");

            DeleteFile(testFilepath);

            // Write a new file.
            string[] lines = { "The purpose of this file is to test 'PgpHelper.EncryptFile and PgpHelper.DecryptFile' methods",
                               "Second Line",
                               "Third Line",
                               "!@#$%^&*()_+-=[]{}/><;':~" };
            File.WriteAllLines(testFilepath, lines);

            // Encrypt the file.
            var encryptedFilePath = testFilepath.Replace(".txt", ".asc");

            DeleteFile(encryptedFilePath);
            PgpHelper.EncryptFile(testFilepath, encryptedFilePath, this.merchantPublickeyId);

            // Assert that the encrypted file has been created.
            var entries = Directory.EnumerateFiles(this.testDir);

            Assert.True(entries.Contains(encryptedFilePath));

            // Decrypt the file.
            var decryptedFilePath = Path.Combine(this.testDir, "test_pgp_decrypted.txt");

            PgpHelper.DecryptFile(encryptedFilePath, decryptedFilePath, this.passphrase);

            // Assert that the decrypted file has been created.
            entries = Directory.EnumerateFiles(this.testDir);
            Assert.True(entries.Contains(decryptedFilePath));

            // Assert the file was encrypted and decrypted correctly.
            var original  = File.ReadAllLines(testFilepath);
            var decrypted = File.ReadAllLines(decryptedFilePath);

            Assert.AreEqual(original, decrypted);
        }
        public void TestEncryptionDecryption()
        {
            var testFilepath = Path.Combine(_testDir, "test_pgp.txt");

            DeleteFile(testFilepath);

            string[] lines = { "The purpose of this file is to test 'PgpHelper.EncryptFile and PgpHelper.DecryptFile' methods",
                               "Second Line",
                               "Third Line",
                               "!@#$%^&*()_+-=[]{}/><;':~" };

            File.WriteAllLines(testFilepath, lines);

            //Encrypt
            var encryptedFilePath = testFilepath.Replace(".txt", ".asc");

            DeleteFile(encryptedFilePath);
            PgpHelper.EncryptFile(testFilepath, encryptedFilePath, _merchantPublickeyId);

            // Check if encrypted file is created
            var entries = Directory.EnumerateFiles(_testDir);

            Assert.True(entries.Contains(encryptedFilePath));

            //Decrypt
            var decryptedFilePath = Path.Combine(_testDir, "test_pgp_decrypted.txt");

            PgpHelper.DecryptFile(encryptedFilePath, decryptedFilePath, _passphrase);

            // Check if decrypted file is created
            entries = Directory.EnumerateFiles(_testDir);
            Assert.True(entries.Contains(decryptedFilePath));

            // Compare decrypted file with original file
            string[] original  = File.ReadAllLines(testFilepath);
            string[] decrypted = File.ReadAllLines(decryptedFilePath);
            Assert.AreEqual(original, decrypted);
        }