Browse code
refactor conversation tests
Greg Wiley authored on 20/04/2017 22:04:36
Showing 1 changed files
Showing 1 changed files
... | ... |
@@ -97,64 +97,36 @@ std::shared_ptr<T> share (T *t) |
97 | 97 |
return std::shared_ptr<T> (t); |
98 | 98 |
} |
99 | 99 |
|
100 |
-bool returns_user_and_token() |
|
100 |
+conversation make_conversation (pam_handle *expected_handle, |
|
101 |
+ const std::string &answer) |
|
101 | 102 |
{ |
102 |
- |
|
103 |
- // given |
|
104 |
- pam_handle *handle = reinterpret_cast<pam_handle *> (29039); |
|
105 |
- std::string user ("user"); |
|
106 |
- std::string token ("token"); |
|
107 | 103 |
pam_message prompt; |
108 | 104 |
prompt.msg_style = PAM_PROMPT_ECHO_OFF; |
109 | 105 |
prompt.msg = const_cast<char *> ("Dual control token: "); |
110 | 106 |
pam_response response; |
111 | 107 |
response.resp_retcode = 0; |
112 |
- std::string response_text (user + ":" + token); |
|
108 |
+ std::string response_text (answer); |
|
113 | 109 |
response.resp = const_cast<char *> (response_text.c_str()); |
114 | 110 |
conversation_data conversation_data = { |
115 | 111 |
std::vector<pam_message> (&prompt, &prompt + 1), |
116 | 112 |
std::vector<pam_response> (&response, &response + 1), |
117 | 113 |
PAM_SUCCESS |
118 | 114 |
}; |
119 |
- pam pam (share (new fake_pam (handle, conversation_data))); |
|
120 |
- pam_request request (handle, 0, 0, 0); |
|
121 |
- |
|
122 |
- conversation conversation (create_conversation (pam)); |
|
115 |
+ pam pam (share (new fake_pam (expected_handle, conversation_data))); |
|
116 |
+ return create_conversation (pam); |
|
123 | 117 |
|
124 |
- // when |
|
125 |
- conversation_result actual = conversation.initiate (request); |
|
126 |
- |
|
127 |
- // then |
|
128 |
- check (actual.user_name == user, "user name does not match"); |
|
129 |
- check (actual.token == token, "token does not match"); |
|
130 |
- |
|
131 |
- succeed(); |
|
132 | 118 |
} |
133 | 119 |
|
134 |
-bool returns_user_and_token_from_pam_conversation() |
|
120 |
+bool returns_user_and_token() |
|
135 | 121 |
{ |
136 | 122 |
|
137 | 123 |
// given |
138 | 124 |
pam_handle *handle = reinterpret_cast<pam_handle *> (29039); |
139 |
- std::string user ("user1"); |
|
140 |
- std::string token ("token1"); |
|
141 |
- pam_message prompt; |
|
142 |
- prompt.msg_style = PAM_PROMPT_ECHO_OFF; |
|
143 |
- prompt.msg = const_cast<char *> ("Dual control token: "); |
|
144 |
- pam_response response; |
|
145 |
- response.resp_retcode = 0; |
|
146 |
- std::string response_text (user + ":" + token); |
|
147 |
- response.resp = const_cast<char *> (response_text.c_str()); |
|
148 |
- conversation_data conversation_data = { |
|
149 |
- std::vector<pam_message> (&prompt, &prompt + 1), |
|
150 |
- std::vector<pam_response> (&response, &response + 1), |
|
151 |
- PAM_SUCCESS |
|
152 |
- }; |
|
153 |
- pam pam (share (new fake_pam (handle, conversation_data))); |
|
125 |
+ std::string user ("user"); |
|
126 |
+ std::string token ("token"); |
|
127 |
+ conversation conversation (make_conversation (handle, user + ":" + token)); |
|
154 | 128 |
pam_request request (handle, 0, 0, 0); |
155 | 129 |
|
156 |
- conversation conversation (create_conversation (pam)); |
|
157 |
- |
|
158 | 130 |
// when |
159 | 131 |
conversation_result actual = conversation.initiate (request); |
160 | 132 |
|
... | ... |
@@ -165,26 +137,14 @@ bool returns_user_and_token_from_pam_conversation() |
165 | 137 |
succeed(); |
166 | 138 |
} |
167 | 139 |
|
168 |
-int returns_empty_user_and_token_when_no_colon() { |
|
140 |
+int returns_empty_user_and_token_when_no_colon() |
|
141 |
+{ |
|
142 |
+ |
|
169 | 143 |
// given |
170 | 144 |
pam_handle *handle = reinterpret_cast<pam_handle *> (29039); |
171 |
- pam_message prompt; |
|
172 |
- prompt.msg_style = PAM_PROMPT_ECHO_OFF; |
|
173 |
- prompt.msg = const_cast<char *> ("Dual control token: "); |
|
174 |
- pam_response response; |
|
175 |
- response.resp_retcode = 0; |
|
176 |
- std::string response_text ("this_has_no_colon"); |
|
177 |
- response.resp = const_cast<char *> (response_text.c_str()); |
|
178 |
- conversation_data conversation_data = { |
|
179 |
- std::vector<pam_message> (&prompt, &prompt + 1), |
|
180 |
- std::vector<pam_response> (&response, &response + 1), |
|
181 |
- PAM_SUCCESS |
|
182 |
- }; |
|
183 |
- pam pam (share (new fake_pam (handle, conversation_data))); |
|
145 |
+ conversation conversation (make_conversation (handle, "nocolon")); |
|
184 | 146 |
pam_request request (handle, 0, 0, 0); |
185 | 147 |
|
186 |
- conversation conversation (create_conversation (pam)); |
|
187 |
- |
|
188 | 148 |
// when |
189 | 149 |
conversation_result actual = conversation.initiate (request); |
190 | 150 |
|
... | ... |
@@ -192,28 +152,15 @@ int returns_empty_user_and_token_when_no_colon() { |
192 | 152 |
check (actual.user_name == "", "user name does not match"); |
193 | 153 |
check (actual.token == "", "token does not match"); |
194 | 154 |
succeed(); |
195 |
- } |
|
155 |
+} |
|
196 | 156 |
|
197 |
-int returns_empty_user_and_token_when_empty_answer() { |
|
157 |
+int returns_empty_user_and_token_when_empty_answer() |
|
158 |
+{ |
|
198 | 159 |
// given |
199 | 160 |
pam_handle *handle = reinterpret_cast<pam_handle *> (29039); |
200 |
- pam_message prompt; |
|
201 |
- prompt.msg_style = PAM_PROMPT_ECHO_OFF; |
|
202 |
- prompt.msg = const_cast<char *> ("Dual control token: "); |
|
203 |
- pam_response response; |
|
204 |
- response.resp_retcode = 0; |
|
205 |
- std::string response_text (""); |
|
206 |
- response.resp = const_cast<char *> (response_text.c_str()); |
|
207 |
- conversation_data conversation_data = { |
|
208 |
- std::vector<pam_message> (&prompt, &prompt + 1), |
|
209 |
- std::vector<pam_response> (&response, &response + 1), |
|
210 |
- PAM_SUCCESS |
|
211 |
- }; |
|
212 |
- pam pam (share (new fake_pam (handle, conversation_data))); |
|
161 |
+ conversation conversation (make_conversation (handle, "")); |
|
213 | 162 |
pam_request request (handle, 0, 0, 0); |
214 | 163 |
|
215 |
- conversation conversation (create_conversation (pam)); |
|
216 |
- |
|
217 | 164 |
// when |
218 | 165 |
conversation_result actual = conversation.initiate (request); |
219 | 166 |
|
... | ... |
@@ -221,7 +168,7 @@ int returns_empty_user_and_token_when_empty_answer() { |
221 | 168 |
check (actual.user_name == "", "user name does not match"); |
222 | 169 |
check (actual.token == "", "token does not match"); |
223 | 170 |
succeed(); |
224 |
- } |
|
171 |
+} |
|
225 | 172 |
|
226 | 173 |
RESET_VARS_START |
227 | 174 |
RESET_VARS_END |
... | ... |
@@ -229,7 +176,6 @@ RESET_VARS_END |
229 | 176 |
int run_tests() |
230 | 177 |
{ |
231 | 178 |
test (returns_user_and_token); |
232 |
- test (returns_user_and_token_from_pam_conversation); |
|
233 | 179 |
test (returns_empty_user_and_token_when_no_colon); |
234 | 180 |
test (returns_empty_user_and_token_when_empty_answer); |
235 | 181 |
succeed(); |