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.
*/
|
55259c47 |
#include "user.h"
#include "test_util.h"
|
d8adc881 |
#include "sys_pwd.h"
#include "sys_unistd.h"
|
189f062b |
class fake_pwd : public pwd_ifc
{
private:
std::string expected_user_name_;
|
5a49538d |
std::string home_directory_;
|
189f062b |
public:
fake_pwd (const std::string expected_user_name) : expected_user_name_
|
4ef9dc34 |
(expected_user_name), home_directory_ ("/somehome") {}
|
189f062b |
int getpwnam_r (const char *user_name, passwd *out, char *buffer,
|
bf756dea |
size_t buffer_sz, passwd **result) const override
|
189f062b |
{
if (expected_user_name_ == user_name) {
|
4ef9dc34 |
out->pw_dir = const_cast<char *> (home_directory_.c_str());
out->pw_name = const_cast<char *> (expected_user_name_.c_str());
|
189f062b |
*result = out;
} else {
|
36518286 |
*result = 0;
}
|
189f062b |
return 0;
}
|
36518286 |
};
|
189f062b |
class match_buffer_pwd : public pwd_ifc
{
private:
long int expected_buffer_sz_;
|
801a5342 |
std::string charbuf_;
|
189f062b |
public:
match_buffer_pwd (long int buffer_sz) : expected_buffer_sz_ (buffer_sz) {}
int getpwnam_r (const char *user_name, passwd *out, char *buffer,
|
bf756dea |
size_t buffer_sz, passwd **result) const override
|
189f062b |
{
if (expected_buffer_sz_ == buffer_sz && buffer != 0) {
|
4ef9dc34 |
out->pw_name = const_cast<char *> (charbuf_.c_str());
out->pw_dir = const_cast<char *> (charbuf_.c_str());
|
189f062b |
*result = out;
} else {
|
36518286 |
*result = 0;
|
d8adc881 |
}
|
189f062b |
return 0;
}
|
d8adc881 |
};
|
189f062b |
class stub_pwnam_err_pwd : public pwd_ifc
{
public:
int getpwnam_r (const char *user_name, passwd *out, char *buffer,
|
bf756dea |
size_t buffer_sz, passwd **result) const override
|
189f062b |
{
*result = out;
return 3;
}
|
0a8bb2f7 |
};
|
189f062b |
class fake_unistd : public unistd_ifc
{
private:
|
b8155a93 |
int expected_sysconf_name_;
const char *expected_username_;
|
189f062b |
long int return_value_;
public:
|
8c62e61c |
fake_unistd (int expected_sysconf_name, long int return_value = 0,
const char *expected_username = "<unspecified>")
|
b8155a93 |
: expected_sysconf_name_ (expected_sysconf_name),
expected_username_ (expected_username),
|
189f062b |
return_value_ (return_value) {}
|
bf756dea |
long int sysconf (int name) const override
|
189f062b |
{
|
b8155a93 |
if (name == expected_sysconf_name_) {
|
36518286 |
return return_value_;
|
d8adc881 |
}
|
189f062b |
return -1;
}
|
8c62e61c |
const char *getlogin() const override
{
|
b8155a93 |
return expected_username_;
}
|
d8adc881 |
};
|
717259b5 |
|
189f062b |
int find_user_happy()
{
|
d8adc881 |
//given
|
189f062b |
std::string user_name ("user");
pwd test_pwd (pwd::delegate (new fake_pwd (user_name)));
unistd test_unistd (unistd::delegate (new fake_unistd (
_SC_GETPW_R_SIZE_MAX)));
directory directory (directory::create (test_unistd, test_pwd));
|
d8adc881 |
//when
|
189f062b |
std::vector<user> results = directory.find_user (user_name);
|
d8adc881 |
//then
|
189f062b |
check (!results.empty(), "user should have been found");
|
d8adc881 |
succeed();
|
dad2109c |
}
|
189f062b |
int user_not_found()
{
|
dad2109c |
//given
|
189f062b |
pwd test_pwd (pwd::delegate (new fake_pwd ("user")));
unistd test_unistd (unistd::delegate (new fake_unistd (
_SC_GETPW_R_SIZE_MAX)));
directory directory (directory::create (test_unistd, test_pwd));
|
d8adc881 |
|
dad2109c |
//when
|
189f062b |
std::vector<user> results = directory.find_user ("not_user");
|
dad2109c |
//then
|
189f062b |
check (results.empty(), "user should not have been found");
|
dad2109c |
succeed();
|
d8adc881 |
|
a8b53afd |
}
|
189f062b |
int find_user_passes_buffer_and_size()
{
|
36518286 |
//given
long int buffer_sz = 5976;
|
189f062b |
unistd test_unistd (unistd::delegate (new fake_unistd (_SC_GETPW_R_SIZE_MAX,
buffer_sz)));
pwd match_pwd (pwd::delegate (new match_buffer_pwd (buffer_sz)));
directory directory (directory::create (test_unistd, match_pwd));
|
9a0bfcb2 |
|
36518286 |
//when
|
189f062b |
std::vector<user> results = directory.find_user ("does_not_matter");
|
717259b5 |
// then
|
189f062b |
check (!results.empty(), "match failed");
|
717259b5 |
succeed();
}
|
189f062b |
int find_user_fails_on_pwnam_r_error_and_result_ok()
{
|
0a8bb2f7 |
//given
|
189f062b |
unistd test_unistd (unistd::delegate (new fake_unistd (
_SC_GETPW_R_SIZE_MAX)));
pwd stub_pwd (pwd::delegate (new stub_pwnam_err_pwd));
directory directory (directory::create (test_unistd, stub_pwd));
|
0a8bb2f7 |
//when
|
189f062b |
std::vector<user> results = directory.find_user ("does_not_matter");
|
0a8bb2f7 |
// then
|
189f062b |
check (results.empty(), "did not check return");
|
0a8bb2f7 |
succeed();
}
|
b8155a93 |
int get_current_user_happy_path()
{
//given
std::string user_name ("user");
|
8c62e61c |
pwd test_pwd (pwd::delegate (new fake_pwd (user_name)));
unistd test_unistd (unistd::delegate (new fake_unistd (_SC_GETPW_R_SIZE_MAX,
0, user_name.c_str())));
|
b8155a93 |
directory directory (directory::create (test_unistd, test_pwd));
//when
std::vector<user> results = directory.get_current_user();
//then
check (!results.empty(), "user should have been found");
|
8c62e61c |
check (! (results[0].home_directory() == user_name),
"user should have been found");
|
b8155a93 |
succeed();
}
int get_current_user_nullptr_path()
{
//given
|
8c62e61c |
pwd test_pwd (pwd::delegate (new fake_pwd ("<unspecified>")));
unistd test_unistd (unistd::delegate (new fake_unistd (_SC_GETPW_R_SIZE_MAX,
0, nullptr)));
|
b8155a93 |
directory directory (directory::create (test_unistd, test_pwd));
//when
std::vector<user> results = directory.get_current_user();
//then
check (results.empty(), "user should have been found");
succeed();
}
|
189f062b |
int run_tests()
{
test (find_user_happy);
|
801a5342 |
test (user_not_found);
test (find_user_passes_buffer_and_size);
test (find_user_fails_on_pwnam_r_error_and_result_ok);
|
b8155a93 |
test (get_current_user_happy_path);
test (get_current_user_nullptr_path);
|
55259c47 |
succeed();
}
|
36518286 |
|
189f062b |
int main (int argc, char **argv)
{
|
55259c47 |
return !run_tests();
}
|
8c62e61c |
|