test/src/engine_tests/GPGConfFixTests.cc
author Krista Bennett <krista@pep-project.org>
Tue, 31 Jul 2018 21:39:01 +0200
branchENGINE-450-MARK-II
changeset 2795 4bd160717c54
parent 2783 83dafb06379c
child 2798 e0abb44b8098
child 2821 7fe5b66e570d
permissions -rw-r--r--
ENGINE-450: Fixes for 450 reintegrated, and fixed a mem bug that doesn't happen when you have a ton of keys, so it got missed before.
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include <string>
     6 #include <iostream>
     7 #include <fstream>
     8 
     9 #include "pEpEngine.h"
    10 
    11 #include "EngineTestIndividualSuite.h"
    12 #include "GPGConfFixTests.h"
    13 
    14 using namespace std;
    15 
    16 GPGConfFixTests::GPGConfFixTests(string suitename, string temp_test_home_dir) :
    17     EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, temp_test_home_dir) {
    18     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_conf_old_db_0"),
    19                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_conf_old_db_0)));
    20     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_conf_old_db_1"),
    21                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_conf_old_db_1)));
    22     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_conf_old_db_2"),
    23                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_conf_old_db_2)));
    24     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_conf_old_db_3"),
    25                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_conf_old_db_3)));
    26     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_conf_old_db_4"),
    27                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_conf_old_db_4)));
    28     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_conf_old_db_5"),
    29                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_conf_old_db_5)));
    30     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_conf_old_db_6"),
    31                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_conf_old_db_6)));                                                                      
    32     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_0"),
    33                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_0)));
    34     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_1"),
    35                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_1)));
    36     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_2"),
    37                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_2)));
    38     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_3"),
    39                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_3)));
    40     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_4"),
    41                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_4)));
    42     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_5"),
    43                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_5)));
    44     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_6"),
    45                                                                       static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_6)));                                                                      
    46                                                                       
    47 }
    48 
    49 void GPGConfFixTests::setup() {
    50     EngineTestSuite::setup();
    51 }
    52 
    53 static bool file_bytes_equal(const char* file_path_1, const char* file_path_2) {
    54     ifstream f1(file_path_1);
    55     ifstream f2(file_path_2);
    56 
    57     char c1, c2;
    58     
    59     while (f1.get(c1)) {
    60         if (f2.get(c2)) {
    61             if (c1 != c2) {
    62                 if (c1 == '\n' && c2 == ' ') {
    63                     if (f2.get(c2)) {
    64                         if (c1 == c2)
    65                             continue;
    66                     }    
    67                 }
    68                 else if (c2 == '\n' && c1 == ' ') {
    69                     if (f1.get(c1)) {
    70                         if (c1 == c2)
    71                             continue;
    72                     }                        
    73                 }
    74                 return false;
    75             }
    76         }
    77         else {
    78             if (c1 == '\n' || c1 == ' ') {
    79                 while (f1.get(c1)) {
    80                     if (c2 == '\n' || c2 == ' ')
    81                         continue;
    82                     return false;
    83                 }
    84             }
    85         }        
    86     }
    87     while (f2.get(c2)) {
    88         if (c2 == '\n' || c2 == ' ')
    89             continue;
    90         return false;
    91     }
    92     
    93     return true;
    94     
    95 }
    96 
    97 void GPGConfFixTests::check_conf_fix_broken_conf_old_db_0() {
    98     set_full_env("test_files/427_bad_gpg_conf_0", NULL, "test_files/427_old_db");
    99     TEST_ASSERT(file_bytes_equal("test_files/427_fixed_gpg_conf_0", (temp_test_home + "/.gnupg/gpg.conf").c_str()));
   100 }
   101 
   102 void GPGConfFixTests::check_conf_fix_broken_conf_old_db_1() {
   103     set_full_env("test_files/427_bad_gpg_conf_1", NULL, "test_files/427_old_db");    
   104     TEST_ASSERT(file_bytes_equal("test_files/427_fixed_gpg_conf_1", (temp_test_home + "/.gnupg/gpg.conf").c_str()));
   105     
   106 }
   107 
   108 void GPGConfFixTests::check_conf_fix_broken_conf_old_db_2() {
   109     set_full_env("test_files/427_bad_gpg_conf_2", NULL, "test_files/427_old_db");        
   110     TEST_ASSERT(file_bytes_equal("test_files/427_fixed_gpg_conf_2", (temp_test_home + "/.gnupg/gpg.conf").c_str()));    
   111 }
   112 
   113 void GPGConfFixTests::check_conf_fix_broken_conf_old_db_3() {
   114     set_full_env("test_files/427_bad_gpg_conf_3", NULL, "test_files/427_old_db");    
   115     TEST_ASSERT(file_bytes_equal("test_files/427_fixed_gpg_conf_3", (temp_test_home + "/.gnupg/gpg.conf").c_str()));    
   116 }
   117 
   118 void GPGConfFixTests::check_conf_fix_broken_conf_old_db_4() {
   119     set_full_env("test_files/427_bad_gpg_conf_4", NULL, "test_files/427_old_db");        
   120     TEST_ASSERT(file_bytes_equal("test_files/427_fixed_gpg_conf_4", (temp_test_home + "/.gnupg/gpg.conf").c_str()));    
   121 }
   122 
   123 void GPGConfFixTests::check_conf_fix_broken_conf_old_db_5() {
   124     set_full_env("test_files/427_bad_gpg_conf_5", NULL, "test_files/427_old_db");        
   125     TEST_ASSERT(file_bytes_equal("test_files/427_fixed_gpg_conf_5", (temp_test_home + "/.gnupg/gpg.conf").c_str()));    
   126 }
   127 
   128 void GPGConfFixTests::check_conf_fix_broken_conf_old_db_6() {
   129     set_full_env("test_files/427_bad_gpg_conf_6", NULL, "test_files/427_old_db");        
   130     TEST_ASSERT(file_bytes_equal("test_files/427_fixed_gpg_conf_6", (temp_test_home + "/.gnupg/gpg.conf").c_str()));    
   131 }
   132 
   133 void GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_0() {
   134     set_full_env(NULL, "test_files/450_bad_gpgagent_conf_0", "test_files/427_old_db");
   135     TEST_ASSERT(file_bytes_equal("test_files/450_good_gpgagent_conf_0", (temp_test_home + "/.gnupg/gpg-agent.conf").c_str()));
   136 }
   137 
   138 void GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_1() {
   139     set_full_env(NULL, "test_files/450_bad_gpgagent_conf_1", "test_files/427_old_db");    
   140     TEST_ASSERT(file_bytes_equal("test_files/450_good_gpgagent_conf_1", (temp_test_home + "/.gnupg/gpg-agent.conf").c_str()));
   141     
   142 }
   143 
   144 void GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_2() {
   145     set_full_env(NULL, "test_files/450_bad_gpgagent_conf_2", "test_files/427_old_db");        
   146     TEST_ASSERT(file_bytes_equal("test_files/450_good_gpgagent_conf_2", (temp_test_home + "/.gnupg/gpg-agent.conf").c_str()));    
   147 }
   148 
   149 void GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_3() {
   150     set_full_env(NULL, "test_files/450_bad_gpgagent_conf_3", "test_files/427_old_db");    
   151     TEST_ASSERT(file_bytes_equal("test_files/450_good_gpgagent_conf_3", (temp_test_home + "/.gnupg/gpg-agent.conf").c_str()));    
   152 }
   153 
   154 void GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_4() {
   155     set_full_env(NULL, "test_files/450_bad_gpgagent_conf_4", "test_files/427_old_db");        
   156     TEST_ASSERT(file_bytes_equal("test_files/450_good_gpgagent_conf_4", (temp_test_home + "/.gnupg/gpg-agent.conf").c_str()));    
   157 }
   158 
   159 void GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_5() {
   160     set_full_env(NULL, "test_files/450_bad_gpgagent_conf_5", "test_files/427_old_db");        
   161     TEST_ASSERT(file_bytes_equal("test_files/450_good_gpgagent_conf_5", (temp_test_home + "/.gnupg/gpg-agent.conf").c_str()));    
   162 }
   163 
   164 void GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_6() {
   165     set_full_env(NULL, "test_files/450_bad_gpgagent_conf_6", "test_files/427_old_db");        
   166     TEST_ASSERT(file_bytes_equal("test_files/450_good_gpgagent_conf_6", (temp_test_home + "/.gnupg/gpg-agent.conf").c_str()));    
   167 }