git.fiddlerwoaroof.com
conversation_test.cc
7684972a
 /* Copyright (C) CJ Affiliate
  *
  * You may use, distribute and modify this code under  the
  * terms of the  GNU General Public License  version 2  or
  * later.
  *
  * You should have received a copy of the license with this
  * file. If not, you will find a copy in the "LICENSE" file
  * at https://github.com/cjdev/dual-control.
  */
 
01c00cfe
 #include <vector>
 #include <algorithm>
 #include <string>
2fade7af
 #include <algorithm>
 #include <memory>
 #include <cstring>
0b6e39a6
 #include <security/pam_modules.h>
 
2fade7af
 #include <iostream>
 
 #include "request.h"
0b6e39a6
 #include "conversation.h"
 #include "test_util.h"
d4af7e88
 #include "sys_pam.h"
0b6e39a6
 
2fade7af
 struct conversation_data {
     std::vector<pam_message> expected_prompts;
     std::vector<pam_response> responses;
     int return_value;
01c00cfe
 };
 
2fade7af
 bool same_prompts (const std::vector<pam_message> &expected,
                    int num_prompts, const pam_message **actual)
cdf7fd74
 {
2fade7af
     if (expected.size() != num_prompts) {
         return false;
cdf7fd74
     }
1bbd05bc
 
2fade7af
     for (int i=0; i< num_prompts; ++i) {
         if (expected[i].msg_style != actual[i]->msg_style) {
             return false;
12570d65
         }
b017a4d2
 
2fade7af
         if (std::strcmp (expected[i].msg, actual[i]->msg)) {
             return false;
f9c4622b
         }
cdf7fd74
     }
f9c4622b
 
2fade7af
     return true;
 }
f9c4622b
 
2fade7af
 template<class T>
 T *address_of (T &t)
cdf7fd74
 {
2fade7af
     return &t;
 }
 
 int fake_conv (int num_msg, const struct pam_message **msg,
                struct pam_response **resp, void *appdata_ptr)
 {
     conversation_data *data = reinterpret_cast<conversation_data *>
                               (appdata_ptr);
b017a4d2
 
2fade7af
     if (data->return_value != PAM_SUCCESS) {
         return data->return_value;
cdf7fd74
     }
c9d7e817
 
2fade7af
     if (!same_prompts (data->expected_prompts, num_msg, msg)) {
         throw std::string ("unexpected prompts");
     }
 
     std::vector<pam_response> &responses = data->responses;
     std::transform (responses.begin(), responses.end(), resp,
                     address_of<pam_response>);
     return data->return_value;
 }
c9d7e817
 
2fade7af
 class fake_pam : public pam_ifc
cdf7fd74
 {
 private:
2fade7af
     pam_handle *expected_handle_;
     conversation_data conversation_data_;
6f31174c
     int get_response_;
bb80149c
     mutable pam_conv conv_;
cdf7fd74
 public:
2ff5dc7c
     fake_pam (pam_handle *expected_handle,
               const conversation_data &conversation_data)
         : expected_handle_ (expected_handle),
6f31174c
           conversation_data_ (conversation_data),
           get_response_ (PAM_SUCCESS)
2fade7af
     {}
6f31174c
     fake_pam (int get_response) : get_response_ (get_response) {}
bb80149c
     int get_conv (pam_handle *handle, const pam_conv **out) const
cdf7fd74
     {
6f31174c
         if (get_response_ != PAM_SUCCESS) {
             return get_response_;
         }
 
2fade7af
         if (expected_handle_ != handle) {
2ff5dc7c
             throw std::string ("unexpected handle");
01c00cfe
         }
2ff5dc7c
 
af675d2b
         conv_.appdata_ptr = (void *) (&conversation_data_);
2fade7af
         conv_.conv = fake_conv;
         *out = &conv_;
         return PAM_SUCCESS;
cdf7fd74
     }
0b6e39a6
 
2fade7af
 };
0b6e39a6
 
2fade7af
 template<class T>
2ff5dc7c
 std::shared_ptr<T> share (T *t)
 {
     return std::shared_ptr<T> (t);
0b6e39a6
 }
 
6f31174c
 conversation make_conversation (pam_handle *expected_handle,
                                 const std::string &answer)
 {
2fade7af
     pam_message prompt;
     prompt.msg_style = PAM_PROMPT_ECHO_OFF;
2ff5dc7c
     prompt.msg = const_cast<char *> ("Dual control token: ");
2fade7af
     pam_response response;
     response.resp_retcode = 0;
e7cf340a
     std::string response_text (answer);
2ff5dc7c
     response.resp = const_cast<char *> (response_text.c_str());
2fade7af
     conversation_data conversation_data = {
2ff5dc7c
         std::vector<pam_message> (&prompt, &prompt + 1),
         std::vector<pam_response> (&response, &response + 1),
2fade7af
         PAM_SUCCESS
     };
e7cf340a
     pam pam (share (new fake_pam (expected_handle, conversation_data)));
0252cd89
     return conversation::create (pam);
2fade7af
 
be7b0e04
 }
 
