PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/libtests/t-cgi.c

https://code.google.com/
C | 134 lines | 95 code | 12 blank | 27 comment | 3 complexity | fa522c13c9bed6653954cc76b7174f49 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. * This file is part of DisOrder.
  3. * Copyright (C) 2008 Richard Kettlewell
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "test.h"
  19. #include "cgi.h"
  20. static void input_from(const char *s) {
  21. FILE *fp = tmpfile();
  22. char buffer[64];
  23. if(fputs(s, fp) < 0
  24. || fputs("wibble wibble\r\nspong", fp) < 0 /* ensure CONTENT_LENGTH
  25. * honored */
  26. || fflush(fp) < 0)
  27. disorder_fatal(errno, "writing to temporary file");
  28. rewind(fp);
  29. xdup2(fileno(fp), 0);
  30. lseek(0, 0/*offset*/, SEEK_SET);
  31. snprintf(buffer, sizeof buffer, "%zu", strlen(s));
  32. setenv("CONTENT_LENGTH", buffer, 1);
  33. }
  34. static void test_cgi(void) {
  35. setenv("REQUEST_METHOD", "GET", 1);
  36. setenv("QUERY_STRING", "foo=bar&a=b+c&c=x%7ey", 1);
  37. cgi_init();
  38. check_string(cgi_get("foo"), "bar");
  39. check_string(cgi_get("a"), "b c");
  40. check_string(cgi_get("c"), "x~y");
  41. setenv("REQUEST_METHOD", "POST", 1);
  42. setenv("CONTENT_TYPE", "application/x-www-form-urlencoded", 1);
  43. unsetenv("QUERY_STRING");
  44. input_from("foo=xbar&a=xb+c&c=xx%7ey");
  45. cgi_init();
  46. check_string(cgi_get("foo"), "xbar");
  47. check_string(cgi_get("a"), "xb c");
  48. check_string(cgi_get("c"), "xx~y");
  49. /* This input string generated by Firefox 2.0.0.14 */
  50. input_from("-----------------------------16128946562344073111198667379\r\n"
  51. "Content-Disposition: form-data; name=\"input1\"\r\n"
  52. "\r\n"
  53. "normal input\r\n"
  54. "-----------------------------16128946562344073111198667379\r\n"
  55. "Content-Disposition: form-data; name=\"input2\"\r\n"
  56. "\r\n"
  57. "with pound sign: \xC2\xA3\r\n"
  58. "-----------------------------16128946562344073111198667379\r\n"
  59. "Content-Disposition: form-data; name=\"input3\"\r\n"
  60. "\r\n"
  61. "hidden input\r\n"
  62. "-----------------------------16128946562344073111198667379\r\n"
  63. "Content-Disposition: form-data; name=\"submit\"\r\n"
  64. "\r\n"
  65. "Submit Query\r\n"
  66. "-----------------------------16128946562344073111198667379--\r\n");
  67. setenv("CONTENT_TYPE", "multipart/form-data; boundary=---------------------------16128946562344073111198667379", 1);
  68. unsetenv("QUERY_STRING");
  69. cgi_init();
  70. check_string(cgi_get("input1"), "normal input");
  71. check_string(cgi_get("input2"), "with pound sign: \xC2\xA3");
  72. check_string(cgi_get("input3"), "hidden input");
  73. check_string(cgi_get("submit"), "Submit Query");
  74. input_from("-----------------------------33919340914020259251659146591\r\n"
  75. "Content-Disposition: form-data; name=\"text\"\r\n"
  76. "\r\n"
  77. "Text with\r\n"
  78. "multiple lines\r\n"
  79. "and trailing spaces \r\n"
  80. "---and leading dashes\r\n"
  81. "and pound sign \xC2\xA3\r\n"
  82. "\r\n"
  83. "-----------------------------33919340914020259251659146591\r\n"
  84. "Content-Disposition: form-data; name=\"empty\"\r\n"
  85. "\r\n"
  86. "\r\n"
  87. "-----------------------------33919340914020259251659146591\r\n"
  88. "Content-Disposition: form-data; name=\"submit\"\r\n"
  89. "\r\n"
  90. "Submit Query\r\n"
  91. "-----------------------------33919340914020259251659146591--\r\n");
  92. setenv("CONTENT_TYPE", "multipart/form-data; boundary=---------------------------33919340914020259251659146591", 1);
  93. cgi_init();
  94. check_string(cgi_get("text"), ("Text with\r\n"
  95. "multiple lines\r\n"
  96. "and trailing spaces \r\n"
  97. "---and leading dashes\r\n"
  98. "and pound sign \xC2\xA3\r\n"
  99. ""));
  100. check_string(cgi_get("empty"), "");
  101. check_string(cgi_sgmlquote("foobar"), "foobar");
  102. check_string(cgi_sgmlquote("<wibble>"), "&#60;wibble&#62;");
  103. check_string(cgi_sgmlquote("\"&\""), "&#34;&#38;&#34;");
  104. check_string(cgi_sgmlquote("\xC2\xA3"), "&#163;");
  105. check_string(cgi_makeurl("http://example.com/", (char *)NULL),
  106. "http://example.com/");
  107. check_string(cgi_makeurl("http://example.com/",
  108. "foo", "bar",
  109. "a", "b c",
  110. "d", "f=g+h",
  111. (char *)NULL),
  112. "http://example.com/?foo=bar&a=b%20c&d=f%3dg%2bh");
  113. }
  114. TEST(cgi);
  115. /*
  116. Local Variables:
  117. c-basic-offset:2
  118. comment-column:40
  119. fill-column:79
  120. indent-tabs-mode:nil
  121. End:
  122. */