uploaded liboauth for tizen_3.0
[platform/upstream/liboauth.git] / tests / commontest.c
1 /**
2  *  @brief test and example code for liboauth.
3  *  @file commontest.c
4  *  @author Robin Gareus <robin@gareus.org>
5  *
6  * Copyright 2007, 2008, 2012 Robin Gareus <robin@gareus.org>
7  * 
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:
14  * 
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  * 
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
24  * THE SOFTWARE.
25  */
26
27 #ifdef TEST_UNICODE
28 #include <locale.h>
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <oauth.h>
35
36 #include "commontest.h"
37
38 extern int loglevel; //< report each successful test
39
40 /*
41  * test parameter encoding
42  */
43 int test_encoding(char *param, char *expected) {
44   int rv=0;
45   char *testcase=NULL;
46   testcase = oauth_url_escape(param);
47   if (strcmp(testcase,expected)) {
48     rv=1;
49     printf("parameter encoding test for '%s' failed.\n"
50            " got: '%s' expected: '%s'\n", param, testcase, expected);
51   } 
52   else if (loglevel) printf("parameter encoding ok. ('%s')\n", testcase);
53   if (testcase) free(testcase);
54   return (rv);
55 }
56
57 #ifdef TEST_UNICODE
58 /*
59  * test unicode paramter encoding
60  */
61 int test_uniencoding(wchar_t *src, char *expected) {
62   size_t n;
63   char *dst;
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};
67
68   if(setlocale(LC_CTYPE, encoding) == NULL) {
69     printf("requested encoding unavailable\n");
70     return -1;
71   }
72
73   n = wcstombs(NULL, src, 0);
74   dst = malloc(n + 1);
75   if(dst == NULL) {
76     printf("memory allocation failed\n");
77     return -2;
78   }
79   if(wcstombs(dst, src, n + 1) != n) {
80     printf("conversion failed\n");
81     free(dst);
82     return -3;
83   }
84   return test_encoding(dst, expected);
85 }
86 #endif
87
88 /*
89  * test request normalization
90  */
91 int test_normalize(char *param, char *expected) {
92   int rv=2;
93   int  i, argc;
94   char **argv = NULL;
95   char *testcase;
96
97   argc = oauth_split_url_parameters(param, &argv);
98   qsort(argv, argc, sizeof(char *), oauth_cmpstringp);
99   testcase= oauth_serialize_url(argc,0, argv);
100
101   rv=strcmp(testcase,expected);
102   if (rv) {
103     printf("parameter normalization test failed for: '%s'.\n"
104            " got: '%s' expected: '%s'\n", param, testcase, expected);
105   }
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);
110   return (rv);
111 }
112
113 /*
114  * test request concatenation
115  */
116 int test_request(char *http_method, char *request, char *expected) {
117   int rv=2;
118   int  i, argc;
119   char **argv = NULL;
120   char *query, *testcase;
121
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);
126
127   rv=strcmp(testcase,expected);
128   if (rv) {
129     printf("request concatenation test failed for: '%s'.\n"
130            " got:      '%s'\n expected: '%s'\n", request, testcase, expected);
131   }
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);
137   return (rv);
138 }
139
140 /*
141  * test hmac-sha1 checksum
142  */
143 int test_sha1(char *c_secret, char *t_secret, char *base, char *expected) {
144   int rv=0;
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);
150     rv=1;
151   } else if (loglevel) printf("HMAC-SHA1 test sucessful.\n");
152   free(b64d);
153   free(okey);
154   return (rv);
155 }
156
157 int test_sign_get(
158     char const * const url,
159     OAuthMethod method,
160     const char *c_key,
161     const char *c_secret,
162     const char *t_key,
163     const char *t_secret,
164     const char *expected) {
165   int rv=0;
166   char *geturl = NULL;
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);
170   if (rv) {
171     printf("test_sign_get failed:\n"
172            " got:      '%s'\n expected: '%s'\n", geturl, expected);
173   }
174   else if (loglevel) printf("PLAINTEXT signature ok.\n");
175   if(geturl) free(geturl);
176   return (rv);
177 }