Skip to content

Commit 7025856

Browse files
committed
odbc: Write a test for SQL_COPT_SS_OLDPWD option
Create a login with an expiring password, check we can login and change the password at the same time. Signed-off-by: Frediano Ziglio <freddy77@gmail.com>
1 parent 9a13fcc commit 7025856

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

src/odbc/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(tests
3333
tvp tokens
3434
describeparam
3535
reexec
36+
oldpwd
3637
)
3738

3839
if(WIN32)

src/odbc/unittests/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ TESTS = \
8686
tokens$(EXEEXT) \
8787
describeparam$(EXEEXT) \
8888
reexec$(EXEEXT) \
89+
oldpwd$(EXEEXT) \
8990
$(NULL)
9091

91-
check_PROGRAMS = $(TESTS) oldpwd$(EXEEXT)
92+
check_PROGRAMS = $(TESTS)
9293

9394
t0001_SOURCES = t0001.c
9495
t0002_SOURCES = t0002.c

src/odbc/unittests/common.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,10 @@ odbc_read_error(void)
638638

639639
memset(odbc_err, 0, sizeof(odbc_err));
640640
memset(odbc_sqlstate, 0, sizeof(odbc_sqlstate));
641-
CHKGetDiagRec(SQL_HANDLE_STMT, odbc_stmt, 1, state, NULL, err, sizeof(odbc_err), NULL, "SI");
641+
if (odbc_stmt != SQL_NULL_HSTMT)
642+
CHKGetDiagRec(SQL_HANDLE_STMT, odbc_stmt, 1, state, NULL, err, sizeof(odbc_err), NULL, "SI");
643+
else
644+
CHKGetDiagRec(SQL_HANDLE_DBC, odbc_conn, 1, state, NULL, err, sizeof(odbc_err), NULL, "SI");
642645
strcpy(odbc_err, C(err));
643646
strcpy(odbc_sqlstate, C(state));
644647
ODBC_FREE();

src/odbc/unittests/oldpwd.c

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,92 @@
11
#include "common.h"
22

3+
#include <assert.h>
4+
35
/* change password on server */
46

7+
#define USER "freetds_oldpwd"
8+
#define PWD1 "TestPWD1?"
9+
#define PWD2 "OtherPWD2@"
10+
511
static void
612
my_attrs(void)
713
{
8-
SQLSetConnectAttr(odbc_conn, 1226 /*SQL_COPT_SS_OLDPWD */ ,
9-
(SQLPOINTER) common_pwd.password, SQL_NTS);
10-
strcpy(common_pwd.password, "testpwd$");
14+
SQLSetConnectAttr(odbc_conn, 1226 /* SQL_COPT_SS_OLDPWD */ ,
15+
(SQLPOINTER) PWD1, SQL_NTS);
16+
strcpy(common_pwd.user, USER);
17+
strcpy(common_pwd.password, PWD2);
1118
}
1219

1320
TEST_MAIN()
1421
{
22+
const char *ci;
23+
char *old_user, *old_pwd;
24+
1525
odbc_use_version3 = true;
26+
27+
/* Check if we are in CI */
28+
ci = getenv("CI");
29+
if (!ci || strcasecmp(ci, "true") != 0) {
30+
odbc_test_skipped();
31+
return 0;
32+
}
33+
34+
odbc_connect();
35+
36+
/* minimum TDS 7.2 and MSSQL 2012 */
37+
if (odbc_tds_version() < 0x702 || odbc_db_version_int() < 0x0a000000u) {
38+
odbc_disconnect();
39+
odbc_test_skipped();
40+
return 0;
41+
}
42+
43+
/* create new login for this test and disconnect */
44+
odbc_command_with_result(odbc_stmt, "DROP LOGIN " USER);
45+
odbc_command("CREATE LOGIN " USER " WITH PASSWORD='" PWD1 "' MUST_CHANGE, "
46+
"DEFAULT_DATABASE = tempdb, CHECK_EXPIRATION = ON");
47+
48+
odbc_disconnect();
49+
50+
old_user = strdup(common_pwd.user);
51+
assert(old_user);
52+
old_pwd = strdup(common_pwd.password);
53+
assert(old_pwd);
54+
55+
/* login with new password should fail */
56+
strcpy(common_pwd.user, USER);
57+
strcpy(common_pwd.password, PWD1);
58+
59+
CHKAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &odbc_env, "S");
60+
SQLSetEnvAttr(odbc_env, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) (SQL_OV_ODBC3), SQL_IS_UINTEGER);
61+
CHKAllocHandle(SQL_HANDLE_DBC, odbc_env, &odbc_conn, "S");
62+
CHKConnect(T(common_pwd.server), SQL_NTS, T(common_pwd.user), SQL_NTS, T(common_pwd.password), SQL_NTS, "E");
63+
odbc_read_error();
64+
if (strcmp(odbc_sqlstate, "42000") != 0 ||
65+
strstr(odbc_err, USER) == NULL || strstr(odbc_err, "The password of the account must be changed") == NULL) {
66+
fprintf(stderr, "Unexpected sql state %s returned\n", odbc_sqlstate);
67+
odbc_disconnect();
68+
return 1;
69+
}
70+
odbc_disconnect();
71+
72+
/* login and change password */
1673
odbc_set_conn_attr = my_attrs;
1774
odbc_connect();
75+
odbc_disconnect();
76+
77+
/* login wiht new password */
78+
odbc_set_conn_attr = NULL;
79+
odbc_connect();
80+
odbc_disconnect();
1881

82+
/* drop created login */
83+
strcpy(common_pwd.user, old_user);
84+
free(old_user);
85+
strcpy(common_pwd.password, old_pwd);
86+
free(old_pwd);
87+
odbc_connect();
88+
odbc_command("DROP LOGIN " USER);
1989
odbc_disconnect();
90+
2091
return 0;
2192
}

0 commit comments

Comments
 (0)