test_env.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2018-2020, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <assert.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. static void assert_env(const char* name, const char* value)
  31. {
  32. char* result = getenv(name);
  33. if (!result) {
  34. perror("getenv");
  35. printf("(When reading value for '%s'; we expected '%s'.)\n", name, value);
  36. ASSERT(false);
  37. }
  38. if (strcmp(result, value) != 0) {
  39. printf("Expected '%s', got '%s' instead.\n", value, result);
  40. ASSERT(false);
  41. }
  42. }
  43. static void test_getenv_preexisting()
  44. {
  45. assert_env("HOME", "/home/anon");
  46. }
  47. static void test_puttenv()
  48. {
  49. char* to_put = strdup("PUTENVTEST=HELLOPUTENV");
  50. int rc = putenv(to_put);
  51. if (rc) {
  52. perror("putenv");
  53. ASSERT(false);
  54. }
  55. assert_env("PUTENVTEST", "HELLOPUTENV");
  56. // Do not free `to_put`!
  57. }
  58. static void test_settenv()
  59. {
  60. int rc = setenv("SETENVTEST", "HELLO SETENV!", 0);
  61. if (rc) {
  62. perror("setenv");
  63. ASSERT(false);
  64. }
  65. // This used to trigger a very silly bug! :)
  66. assert_env("SETENVTEST", "HELLO SETENV!");
  67. rc = setenv("SETENVTEST", "How are you today?", 0);
  68. if (rc) {
  69. perror("setenv");
  70. ASSERT(false);
  71. }
  72. assert_env("SETENVTEST", "HELLO SETENV!");
  73. rc = setenv("SETENVTEST", "Goodbye, friend!", 1);
  74. if (rc) {
  75. perror("setenv");
  76. ASSERT(false);
  77. }
  78. assert_env("SETENVTEST", "Goodbye, friend!");
  79. }
  80. static void test_settenv_overwrite_empty()
  81. {
  82. int rc = setenv("EMPTYTEST", "Forcefully overwrite non-existing envvar", 1);
  83. if (rc) {
  84. perror("setenv");
  85. ASSERT(false);
  86. }
  87. assert_env("EMPTYTEST", "Forcefully overwrite non-existing envvar");
  88. }
  89. int main(int, char**)
  90. {
  91. #define RUNTEST(x) \
  92. { \
  93. printf("Running " #x " ...\n"); \
  94. x(); \
  95. printf("Success!\n"); \
  96. }
  97. RUNTEST(test_getenv_preexisting);
  98. RUNTEST(test_puttenv);
  99. RUNTEST(test_settenv);
  100. RUNTEST(test_settenv_overwrite_empty);
  101. printf("PASS\n");
  102. return 0;
  103. }