Browse code
pam token conversation returns correct style
Greg Wiley authored on 17/04/2017 21:31:15
Showing 2 changed files
Showing 2 changed files
... | ... |
@@ -16,6 +16,7 @@ pam_token_conversation::pam_token_conversation(pam_handle_t *pamh, const pam_p p |
16 | 16 |
struct pam_message prompt; |
17 | 17 |
std::string message("Dual control token: "); |
18 | 18 |
prompt.msg = const_cast<char *>(message.c_str()); |
19 |
+ prompt.msg_style = PAM_PROMPT_ECHO_OFF; |
|
19 | 20 |
std::vector<const struct pam_message *> prompts(1); |
20 | 21 |
prompts[0] = &prompt; |
21 | 22 |
std::vector<struct pam_response *> answers(1); |
... | ... |
@@ -26,13 +26,13 @@ class fake_pam_conversation : public pam_conversation { |
26 | 26 |
} |
27 | 27 |
}; |
28 | 28 |
|
29 |
-class mock_pam_conversation : public pam_conversation { |
|
29 |
+class match_prompt_text_conversation : public pam_conversation { |
|
30 | 30 |
private: |
31 | 31 |
pam_response response_; |
32 | 32 |
std::string answer_; |
33 | 33 |
std::string prompt_; |
34 | 34 |
public: |
35 |
- mock_pam_conversation(const std::string &prompt) : prompt_(prompt), answer_("ok:123") {} |
|
35 |
+ match_prompt_text_conversation(const std::string &prompt) : prompt_(prompt), answer_("ok:123") {} |
|
36 | 36 |
int conv(const std::vector<const struct pam_message *> &prompts, std::vector<struct pam_response *> &answers) { |
37 | 37 |
if (prompt_ != prompts[0]->msg) { |
38 | 38 |
throw std::string("prompt does not match"); |
... | ... |
@@ -46,6 +46,26 @@ class mock_pam_conversation : public pam_conversation { |
46 | 46 |
|
47 | 47 |
}; |
48 | 48 |
|
49 |
+class match_prompt_style_conversation : public pam_conversation { |
|
50 |
+ private: |
|
51 |
+ pam_response response_; |
|
52 |
+ std::string answer_; |
|
53 |
+ int style_; |
|
54 |
+ public: |
|
55 |
+ match_prompt_style_conversation(int style) : style_(style), answer_("ok:123") {} |
|
56 |
+ int conv(const std::vector<const struct pam_message *> &prompts, std::vector<struct pam_response *> &answers) { |
|
57 |
+ if (style_ != prompts[0]->msg_style) { |
|
58 |
+ throw std::string("style does not match"); |
|
59 |
+ } |
|
60 |
+ response_.resp_retcode = 0; |
|
61 |
+ response_.resp = const_cast<char *>(answer_.c_str()); |
|
62 |
+ answers.resize(1); |
|
63 |
+ answers[0] = &response_; |
|
64 |
+ return 0; |
|
65 |
+ } |
|
66 |
+ |
|
67 |
+}; |
|
68 |
+ |
|
49 | 69 |
class fake_pam : public pam { |
50 | 70 |
private: |
51 | 71 |
std::shared_ptr<pam_conversation> conversation_; |
... | ... |
@@ -169,7 +189,7 @@ int returns_empty_user_and_token_when_pam_cant_create_conversation() { |
169 | 189 |
int prompts_user_with_correct_text() { |
170 | 190 |
// given |
171 | 191 |
pam_handle_t *pamh; |
172 |
- pam_conversation_p match_conversation = (pam_conversation_p) new mock_pam_conversation("Dual control token: "); |
|
192 |
+ pam_conversation_p match_conversation = (pam_conversation_p) new match_prompt_text_conversation("Dual control token: "); |
|
173 | 193 |
pam_p pam = (pam_p)new fake_pam(match_conversation); |
174 | 194 |
|
175 | 195 |
|
... | ... |
@@ -181,10 +201,27 @@ int prompts_user_with_correct_text() { |
181 | 201 |
fail(); |
182 | 202 |
} |
183 | 203 |
|
204 |
+} |
|
205 |
+ |
|
206 |
+int prompts_user_with_correct_style() { |
|
207 |
+ // given |
|
208 |
+ pam_handle_t *pamh; |
|
209 |
+ pam_conversation_p match_conversation = (pam_conversation_p) new match_prompt_style_conversation(PAM_PROMPT_ECHO_OFF); |
|
210 |
+ pam_p pam = (pam_p)new fake_pam(match_conversation); |
|
184 | 211 |
|
185 | 212 |
|
213 |
+ // when / then |
|
214 |
+ try { |
|
215 |
+ pam_token_conversation conversation(pamh, pam); |
|
216 |
+ succeed(); |
|
217 |
+ } catch (const std::string &x) { |
|
218 |
+ fail(); |
|
219 |
+ } |
|
186 | 220 |
} |
187 | 221 |
|
222 |
+ |
|
223 |
+ |
|
224 |
+ |
|
188 | 225 |
RESET_VARS_START |
189 | 226 |
RESET_VARS_END |
190 | 227 |
|
... | ... |
@@ -197,6 +234,7 @@ int run_tests() { |
197 | 234 |
test(returns_empty_user_and_token_when_empty_answer); |
198 | 235 |
test(returns_empty_user_and_token_when_pam_cant_create_conversation); |
199 | 236 |
test(prompts_user_with_correct_text); |
237 |
+ test(prompts_user_with_correct_style); |
|
200 | 238 |
succeed(); |
201 | 239 |
} |
202 | 240 |
|