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.
*/
|
bef74c28 |
#include <memory>
|
8ab94c71 |
#include <cstring>
|
69be5e4e |
#include <pwd.h>
|
8ab94c71 |
#include <cstdio>
|
69be5e4e |
#include <sys/stat.h>
|
4bf199d2 |
#include <fstream>
|
215ea751 |
#include <sstream>
|
69be5e4e |
|
de34378f |
#include "token.h"
|
1c7f8bf0 |
#include "test_util.h"
|
bef74c28 |
#include "user.h"
|
215ea751 |
#include "sys_fstream.h"
|
8ddb4fe9 |
|
0e218820 |
class fake_user : public user_ifc
{
private:
std::string home_directory_;
public:
|
8ddb4fe9 |
fake_user() {}
|
bb783d31 |
fake_user (const std::string &home_directory) :
home_directory_ (home_directory)
|
0e218820 |
{
}
std::string home_directory()
{
return home_directory_;
}
|
bef74c28 |
};
|
bc6c3d35 |
class fake_fstreams : public fstreams_ifc
{
private:
std::string expected_file_path_;
std::string file_contents_;
public:
fake_fstreams (const std::string &expected_file_path,
const std::string &file_contents)
: expected_file_path_ (expected_file_path),
file_contents_ (file_contents) {}
pstream open_fstream (const std::string &file_path)
{
if (file_path == expected_file_path_) {
return fstreams::pstream (new std::istringstream (file_contents_));
} else {
return fstreams_ifc::open_fstream (file_path);
|
215ea751 |
}
|
bc6c3d35 |
}
|
215ea751 |
};
|
0e218820 |
int reads_from_the_right_file ()
{
|
cb403154 |
//given
|
bb783d31 |
std::string home_directory = "/somedir";
|
215ea751 |
// hardcoded file name is .dual_control in the user's home directory
std::string token_file = home_directory + "/.dual_control";
|
bc6c3d35 |
std::string token ("123456");
fstreams test_streams (fstreams::delegate (new fake_fstreams (token_file,
token)));
|
215ea751 |
//file_reader test_file_reader (file_reader::delegate (new fake_file_reader));
|
bb783d31 |
user test_user (user::delegate (new fake_user (home_directory)));
|
0e218820 |
user_token_supplier supplier (user_token_supplier::create (
|
215ea751 |
test_streams));
|
cb403154 |
//when
|
0e218820 |
std::string actual = supplier.token (test_user);
|
69be5e4e |
//then
|
215ea751 |
check (actual == token, "token does not match");
|
69be5e4e |
succeed();
}
|
43794015 |
int returns_empty_string_if_file_open_fail()
{
|
8ddb4fe9 |
//given
|
215ea751 |
std::string home_directory = "/somedir";
// hardcoded file name is .dual_control in the user's home directory
std::string token_file = home_directory + "/.not_dual_control";
|
bc6c3d35 |
fstreams test_streams (fstreams::delegate (new fake_fstreams (token_file,
"654321")));
|
215ea751 |
user test_user (user::delegate (new fake_user (home_directory)));
|
8ddb4fe9 |
user_token_supplier supplier (user_token_supplier::create (
|
215ea751 |
test_streams));
|
8ddb4fe9 |
//when
std::string actual = supplier.token (test_user);
//then
|
43794015 |
check (actual == "", "should have returned empty string");
|
8ddb4fe9 |
succeed();
}
|
bef74c28 |
RESET_VARS_START
RESET_VARS_END
|
6f0de833 |
|
0e218820 |
int run_tests()
{
test (reads_from_the_right_file);
|
ca42f792 |
test (returns_empty_string_if_file_open_fail);
|
de34378f |
succeed();
}
|
0e218820 |
int main (int argc, char *argv[])
{
|
bef74c28 |
return !run_tests();
|
de34378f |
}
|