uploaded liboauth for tizen_3.0
[platform/upstream/liboauth.git] / tests / selftest_other.c
1 /**
2  *  @brief self-test for liboauth.
3  *  @file selftest.c
4  *  @author Robin Gareus <robin@gareus.org>
5  *
6  * Copyright 2009, 2010, 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 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <oauth.h>
31
32 #include "commontest.h"
33
34 int loglevel = 1; //< report each successful test
35
36 int main (int argc, char **argv) {
37   int fail=0;
38
39   if (loglevel) printf("\n *** Testing query parameter array encoding.\n");
40
41   fail|=test_request("GET", "http://example.com" 
42       "?k1=v1"
43       "&a1[ak1]=av1"
44       "&a1[aa1][aak2]=aav2"
45       "&a1[aa1][aak1]=aav1"
46       "&k2=v2"
47       "&a1[ak2]=av2",
48   "GET&http%3A%2F%2Fexample.com%2F&a1%255Baa1%255D%255Baak1%255D%3Daav1%26a1%255Baa1%255D%255Baak2%255D%3Daav2%26a1%255Bak1%255D%3Dav1%26a1%255Bak2%255D%3Dav2%26k1%3Dv1%26k2%3Dv2");
49
50   if (loglevel) printf("\n *** Testing http://tools.ietf.org/html/rfc5849 example.\n");
51
52   fail|=test_request("GET", "http://example.com" 
53       "/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b"
54       "&c2&a3=2+q"
55       "&oauth_consumer_key=9djdj82h48djs9d2"
56       "&oauth_token=kkk9d7dh3k39sjv7"
57       "&oauth_signature_method=HMAC-SHA1"
58       "&oauth_timestamp=137131201"
59       "&oauth_nonce=7d8f3e4a"
60       "&oauth_signature=djosJKDKJSD8743243%2Fjdk33klY%3D",
61    "GET&http%3A%2F%2Fexample.com%2Frequest&a2%3Dr%2520b%26a3%3D2%2520q%26a3%3Da%26b5%3D%253D%25253D%26c%2540%3D%26c2%3D%26oauth_consumer_key%3D9djdj82h48djs9d2%26oauth_nonce%3D7d8f3e4a%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D137131201%26oauth_token%3Dkkk9d7dh3k39sjv7");
62
63   if (loglevel) printf("\n *** Testing body hash calculation.\n");
64
65   char *bh;
66   const char *teststring="Hello World!";
67   bh=oauth_body_hash_data(strlen(teststring), teststring);
68   if (bh) { 
69     if (strcmp(bh,"oauth_body_hash=Lve95gjOVATpfV8EL5X4nxwjKHE=")) fail|=1;
70     free(bh);
71   } else { 
72     fail|=1;
73   }
74
75   if (loglevel) printf("\n *** Testing PLAINTEXT signature.\n");
76   fail |= test_sign_get(
77       "http://host.net/resource" "?" "name=value&name=value"
78       "&oauth_nonce=fake&&oauth_timestamp=1",
79       OA_PLAINTEXT,
80       "abcd", "&",
81       "1234", "&",
82       "http://host.net/resource?name=value&name=value&oauth_consumer_key=abcd&oauth_nonce=fake&oauth_signature_method=PLAINTEXT&oauth_timestamp=1&oauth_token=1234&oauth_version=1.0&oauth_signature=%2526%26%2526"
83       );
84
85
86   // report
87   if (fail) {
88     printf("\n !!! One or more test cases failed.\n\n");
89   } else {
90     printf(" *** Test cases verified sucessfully.\n");
91   }
92
93   return (fail?1:0);
94 }