7ed991dd351c203fc3ff94689fb0aaba89f6ec4a
[platform/upstream/curl.git] / tests / libtest / lib558.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id$
9  */
10
11 #include "test.h"
12
13 #ifdef HAVE_SYS_SOCKET_H
14 #  include <sys/socket.h>
15 #endif
16 #ifdef HAVE_NETINET_IN_H
17 #  include <netinet/in.h>
18 #endif
19 #ifdef HAVE_NETDB_H
20 #  include <netdb.h>
21 #endif
22 #ifdef HAVE_ARPA_INET_H
23 #  include <arpa/inet.h>
24 #endif
25
26 #define ENABLE_CURLX_PRINTF
27 #include "curlx.h"
28
29 #include "hash.h"
30 #include "hostip.h"
31
32 #include "curl_memory.h"
33 #include "memdebug.h"
34
35 /* This source file is used for test # 558 and 559 */
36
37 /*
38  * This hacky test bypasses the library external API,
39  * using internal only libcurl functions. So don't be
40  * surprised if we cannot run it when the library has
41  * been built with hidden symbols, exporting only the
42  * ones in the public API.
43  */
44
45 #if defined(CURL_HIDDEN_SYMBOLS)
46 #  define SKIP_TEST 1
47 #elif defined(WIN32) && !defined(CURL_STATICLIB)
48 #  define SKIP_TEST 1
49 #else
50 #  undef  SKIP_TEST
51 #endif
52
53
54 #if !defined(SKIP_TEST)
55
56 #ifdef LIB559
57 static Curl_addrinfo *fake_ai(void)
58 {
59   Curl_addrinfo *ai;
60   int ss_size;
61
62   ss_size = sizeof (struct sockaddr_in);
63
64   if((ai = calloc(1, sizeof(Curl_addrinfo))) == NULL)
65     return NULL;
66
67   if((ai->ai_canonname = strdup("dummy")) == NULL) {
68     free(ai);
69     return NULL;
70   }
71
72   if((ai->ai_addr = calloc(1, ss_size)) == NULL) {
73     free(ai->ai_canonname);
74     free(ai);
75     return NULL;
76   }
77
78   ai->ai_family = AF_INET;
79   ai->ai_addrlen = ss_size;
80
81   return ai;
82 }
83 #endif /* LIB559 */
84
85
86 int test(char *URL)
87 {
88   CURL *easyh = NULL;
89   struct curl_hash *hp = NULL;
90   int result = 0;
91
92   if(!strcmp(URL, "check")) {
93     /* test harness script verifying if this test can run */
94     return 0; /* sure, run this! */
95   }
96
97   easyh = curl_easy_init();
98   if(!easyh) {
99     fprintf(stdout, "easy handle init failed\n");
100     result = TEST_ERR_MAJOR_BAD;
101     goto cleanup;
102   }
103   fprintf(stdout, "easy handle init OK\n");
104
105   fprintf(stdout, "creating hash...\n");
106   hp = Curl_mk_dnscache();
107   if(!hp) {
108     fprintf(stdout, "hash creation failed\n");
109     result = TEST_ERR_MAJOR_BAD;
110     goto cleanup;
111   }
112   fprintf(stdout, "hash creation OK\n");
113
114   /**/
115 #ifdef LIB559
116   {
117     char *data_key;
118     struct Curl_dns_entry *data_node;
119     struct Curl_dns_entry *nodep;
120     size_t key_len;
121
122     data_key = aprintf("%s:%d", "dummy", 0);
123     if(!data_key) {
124       fprintf(stdout, "data key creation failed\n");
125       result = TEST_ERR_MAJOR_BAD;
126       goto cleanup;
127     }
128     key_len = strlen(data_key);
129
130     data_node = calloc(1, sizeof(struct Curl_dns_entry));
131     if(!data_node) {
132       fprintf(stdout, "data node creation failed\n");
133       result = TEST_ERR_MAJOR_BAD;
134       free(data_key);
135       goto cleanup;
136     }
137
138     data_node->addr = fake_ai();
139     if(!data_node->addr) {
140       fprintf(stdout, "actual data creation failed\n");
141       result = TEST_ERR_MAJOR_BAD;
142       free(data_node);
143       free(data_key);
144       goto cleanup;
145     }
146
147     nodep = Curl_hash_add(hp, data_key, key_len+1, (void *)data_node);
148     if(!nodep) {
149       fprintf(stdout, "insertion into hash failed\n");
150       result = TEST_ERR_MAJOR_BAD;
151       Curl_freeaddrinfo(data_node->addr);
152       free(data_node);
153       free(data_key);
154       goto cleanup;
155     }
156
157     free(data_key);
158   }
159 #endif /* LIB559 */
160   /**/
161
162 cleanup:
163
164   fprintf(stdout, "destroying hash...\n");
165   Curl_hash_destroy(hp);
166   fprintf(stdout, "hash destruction OK\n");
167
168   fprintf(stdout, "destroying easy handle...\n");
169   curl_easy_cleanup(easyh);
170   fprintf(stdout, "easy handle destruction OK\n");
171
172   curl_global_cleanup();
173
174   return result;
175 }
176
177
178 #else /* !defined(SKIP_TEST) */
179
180
181 int test(char *URL)
182 {
183   (void)URL;
184   fprintf(stdout, "libcurl built with hidden symbols");
185   return 1; /* skip test */
186 }
187
188
189 #endif /* !defined(SKIP_TEST) */