p≡p engine FORK
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.

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