~ubuntu-branches/debian/lenny/freetds/lenny

« back to all changes in this revision

Viewing changes to src/odbc/unittests/timeout3.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2008-08-02 11:49:53 UTC
  • mfrom: (2.1.10 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080802114953-0qdeowgl63k42n2c
Tags: 0.82-4
* Fix a typo in the freetds-common description, ugh
* Versioned replaces of libct4 by freetds-common, since the current one
  obviously doesn't have overlapping files.
* tdsodbc: check for /var/lib/odbc existence before removing it in the
  postinst, since there are cases where it won't exist on upgrade (i.e.,
  if the driver was never enabled in the first place).  Closes: #493303.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "common.h"
 
2
 
 
3
/* TODO port to windows, use thread */
 
4
#if defined(TDS_HAVE_PTHREAD_MUTEX) && HAVE_ALARM
 
5
 
 
6
#if HAVE_UNISTD_H
 
7
#include <unistd.h>
 
8
#endif /* HAVE_UNISTD_H */
 
9
 
 
10
#if TIME_WITH_SYS_TIME
 
11
# if HAVE_SYS_TIME_H
 
12
#  include <sys/time.h>
 
13
# endif
 
14
# include <time.h>
 
15
#else
 
16
# if HAVE_SYS_TIME_H
 
17
#  include <sys/time.h>
 
18
# else
 
19
#  include <time.h>
 
20
# endif
 
21
#endif
 
22
 
 
23
#if HAVE_ERRNO_H
 
24
#include <errno.h>
 
25
#endif /* HAVE_ERRNO_H */
 
26
 
 
27
#if HAVE_SYS_SOCKET_H
 
28
#include <sys/socket.h>
 
29
#endif /* HAVE_SYS_SOCKET_H */
 
30
 
 
31
#if HAVE_NETINET_IN_H
 
32
#include <netinet/in.h>
 
33
#endif /* HAVE_NETINET_IN_H */
 
34
 
 
35
#include <pthread.h>
 
36
 
 
37
#include "tds.h"
 
38
 
 
39
/*
 
40
        test connection timeout
 
41
*/
 
42
 
 
43
static char software_version[] = "$Id: timeout3.c,v 1.6 2007/11/26 18:12:31 freddy77 Exp $";
 
44
static void *no_unused_var_warn[] = { software_version, no_unused_var_warn };
 
45
 
 
46
static void init_connect(void);
 
47
 
 
48
static void
 
49
init_connect(void)
 
50
{
 
51
        if (SQLAllocEnv(&Environment) != SQL_SUCCESS) {
 
52
                printf("Unable to allocate env\n");
 
53
                exit(1);
 
54
        }
 
55
 
 
56
        SQLSetEnvAttr(Environment, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) (SQL_OV_ODBC3), SQL_IS_UINTEGER);
 
57
 
 
58
        if (SQLAllocConnect(Environment, &Connection) != SQL_SUCCESS) {
 
59
                printf("Unable to allocate connection\n");
 
60
                SQLFreeEnv(Environment);
 
61
                exit(1);
 
62
        }
 
63
}
 
64
 
 
65
static pthread_t      fake_thread;
 
66
static TDS_SYS_SOCKET fake_sock;
 
67
 
 
68
static void *fake_thread_proc(void * arg);
 
69
 
 
70
static int
 
71
init_fake_server(int ip_port)
 
72
{
 
73
        struct sockaddr_in sin;
 
74
        TDS_SYS_SOCKET s;
 
75
        int err;
 
76
 
 
77
        memset(&sin, 0, sizeof(sin));
 
78
        sin.sin_addr.s_addr = INADDR_ANY;
 
79
        sin.sin_port = htons((short) ip_port);
 
80
        sin.sin_family = AF_INET;
 
81
 
 
82
        if (TDS_IS_SOCKET_INVALID(s = socket(AF_INET, SOCK_STREAM, 0))) {
 
83
                perror("socket");
 
84
                exit(1);
 
85
        }
 
86
        if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
 
87
                perror("bind");
 
88
                CLOSESOCKET(s);
 
89
                return 1;
 
90
        }
 
91
        listen(s, 5);
 
92
        err = pthread_create(&fake_thread, NULL, fake_thread_proc, int2ptr(s));
 
93
        if (err != 0) {
 
94
                perror("pthread_create");
 
95
                exit(1);
 
96
        }
 
97
        return 0;
 
98
}
 
99
 
 
100
static void *
 
101
fake_thread_proc(void * arg)
 
102
{
 
103
        TDS_SYS_SOCKET s = ptr2int(arg);
 
104
        socklen_t len;
 
105
        char buf[128];
 
106
        struct sockaddr_in sin;
 
107
 
 
108
        memset(&sin, 0, sizeof(sin));
 
109
        len = sizeof(sin);
 
110
        alarm(30);
 
111
        if ((fake_sock = accept(s, (struct sockaddr *) &sin, &len)) < 0) {
 
112
                perror("accept");
 
113
                exit(1);
 
114
        }
 
115
        CLOSESOCKET(s);
 
116
 
 
117
        for (;;) {
 
118
                /* just read and discard */
 
119
                len = READSOCKET(fake_sock, buf, sizeof(buf));
 
120
                if (len == 0)
 
121
                        break;
 
122
                if (len < 0 && sock_errno != TDSSOCK_EINPROGRESS)
 
123
                        break;
 
124
        }
 
125
        return NULL;
 
126
}
 
