Browse code
handle get item error
Showing 2 changed files
... | ... |
@@ -7,6 +7,7 @@ |
7 | 7 |
|
8 | 8 |
namespace |
9 | 9 |
{ |
10 |
+conversation_result err = {"",""}; |
|
10 | 11 |
class impl : public conversation_ifc |
11 | 12 |
{ |
12 | 13 |
private: |
... | ... |
@@ -17,6 +18,11 @@ public: |
17 | 18 |
{ |
18 | 19 |
const pam_conv *conv; |
19 | 20 |
int get_conv_result = pam_.get_conv (request.handle(), &conv); |
21 |
+ |
|
22 |
+ if (get_conv_result != PAM_SUCCESS) { |
|
23 |
+ return err; |
|
24 |
+ } |
|
25 |
+ |
|
20 | 26 |
pam_message msg; |
21 | 27 |
msg.msg = const_cast<char *> ("Dual control token: "); |
22 | 28 |
msg.msg_style = PAM_PROMPT_ECHO_OFF; |
... | ... |
@@ -27,16 +33,17 @@ public: |
27 | 33 |
conv->appdata_ptr); |
28 | 34 |
std::string answer (responses[0]->resp); |
29 | 35 |
|
30 |
- std::string user; |
|
31 |
- std::string token; |
|
32 | 36 |
std::string::iterator delim = std::find (answer.begin(), answer.end(), ':'); |
33 | 37 |
|
34 |
- if (delim != answer.end()) { |
|
35 |
- user = std::string (answer.begin(), delim); |
|
36 |
- token = std::string (delim + 1, answer.end()); |
|
38 |
+ if (delim == answer.end()) { |
|
39 |
+ return err; |
|
37 | 40 |
} |
38 | 41 |
|
39 |
- return {user, token}; |
|
42 |
+ return { |
|
43 |
+ std::string (answer.begin(), delim), |
|
44 |
+ std::string (delim + 1, answer.end()) |
|
45 |
+ }; |
|
46 |
+ |
|
40 | 47 |
} |
41 | 48 |
}; |
42 | 49 |
} |
... | ... |
@@ -70,15 +70,22 @@ class fake_pam : public pam_ifc |
70 | 70 |
private: |
71 | 71 |
pam_handle *expected_handle_; |
72 | 72 |
conversation_data conversation_data_; |
73 |
+ int get_response_; |
|
73 | 74 |
pam_conv conv_; |
74 | 75 |
public: |
75 | 76 |
fake_pam (pam_handle *expected_handle, |
76 | 77 |
const conversation_data &conversation_data) |
77 | 78 |
: expected_handle_ (expected_handle), |
78 |
- conversation_data_ (conversation_data) |
|
79 |
+ conversation_data_ (conversation_data), |
|
80 |
+ get_response_ (PAM_SUCCESS) |
|
79 | 81 |
{} |
82 |
+ fake_pam (int get_response) : get_response_ (get_response) {} |
|
80 | 83 |
int get_conv (pam_handle *handle, const pam_conv **out) |
81 | 84 |
{ |
85 |
+ if (get_response_ != PAM_SUCCESS) { |
|
86 |
+ return get_response_; |
|
87 |
+ } |
|
88 |
+ |
|
82 | 89 |
if (expected_handle_ != handle) { |
83 | 90 |
throw std::string ("unexpected handle"); |
84 | 91 |
} |
... | ... |
@@ -97,7 +104,9 @@ std::shared_ptr<T> share (T *t) |
97 | 104 |
return std::shared_ptr<T> (t); |
98 | 105 |
} |
99 | 106 |
|
100 |
-conversation make_conversation(pam_handle *expected_handle, const std::string &answer) { |
|
107 |
+conversation make_conversation (pam_handle *expected_handle, |
|
108 |
+ const std::string &answer) |
|
109 |
+{ |
|
101 | 110 |
pam_message prompt; |
102 | 111 |
prompt.msg_style = PAM_PROMPT_ECHO_OFF; |
103 | 112 |
prompt.msg = const_cast<char *> ("Dual control token: "); |
... | ... |
@@ -115,10 +124,12 @@ conversation make_conversation(pam_handle *expected_handle, const std::string &a |
115 | 124 |
|
116 | 125 |
} |
117 | 126 |
|
118 |
-bool check_conversation_response(const std::string &answer, const std::string &expected_user, const std::string &expected_token) { |
|
127 |
+bool check_conversation_response (const std::string &answer, |
|
128 |
+ const std::string &expected_user, const std::string &expected_token) |
|
129 |
+{ |
|
119 | 130 |
// given |
120 | 131 |
pam_handle *handle = reinterpret_cast<pam_handle *> (29039); |
121 |
- conversation conversation (make_conversation(handle, answer)); |
|
132 |
+ conversation conversation (make_conversation (handle, answer)); |
|
122 | 133 |
pam_request request (handle, 0, 0, 0); |
123 | 134 |
|
124 | 135 |
// when |
... | ... |
@@ -135,27 +146,48 @@ bool returns_user_and_token() |
135 | 146 |
{ |
136 | 147 |
std::string user ("user"); |
137 | 148 |
std::string token ("token"); |
138 |
- return check_conversation_response(user + ":" + token, user, token); |
|
149 |
+ return check_conversation_response (user + ":" + token, user, token); |
|
139 | 150 |
} |
140 | 151 |
|
141 |
-int returns_empty_user_and_token_when_no_colon() { |
|
142 |
- return check_conversation_response("nocolon", "", ""); |
|
143 |
- } |
|
152 |
+int returns_empty_user_and_token_when_no_colon() |
|
153 |
+{ |
|
154 |
+ return check_conversation_response ("nocolon", "", ""); |
|
155 |
+} |
|
144 | 156 |
|
145 |
-int returns_empty_user_and_token_when_empty_answer() { |
|
146 |
- return check_conversation_response("", "", ""); |
|
147 |
- } |
|
157 |
+int returns_empty_user_and_token_when_empty_answer() |
|
158 |
+{ |
|
159 |
+ return check_conversation_response ("", "", ""); |
|
160 |
+} |
|
148 | 161 |
|
149 |
-int returns_empty_token_when_colon_end() { |
|
162 |
+int returns_empty_token_when_colon_end() |
|
163 |
+{ |
|
150 | 164 |
std::string user ("user"); |
151 | 165 |
std::string token (""); |
152 |
- return check_conversation_response(user + ":" + token, user, token); |
|
166 |
+ return check_conversation_response (user + ":" + token, user, token); |
|
153 | 167 |
} |
154 | 168 |
|
155 |
-int returns_empty_user_when_colon_start() { |
|
169 |
+int returns_empty_user_when_colon_start() |
|
170 |
+{ |
|
156 | 171 |
std::string user (""); |
157 | 172 |
std::string token ("token"); |
158 |
- return check_conversation_response(user + ":" + token, user, token); |
|
173 |
+ return check_conversation_response (user + ":" + token, user, token); |
|
174 |
+} |
|
175 |
+ |
|
176 |
+int returns_empty_user_and_token_when_pam_cant_create_conversation() |
|
177 |
+{ |
|
178 |
+ // given |
|
179 |
+ pam pam (share (new fake_pam (PAM_SERVICE_ERR))); |
|
180 |
+ conversation conversation = create_conversation (pam); |
|
181 |
+ pam_request request (0, 0, 0, 0); |
|
182 |
+ |
|
183 |
+ // when |
|
184 |
+ conversation_result actual = conversation.initiate (request); |
|
185 |
+ |
|
186 |
+ // then |
|
187 |
+ check (actual.user_name == "", "user name does not match"); |
|
188 |
+ check (actual.token == "", "token does not match"); |
|
189 |
+ |
|
190 |
+ succeed(); |
|
159 | 191 |
} |
160 | 192 |
|
161 | 193 |
RESET_VARS_START |
... | ... |
@@ -168,6 +200,7 @@ int run_tests() |
168 | 200 |
test (returns_empty_user_and_token_when_empty_answer); |
169 | 201 |
test (returns_empty_token_when_colon_end); |
170 | 202 |
test (returns_empty_user_when_colon_start); |
203 |
+ test (returns_empty_user_and_token_when_pam_cant_create_conversation); |
|
171 | 204 |
succeed(); |
172 | 205 |
} |
173 | 206 |
|
... | ... |
@@ -177,8 +210,6 @@ int main (int argc, char **argv) |
177 | 210 |
} |
178 | 211 |
|
179 | 212 |
/* |
180 |
-int returns_empty_token_when_colon_end() |
|
181 |
-int returns_empty_user_when_colon_begin() |
|
182 | 213 |
int returns_empty_user_and_token_when_pam_cant_create_conversation() |
183 | 214 |
int prompts_user_with_correct_text() |
184 | 215 |
int prompts_user_with_correct_style() |