C++11 library providing functionality common to all adapters.
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.

306 lines
9.6 KiB

  1. #include "../src/listmanager_dummy.hh"
  2. #include "../src/utils.hh"
  3. #include "framework/test_utils.hh"
  4. #include <exception>
  5. #include <map>
  6. #include <cassert>
  7. using namespace std;
  8. using namespace pEp;
  9. using namespace pEp::Adapter::pEpLog;
  10. using namespace pEp::Utils;
  11. struct lm_list {
  12. string addr;
  13. string mod;
  14. vector<string> members;
  15. };
  16. struct model_test_lmd {
  17. string alice;
  18. string bob;
  19. string carol;
  20. string joe;
  21. string db_path;
  22. vector<lm_list> lists;
  23. vector<string> lists_addr() const
  24. {
  25. vector<string> ret;
  26. for (const lm_list& l : this->lists) {
  27. ret.push_back(l.addr);
  28. }
  29. return ret;
  30. }
  31. };
  32. void apply_model(ListManagerDummy& lmd, const model_test_lmd& model)
  33. {
  34. // log("apply_model()");
  35. for (const lm_list& l : model.lists) {
  36. lmd.list_add(l.addr, l.mod);
  37. for (const string& m : l.members) {
  38. lmd.member_add(l.addr, m);
  39. }
  40. }
  41. }
  42. void verify_model(ListManagerDummy& lmd, const model_test_lmd& model)
  43. {
  44. // log("verify_model()");
  45. assert((model.lists_addr()) == lmd.lists());
  46. for (const lm_list& l : model.lists) {
  47. assert(l.members == lmd.members(l.addr));
  48. }
  49. for (const lm_list& l : model.lists) {
  50. assert(l.mod == lmd.moderator(l.addr));
  51. }
  52. }
  53. void recreate_apply_and_verify_model(ListManagerDummy& lmd, const model_test_lmd& model)
  54. {
  55. try {
  56. lmd.delete_db();
  57. } catch (const exception& e) {
  58. }
  59. assert(!path_exists(model.db_path));
  60. apply_model(lmd, model);
  61. verify_model(lmd, model);
  62. }
  63. model_test_lmd create_default_model()
  64. {
  65. model_test_lmd model;
  66. model.db_path = "test_lmd.db";
  67. model.alice = "alice@peptest.org";
  68. model.bob = "bob@peptest.org";
  69. model.carol = "carol@peptest.org";
  70. model.joe = "joe@peptest.org";
  71. lm_list l1;
  72. l1.addr = "list1@peptest.org";
  73. l1.mod = model.alice;
  74. l1.members.push_back(model.bob);
  75. l1.members.push_back(model.carol);
  76. lm_list l2;
  77. l2.addr = "list2@peptest.org";
  78. l2.mod = model.alice;
  79. l2.members.push_back(model.bob);
  80. l2.members.push_back(model.carol);
  81. l2.members.push_back(model.joe);
  82. lm_list l3;
  83. l3.addr = "list3@peptest.org";
  84. l3.mod = model.bob;
  85. l3.members.push_back(model.carol);
  86. l3.members.push_back(model.joe);
  87. model.lists.push_back(l1);
  88. model.lists.push_back(l2);
  89. model.lists.push_back(l3);
  90. return model;
  91. }
  92. model_test_lmd create_model_bad_path()
  93. {
  94. model_test_lmd model = create_default_model();
  95. model.db_path = "/wont_create_dirs/bad.db";
  96. return model;
  97. }
  98. int main(int argc, char* argv[])
  99. {
  100. // pEpSQLite::log_enabled = true;
  101. ListManagerDummy::log_enabled = false;
  102. logH1("Testing SUCCESS conditions");
  103. {
  104. logH2("Test create new db");
  105. model_test_lmd model = create_default_model();
  106. ListManagerDummy lmd(model.db_path);
  107. recreate_apply_and_verify_model(lmd, model);
  108. }
  109. {
  110. logH2("Test re-open db");
  111. model_test_lmd model = create_default_model();
  112. assert(path_exists(model.db_path));
  113. ListManagerDummy lmd(model.db_path);
  114. verify_model(lmd, model);
  115. logH2("Test list_delete");
  116. lmd.list_delete(model.lists.at(2).addr);
  117. model.lists.pop_back();
  118. verify_model(lmd, model);
  119. logH2("Test auto reopen after close()");
  120. lmd.close_db();
  121. logH2("Test member_remove");
  122. lmd.member_remove(model.lists.at(0).addr, model.lists.at(0).members.at(1));
  123. model.lists.at(0).members.pop_back();
  124. verify_model(lmd, model);
  125. logH2("Test list_exists() - true");
  126. assert(lmd.list_exists(model.lists.at(0).addr));
  127. logH2("Test list_exists() - false");
  128. assert(!lmd.list_exists("does_not_exist_for_sure"));
  129. logH2("Test member_exists() - true");
  130. assert(lmd.member_exists(model.lists.at(0).addr, model.lists.at(0).members.at(0)));
  131. logH2("Test member_exists() - false");
  132. assert(!lmd.member_exists(model.lists.at(0).addr, "does_not_exist_for_sure"));
  133. logH2("Test delete_db");
  134. lmd.delete_db();
  135. assert(!path_exists(model.db_path));
  136. }
  137. logH1("Testing ERROR conditions");
  138. {
  139. logH2("Testing success on close_db() on model_bad_path");
  140. model_test_lmd model = create_model_bad_path();
  141. ListManagerDummy lmd(model.db_path);
  142. lmd.close_db();
  143. }
  144. {
  145. logH2("Testing exception on delete_db() on model_bad_path");
  146. model_test_lmd model = create_model_bad_path();
  147. ListManagerDummy lmd(model.db_path);
  148. ASSERT_EXCEPT(lmd.delete_db());
  149. }
  150. {
  151. logH2("Testing exception on lists() on: on model_bad_path");
  152. model_test_lmd model = create_default_model();
  153. model.db_path = "/wont_create_dirs/bad.db";
  154. ListManagerDummy lmd(model.db_path);
  155. ASSERT_EXCEPT(lmd.lists());
  156. }
  157. // ------------------------------------------------------------------------------------
  158. logH1("list_add() Error conditions");
  159. {
  160. logH2("Testing list_add() AlreadyExistsException");
  161. model_test_lmd model = create_default_model();
  162. ListManagerDummy lmd(model.db_path);
  163. recreate_apply_and_verify_model(lmd, model);
  164. try {
  165. lmd.list_add(model.lists.at(0).addr, "any");
  166. assert(false);
  167. } catch (const AlreadyExistsException& e) {
  168. log(nested_exception_to_string(e));
  169. } catch (...) {
  170. assert(false);
  171. }
  172. }
  173. // ------------------------------------------------------------------------------------
  174. logH1("list_delete() Error conditions");
  175. {
  176. logH2("Testing list_delete() DoesNotExistException");
  177. model_test_lmd model = create_default_model();
  178. ListManagerDummy lmd(model.db_path);
  179. recreate_apply_and_verify_model(lmd, model);
  180. try {
  181. lmd.list_delete("does_not_exist_for_sure");
  182. assert(false);
  183. } catch (const ListDoesNotExistException& e) {
  184. log(nested_exception_to_string(e));
  185. } catch (...) {
  186. assert(false);
  187. }
  188. }
  189. // ------------------------------------------------------------------------------------
  190. logH1("member_add() Error conditions");
  191. {
  192. logH2("Testing member_add() AlreadyExistsException");
  193. model_test_lmd model = create_default_model();
  194. ListManagerDummy lmd(model.db_path);
  195. recreate_apply_and_verify_model(lmd, model);
  196. try {
  197. lmd.member_add(model.lists.at(0).addr, model.lists.at(0).members.at(0));
  198. assert(false);
  199. } catch (const AlreadyExistsException& e) {
  200. log(nested_exception_to_string(e));
  201. } catch (...) {
  202. assert(false);
  203. }
  204. }
  205. {
  206. logH2("Testing member_add() to not existing list - DoesNotExistException");
  207. model_test_lmd model = create_default_model();
  208. ListManagerDummy lmd(model.db_path);
  209. recreate_apply_and_verify_model(lmd, model);
  210. try {
  211. lmd.member_add("does_not_exist_for_sure", model.lists.at(0).members.at(0));
  212. assert(false);
  213. } catch (const ListDoesNotExistException& e) {
  214. log(nested_exception_to_string(e));
  215. } catch (...) {
  216. assert(false);
  217. }
  218. }
  219. // ------------------------------------------------------------------------------------
  220. logH1("member_remove() Error conditions");
  221. {
  222. logH2("Testing member_remove() not existing member - DoesNotExistException");
  223. model_test_lmd model = create_default_model();
  224. ListManagerDummy lmd(model.db_path);
  225. recreate_apply_and_verify_model(lmd, model);
  226. try {
  227. lmd.member_remove(model.lists.at(0).addr, "does_not_exist_for_sure");
  228. assert(false);
  229. } catch (const MemberDoesNotExistException& e) {
  230. log(nested_exception_to_string(e));
  231. } catch (...) {
  232. assert(false);
  233. }
  234. }
  235. {
  236. logH2("Testing member_remove() not existing list - DoesNotExistException");
  237. model_test_lmd model = create_default_model();
  238. ListManagerDummy lmd(model.db_path);
  239. recreate_apply_and_verify_model(lmd, model);
  240. try {
  241. lmd.member_remove("does_not_exist_for_sure", model.lists.at(0).members.at(0));
  242. assert(false);
  243. } catch (const ListDoesNotExistException& e) {
  244. log(nested_exception_to_string(e));
  245. } catch (...) {
  246. assert(false);
  247. }
  248. }
  249. // ------------------------------------------------------------------------------------
  250. logH1("moderator() Error conditions");
  251. {
  252. logH2("Testing moderator() DoesNotExistException");
  253. model_test_lmd model = create_default_model();
  254. ListManagerDummy lmd(model.db_path);
  255. recreate_apply_and_verify_model(lmd, model);
  256. try {
  257. lmd.moderator("does_not_exist_for_sure");
  258. assert(false);
  259. } catch (const ListDoesNotExistException& e) {
  260. log(nested_exception_to_string(e));
  261. } catch (...) {
  262. assert(false);
  263. }
  264. }
  265. // ------------------------------------------------------------------------------------
  266. logH1("members() Error conditions");
  267. {
  268. logH2("Testing members() DoesNotExistException");
  269. model_test_lmd model = create_default_model();
  270. ListManagerDummy lmd(model.db_path);
  271. recreate_apply_and_verify_model(lmd, model);
  272. try {
  273. lmd.members("does_not_exist_for_sure");
  274. assert(false);
  275. } catch (const ListDoesNotExistException& e) {
  276. log(nested_exception_to_string(e));
  277. } catch (...) {
  278. assert(false);
  279. }
  280. }
  281. logH1("All Tests SUCCESSFUL");
  282. }