test-env-vars.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Copyright libuv contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <string.h>
  24. #define BUF_SIZE 10
  25. TEST_IMPL(env_vars) {
  26. const char* name = "UV_TEST_FOO";
  27. const char* name2 = "UV_TEST_FOO2";
  28. char buf[BUF_SIZE];
  29. size_t size;
  30. int i, r, envcount, found, found_win_special;
  31. uv_env_item_t* envitems;
  32. /* Reject invalid inputs when setting an environment variable */
  33. r = uv_os_setenv(NULL, "foo");
  34. ASSERT(r == UV_EINVAL);
  35. r = uv_os_setenv(name, NULL);
  36. ASSERT(r == UV_EINVAL);
  37. r = uv_os_setenv(NULL, NULL);
  38. ASSERT(r == UV_EINVAL);
  39. /* Reject invalid inputs when retrieving an environment variable */
  40. size = BUF_SIZE;
  41. r = uv_os_getenv(NULL, buf, &size);
  42. ASSERT(r == UV_EINVAL);
  43. r = uv_os_getenv(name, NULL, &size);
  44. ASSERT(r == UV_EINVAL);
  45. r = uv_os_getenv(name, buf, NULL);
  46. ASSERT(r == UV_EINVAL);
  47. size = 0;
  48. r = uv_os_getenv(name, buf, &size);
  49. ASSERT(r == UV_EINVAL);
  50. /* Reject invalid inputs when deleting an environment variable */
  51. r = uv_os_unsetenv(NULL);
  52. ASSERT(r == UV_EINVAL);
  53. /* Successfully set an environment variable */
  54. r = uv_os_setenv(name, "123456789");
  55. ASSERT(r == 0);
  56. /* Successfully read an environment variable */
  57. size = BUF_SIZE;
  58. buf[0] = '\0';
  59. r = uv_os_getenv(name, buf, &size);
  60. ASSERT(r == 0);
  61. ASSERT(strcmp(buf, "123456789") == 0);
  62. ASSERT(size == BUF_SIZE - 1);
  63. /* Return UV_ENOBUFS if the buffer cannot hold the environment variable */
  64. size = BUF_SIZE - 1;
  65. buf[0] = '\0';
  66. r = uv_os_getenv(name, buf, &size);
  67. ASSERT(r == UV_ENOBUFS);
  68. ASSERT(size == BUF_SIZE);
  69. /* Successfully delete an environment variable */
  70. r = uv_os_unsetenv(name);
  71. ASSERT(r == 0);
  72. /* Return UV_ENOENT retrieving an environment variable that does not exist */
  73. r = uv_os_getenv(name, buf, &size);
  74. ASSERT(r == UV_ENOENT);
  75. /* Successfully delete an environment variable that does not exist */
  76. r = uv_os_unsetenv(name);
  77. ASSERT(r == 0);
  78. /* Setting an environment variable to the empty string does not delete it. */
  79. r = uv_os_setenv(name, "");
  80. ASSERT(r == 0);
  81. size = BUF_SIZE;
  82. r = uv_os_getenv(name, buf, &size);
  83. ASSERT(r == 0);
  84. ASSERT(size == 0);
  85. ASSERT(strlen(buf) == 0);
  86. /* Check getting all env variables. */
  87. r = uv_os_setenv(name, "123456789");
  88. ASSERT(r == 0);
  89. r = uv_os_setenv(name2, "");
  90. ASSERT(r == 0);
  91. #ifdef _WIN32
  92. /* Create a special environment variable on Windows in case there are no
  93. naturally occurring ones. */
  94. r = uv_os_setenv("=Z:", "\\");
  95. ASSERT(r == 0);
  96. #endif
  97. r = uv_os_environ(&envitems, &envcount);
  98. ASSERT(r == 0);
  99. ASSERT(envcount > 0);
  100. found = 0;
  101. found_win_special = 0;
  102. for (i = 0; i < envcount; i++) {
  103. /* printf("Env: %s = %s\n", envitems[i].name, envitems[i].value); */
  104. if (strcmp(envitems[i].name, name) == 0) {
  105. found++;
  106. ASSERT(strcmp(envitems[i].value, "123456789") == 0);
  107. } else if (strcmp(envitems[i].name, name2) == 0) {
  108. found++;
  109. ASSERT(strlen(envitems[i].value) == 0);
  110. } else if (envitems[i].name[0] == '=') {
  111. found_win_special++;
  112. }
  113. }
  114. ASSERT(found == 2);
  115. #ifdef _WIN32
  116. ASSERT(found_win_special > 0);
  117. #endif
  118. uv_os_free_environ(envitems, envcount);
  119. r = uv_os_unsetenv(name);
  120. ASSERT(r == 0);
  121. r = uv_os_unsetenv(name2);
  122. ASSERT(r == 0);
  123. return 0;
  124. }