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