[capi-http] Fixed crashes in http_transaction_destroy & http_session_destroy
[platform/core/api/http.git] / test / http_test.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <glib.h>
21 #include <gio/gio.h>
22
23 #include "http.h"
24
25 #define ERR(x, y) printf("[ERR] %s(%d)\n", x, y)
26 #define PRG(x, y) printf("[PRG] %s(%p)\n", x, y)
27 #define DBG     printf
28 #define MAX_URI_LEN 1024
29
30 FILE* fp1 = NULL;
31 FILE* fp2 = NULL;
32
33 http_session_h session = NULL;
34
35 void __transaction_header_cb(http_transaction_h transaction, char *header, size_t header_len, void *user_data)
36 {
37         PRG("transaction_header_cb", transaction);
38 }
39
40 void __transaction_body_cb(http_transaction_h transaction, char *body, size_t size, size_t nmemb, void *user_data)
41 {
42         PRG("transaction_body_cb", transaction);
43         int written = size * nmemb;
44         DBG("Received: %d\n", written);
45 }
46
47 void __transaction_write_cb(http_transaction_h transaction, int recommended_chunk_size, void *user_data)
48 {
49         PRG("transaction_write_cb", transaction);
50         DBG("recommended_chunk_size:%d\n", recommended_chunk_size);
51 }
52
53 void __transaction_completed_cb(http_transaction_h transaction, void *user_data)
54 {
55         PRG("transaction_completed_cb", transaction);
56
57         http_status_code_e status = 0;
58         int ret;
59         char *uri = NULL;
60
61         ret = http_transaction_request_get_uri(transaction, &uri);
62         ret = http_transaction_response_get_status_code(transaction, &status);
63
64         http_transaction_header_remove_field(transaction, "Content-Length");
65         DBG("%s - status(%d)\n", uri, status);
66         ret = http_transaction_destroy(transaction);
67         if (ret == HTTP_ERROR_NONE) DBG("Success to close transaction\n");
68         else DBG("Fail to close transaction\n");
69
70         transaction = NULL;
71 }
72
73 void __transaction_aborted_cb(http_transaction_h transaction, int reason, void *user_data)
74 {
75         PRG("transaction_aborted_cb", transaction);
76         DBG("aborted reason: %d\n", reason);
77 }
78
79 void _register_callbacks(http_transaction_h transaction)
80 {
81         http_transaction_set_received_header_cb(transaction, __transaction_header_cb, NULL);
82         http_transaction_set_received_body_cb(transaction, __transaction_body_cb, NULL);
83         http_transaction_set_uploaded_cb(transaction, __transaction_write_cb, NULL);
84         http_transaction_set_completed_cb(transaction, __transaction_completed_cb, NULL);
85         http_transaction_set_aborted_cb(transaction, __transaction_aborted_cb, NULL);
86 }
87
88 int test_http_init(void)
89 {
90         int ret = http_init();
91         if (ret == HTTP_ERROR_NONE)
92                 return 1;
93         else return 0;
94 }
95
96 int test_http_deinit(void)
97 {
98         int ret = http_deinit();
99         if (ret == HTTP_ERROR_NONE)
100                 return 1;
101         else return 0;
102 }
103
104 int test_http_session_create(void)
105 {
106         int ret;
107         int session_mode;
108
109         printf("Select session mode(0: NORMAL 1: PIPELINING): ");
110         ret = scanf("%d", &session_mode);
111
112         ret = http_session_create(session_mode, &session);
113         if (ret != HTTP_ERROR_NONE) {
114                 ERR("Fail to create session", ret);
115                 return 0;
116         }
117         return 1;
118 }
119
120 int test_http_session_destroy(void)
121 {
122         int ret = http_session_destroy(session);
123         if (ret != HTTP_ERROR_NONE) {
124                 ERR("Fail to destroy session", ret);
125                 return 0;
126         }
127
128         session = NULL;
129
130         return 1;
131 }
132
133 int test_simple_get(void)
134 {
135         char uri[1024];
136         int ret;
137         http_transaction_h transaction = NULL;
138         http_method_e method;
139
140         printf("Input uri: ");
141         ret = scanf("%1023s", uri);
142
143         ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction);
144         if (ret != 0) {
145                 ERR("Fail to open transaction", ret);
146                 return 0;
147         }
148
149         http_transaction_request_get_method(transaction, &method);
150         ret = http_transaction_request_set_uri(transaction, uri);
151         if (ret != 0) {
152                 ERR("Fail to set URI", ret);
153                 return 0;
154         }
155
156         _register_callbacks(transaction);
157         ret = http_transaction_submit(transaction);
158
159         if (ret != 0) {
160                 ERR("Fail to submit transaction", ret);
161                 return 0;
162         }
163
164         return 1;
165 }
166
167 int test_multiple_get(void)
168 {
169         int ret;
170         http_transaction_h transaction[10];
171         int count;
172         int i;
173
174         printf("Input count of transactions(1~10): ");
175         ret = scanf("%d", &count);
176
177         for (i = 0; i < count; i++) {
178                 char uri[1024];
179                 printf("Input uri for transaction[%d]: ", i + 1);
180                 ret = scanf("%1023s", uri);
181                 ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction[i]);
182                 if (ret != 0) {
183                         ERR("Fail to open transaction", ret);
184                         return 0;
185                 }
186                 ret = http_transaction_request_set_uri(transaction[i], uri);
187                 if (ret != 0) {
188                         ERR("Fail to set URI", ret);
189                         return 0;
190                 }
191                 _register_callbacks(transaction[i]);
192                 http_transaction_submit(transaction[i]);
193         }
194
195         return 1;
196 }
197
198 int test_simple_post(void)
199 {
200         int ret;
201         http_transaction_h transaction;
202         const char* post_msg = "name=tizen&project=capi-network-curl";
203         char field_value[15];
204
205         ret = http_session_open_transaction(session, HTTP_METHOD_POST, &transaction);
206         if (ret != 0) {
207                 ERR("Fail to open transaction", ret);
208                 return 0;
209         }
210         ret = http_transaction_request_set_uri(transaction, "http://posttestserver.com/post.php");
211         if (ret != 0) {
212                 ERR("Fail to set URI", ret);
213                 return 0;
214         }
215         http_transaction_set_ready_to_write(transaction, TRUE);
216         http_transaction_request_write_body(transaction, post_msg);
217
218         sprintf(field_value, "%d", strlen(post_msg));
219         printf("[dbg] post size (%s)\n", field_value);
220         http_transaction_header_add_field(transaction, "Content-Length", field_value);
221
222         _register_callbacks(transaction);
223         http_transaction_submit(transaction);
224
225         return 1;
226 }
227
228 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
229 {
230         int rv;
231         char a[10];
232
233         printf("Event received from stdin\n");
234
235         rv = read(0, a, 10);
236
237         if (rv <= 0 || a[0] == '0')
238                 exit(1);
239
240         if (a[0] == '\n' || a[0] == '\r') {
241                 printf("\n\n Network Connection API Test App\n\n");
242                 printf("Options..\n");
243                 printf("1       - Initialize\n");
244                 printf("2       - Deinitialize\n");
245                 printf("3       - Create session\n");
246                 printf("4       - Destroy session\n");
247                 printf("5       - Simple GET\n");
248                 printf("6       - Multiple GET\n");
249                 printf("7       - Simple POST\n");
250                 printf("8       - \n");
251                 printf("0       - Exit \n");
252                 printf("ENTER  - Show options menu.......\n");
253         }
254
255         switch (a[0]) {
256         case '1':
257                 rv = test_http_init();
258                 break;
259         case '2':
260                 rv = test_http_deinit();
261                 break;
262         case '3':
263                 rv = test_http_session_create();
264                 break;
265         case '4':
266                 rv = test_http_session_destroy();
267                 break;
268         case '5':
269                 rv = test_simple_get();
270                 break;
271         case '6':
272                 rv = test_multiple_get();
273                 break;
274         case '7':
275                 rv = test_simple_post();
276                 break;
277         }
278
279         if (rv == 1)
280                 printf("Operation succeeded!\n");
281         else
282                 printf("Operation failed!\n");
283
284         return true;
285 }
286
287 int main(int argc, char **argv)
288 {
289         GMainLoop *mainloop;
290
291 #if !GLIB_CHECK_VERSION(2, 36, 0)
292         g_type_init();
293 #endif
294         mainloop = g_main_loop_new(NULL, false);
295
296         GIOChannel *channel = g_io_channel_unix_new(0);
297         g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
298         printf("Test Thread created...\n");
299         g_main_loop_run(mainloop);
300
301         return 0;
302 }
303
304
305