Addes OOM handling for curl_easy_setopt() calls in test
[platform/upstream/curl.git] / tests / libtest / lib568.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id$
9  */
10
11 #include "test.h"
12
13 #ifdef HAVE_SYS_STAT_H
14 #include <sys/stat.h>
15 #endif
16 #ifdef HAVE_FCNTL_H
17 #include <fcntl.h>
18 #endif
19
20 #include <curl/mprintf.h>
21
22 #include "memdebug.h"
23
24 /* build request url */
25 static char *suburl(const char *base, int i)
26 {
27   return curl_maprintf("%s%.4d", base, i);
28 }
29
30 /*
31  * Test the Client->Server ANNOUNCE functionality (PUT style)
32  */
33 int test(char *URL)
34 {
35   CURLcode res;
36   CURL *curl;
37   int sdp;
38   FILE *sdpf = NULL;
39   struct_stat file_info;
40   char *stream_uri = NULL;
41   int request=1;
42   struct curl_slist *custom_headers=NULL;
43
44   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
45     fprintf(stderr, "curl_global_init() failed\n");
46     return TEST_ERR_MAJOR_BAD;
47   }
48
49   if ((curl = curl_easy_init()) == NULL) {
50     fprintf(stderr, "curl_easy_init() failed\n");
51     curl_global_cleanup();
52     return TEST_ERR_MAJOR_BAD;
53   }
54
55   test_setopt(curl, CURLOPT_HEADERDATA, stdout);
56   test_setopt(curl, CURLOPT_WRITEDATA, stdout);
57
58   test_setopt(curl, CURLOPT_URL, URL);
59
60   if((stream_uri = suburl(URL, request++)) == NULL) {
61     res = TEST_ERR_MAJOR_BAD;
62     goto test_cleanup;
63   }
64   test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
65   free(stream_uri);
66   stream_uri = NULL;
67
68   sdp = open("log/file568.txt", O_RDONLY);
69   fstat(sdp, &file_info);
70   close(sdp);
71
72   sdpf = fopen("log/file568.txt", "rb");
73   if(sdpf == NULL) {
74     fprintf(stderr, "can't open log/file568.txt\n");
75     res = TEST_ERR_MAJOR_BAD;
76     goto test_cleanup;
77   }
78   test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_ANNOUNCE);
79
80   test_setopt(curl, CURLOPT_READDATA, sdpf);
81   test_setopt(curl, CURLOPT_UPLOAD, 1L);
82   test_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t) file_info.st_size);
83
84   /* Do the ANNOUNCE */
85   res = curl_easy_perform(curl);
86   if(res)
87     goto test_cleanup;
88
89   test_setopt(curl, CURLOPT_UPLOAD, 0L);
90   fclose(sdpf);
91   sdpf = NULL;
92
93   /* Make sure we can do a normal request now */
94   if((stream_uri = suburl(URL, request++)) == NULL) {
95     res = TEST_ERR_MAJOR_BAD;
96     goto test_cleanup;
97   }
98   test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
99   free(stream_uri);
100   stream_uri = NULL;
101
102   test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_DESCRIBE);
103   res = curl_easy_perform(curl);
104   if(res)
105     goto test_cleanup;
106
107   /* Now do a POST style one */
108
109   if((stream_uri = suburl(URL, request++)) == NULL) {
110     res = TEST_ERR_MAJOR_BAD;
111     goto test_cleanup;
112   }
113   test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
114   free(stream_uri);
115   stream_uri = NULL;
116
117   custom_headers = curl_slist_append(custom_headers,
118                                      "Content-Type: posty goodness");
119   if(!custom_headers) {
120     res = TEST_ERR_MAJOR_BAD;
121     goto test_cleanup;
122   }
123   test_setopt(curl, CURLOPT_RTSPHEADER, custom_headers);
124   test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_ANNOUNCE);
125   test_setopt(curl, CURLOPT_POSTFIELDS, "postyfield=postystuff&project=curl\n");
126
127   res = curl_easy_perform(curl);
128   if(res)
129     goto test_cleanup;
130
131   test_setopt(curl, CURLOPT_POSTFIELDS, NULL);
132   test_setopt(curl, CURLOPT_RTSPHEADER, NULL);
133   curl_slist_free_all(custom_headers);
134   custom_headers = NULL;
135
136   /* Make sure we can do a normal request now */
137   if((stream_uri = suburl(URL, request++)) == NULL) {
138     res = TEST_ERR_MAJOR_BAD;
139     goto test_cleanup;
140   }
141   test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
142   free(stream_uri);
143   stream_uri = NULL;
144
145   test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_OPTIONS);
146   res = curl_easy_perform(curl);
147
148 test_cleanup:
149
150   if(sdpf)
151     fclose(sdpf);
152
153   if(stream_uri)
154     free(stream_uri);
155
156   if(custom_headers)
157     curl_slist_free_all(custom_headers);
158
159   curl_easy_cleanup(curl);
160   curl_global_cleanup();
161
162   return res;
163 }
164