You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

409 lines
16 KiB

  1. #include <stdlib.h>
  2. #include <string>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <unistd.h>
  9. #include "pEpEngine.h"
  10. #include "pEp_internal.h"
  11. #include "TestUtilities.h"
  12. #include "TestConstants.h"
  13. #include "Engine.h"
  14. #include <gtest/gtest.h>
  15. namespace {
  16. //The fixture for ImportKeyTest
  17. class ImportKeyTest : public ::testing::Test {
  18. public:
  19. Engine* engine;
  20. PEP_SESSION session;
  21. protected:
  22. // You can remove any or all of the following functions if its body
  23. // is empty.
  24. ImportKeyTest() {
  25. // You can do set-up work for each test here.
  26. test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->GTEST_SUITE_SYM();
  27. test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
  28. test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
  29. }
  30. ~ImportKeyTest() override {
  31. // You can do clean-up work that doesn't throw exceptions here.
  32. }
  33. // If the constructor and destructor are not enough for setting up
  34. // and cleaning up each test, you can define the following methods:
  35. void SetUp() override {
  36. // Code here will be called immediately after the constructor (right
  37. // before each test).
  38. // Leave this empty if there are no files to copy to the home directory path
  39. std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
  40. // Get a new test Engine.
  41. engine = new Engine(test_path);
  42. ASSERT_NOTNULL(engine);
  43. // Ok, let's initialize test directories etc.
  44. engine->prep(NULL, NULL, NULL, init_files);
  45. // Ok, try to start this bugger.
  46. engine->start();
  47. ASSERT_NOTNULL(engine->session);
  48. session = engine->session;
  49. // Engine is up. Keep on truckin'
  50. }
  51. void TearDown() override {
  52. // Code here will be called immediately after each test (right
  53. // before the destructor).
  54. engine->shut_down();
  55. delete engine;
  56. engine = NULL;
  57. session = NULL;
  58. }
  59. private:
  60. const char* test_suite_name;
  61. const char* test_name;
  62. string test_path;
  63. // Objects declared here can be used by all tests in the ImportKeyTest suite.
  64. };
  65. } // namespace
  66. TEST_F(ImportKeyTest, check_import_fpr_pub_new) {
  67. PEP_STATUS status = PEP_STATUS_OK;
  68. string pubkey = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
  69. stringlist_t* keylist = NULL;
  70. status = import_key_with_fpr_return(session, pubkey.c_str(), pubkey.size(), NULL, &keylist, NULL);
  71. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  72. ASSERT_NOTNULL(keylist);
  73. ASSERT_STREQ(keylist->value, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
  74. ASSERT_NULL(keylist->next);
  75. // FIXME, check key is actually imported
  76. }
  77. TEST_F(ImportKeyTest, check_import_change_pub_new) {
  78. PEP_STATUS status = PEP_STATUS_OK;
  79. string pubkey = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
  80. stringlist_t* keylist = NULL;
  81. uint64_t changes = 0;
  82. status = import_key_with_fpr_return(session, pubkey.c_str(), pubkey.size(), NULL, &keylist, &changes);
  83. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  84. ASSERT_NOTNULL(keylist);
  85. ASSERT_STREQ(keylist->value, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
  86. ASSERT_NULL(keylist->next);
  87. ASSERT_EQ(changes, 1);
  88. // FIXME, check key is actually imported
  89. }
  90. TEST_F(ImportKeyTest, check_import_fpr_priv_new) {
  91. PEP_STATUS status = PEP_STATUS_OK;
  92. string pubkey = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
  93. stringlist_t* keylist = NULL;
  94. status = import_key_with_fpr_return(session, pubkey.c_str(), pubkey.size(), NULL, &keylist, NULL);
  95. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  96. ASSERT_NOTNULL(keylist);
  97. ASSERT_STREQ(keylist->value, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
  98. ASSERT_NULL(keylist->next);
  99. // FIXME, check key is actually imported
  100. }
  101. TEST_F(ImportKeyTest, check_import_change_pub_nochange) {
  102. PEP_STATUS status = PEP_STATUS_OK;
  103. string pubkey = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
  104. stringlist_t* keylist = NULL;
  105. uint64_t changes = 0;
  106. status = import_key_with_fpr_return(session, pubkey.c_str(), pubkey.size(), NULL, &keylist, &changes);
  107. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  108. ASSERT_NOTNULL(keylist);
  109. ASSERT_STREQ(keylist->value, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
  110. ASSERT_NULL(keylist->next);
  111. ASSERT_EQ(changes, 1);
  112. // import again!
  113. free_stringlist(keylist);
  114. keylist = NULL;
  115. changes = 0;
  116. status = import_key_with_fpr_return(session, pubkey.c_str(), pubkey.size(), NULL, &keylist, &changes);
  117. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  118. ASSERT_NOTNULL(keylist);
  119. ASSERT_STREQ(keylist->value, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
  120. ASSERT_NULL(keylist->next);
  121. ASSERT_EQ(changes, 0);
  122. }
  123. TEST_F(ImportKeyTest, check_import_change_pub_nochange_binary_bigkey) {
  124. PEP_STATUS status = PEP_STATUS_OK;
  125. int retval = 0;
  126. #ifndef WIN32
  127. struct stat fst;
  128. retval = stat("test_keys/bigkey.pgp", &fst);
  129. #else
  130. struct _stat fst;
  131. retval = _stat("test_keys/bigkey.pgp", &fst);
  132. #endif
  133. ASSERT_EQ(retval, 0);
  134. size_t img_size = (size_t)(fst.st_size);
  135. ASSERT_NE(img_size, 0);
  136. char* img = (char*)calloc(1, img_size);
  137. ifstream img_file("test_keys/bigkey.pgp", ios::in | ios::binary);
  138. img_file.read(img, img_size);
  139. img_file.close();
  140. cout << img_size << endl;
  141. stringlist_t* keylist = NULL;
  142. uint64_t changes = 0;
  143. status = import_key_with_fpr_return(session, img, img_size, NULL, &keylist, &changes);
  144. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  145. ASSERT_NE(keylist, nullptr);
  146. // ASSERT_STREQ(keylist->value, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
  147. ASSERT_EQ(keylist->next, nullptr);
  148. ASSERT_EQ(changes, 1);
  149. // import again!
  150. free_stringlist(keylist);
  151. keylist = NULL;
  152. changes = 0;
  153. status = import_key_with_fpr_return(session, img, img_size, NULL, &keylist, &changes);
  154. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  155. ASSERT_NE(keylist, nullptr);
  156. // ASSERT_STREQ(keylist->value, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
  157. ASSERT_EQ(keylist->next, nullptr);
  158. ASSERT_EQ(changes, 0);
  159. }
  160. TEST_F(ImportKeyTest, check_import_change_wo_fpr_illegal) {
  161. PEP_STATUS status = PEP_STATUS_OK;
  162. string pubkey = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
  163. uint64_t changes = 0;
  164. status = import_key_with_fpr_return(session, pubkey.c_str(), pubkey.size(), NULL, NULL, &changes);
  165. ASSERT_EQ(status, PEP_ILLEGAL_VALUE);
  166. }
  167. TEST_F(ImportKeyTest, check_import_fpr_list_pub_concat) {
  168. // Contains 10 keys
  169. string pubkey_material = slurp("test_keys/pub/import_keys_multi_pub_concat.asc");
  170. stringlist_t* keylist = NULL;
  171. uint64_t changes = 0;
  172. PEP_STATUS status = import_key_with_fpr_return(session, pubkey_material.c_str(), pubkey_material.size(), NULL, &keylist, &changes);
  173. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  174. ASSERT_NOTNULL(keylist);
  175. ASSERT_EQ(stringlist_length(keylist), 10);
  176. ASSERT_EQ(changes, 1023); // 2^10 - 1
  177. }
  178. TEST_F(ImportKeyTest, check_import_fpr_list_priv_concat) {
  179. // Contains 10 keys
  180. string privkey_material = slurp("test_keys/priv/import_keys_multi_priv_concat.asc");
  181. stringlist_t* keylist = NULL;
  182. uint64_t changes = 0;
  183. PEP_STATUS status = import_key_with_fpr_return(session, privkey_material.c_str(), privkey_material.size(), NULL, &keylist, &changes);
  184. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  185. ASSERT_NOTNULL(keylist);
  186. ASSERT_EQ(stringlist_length(keylist), 10);
  187. ASSERT_EQ(changes, 1023); // The answer to this might be implementation dependent and we don't care.
  188. }
  189. TEST_F(ImportKeyTest, check_import_fpr_list_priv_then_pub) {
  190. // Contains 10 keys
  191. string privkey_material = slurp("test_keys/priv/import_keys_multi_priv_concat.asc");
  192. stringlist_t* keylist = NULL;
  193. uint64_t changes = 0;
  194. PEP_STATUS status = import_key_with_fpr_return(session, privkey_material.c_str(), privkey_material.size(), NULL, &keylist, &changes);
  195. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  196. ASSERT_NOTNULL(keylist);
  197. ASSERT_EQ(stringlist_length(keylist), 10);
  198. ASSERT_EQ(changes, 1023);
  199. free_stringlist(keylist);
  200. keylist = NULL;
  201. changes = 0;
  202. string pubkey_material = slurp("test_keys/pub/import_keys_multi_pub_concat.asc");
  203. status = import_key_with_fpr_return(session, pubkey_material.c_str(), pubkey_material.size(), NULL, &keylist, &changes);
  204. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  205. ASSERT_NOTNULL(keylist);
  206. ASSERT_EQ(stringlist_length(keylist), 10);
  207. // ASSERT_EQ(changes, 0); Answer may be implementation dependent. Ignore.
  208. }
  209. TEST_F(ImportKeyTest, check_import_fpr_list_pub_then_priv) {
  210. // Contains 10 keys
  211. string pubkey_material = slurp("test_keys/pub/import_keys_multi_pub_concat.asc");
  212. stringlist_t* keylist = NULL;
  213. uint64_t changes = 0;
  214. PEP_STATUS status = import_key_with_fpr_return(session, pubkey_material.c_str(), pubkey_material.size(), NULL, &keylist, &changes);
  215. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  216. ASSERT_NOTNULL(keylist);
  217. ASSERT_EQ(stringlist_length(keylist), 10);
  218. ASSERT_EQ(changes, 1023);
  219. free_stringlist(keylist);
  220. keylist = NULL;
  221. changes = 0;
  222. string privkey_material = slurp("test_keys/priv/import_keys_multi_priv_concat.asc");
  223. status = import_key_with_fpr_return(session, privkey_material.c_str(), privkey_material.size(), NULL, &keylist, &changes);
  224. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  225. ASSERT_NOTNULL(keylist);
  226. ASSERT_EQ(stringlist_length(keylist), 10);
  227. // ASSERT_EQ(changes, 1023); // The answer to this might be implementation dependent and we don't care.
  228. free_stringlist(keylist);
  229. }
  230. TEST_F(ImportKeyTest, check_import_fpr_list_pub_blob) {
  231. // Contains 10 keys
  232. string pubkey_material = slurp("test_keys/pub/import_keys_multi_pub_serial_blob.asc");
  233. stringlist_t* keylist = NULL;
  234. uint64_t changes = 0;
  235. PEP_STATUS status = import_key_with_fpr_return(session, pubkey_material.c_str(), pubkey_material.size(), NULL, &keylist, &changes);
  236. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  237. ASSERT_NOTNULL(keylist);
  238. ASSERT_EQ(stringlist_length(keylist), 10);
  239. ASSERT_EQ(changes, 1023); // 2^10 - 1
  240. }
  241. TEST_F(ImportKeyTest, check_import_fpr_list_priv_blob) {
  242. // Contains 10 keys
  243. string privkey_material = slurp("test_keys/priv/import_keys_multi_priv_serial_blob.asc");
  244. stringlist_t* keylist = NULL;
  245. uint64_t changes = 0;
  246. PEP_STATUS status = import_key_with_fpr_return(session, privkey_material.c_str(), privkey_material.size(), NULL, &keylist, &changes);
  247. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  248. ASSERT_NOTNULL(keylist);
  249. ASSERT_EQ(stringlist_length(keylist), 10);
  250. // ASSERT_EQ(changes, 1023); // The answer to this might be implementation dependent and we don't care.
  251. }
  252. TEST_F(ImportKeyTest, check_import_added_subkey_then_revoke_subkey) {
  253. PEP_STATUS status = PEP_STATUS_OK;
  254. string pubkey = slurp("test_keys/pub/import_keys_multi_9-0x045134F0_pub.asc");
  255. stringlist_t* keylist = NULL;
  256. uint64_t changes = 0;
  257. status = import_key_with_fpr_return(session, pubkey.c_str(), pubkey.size(), NULL, &keylist, &changes);
  258. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  259. ASSERT_NOTNULL(keylist);
  260. ASSERT_STREQ(keylist->value, "25D08DAFD15F21F6A9492FB00A958FA5045134F0");
  261. ASSERT_NULL(keylist->next);
  262. ASSERT_EQ(changes, 1);
  263. pubkey = slurp("test_keys/pub/import_keys_multi_9_add_rsa-0x045134F0_pub.asc");
  264. // import again!
  265. free_stringlist(keylist);
  266. keylist = NULL;
  267. changes = 0;
  268. status = import_key_with_fpr_return(session, pubkey.c_str(), pubkey.size(), NULL, &keylist, &changes);
  269. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  270. ASSERT_NOTNULL(keylist);
  271. ASSERT_STREQ(keylist->value, "25D08DAFD15F21F6A9492FB00A958FA5045134F0");
  272. ASSERT_NULL(keylist->next);
  273. ASSERT_EQ(changes, 1);
  274. pubkey = slurp("test_keys/pub/import_keys_multi_9_add_rsa_rev_sub-0x045134F0_pub.asc");
  275. // import again!
  276. free_stringlist(keylist);
  277. keylist = NULL;
  278. changes = 0;
  279. status = import_key_with_fpr_return(session, pubkey.c_str(), pubkey.size(), NULL, &keylist, &changes);
  280. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  281. ASSERT_NOTNULL(keylist);
  282. ASSERT_STREQ(keylist->value, "25D08DAFD15F21F6A9492FB00A958FA5045134F0");
  283. ASSERT_NULL(keylist->next);
  284. ASSERT_EQ(changes, 1);
  285. }
  286. // This is pulling too much weight, but it'll get a lot done at once.
  287. // FIXME: Break out tests individually
  288. TEST_F(ImportKeyTest, check_import_huge_concat_then_change) {
  289. // Contains 10 keys
  290. string pubkey_material = slurp("test_keys/pub/import_keys_multi_pub_concat.asc");
  291. stringlist_t* keylist = NULL;
  292. uint64_t changes = 0;
  293. PEP_STATUS status = import_key_with_fpr_return(session, pubkey_material.c_str(), pubkey_material.size(), NULL, &keylist, &changes);
  294. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  295. ASSERT_NOTNULL(keylist);
  296. ASSERT_EQ(stringlist_length(keylist), 10);
  297. ASSERT_EQ(changes, 1023);
  298. free_stringlist(keylist);
  299. keylist = NULL;
  300. changes = 0;
  301. string some_changed_material = slurp("test_keys/pub/import_keys_multi_with_mult_changes_concat.asc");
  302. status = import_key_with_fpr_return(session, some_changed_material.c_str(), some_changed_material.size(), NULL, &keylist, &changes);
  303. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  304. ASSERT_NOTNULL(keylist);
  305. ASSERT_EQ(stringlist_length(keylist), 10);
  306. ASSERT_EQ(changes, 938); // 1, 3, 5, 7, 8, 9 = 1110101010 = 938
  307. free_stringlist(keylist);
  308. }
  309. TEST_F(ImportKeyTest, check_non_cleared_list_usage) {
  310. // Contains 10 keys
  311. string pubkey_material = slurp("test_keys/pub/import_keys_multi_pub_concat.asc");
  312. stringlist_t* keylist = NULL;
  313. uint64_t changes = 0;
  314. PEP_STATUS status = import_key_with_fpr_return(session, pubkey_material.c_str(), pubkey_material.size(), NULL, &keylist, &changes);
  315. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  316. ASSERT_NOTNULL(keylist);
  317. ASSERT_EQ(stringlist_length(keylist), 10);
  318. ASSERT_EQ(changes, 1023);
  319. string some_changed_material = slurp("test_keys/pub/import_keys_multi_with_mult_changes_concat.asc");
  320. status = import_key_with_fpr_return(session, some_changed_material.c_str(), some_changed_material.size(), NULL, &keylist, &changes);
  321. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  322. ASSERT_NOTNULL(keylist);
  323. ASSERT_EQ(stringlist_length(keylist), 20);
  324. ASSERT_EQ(changes, 0xEABFF); // (938 << 10 | 1023) -> 11101010101111111111 = 0xEABFF
  325. free_stringlist(keylist);
  326. }
  327. TEST_F(ImportKeyTest, check_770_import_priv_asc) {
  328. PEP_STATUS status = PEP_STATUS_OK;
  329. string pubkey = slurp("test_keys/770_priv.asc");
  330. stringlist_t* keylist = NULL;
  331. status = import_key_with_fpr_return(session, pubkey.c_str(), pubkey.size(), NULL, &keylist, NULL);
  332. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  333. ASSERT_NOTNULL(keylist);
  334. ASSERT_STREQ(keylist->value, "0521111E12084FDEA58A38E880D9FB378DCC789D");
  335. ASSERT_NULL(keylist->next);
  336. // FIXME, check key is actually imported
  337. }
  338. TEST_F(ImportKeyTest, check_770_import_priv_pgp) {
  339. PEP_STATUS status = PEP_STATUS_OK;
  340. string pubkey = slurp("test_keys/770_priv.pgp");
  341. stringlist_t* keylist = NULL;
  342. status = import_key_with_fpr_return(session, pubkey.c_str(), pubkey.size(), NULL, &keylist, NULL);
  343. ASSERT_EQ(status, PEP_KEY_IMPORTED);
  344. ASSERT_NOTNULL(keylist);
  345. ASSERT_STREQ(keylist->value, "0521111E12084FDEA58A38E880D9FB378DCC789D");
  346. ASSERT_NULL(keylist->next);
  347. // FIXME, check key is actually imported
  348. }