6f31174c
 bool check_conversation_response (const std::string &answer,
                                   const std::string &expected_user, const std::string &expected_token)
 {
3e8b9587
     // given
     pam_handle *handle = reinterpret_cast<pam_handle *> (29039);
6f31174c
     conversation conversation (make_conversation (handle, answer));
3e8b9587
     pam_request request (handle, 0, 0, 0);
 
     // when
     conversation_result actual = conversation.initiate (request);
 
     // then
a9ede8a3
     check (actual.user_name == expected_user, "user name does not match");
     check (actual.token == expected_token, "token does not match");
3e8b9587
 
     succeed();
 }
 
a9ede8a3
 bool returns_user_and_token()
 {
     std::string user ("user");
     std::string token ("token");
6f31174c
     return check_conversation_response (user + ":" + token, user, token);
a9ede8a3
 }
9eeff687
 
6f31174c
 int returns_empty_user_and_token_when_no_colon()
 {
     return check_conversation_response ("nocolon", "", "");
 }
9eeff687
 
6f31174c
 int returns_empty_user_and_token_when_empty_answer()
 {
     return check_conversation_response ("", "", "");
 }
c8a98157
 
6f31174c
 int returns_empty_token_when_colon_end()
 {
a9ede8a3
     std::string user ("user");
     std::string token ("");
6f31174c
     return check_conversation_response (user + ":" + token, user, token);
c8a98157
 }
 
6f31174c
 int returns_empty_user_when_colon_start()
 {
a9ede8a3
     std::string user ("");
     std::string token ("token");
6f31174c
     return check_conversation_response (user + ":" + token, user, token);
 }
 
 int returns_empty_user_and_token_when_pam_cant_create_conversation()
 {
     // given
     pam pam (share (new fake_pam (PAM_SERVICE_ERR)));
0252cd89
     conversation conversation = conversation::create (pam);
6f31174c
     pam_request request (0, 0, 0, 0);
 
     // when
     conversation_result actual = conversation.initiate (request);
 
     // then
     check (actual.user_name == "", "user name does not match");
     check (actual.token == "", "token does not match");
 
     succeed();
e7cf340a
 }
9eeff687
 
fa74284e
 int returns_empty_user_and_token_when_conversation_fails()
 {
9708ef79
     pam_message prompt;
     prompt.msg_style = PAM_PROMPT_ECHO_OFF;
     prompt.msg = const_cast<char *> ("Dual control token: ");
     conversation_data conversation_data = {
fa74284e
         std::vector<pam_message> (&prompt, &prompt + 1),
9708ef79
         std::vector<pam_response> (),
         PAM_SERVICE_ERR
     };
     pam pam (share (new fake_pam (0, conversation_data)));
0252cd89
     conversation conversation = conversation::create (pam);
fa74284e
     pam_request request (0, 0, 0, 0);
9708ef79
 
     //when
     conversation_result actual = conversation.initiate (request);
 
     //then
     check (actual.user_name == "", "user name does not match");
     check (actual.token == "", "token does not match");
     succeed();
 }
 
fa74284e
 int returns_empty_user_and_token_when_conversation_answer_fails()
 {
aea12a9e
     pam_message prompt;
     prompt.msg_style = PAM_PROMPT_ECHO_OFF;
     prompt.msg = const_cast<char *> ("Dual control token: ");
     pam_response response;
     response.resp_retcode = 1212;
     std::string response_text ("user:token");
     response.resp = const_cast<char *> (response_text.c_str());
     conversation_data conversation_data = {
         std::vector<pam_message> (&prompt, &prompt + 1),
         std::vector<pam_response> (&response, &response + 1),
         PAM_SUCCESS
     };
     pam pam (share (new fake_pam (0, conversation_data)));
0252cd89
     conversation conversation = conversation::create (pam);
fa74284e
     pam_request request (0, 0, 0, 0);
aea12a9e
 
     //when
     conversation_result actual = conversation.initiate (request);
 
     //then
     check (actual.user_name == "", "user name does not match");
     check (actual.token == "", "token does not match");
     succeed();
 }
 
2fade7af
 RESET_VARS_START
 RESET_VARS_END
0b6e39a6
 
2fade7af
 int run_tests()
cdf7fd74
 {
3e8b9587
     test (returns_user_and_token);
9eeff687
     test (returns_empty_user_and_token_when_no_colon);
     test (returns_empty_user_and_token_when_empty_answer);
c8a98157
     test (returns_empty_token_when_colon_end);
     test (returns_empty_user_when_colon_start);
6f31174c
     test (returns_empty_user_and_token_when_pam_cant_create_conversation);
9708ef79
     test (returns_empty_user_and_token_when_conversation_fails);
aea12a9e
     test (returns_empty_user_and_token_when_conversation_answer_fails);
26830ef5
     succeed();
 }
 
2fade7af
 int main (int argc, char **argv)
cdf7fd74
 {
2fade7af
     return !run_tests();
231e2d6e
 }