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 |
{
}
|
af3a4cd4 |
std::string home_directory() const override
|
0e218820 |
{
return home_directory_;
}
|
bef74c28 |
};
|
d10906ee |
class mock_write_fstreams: public fstreams_ifc
{
private:
mutable std::string captured_filename_;
mutable std::shared_ptr<std::ostringstream> capture_stream_;
public:
postream open_ofstream (const std::string &file_path,
|
fefe31f5 |
std::ios_base::openmode mode) const override
|
d10906ee |
{
captured_filename_ = file_path;
capture_stream_ = std::make_shared<std::ostringstream>();
return capture_stream_;
}
std::string captured_filename()
{
return captured_filename_;
}
std::string captured_written()
{
return capture_stream_->str();
}
|
af3a4cd4 |
};
|
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) {}
|
af3a4cd4 |
pstream open_fstream (const std::string &file_path) const override
|
bc6c3d35 |
{
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)));
|
e064ffa9 |
tokens supplier (tokens::create (
|
6603197e |
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)));
|
e064ffa9 |
tokens supplier (tokens::create (
|
6603197e |
test_streams));
|
215ea751 |
|
8ddb4fe9 |
//when
std::string actual = supplier.token (test_user);
//then
|
43794015 |
check (actual == "", "should have returned empty string");
|
8ddb4fe9 |
succeed();
}
|
d10906ee |
int writes_the_token ()
{
|
af3a4cd4 |
// given
|
d10906ee |
std::string home_directory ("/somedir");
user test_user (user::delegate (new fake_user (home_directory)));
mock_write_fstreams *mockfs (new mock_write_fstreams);
fstreams test_streams{fstreams::delegate (mockfs)};
std::string token ("token");
|
6603197e |
tokens tokens (tokens::create (test_streams));
|
af3a4cd4 |
//when
|
decf9568 |
tokens.save (test_user, token);
|
af3a4cd4 |
|
d10906ee |
// then
|
af3a4cd4 |
std::ostringstream temp;
temp << token << std::endl;
std::string expected_written = temp.str();
|
d10906ee |
std::string expected_filename (home_directory + "/.dual_control");
check (mockfs->captured_filename() == expected_filename,
"filename does not match");
check (mockfs->captured_written() == expected_written,
"token does not match");
succeed();
|
af3a4cd4 |
}
|
0e218820 |
int run_tests()
{
test (reads_from_the_right_file);
|
ca42f792 |
test (returns_empty_string_if_file_open_fail);
|
af3a4cd4 |
test (writes_the_token);
|
de34378f |
succeed();
}
|
0e218820 |
int main (int argc, char *argv[])
{
|
bef74c28 |
return !run_tests();
|
de34378f |
}
|