git.fiddlerwoaroof.com
Browse code

check for nonzer conversation result

Greg Wiley authored on 17/04/2017 21:43:05
Showing 2 changed files
... ...
@@ -8,8 +8,8 @@
8 8
 
9 9
 pam_token_conversation::pam_token_conversation(pam_handle_t *pamh, const pam_p pam) {
10 10
     pam_conversation_p pam_conversation;
11
-    int conversation_result = pam->get_conversation(pamh, pam_conversation);
12
-    if (conversation_result != 0) {
11
+    int get_conversation_result = pam->get_conversation(pamh, pam_conversation);
12
+    if (get_conversation_result != 0) {
13 13
         return;
14 14
     }
15 15
 
... ...
@@ -20,7 +20,10 @@ pam_token_conversation::pam_token_conversation(pam_handle_t *pamh, const pam_p p
20 20
     std::vector<const struct pam_message *> prompts(1);
21 21
     prompts[0] = &prompt;
22 22
     std::vector<struct pam_response *> answers(1);
23
-    pam_conversation->conv(prompts, answers);
23
+    int conversation_result = pam_conversation->conv(prompts, answers);
24
+    if (conversation_result) {
25
+        return;
26
+    }
24 27
     std::string answer(answers[0]->resp);
25 28
     std::string::iterator delim = std::find(answer.begin(), answer.end(), ':');
26 29
     if (delim == answer.end()) {
... ...
@@ -26,6 +26,14 @@ class fake_pam_conversation : public pam_conversation {
26 26
         }
27 27
 };
28 28
 
29
+class fake_failing_conversation: public pam_conversation {
30
+
31
+    public:
32
+        int conv(const std::vector<const struct pam_message *> &prompts, std::vector<struct pam_response *> &answers) {
33
+            return 1;
34
+        }
35
+};
36
+
29 37
 class match_prompt_text_conversation : public pam_conversation {
30 38
     private:
31 39
         pam_response response_;
... ...
@@ -219,7 +227,20 @@ int prompts_user_with_correct_style() {
219 227
     }
220 228
 }
221 229
 
230
+int returns_empty_user_and_token_when_conversation_fails() {
231
+    //given
232
+    pam_handle_t *pamh;
233
+    pam_conversation_p fake_conversation = (pam_conversation_p) new fake_failing_conversation;
234
+    pam_p pam = (pam_p) new fake_pam(fake_conversation);
235
+
236
+    //when
237
+    pam_token_conversation conversation(pamh, pam);
222 238
 
239
+    //then
240
+    check(conversation.user_name() == "", "did not return empty user name");
241
+    check(conversation.token() == "", "did not return empty token");
242
+    succeed();
243
+}
223 244
 
224 245
 
225 246
 RESET_VARS_START
... ...
@@ -235,6 +256,7 @@ int run_tests() {
235 256
     test(returns_empty_user_and_token_when_pam_cant_create_conversation);
236 257
     test(prompts_user_with_correct_text);
237 258
     test(prompts_user_with_correct_style);
259
+    test(returns_empty_user_and_token_when_conversation_fails);
238 260
     succeed();
239 261
 }
240 262