git.fiddlerwoaroof.com
become_test.cc
0d8b9a17
 /* 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.
  */
 
21e4960f
 #include "become.h"
 #include "sys_unistd.h"
 #include "sys_pwd.h"
 #include "user.h"
 #include "test_util.h"
 
 template<class T>
 std::shared_ptr<T> share (T *t)
 {
     return std::shared_ptr<T> (t);
 }
 
 class mock_unistd : public unistd_ifc
 {
 private:
     uid_t expected_euid_;
 public:
     mutable uid_t actual_uid;
     mock_unistd (uid_t expected_euid)
         : expected_euid_ (expected_euid)
     {}
 
     int seteuid (uid_t euid) const override
     {
         actual_uid = euid;
         return euid == expected_euid_ ? 0 : -1;
     }
 };
 
 class fake_user : public user_ifc
 {
 private:
     uid_t uid_;
 public:
     fake_user (uid_t uid) : uid_ (uid)
     {}
     uid_t uid() const override
     {
         return uid_;
     }
 };
 
 int become_calls_seteuid_with_right_arguments ()
 {
     uid_t expected_uid = 1000;
0d8b9a17
     std::shared_ptr<mock_unistd> mock_unistd  = share (new class mock_unistd (
                 expected_uid));
21e4960f
     unistd fake_unistd (mock_unistd);
0d8b9a17
     user fake_user (share (new class fake_user (expected_uid)));
21e4960f
 
     become become_ (fake_user, fake_unistd);
 
     check (mock_unistd->actual_uid == expected_uid, "become does not seteuid");
 
     succeed();
 }
 
 bool run_tests()
 {
     test (become_calls_seteuid_with_right_arguments);
     succeed();
 }
 
 int main (int argc, char *argv[])
 {
     return !run_tests();
 }
0d8b9a17