Exemplo n.º 1
0
        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."), "Actual error message: " + e.Message);
            }
        }
Exemplo n.º 2
0
        public void TestNonExistantFileToDecrypt()
        {
            var encryptedFilePath = "bad_file_path";
            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 the input file exists and has read permission."));
            }
        }
Exemplo n.º 3
0
        public void TestNonExistentFileToDecrypt()
        {
            var encryptedFilePath = "bad_file_path";
            var decryptedFilePath = Path.Combine(this.testDir, "test_pgp_decrypted.txt");

            // Assert that encrypting a file throws the correct exception.
            try
            {
                PgpHelper.DecryptFile(encryptedFilePath, decryptedFilePath, this.passphrase);
                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);
            }
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }