uploaded liboauth for tizen_3.0
[platform/upstream/liboauth.git] / tests / oauthtest.c
1 /**
2  *  @brief self-test and example code for liboauth 
3  *  @file oauthtest.c
4  *  @author Robin Gareus <robin@gareus.org>
5  *
6  * Copyright 2007, 2008, 2010 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 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <oauth.h>
31
32 /* 
33  * a example requesting and parsing a request-token from an OAuth service-provider
34  * excercising the oauth-HTTP GET function. - it is almost the same as 
35  * \ref request_token_example_post below. 
36  */
37 void request_token_example_get(void) {
38 #if 0
39   const char *request_token_uri = "http://oauth-sandbox.mediamatic.nl/module/OAuth/request_token";
40   const char *req_c_key         = "17b09ea4c9a4121145936f0d7d8daa28047583796"; //< consumer key
41   const char *req_c_secret      = "942295b08ffce77b399419ee96ac65be"; //< consumer secret
42 #else
43   const char *request_token_uri = "http://term.ie/oauth/example/request_token.php";
44   const char *req_c_key         = "key"; //< consumer key
45   const char *req_c_secret      = "secret"; //< consumer secret
46 #endif
47   char *res_t_key    = NULL; //< replied key
48   char *res_t_secret = NULL; //< replied secret
49
50   char *req_url = NULL;
51   char *reply;
52
53   req_url = oauth_sign_url2(request_token_uri, NULL, OA_HMAC, NULL, req_c_key, req_c_secret, NULL, NULL);
54
55   printf("request URL:%s\n\n", req_url);
56   reply = oauth_http_get(req_url,NULL);
57   if (!reply) 
58     printf("HTTP request for an oauth request-token failed.\n");
59   else {
60     // parse reply - example:
61     //"oauth_token=2a71d1c73d2771b00f13ca0acb9836a10477d3c56&oauth_token_secret=a1b5c00c1f3e23fb314a0aa22e990266"
62     int rc;
63     char **rv = NULL;
64
65     printf("HTTP-reply: %s\n", reply);
66     rc = oauth_split_url_parameters(reply, &rv);
67     qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
68     if( rc==2 
69         && !strncmp(rv[0],"oauth_token=",11)
70         && !strncmp(rv[1],"oauth_token_secret=",18) ){
71           res_t_key=strdup(&(rv[0][12]));
72           res_t_secret=strdup(&(rv[1][19]));
73           printf("key:    '%s'\nsecret: '%s'\n",res_t_key, res_t_secret);
74     }
75     if(rv) free(rv);
76   }
77
78   if(req_url) free(req_url);
79   if(reply) free(reply);
80   if(res_t_key) free(res_t_key);
81   if(res_t_secret) free(res_t_secret);
82 }
83
84 /*
85  * a example requesting and parsing a request-token from an OAuth service-provider
86  * using the oauth-HTTP POST function.
87  */
88 void request_token_example_post(void) {
89 #if 0
90   const char *request_token_uri = "http://oauth-sandbox.mediamatic.nl/module/OAuth/request_token";
91   const char *req_c_key         = "17b09ea4c9a4121145936f0d7d8daa28047583796"; //< consumer key
92   const char *req_c_secret      = "942295b08ffce77b399419ee96ac65be"; //< consumer secret
93 #else
94   const char *request_token_uri = "http://term.ie/oauth/example/request_token.php";
95   const char *req_c_key         = "key"; //< consumer key
96   const char *req_c_secret      = "secret"; //< consumer secret
97 #endif
98   char *res_t_key    = NULL; //< replied key
99   char *res_t_secret = NULL; //< replied secret
100
101   char *postarg = NULL;
102   char *req_url;
103   char *reply;
104
105   req_url = oauth_sign_url2(request_token_uri, &postarg, OA_HMAC, NULL, req_c_key, req_c_secret, NULL, NULL);
106
107   printf("request URL:%s\n\n", req_url);
108   reply = oauth_http_post(req_url,postarg);
109   if (!reply) 
110     printf("HTTP request for an oauth request-token failed.\n");
111   else {
112     //parse reply - example:
113     //"oauth_token=2a71d1c73d2771b00f13ca0acb9836a10477d3c56&oauth_token_secret=a1b5c00c1f3e23fb314a0aa22e990266"
114     int rc;
115     char **rv = NULL;
116     printf("HTTP-reply: %s\n", reply);
117     rc = oauth_split_url_parameters(reply, &rv);
118     qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
119     if( rc==2 
120         && !strncmp(rv[0],"oauth_token=",11)
121         && !strncmp(rv[1],"oauth_token_secret=",18) ){
122           res_t_key=strdup(&(rv[0][12]));
123           res_t_secret=strdup(&(rv[1][19]));
124           printf("key:    '%s'\nsecret: '%s'\n",res_t_key, res_t_secret);
125     }
126     if(rv) free(rv);
127   }
128
129   if(req_url) free(req_url);
130   if(postarg) free(postarg);
131   if(reply) free(reply);
132   if(res_t_key) free(res_t_key);
133   if(res_t_secret) free(res_t_secret);
134 }
135
136
137 /*
138  * Main Test and Example Code.
139  * 
140  * compile:
141  *  gcc -lssl -loauth -o oauthtest oauthtest.c
142  */
143 int main (int argc, char **argv) {
144   int fail=0;
145
146   const char *url      = "http://base.url/&just=append?post=or_get_parameters"
147                          "&arguments=will_be_formatted_automatically?&dont_care"
148                          "=about_separators";
149                          //< the url to sign
150   const char *c_key    = "1234567890abcdef1234567890abcdef123456789";
151                         //< consumer key
152   const char *c_secret = "01230123012301230123012301230123";
153                         //< consumer secret
154   const char *t_key    = "0987654321fedcba0987654321fedcba098765432";
155                         //< token key
156   const char *t_secret = "66666666666666666666666666666666";
157                         //< token secret
158
159 #if 1 // example sign GET request and print the signed request URL
160   {
161     char *geturl = NULL;
162     geturl = oauth_sign_url2(url, NULL, OA_HMAC, NULL, c_key, c_secret, t_key, t_secret);
163     printf("GET: URL:%s\n\n", geturl);
164     if(geturl) free(geturl);
165   }
166 #endif
167
168 #if 1 // sign POST ;) example 
169   {
170     char *postargs = NULL, *post = NULL;
171     post = oauth_sign_url2(url, &postargs, OA_HMAC, NULL, c_key, c_secret, t_key, t_secret);
172     printf("POST: URL:%s\n      PARAM:%s\n\n", post, postargs);
173     if(post) free(post);
174     if(postargs) free(postargs);
175   }
176 #endif
177
178   printf(" *** sending HTTP request *** \n\n");
179
180 // These two will perform a HTTP request, requesting an access token. 
181 // it's intended both as test (verify signature) 
182 // and example code.
183 #if 1 // POST a request-token request
184   request_token_example_post();
185 #endif
186 #if 1 // GET a request-token
187   request_token_example_get();
188 #endif
189
190   return (fail?1:0);
191 }