git.fiddlerwoaroof.com
session_test.cc
af675d2b
 /* 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.
  */
 
bb80149c
 #include <string>
 #include <memory>
 
 #include "session.h"
 #include "test_util.h"
 #include "sys_pam.h"
 #include "request.h"
 
af675d2b
 class fake_sys_pam : public pam_ifc
 {
 private:
     std::string user_name_;
 public:
     fake_sys_pam (std::string user_name) : user_name_ (user_name) {}
     int get_user (pam_handle *handle, const char **out) const
     {
bb80149c
         *out = user_name_.c_str();
         return PAM_SUCCESS;
     }
 };
 
af675d2b
 int gets_user_from_pam()
 {
bb80149c
     //given
af675d2b
     std::string user_name ("user");
     pam fake_sys_pam (std::make_shared<fake_sys_pam> (user_name));
     sessions sessions (sessions::create (fake_sys_pam));
     pam_request request (0, 0, 0, 0);
bb80149c
 
     //when
af675d2b
     std::string actual = sessions.user_name (request);
bb80149c
 
     //
     //then
af675d2b
     check (actual == user_name, "returned wrong user name");
bb80149c
     succeed();
 }
 
af675d2b
 int run_tests()
 {
     test (gets_user_from_pam);
bb80149c
     succeed();
 }
 
af675d2b
 int main (int argc, char *argv[])
 {
bb80149c
     return !run_tests();
 }
af675d2b