2 * @brief test and example code for liboauth.
4 * @author Robin Gareus <robin@gareus.org>
6 * Copyright 2007, 2008, 2012 Robin Gareus <robin@gareus.org>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36 #include "commontest.h"
38 extern int loglevel; //< report each successful test
41 * test parameter encoding
43 int test_encoding(char *param, char *expected) {
46 testcase = oauth_url_escape(param);
47 if (strcmp(testcase,expected)) {
49 printf("parameter encoding test for '%s' failed.\n"
50 " got: '%s' expected: '%s'\n", param, testcase, expected);
52 else if (loglevel) printf("parameter encoding ok. ('%s')\n", testcase);
53 if (testcase) free(testcase);
59 * test unicode paramter encoding
61 int test_uniencoding(wchar_t *src, char *expected) {
64 // check unicode: http://www.thescripts.com/forum/thread223350.html
65 const char *encoding = "en_US.UTF-8"; // or try en_US.ISO-8859-1 etc.
66 //wchar_t src[] = {0x0080, 0};
68 if(setlocale(LC_CTYPE, encoding) == NULL) {
69 printf("requested encoding unavailable\n");
73 n = wcstombs(NULL, src, 0);
76 printf("memory allocation failed\n");
79 if(wcstombs(dst, src, n + 1) != n) {
80 printf("conversion failed\n");
84 return test_encoding(dst, expected);
89 * test request normalization
91 int test_normalize(char *param, char *expected) {
97 argc = oauth_split_url_parameters(param, &argv);
98 qsort(argv, argc, sizeof(char *), oauth_cmpstringp);
99 testcase= oauth_serialize_url(argc,0, argv);
101 rv=strcmp(testcase,expected);
103 printf("parameter normalization test failed for: '%s'.\n"
104 " got: '%s' expected: '%s'\n", param, testcase, expected);
106 else if (loglevel) printf("parameter normalization ok. ('%s')\n", testcase);
107 for (i=0;i<argc;i++) free(argv[i]);
108 if (argv) free(argv);
109 if (testcase) free(testcase);
114 * test request concatenation
116 int test_request(char *http_method, char *request, char *expected) {
120 char *query, *testcase;
122 argc = oauth_split_url_parameters(request, &argv);
123 qsort(&argv[1], argc-1, sizeof(char *), oauth_cmpstringp);
124 query= oauth_serialize_url(argc,1, argv);
125 testcase = oauth_catenc(3, http_method, argv[0], query);
127 rv=strcmp(testcase,expected);
129 printf("request concatenation test failed for: '%s'.\n"
130 " got: '%s'\n expected: '%s'\n", request, testcase, expected);
132 else if (loglevel) printf("request concatenation ok.\n");
133 for (i=0;i<argc;i++) free(argv[i]);
134 if (argv) free(argv);
135 if (query) free(query);
136 if (testcase) free(testcase);
141 * test hmac-sha1 checksum
143 int test_sha1(char *c_secret, char *t_secret, char *base, char *expected) {
145 char *okey = oauth_catenc(2, c_secret, t_secret);
146 char *b64d = oauth_sign_hmac_sha1(base, okey);
147 if (strcmp(b64d,expected)) {
148 printf("HMAC-SHA1 invalid. base:'%s' secrets:'%s'\n"
149 " got: '%s' expected: '%s'\n", base, okey, b64d, expected);
151 } else if (loglevel) printf("HMAC-SHA1 test sucessful.\n");
158 char const * const url,
161 const char *c_secret,
163 const char *t_secret,
164 const char *expected) {
167 geturl = oauth_sign_url2(url, NULL, method, NULL, c_key, c_secret, t_key, t_secret);
168 printf("GET: URL:%s\n", geturl);
169 rv=strcmp(geturl,expected);
171 printf("test_sign_get failed:\n"
172 " got: '%s'\n expected: '%s'\n", geturl, expected);
174 else if (loglevel) printf("PLAINTEXT signature ok.\n");
175 if(geturl) free(geturl);