|
| 1 | +/* |
| 2 | +Copyright (c) 2009-2013 |
| 3 | + Lars-Dominik Braun <[email protected]> |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | +The above copyright notice and this permission notice shall be included in |
| 13 | +all copies or substantial portions of the Software. |
| 14 | +
|
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +THE SOFTWARE. |
| 22 | +*/ |
| 23 | + |
| 24 | +/* test cases for libwaitress */ |
| 25 | + |
| 26 | +/* we are testing static methods and therefore have to include the .c */ |
| 27 | +#include "waitress.c" |
| 28 | + |
| 29 | +#include <stdio.h> |
| 30 | +#include <stdbool.h> |
| 31 | +#include <string.h> |
| 32 | + |
| 33 | +#define streq(a,b) (strcmp(a,b) == 0) |
| 34 | + |
| 35 | +/* string equality test (memory location or content) |
| 36 | + */ |
| 37 | +static bool streqtest (const char *x, const char *y) { |
| 38 | + return (x == y) || (x != NULL && y != NULL && streq (x, y)); |
| 39 | +} |
| 40 | + |
| 41 | +/* test WaitressSplitUrl |
| 42 | + * @param tested url |
| 43 | + * @param expected user |
| 44 | + * @param expected password |
| 45 | + * @param expected host |
| 46 | + * @param expected port |
| 47 | + * @param expected path |
| 48 | + */ |
| 49 | +static void compareUrl (const char *url, const char *user, |
| 50 | + const char *password, const char *host, const char *port, |
| 51 | + const char *path) { |
| 52 | + WaitressUrl_t splitUrl; |
| 53 | + |
| 54 | + memset (&splitUrl, 0, sizeof (splitUrl)); |
| 55 | + |
| 56 | + WaitressSplitUrl (url, &splitUrl); |
| 57 | + |
| 58 | + bool userTest, passwordTest, hostTest, portTest, pathTest, overallTest; |
| 59 | + |
| 60 | + userTest = streqtest (splitUrl.user, user); |
| 61 | + passwordTest = streqtest (splitUrl.password, password); |
| 62 | + hostTest = streqtest (splitUrl.host, host); |
| 63 | + portTest = streqtest (splitUrl.port, port); |
| 64 | + pathTest = streqtest (splitUrl.path, path); |
| 65 | + |
| 66 | + overallTest = userTest && passwordTest && hostTest && portTest && pathTest; |
| 67 | + |
| 68 | + if (!overallTest) { |
| 69 | + printf ("FAILED test(s) for %s\n", url); |
| 70 | + if (!userTest) { |
| 71 | + printf ("user: %s vs %s\n", splitUrl.user, user); |
| 72 | + } |
| 73 | + if (!passwordTest) { |
| 74 | + printf ("password: %s vs %s\n", splitUrl.password, password); |
| 75 | + } |
| 76 | + if (!hostTest) { |
| 77 | + printf ("host: %s vs %s\n", splitUrl.host, host); |
| 78 | + } |
| 79 | + if (!portTest) { |
| 80 | + printf ("port: %s vs %s\n", splitUrl.port, port); |
| 81 | + } |
| 82 | + if (!pathTest) { |
| 83 | + printf ("path: %s vs %s\n", splitUrl.path, path); |
| 84 | + } |
| 85 | + } else { |
| 86 | + printf ("OK for %s\n", url); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/* compare two strings |
| 91 | + */ |
| 92 | +void compareStr (const char *result, const char *expected) { |
| 93 | + if (!streq (result, expected)) { |
| 94 | + printf ("FAIL for %s, result was %s\n", expected, result); |
| 95 | + } else { |
| 96 | + printf ("OK for %s\n", expected); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/* test entry point |
| 101 | + */ |
| 102 | +int main () { |
| 103 | + /* WaitressSplitUrl tests */ |
| 104 | + compareUrl ("http://www.example.com/", NULL, NULL, "www.example.com", NULL, |
| 105 | + ""); |
| 106 | + compareUrl ("http://www.example.com", NULL, NULL, "www.example.com", NULL, |
| 107 | + NULL); |
| 108 | + compareUrl ("http://www.example.com:80/", NULL, NULL, "www.example.com", |
| 109 | + "80", ""); |
| 110 | + compareUrl ("http://www.example.com:/", NULL, NULL, "www.example.com", "", |
| 111 | + ""); |
| 112 | + compareUrl ("http://:80/", NULL, NULL, "", "80", ""); |
| 113 | + compareUrl ("http://www.example.com/foobar/barbaz", NULL, NULL, |
| 114 | + "www.example.com", NULL, "foobar/barbaz"); |
| 115 | + compareUrl ("http://www.example.com:80/foobar/barbaz", NULL, NULL, |
| 116 | + "www.example.com", "80", "foobar/barbaz"); |
| 117 | + compareUrl ( "http://foo:[email protected]:80/foobar/barbaz", "foo", "bar", |
| 118 | + "www.example.com", "80", "foobar/barbaz"); |
| 119 | + compareUrl ("http://foo:@www.example.com:80/foobar/barbaz", "foo", "", |
| 120 | + "www.example.com", "80", "foobar/barbaz"); |
| 121 | + compareUrl ( "http://[email protected]:80/foobar/barbaz", "foo", NULL, |
| 122 | + "www.example.com", "80", "foobar/barbaz"); |
| 123 | + compareUrl ( "http://:[email protected]:80/foobar/barbaz", "", "foo", |
| 124 | + "www.example.com", "80", "foobar/barbaz"); |
| 125 | + compareUrl ("http://:@:80", "", "", "", "80", NULL); |
| 126 | + compareUrl ("http://", NULL, NULL, NULL, NULL, NULL); |
| 127 | + compareUrl ("http:///", NULL, NULL, "", NULL, ""); |
| 128 | + compareUrl ("http://foo:bar@", "foo", "bar", "", NULL, NULL); |
| 129 | + |
| 130 | + /* WaitressBase64Encode tests */ |
| 131 | + compareStr (WaitressBase64Encode ("M"), "TQ=="); |
| 132 | + compareStr (WaitressBase64Encode ("Ma"), "TWE="); |
| 133 | + compareStr (WaitressBase64Encode ("Man"), "TWFu"); |
| 134 | + compareStr (WaitressBase64Encode ("The quick brown fox jumped over the lazy dog."), |
| 135 | + "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cu"); |
| 136 | + compareStr (WaitressBase64Encode ("The quick brown fox jumped over the lazy dog"), |
| 137 | + "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2c="); |
| 138 | + compareStr (WaitressBase64Encode ("The quick brown fox jumped over the lazy do"), |
| 139 | + "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkbw=="); |
| 140 | + |
| 141 | + return EXIT_SUCCESS; |
| 142 | +} |
| 143 | + |
0 commit comments