127
 
 
128
int
 
129
main(int argc, char *argv[])
 
130
{
 
131
        int res;
 
132
        char tmp[2048];
 
133
        char sqlstate[6];
 
134
        SQLSMALLINT len;
 
135
        int port;
 
136
        time_t start_time, end_time;
 
137
 
 
138
        if (read_login_info())
 
139
                exit(1);
 
140
 
 
141
        /*
 
142
         * prepare our odbcinst.ini 
 
143
         * is better to do it before connect cause uniODBC cache INIs
 
144
         * the name must be odbcinst.ini cause unixODBC accept only this name
 
145
         */
 
146
        if (DRIVER[0]) {
 
147
                FILE *f = fopen("odbcinst.ini", "w");
 
148
 
 
149
                if (f) {
 
150
                        fprintf(f, "[FreeTDS]\nDriver = %s\n", DRIVER);
 
151
                        fclose(f);
 
152
                        /* force iODBC */
 
153
                        setenv("ODBCINSTINI", "./odbcinst.ini", 1);
 
154
                        setenv("SYSODBCINSTINI", "./odbcinst.ini", 1);
 
155
                        /* force unixODBC (only directory) */
 
156
                        setenv("ODBCSYSINI", ".", 1);
 
157
                }
 
158
        }
 
159
 
 
160
        for (port = 12340; port < 12350; ++port)
 
161
                if (!init_fake_server(port))
 
162
                        break;
 
163
        if (port == 12350) {
 
164
                fprintf(stderr, "Cannot bind to a port\n");
 
165
                return 1;
 
166
        }
 
167
        printf("Fake server binded at port %d\n", port);
 
168
 
 
169
        init_connect();
 
170
        res = SQLSetConnectAttr(Connection, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER) 10, sizeof(SQLINTEGER));
 
171
        if (!SQL_SUCCEEDED(res))
 
172
                ODBC_REPORT_ERROR("SQLSetConnectAttr error");
 
173
        res = SQLSetConnectAttr(Connection, SQL_ATTR_LOGIN_TIMEOUT, (SQLPOINTER) 10, sizeof(SQLINTEGER));
 
174
        if (!SQL_SUCCEEDED(res))
 
175
                ODBC_REPORT_ERROR("SQLSetConnectAttr error");
 
176
 
 
177
        /* this is expected to work with unixODBC */
 
178
        printf("try to connect to our port just to check connection timeout\n");
 
179
        sprintf(tmp, "DRIVER=FreeTDS;SERVER=127.0.0.1;Port=%d;TDS_Version=7.0;UID=test;PWD=test;DATABASE=tempdb;", port);
 
180
        start_time = time(NULL);
 
181
        res = SQLDriverConnect(Connection, NULL, (SQLCHAR *) tmp, SQL_NTS, (SQLCHAR *) tmp, sizeof(tmp), &len, SQL_DRIVER_NOPROMPT);
 
182
        if (SQL_SUCCEEDED(res)) {
 
183
                fprintf(stderr, "SQLDriverConnect should fail (res=%d)\n", (int) res);
 
184
                return 1;
 
185
        }
 
186
        end_time = time(NULL);
 
187
 
 
188
        strcpy(sqlstate, "XXXXX");
 
189
        tmp[0] = 0;
 
190
        res = SQLGetDiagRec(SQL_HANDLE_DBC, Connection, 1, (SQLCHAR *) sqlstate, NULL, (SQLCHAR *) tmp, sizeof(tmp), NULL);
 
191
        if (!SQL_SUCCEEDED(res)) {
 
192
                printf("SQLGetDiagRec should not fail\n");
 
193
                return 1;
 
194
        }
 
195
        Disconnect();
 
196
        CLOSESOCKET(fake_sock);
 
197
        pthread_join(fake_thread, NULL);
 
198
 
 
199
        printf("Message: %s - %s\n", sqlstate, tmp);
 
200
        if (strcmp(sqlstate, "HYT00") || !strstr(tmp, "Timeout")) {
 
201
                fprintf(stderr, "Invalid timeout message\n");
 
202
                return 1;
 
203
        }
 
204
        if (end_time - start_time < 10 || end_time - start_time > 16) {
 
205
                fprintf(stderr, "Unexpected connect timeout (%d)\n", (int) (end_time - start_time));
 
206
                return 1;
 
207
        }
 
208
 
 
209
        printf("Done.\n");
 
210
        return 0;
 
211
}
 
212
 
 
213
#else   /* !TDS_HAVE_PTHREAD_MUTEX */
 
214
int
 
215
main(void)
 
216
{
 
217
        printf("Not possible for this platform.\n");
 
218
        return 0;
 
219
}
 
220
#endif