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 : */
84 10 : init_connect();
85 10 : sprintf(tmp,
86 : "DRIVER=FreeTDS;SERVERNAME=%s;UID=%s;PWD=%s;DATABASE=%s;",
87 : common_pwd.server, common_pwd.user, common_pwd.password, common_pwd.database);
88 10 : rc = CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp) / sizeof(SQLTCHAR), &len,
89 : SQL_DRIVER_NOPROMPT, "SIE");
90 10 : if (rc == SQL_ERROR) {
91 0 : printf("Unable to open data source (ret=%d)\n", rc);
92 : #ifdef _WIN32
93 : printf("Try from admin command prompt:\n"
94 : "\todbcconf /A {INSTALLDRIVER \"FreeTDS|Driver=C:\\Program Files(x86)\\FreeTDS\\bin\\tdsodbc.dll\"}\n"
95 : "(replace path with your installation path for tdsodbc.dll if necessary)\n");
96 : #endif
97 : } else {
98 : ++succeeded;
99 : }
100 10 : odbc_disconnect();
101 :
102 : /* this is expected to work with iODBC (passing full path to driver)
103 : */
104 10 : if (common_pwd.driver[0]) {
105 10 : printf("SQLDriverConnect(DRIVER=%s;...)\n", common_pwd.driver);
106 10 : init_connect();
107 10 : sprintf(tmp,
108 : "DRIVER=%s;SERVERNAME=%s;UID=%s;PWD=%s;DATABASE=%s;",
109 : common_pwd.driver, common_pwd.server, common_pwd.user, common_pwd.password, common_pwd.database);
110 10 : rc = CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp) / sizeof(SQLTCHAR), &len,
111 : SQL_DRIVER_NOPROMPT, "SIE");
112 10 : if (rc == SQL_ERROR) {
113 0 : printf("Unable to open data source via iODBC syntax (ret=%d)\n", rc);
114 : } else {
115 10 : ++succeeded;
116 : }
117 10 : odbc_disconnect();
118 : }
119 :
120 10 : if (is_ms) {
121 : char app_name[130];
122 :
123 8 : memset(app_name, 'a', sizeof(app_name));
124 8 : app_name[sizeof(app_name) - 1] = 0;
125 :
126 : /* Try passing very long APP string.
127 : * The server is supposed to fail the connection if
128 : * this string is too long, make sure we truncate it.
129 : */
130 8 : printf("connect string DSN connect with a long APP..\n");
131 8 : init_connect();
132 8 : sprintf(tmp, "DSN=%s;UID=%s;PWD=%s;DATABASE=%s;APP=%s",
133 : common_pwd.server, common_pwd.user, common_pwd.password, common_pwd.database, app_name);
134 8 : CHKDriverConnect(NULL, T(tmp), SQL_NTS, (SQLTCHAR *) tmp, sizeof(tmp) / sizeof(SQLTCHAR), &len,
135 : SQL_DRIVER_NOPROMPT, "SI");
136 8 : odbc_disconnect();
137 : }
138 :
139 : /* at least one should success.. */
140 10 : if (succeeded < 3) {
141 0 : ODBC_REPORT_ERROR("Too few successes");
142 0 : exit(1);
143 : }
144 :
145 10 : printf("Done.\n");
146 10 : return 0;
147 : }
|