git.fiddlerwoaroof.com
Browse code

pam token conversation handles empty conversation

Greg Wiley authored on 17/04/2017 18:08:31
Showing 2 changed files
... ...
@@ -8,7 +8,10 @@
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
-    pam->get_conversation(pamh, pam_conversation);
11
+    int conversation_result = pam->get_conversation(pamh, pam_conversation);
12
+    if (conversation_result != 0) {
13
+        return;
14
+    }
12 15
 
13 16
     const std::vector<const struct pam_message *> prompts(1);
14 17
     std::vector<struct pam_response *> answers(1);
... ...
@@ -31,9 +31,13 @@ class fake_pam : public pam {
31 31
         std::shared_ptr<pam_conversation> conversation_;
32 32
     public:
33 33
         fake_pam(std::shared_ptr<pam_conversation> conversation) : conversation_(conversation) {}
34
+        fake_pam() {}
34 35
         int get_conversation(pam_handle_t *pamh, std::shared_ptr<pam_conversation> &conversation) {
35
-           conversation = conversation_;
36
-           return 0;
36
+            if (conversation_) {
37
+               conversation = conversation_;
38
+               return 0;
39
+            }
40
+            return 12;
37 41
         }
38 42
 };
39 43
 
... ...
@@ -127,6 +131,21 @@ int returns_empty_user_when_colon_begin() {
127 131
     succeed();
128 132
 }
129 133
 
134
+int returns_empty_user_and_token_when_pam_cant_create_conversation() {
135
+    // given
136
+    pam_handle_t *pamh;
137
+    pam_p pam = (pam_p)new fake_pam;
138
+
139
+    //when
140
+    pam_token_conversation conversation(pamh, pam);
141
+
142
+    //then
143
+    check(conversation.user_name() == "", "did not return empty user name");
144
+    check(conversation.token() == "", "did not return empty token");
145
+    succeed();
146
+
147
+}
148
+
130 149
 
131 150
 RESET_VARS_START
132 151
 RESET_VARS_END
... ...
@@ -138,6 +157,7 @@ int run_tests() {
138 157
     test(returns_empty_token_when_colon_end);
139 158
     test(returns_empty_user_when_colon_begin);
140 159
     test(returns_empty_user_and_token_when_empty_answer);
160
+    test(returns_empty_user_and_token_when_pam_cant_create_conversation);
141 161
     succeed();
142 162
 }
143 163