envtests.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <iostream>
  2. #include <string>
  3. #include <cassert>
  4. #include <cstring>
  5. #include "baseproc-sys.h"
  6. #include "dinit-env.h"
  7. #ifdef NDEBUG
  8. #error "This file must be built with assertions ENABLED!"
  9. #endif
  10. void test_env_1()
  11. {
  12. using namespace bp_sys;
  13. bp_sys::clearenv();
  14. environment env;
  15. assert(env.get("ABC").data() == nullptr);
  16. env.set_var("ABC=DEF");
  17. assert(strcmp(env.get("ABC").data(), "ABC=DEF") == 0);
  18. bp_sys::setenv("ABC","GHIJK",1);
  19. assert(strcmp(env.get("ABC").data(), "ABC=DEF") == 0);
  20. env.import_parent_var("ABC");
  21. assert(strcmp(env.get("ABC").data(), "ABC=GHIJK") == 0);
  22. env.clear_no_inherit();
  23. assert(env.get("ABC").data() == nullptr);
  24. env.import_parent_var("ABC");
  25. assert(strcmp(env.get("ABC").data(), "ABC=GHIJK") == 0);
  26. bp_sys::clearenv();
  27. }
  28. void test_env_2()
  29. {
  30. using namespace bp_sys;
  31. bp_sys::clearenv();
  32. environment env1; // parent
  33. environment env2;
  34. bp_sys::setenv("VAR1","VAR1-env",1); // value to be inherited right through
  35. bp_sys::setenv("VAR2","VAR2-env",1); // value to be overridden in env1
  36. bp_sys::setenv("VAR3","VAR3-env",1); // value to be undefined in env1
  37. bp_sys::setenv("VAR4","VAR4-env",1); // value to be undefined in env1 and overridden in env2
  38. bp_sys::setenv("VAR5","VAR5-env",1); // value to be overridden in env2
  39. bp_sys::setenv("VAR6","VAR6-env",1); // value to be overridden in both env1 and env2
  40. bp_sys::setenv("VAR7","VAR7-env",1); // value to be overridden env1 and undefined in env2
  41. bp_sys::setenv("VAR8","VAR8-env",1); // value to be undefined in env2
  42. env1.set_var("VAR2=VAR2-env1");
  43. env1.undefine_var("VAR3");
  44. env1.undefine_var("VAR4");
  45. env1.set_var("VAR6=VAR6-env1");
  46. env1.set_var("VAR7=VAR7-env1");
  47. env2.set_var("VAR4=VAR4-env2");
  48. env2.set_var("VAR5=VAR5-env2");
  49. env2.set_var("VAR6=VAR6-env2");
  50. env2.undefine_var("VAR7");
  51. env2.undefine_var("VAR8");
  52. environment::env_map mapping = env2.build(env1);
  53. assert(mapping.env_list.size() == 6); // 5 + null terminator
  54. auto checkvalue = [&](const std::string &name, const char *val) {
  55. for (const char *vv : mapping.env_list) {
  56. if (vv == nullptr) break;
  57. if (strncmp(vv, name.c_str(), name.size()) == 0 && vv[name.size()] == '=') {
  58. const char *vv_val = vv + name.size() + 1;
  59. assert(val != nullptr && "expected undefined variable, but has value");
  60. if (strcmp(vv_val, val) != 0) {
  61. std::cout << "\nfor: " << name << ", expected: " << val << ", actual: " << vv_val << "\n";
  62. }
  63. assert(strcmp(vv_val, val) == 0 && "variable has wrong value");
  64. return;
  65. }
  66. }
  67. assert(val == nullptr && "expected defined variable, but variable not found");
  68. };
  69. checkvalue("VAR1", "VAR1-env");
  70. checkvalue("VAR2", "VAR2-env1");
  71. checkvalue("VAR3", nullptr);
  72. checkvalue("VAR4", "VAR4-env2");
  73. checkvalue("VAR5", "VAR5-env2");
  74. checkvalue("VAR6", "VAR6-env2");
  75. checkvalue("VAR7", nullptr);
  76. checkvalue("VAR8", nullptr);
  77. bp_sys::clearenv();
  78. }
  79. #define RUN_TEST(name, spacing) \
  80. std::cout << #name "..." spacing << std::flush; \
  81. name(); \
  82. std::cout << "PASSED" << std::endl;
  83. int main(int argc, char **argv)
  84. {
  85. RUN_TEST(test_env_1, " ");
  86. RUN_TEST(test_env_2, " ");
  87. }