Line data Source code
1 : #include "common.h"
2 :
3 :
4 : static void init_connect(void);
5 :
6 : static void
7 38 : init_connect(void)
8 : {
9 38 : CHKAllocEnv(&odbc_env, "S");
10 38 : CHKAllocConnect(&odbc_conn, "S");
11 38 : }
12 :
13 10 : TEST_MAIN()
14 : {
15 : char tmp[1024*4];
16 : SQLSMALLINT len;
17 10 : int succeeded = 0;
18 : bool is_freetds;
19 : bool is_ms;
20 : SQLRETURN rc;
21 :
22 : /* NOTE: In Windows, this function will copy the data from PWD into
23 : * a registry entry HKCU\Software\ODBC\ODBC.INI\{servername}
24 : */
25 10 : if (odbc_read_login_info())
26 0 : exit(1);
27 :
28 : #ifndef _WIN32
29 : /*
30 : * prepare our odbcinst.ini
31 : * is better to do it before connect cause uniODBC cache INIs
32 : * the name must be odbcinst.ini cause unixODBC accept only this name
33 : *
34 : * In Windows, odbcinst.ini text file is not used - you will need to
35 : * configure FreeTDS via the Odbcinst.ini registry key.
36 : */
37 10 : if (common_pwd.driver[0]) {
38 10 : FILE *f = fopen("odbcinst.ini", "w");
39 :
40 10 : if (f) {
41 10 : fprintf(f, "[FreeTDS]\nDriver = %s\n", common_pwd.driver);
42 10 : fclose(f);
43 : /* force iODBC */
44 10 : setenv("ODBCINSTINI", "./odbcinst.ini", 1);
45 10 : setenv("SYSODBCINSTINI", "./odbcinst.ini", 1);
46 : /* force unixODBC (only directory) */
47 10 : setenv("ODBCSYSINI", ".", 1);
48 : }
49 : }
50 : #endif
51 : /* Connect using SQLConnect() with Server, User and Password from PWD file
52 : * NOTE: In Windows this looks up DSN in HKCU\Software\ODBC\ODBC.INI, with fallback to HKLM
53 : */
54 10 : printf("SQLConnect(server=%s, ...) connect..\n", common_pwd.server);
55 10 : odbc_connect();
56 10 : is_freetds = odbc_driver_is_freetds();
57 10 : is_ms = odbc_db_is_microsoft();
58 10 : odbc_disconnect();
59 10 : ++succeeded;
60 :
61 10 : if (!is_freetds) {
62 0 : printf("Driver is not FreeTDS, exiting\n");
63 0 : odbc_test_skipped();
64 0 : return 0;
65 : }
66 :
67 : /* try connect string with using DSN= (should behave the same as SQLConnect)
68 : */
69 10 : printf("SQLDriverConnect(DSN=%s;...)\n", common_pwd.server);
70 10 : init_connect();
71 10 : sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;DATABASE=%s;", common_pwd.server,
72 : common_pwd.user, common_pwd.password, common_pwd.database);
73 10 : CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp)/sizeof(SQLTCHAR), &len, SQL_DRIVER_NOPROMPT, "SI");
74 10 : odbc_disconnect();
75 10 : ++succeeded;
76 :
77 : /* try connect string using DRIVER= (this means to look up DRIVER name in ODBCINST.INI) */
78 10 : printf("SQLDriverConnect(DRIVER=FreeTDS;SERVERNAME=%s;...)\n", common_pwd.server);
79 : #ifndef _WIN32
80 10 : printf("odbcinst.ini must exist and contain an entry [FreeTDS].\n");
81 : #endif
82 : /* this is expected to work with unixODBC, and Windows.
83 : * NOTE: This will connect to whatever driver you have configured in ODBCINST.INI,
84 : * not the actual driver we just built and are testing. However for this test,
85 : * that's OK as here we are testing connecting to a driver, not testing that driver.
86 : */
87 10 : init_connect();
88 10 : sprintf(tmp,
89 : "DRIVER=FreeTDS;SERVERNAME=%s;UID=%s;PWD=%s;DATABASE=%s;",
90 : common_pwd.server, common_pwd.user, common_pwd.password, common_pwd.database);
91 10 : rc = CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp) / sizeof(SQLTCHAR), &len,
92 : SQL_DRIVER_NOPROMPT, "SIE");
93 10 : if (rc == SQL_ERROR) {
94 0 : printf("Unable to open data source (ret=%d)\n", rc);
95 : #ifdef _WIN32
96 : printf("Try from admin command prompt:\n"
97 : "\todbcconf /A {INSTALLDRIVER \"FreeTDS|Driver=C:\\Program Files(x86)\\FreeTDS\\bin\\tdsodbc.dll\"}\n"
98 : "(replace path with your installation path for tdsodbc.dll if necessary)\n");
99 : #endif
100 : } else {
101 : ++succeeded;
102 : }
103 10 : odbc_disconnect();
104 :
105 : /* this is expected to work with iODBC (passing full path to driver)
106 : */
107 10 : if (common_pwd.driver[0]) {
108 10 : printf("SQLDriverConnect(DRIVER=%s;...)\n", common_pwd.driver);
109 10 : init_connect();
110 10 : sprintf(tmp,
111 : "DRIVER=%s;SERVERNAME=%s;UID=%s;PWD=%s;DATABASE=%s;",
112 : common_pwd.driver, common_pwd.server, common_pwd.user, common_pwd.password, common_pwd.database);
113 10 : rc = CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp) / sizeof(SQLTCHAR), &len,
114 : SQL_DRIVER_NOPROMPT, "SIE");
115 10 : if (rc == SQL_ERROR) {
116 0 : printf("Unable to open data source via iODBC syntax (ret=%d)\n", rc);
117 : } else {
118 10 : ++succeeded;
119 : }
120 10 : odbc_disconnect();
121 : }
122 :
123 10 : if (is_ms) {
124 : char app_name[130];
125 :
126 8 : memset(app_name, 'a', sizeof(app_name));
127 8 : app_name[sizeof(app_name) - 1] = 0;
128 :
129 : /* Try passing very long APP string.
130 : * The server is supposed to fail the connection if
131 : * this string is too long, make sure we truncate it.
132 : */
133 8 : printf("connect string DSN connect with a long APP..\n");
134 8 : init_connect();
135 8 : sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;DATABASE=%s;APP=%s",
136 : common_pwd.server, common_pwd.user, common_pwd.password, common_pwd.database, app_name);
137 8 : CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp) / sizeof(SQLTCHAR), &len,
138 : SQL_DRIVER_NOPROMPT, "SI");
139 8 : odbc_disconnect();
140 : }
141 :
142 : /* at least one should success.. */
143 10 : if (succeeded < 3) {
144 0 : ODBC_REPORT_ERROR("Too few successes");
145 0 : exit(1);
146 : }
147 :
148 10 : printf("Done.\n");
149 10 : return 0;
150 : }
|