/** * @brief test and example code for liboauth. * @file commontest.c * @author Robin Gareus * * Copyright 2007, 2008, 2012 Robin Gareus * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #ifdef TEST_UNICODE #include #endif #include #include #include #include #include "commontest.h" extern int loglevel; //< report each successful test /* * test parameter encoding */ int test_encoding(char *param, char *expected) { int rv=0; char *testcase=NULL; testcase = oauth_url_escape(param); if (strcmp(testcase,expected)) { rv=1; printf("parameter encoding test for '%s' failed.\n" " got: '%s' expected: '%s'\n", param, testcase, expected); } else if (loglevel) printf("parameter encoding ok. ('%s')\n", testcase); if (testcase) free(testcase); return (rv); } #ifdef TEST_UNICODE /* * test unicode paramter encoding */ int test_uniencoding(wchar_t *src, char *expected) { size_t n; char *dst; // check unicode: http://www.thescripts.com/forum/thread223350.html const char *encoding = "en_US.UTF-8"; // or try en_US.ISO-8859-1 etc. //wchar_t src[] = {0x0080, 0}; if(setlocale(LC_CTYPE, encoding) == NULL) { printf("requested encoding unavailable\n"); return -1; } n = wcstombs(NULL, src, 0); dst = malloc(n + 1); if(dst == NULL) { printf("memory allocation failed\n"); return -2; } if(wcstombs(dst, src, n + 1) != n) { printf("conversion failed\n"); free(dst); return -3; } return test_encoding(dst, expected); } #endif /* * test request normalization */ int test_normalize(char *param, char *expected) { int rv=2; int i, argc; char **argv = NULL; char *testcase; argc = oauth_split_url_parameters(param, &argv); qsort(argv, argc, sizeof(char *), oauth_cmpstringp); testcase= oauth_serialize_url(argc,0, argv); rv=strcmp(testcase,expected); if (rv) { printf("parameter normalization test failed for: '%s'.\n" " got: '%s' expected: '%s'\n", param, testcase, expected); } else if (loglevel) printf("parameter normalization ok. ('%s')\n", testcase); for (i=0;i