-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconnection_tests.rs
151 lines (140 loc) · 6.4 KB
/
connection_tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#![allow(
clippy::ptr_as_ptr,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap
)]
use std::sync::Mutex;
mod common;
static TEST_SEQUENTIAL: Mutex<Executor> = Mutex::new(Executor);
#[derive(Clone, Copy)]
struct Executor;
impl Executor {
fn run_test(self, f: impl FnOnce()) {
f()
}
}
macro_rules! test_connection_diagnostics {
($func_name:ident,
in_connection_string = $in_connection_string:expr,
driver_completion = $driver_completion:expr,
expected_sql_state = $expected_sql_state:expr,
expected_sql_return = $expected_sql_return:expr,
expected_error_message = $expected_error_message:expr) => {
#[test]
fn $func_name() {
use cstr::WideChar;
use definitions::SmallInt;
let in_connection_string = $in_connection_string;
let driver_completion = $driver_completion;
let expected_sql_state = $expected_sql_state;
let expected_sql_return = $expected_sql_return;
let expected_error_message = $expected_error_message;
let mut out_connection_string: [WideChar; 64] = [0; 64];
let out_connection_string = &mut out_connection_string as *mut WideChar;
let string_length_2 = &mut 0;
let buffer_length: SmallInt = 65;
let mut env_handl: Handle = null_mut();
let mut conn_handl: Handle = null_mut();
let mut expected_sql_state_encoded =
cstr::to_widechar_vec(expected_sql_state.odbc_3_state);
expected_sql_state_encoded.push(0);
let mut in_connection_string_encoded = cstr::to_widechar_vec(in_connection_string);
in_connection_string_encoded.push(0);
crate::TEST_SEQUENTIAL.lock().unwrap().run_test(|| {
unsafe {
let _ = SQLAllocHandle(
HandleType::SQL_HANDLE_ENV,
std::ptr::null_mut(),
&mut env_handl as *mut Handle,
);
let _ = SQLAllocHandle(
HandleType::SQL_HANDLE_DBC,
env_handl,
&mut conn_handl as *mut Handle,
);
let actual_return_val = SQLDriverConnectW(
conn_handl as *mut _,
std::ptr::null_mut(),
in_connection_string_encoded.as_ptr(),
in_connection_string.len().try_into().unwrap(),
out_connection_string,
buffer_length,
string_length_2,
driver_completion as u16,
);
assert_eq!(expected_sql_return, actual_return_val);
verify_sql_diagnostics(
HandleType::SQL_HANDLE_DBC,
conn_handl as *mut _,
1,
expected_sql_state.odbc_3_state,
expected_error_message,
0,
);
let _ = SQLDisconnect(conn_handl as *mut _);
let _ = SQLFreeHandle(HandleType::SQL_HANDLE_DBC, conn_handl);
let _ = SQLFreeHandle(HandleType::SQL_HANDLE_ENV, env_handl);
};
});
}
};
}
mod integration {
use crate::common::verify_sql_diagnostics;
use atsql::{SQLAllocHandle, SQLDisconnect, SQLDriverConnectW, SQLFreeHandle};
use constants::{NO_DSN_OR_DRIVER, UNABLE_TO_CONNECT};
use definitions::{DriverConnectOption, Handle, HandleType, SqlReturn};
use std::ptr::null_mut;
test_connection_diagnostics! (
missing_user_in_connection_string,
in_connection_string = "Driver=MongoDB Atlas SQL ODBC Driver;SERVER=N_A;PWD=N_A",
driver_completion = DriverConnectOption::SQL_DRIVER_NO_PROMPT,
expected_sql_state = UNABLE_TO_CONNECT,
expected_sql_return = SqlReturn::ERROR,
expected_error_message = "[MongoDB][Core] Invalid Uri: One of [\"uid\", \"user\"] is required for a valid Mongo ODBC Uri"
);
test_connection_diagnostics! (
missing_pwd_in_connection_string,
in_connection_string = "Driver=MongoDB Atlas SQL ODBC Driver;SERVER=N_A;USER=N_A",
driver_completion = DriverConnectOption::SQL_DRIVER_NO_PROMPT,
expected_sql_state = UNABLE_TO_CONNECT,
expected_sql_return = SqlReturn::ERROR,
expected_error_message = "[MongoDB][Core] Invalid Uri: One of [\"password\", \"pwd\"] is required for a valid Mongo ODBC Uri"
);
test_connection_diagnostics!(
missing_driver_in_connection_string,
in_connection_string = "USER=N_A;SERVER=N_A;PWD=N_A",
driver_completion = DriverConnectOption::SQL_DRIVER_NO_PROMPT,
expected_sql_state = NO_DSN_OR_DRIVER,
expected_sql_return = SqlReturn::ERROR,
expected_error_message =
"[MongoDB][API] Missing property \"Driver\" or \"DSN\" in connection string"
);
test_connection_diagnostics!(
missing_driver_driver_connect_option_prompt,
in_connection_string = "USER=N_A;SERVER=N_A;PWD=N_A",
driver_completion = DriverConnectOption::SQL_DRIVER_PROMPT,
expected_sql_state = NO_DSN_OR_DRIVER,
expected_sql_return = SqlReturn::ERROR,
expected_error_message =
"[MongoDB][API] Missing property \"Driver\" or \"DSN\" in connection string"
);
test_connection_diagnostics!(
missing_driver_driver_connect_option_complete,
in_connection_string = "USER=N_A;SERVER=N_A;PWD=N_A",
driver_completion = DriverConnectOption::SQL_DRIVER_COMPLETE,
expected_sql_state = NO_DSN_OR_DRIVER,
expected_sql_return = SqlReturn::ERROR,
expected_error_message =
"[MongoDB][API] Missing property \"Driver\" or \"DSN\" in connection string"
);
test_connection_diagnostics!(
missing_driver_driver_connect_option_complete_required,
in_connection_string = "USER=N_A;SERVER=N_A;PWD=N_A",
driver_completion = DriverConnectOption::SQL_DRIVER_COMPLETE_REQUIRED,
expected_sql_state = NO_DSN_OR_DRIVER,
expected_sql_return = SqlReturn::ERROR,
expected_error_message =
"[MongoDB][API] Missing property \"Driver\" or \"DSN\" in connection string"
);
}