Browse code
installer create happy path
Greg Wiley authored on 03/05/2017 22:07:01
Showing 11 changed files
Showing 11 changed files
- installer.cc
- installer.h
- installer_test.cc
- sys_pwd.cc
- sys_pwd.h
- sys_unistd.cc
- sys_unistd.h
- user.cc
- user.h
- user_test.cc
- validator_test.cc
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,38 @@ |
1 |
+#include <string> |
|
2 |
+#include <vector> |
|
3 |
+ |
|
4 |
+#include "installer.h" |
|
5 |
+#include "token.h" |
|
6 |
+#include "sys_unistd.h" |
|
7 |
+#include "user.h" |
|
8 |
+ |
|
9 |
+namespace { |
|
10 |
+ |
|
11 |
+class impl : public installer_ifc { |
|
12 |
+ private: |
|
13 |
+ tokens tokens_; |
|
14 |
+ unistd unistd_; |
|
15 |
+ directory directory_; |
|
16 |
+ generator generator_; |
|
17 |
+ public: |
|
18 |
+ impl(const tokens &tokens, const unistd &unistd, |
|
19 |
+ const directory &directory, const installer_ifc::generator &generator) : |
|
20 |
+ tokens_(tokens), unistd_(unistd), directory_(directory), generator_(generator) {} |
|
21 |
+ std::string install_token() const override { |
|
22 |
+ std::string user_name = unistd_.getlogin(); |
|
23 |
+ auto found_user = directory_.find_user(user_name); |
|
24 |
+ user user(found_user[0]); |
|
25 |
+ std::string token(generator_()); |
|
26 |
+ tokens_.save(user, token); |
|
27 |
+ return "123456"; |
|
28 |
+ } |
|
29 |
+}; |
|
30 |
+ |
|
31 |
+} |
|
32 |
+ |
|
33 |
+ |
|
34 |
+installer installer::create(const tokens &tokens, const unistd &unistd, |
|
35 |
+ const directory &directory, const installer_ifc::generator &generator) { |
|
36 |
+ return installer(std::make_shared<impl>(tokens, unistd, directory, generator)); |
|
37 |
+} |
|
38 |
+ |
... | ... |
@@ -2,12 +2,37 @@ |
2 | 2 |
#define INSTALLER_H_ |
3 | 3 |
|
4 | 4 |
#include <string> |
5 |
+#include <memory> |
|
6 |
+#include <functional> |
|
5 | 7 |
|
6 |
-class installer { |
|
8 |
+#include "token.h" |
|
9 |
+#include "sys_unistd.h" |
|
10 |
+#include "user.h" |
|
11 |
+ |
|
12 |
+ |
|
13 |
+class installer_ifc { |
|
7 | 14 |
public: |
8 |
- std::string install_token() { |
|
15 |
+ using generator = std::function<std::string()>; |
|
16 |
+ virtual std::string install_token() const { |
|
9 | 17 |
return "123456"; |
10 | 18 |
} |
11 | 19 |
}; |
12 | 20 |
|
21 |
+class installer { |
|
22 |
+ public: |
|
23 |
+ using delegate = std::shared_ptr<installer_ifc>; |
|
24 |
+ private: |
|
25 |
+ delegate delegate_; |
|
26 |
+ public: |
|
27 |
+ installer(const delegate &delegate = std::make_shared<installer_ifc>()) : delegate_(delegate) {} |
|
28 |
+ std::string install_token() const { |
|
29 |
+ return delegate_->install_token(); |
|
30 |
+ } |
|
31 |
+ static installer create(const tokens &tokens, const unistd &unistd, |
|
32 |
+ const directory &directory, const installer_ifc::generator &generator); |
|
33 |
+}; |
|
34 |
+ |
|
35 |
+ |
|
36 |
+ |
|
37 |
+ |
|
13 | 38 |
#endif |
... | ... |
@@ -1,19 +1,76 @@ |
1 |
+#include <functional> |
|
2 |
+#include <memory> |
|
3 |
+#include <string> |
|
4 |
+#include <vector> |
|
5 |
+ |
|
1 | 6 |
#include "installer.h" |
7 |
+#include "user.h" |
|
8 |
+#include "token.h" |
|
9 |
+#include "test_util.h" |
|
10 |
+ |
|
11 |
+class mock_tokens : public tokens_ifc { |
|
12 |
+ public: |
|
13 |
+ mutable std::string captured_token; |
|
14 |
+ void save (const user &user, const std::string &token) const override { |
|
15 |
+ captured_token = token; |
|
16 |
+ } |
|
17 |
+}; |
|
18 |
+ |
|
19 |
+class fake_unistd : public unistd_ifc { |
|
20 |
+ private: |
|
21 |
+ std::string user_name_; |
|
22 |
+ public: |
|
23 |
+ fake_unistd(const std::string &user_name) : user_name_(user_name) {} |
|
24 |
+ const char *getlogin() const override { |
|
25 |
+ return user_name_.c_str(); |
|
26 |
+ } |
|
27 |
+}; |
|
2 | 28 |
|
3 |
-int first_test() { |
|
29 |
+class fake_directory : public directory_ifc { |
|
30 |
+ private: |
|
31 |
+ std::string expected_user_name_; |
|
32 |
+ public: |
|
33 |
+ fake_directory(const std::string &expected_user_name) : expected_user_name_(expected_user_name) { |
|
34 |
+ } |
|
35 |
+ std::vector<user> find_user (const std::string &user_name) const override { |
|
36 |
+ std::vector<user> rval; |
|
37 |
+ if (user_name == expected_user_name_) { |
|
38 |
+ rval.push_back(user()); |
|
39 |
+ } |
|
40 |
+ return rval; |
|
41 |
+ } |
|
42 |
+ |
|
43 |
+}; |
|
44 |
+ |
|
45 |
+int installs_token() { |
|
4 | 46 |
//given |
5 |
-/* generator generator |
|
6 |
- tokens tokens |
|
7 |
- installer installer //= create (generator, tokens); |
|
47 |
+ user user; |
|
48 |
+ std::string user_name("user"); |
|
49 |
+ std::string token("token"); |
|
50 |
+ auto test_tokens = std::make_shared<mock_tokens>(); |
|
51 |
+ tokens tokens{test_tokens}; |
|
52 |
+ unistd unistd(std::make_shared<fake_unistd>(user_name)); |
|
53 |
+ directory directory(std::make_shared<fake_directory>(user_name)); |
|
54 |
+ installer_ifc::generator generator = [&] { return token; }; |
|
55 |
+ |
|
56 |
+ installer installer = installer::create (tokens, unistd, directory, generator); |
|
8 | 57 |
|
9 | 58 |
//when |
10 | 59 |
installer.install_token(); |
11 | 60 |
|
12 | 61 |
//then |
13 |
- tokens.captured user == expected user |
|
14 |
- token.captured token == generated token |
|
15 |
-*/ |
|
62 |
+ check(test_tokens->captured_token == token, "installed wrong token"); |
|
63 |
+ succeed(); |
|
16 | 64 |
} |
17 | 65 |
|
18 |
-int main(int argc, char *argv[]) {return 0;} |
|
66 |
+ |
|
67 |
+ |
|
68 |
+int run_tests() { |
|
69 |
+ test(installs_token); |
|
70 |
+ succeed(); |
|
71 |
+} |
|
72 |
+ |
|
73 |
+int main(int argc, char *argv[]) { |
|
74 |
+ return !run_tests(); |
|
75 |
+} |
|
19 | 76 |
|
... | ... |
@@ -19,7 +19,7 @@ class impl : public pwd_ifc |
19 | 19 |
{ |
20 | 20 |
public: |
21 | 21 |
int getpwnam_r (const char *user_name, passwd *out, char *buffer, |
22 |
- size_t buffer_sz, passwd **result) |
|
22 |
+ size_t buffer_sz, passwd **result) const override |
|
23 | 23 |
{ |
24 | 24 |
return ::getpwnam_r (user_name, out, buffer, buffer_sz, result); |
25 | 25 |
} |
... | ... |
@@ -20,7 +20,7 @@ class pwd_ifc |
20 | 20 |
public: |
21 | 21 |
virtual ~pwd_ifc() {} |
22 | 22 |
virtual int getpwnam_r (const char *user_name, passwd *out, char *buffer, |
23 |
- size_t buffer_sz, passwd **result) |
|
23 |
+ size_t buffer_sz, passwd **result) const |
|
24 | 24 |
{ |
25 | 25 |
*result = 0; |
26 | 26 |
return 0; |
... | ... |
@@ -39,7 +39,7 @@ public: |
39 | 39 |
pwd (const delegate delegate) : delegate_ (delegate) {} |
40 | 40 |
pwd() : delegate_ (delegate (new pwd_ifc)) {} |
41 | 41 |
int getpwnam_r (const char *user_name, passwd *out, char *buffer, |
42 |
- size_t buffer_sz, passwd **result) |
|
42 |
+ size_t buffer_sz, passwd **result) const |
|
43 | 43 |
{ |
44 | 44 |
return delegate_-> getpwnam_r (user_name, out, buffer, buffer_sz, result); |
45 | 45 |
} |
... | ... |
@@ -20,10 +20,13 @@ class unistd_ifc |
20 | 20 |
{ |
21 | 21 |
public: |
22 | 22 |
virtual ~unistd_ifc() {} |
23 |
- virtual long int sysconf (int name) |
|
23 |
+ virtual long int sysconf (int name) const |
|
24 | 24 |
{ |
25 | 25 |
return -1; |
26 | 26 |
} |
27 |
+ virtual const char *getlogin() const { |
|
28 |
+ return ""; |
|
29 |
+ } |
|
27 | 30 |
}; |
28 | 31 |
|
29 | 32 |
class unistd |
... | ... |
@@ -37,10 +40,13 @@ private: |
37 | 40 |
public: |
38 | 41 |
unistd (delegate delegate): delegate_ (delegate) {} |
39 | 42 |
unistd() : delegate_ (delegate (new unistd_ifc)) {} |
40 |
- long int sysconf (int name) |
|
43 |
+ long int sysconf (int name) const |
|
41 | 44 |
{ |
42 | 45 |
return delegate_->sysconf (name); |
43 | 46 |
} |
47 |
+ const char *getlogin() const { |
|
48 |
+ return delegate_->getlogin(); |
|
49 |
+ } |
|
44 | 50 |
static unistd create(); |
45 | 51 |
}; |
46 | 52 |
|
... | ... |
@@ -40,7 +40,7 @@ private: |
40 | 40 |
pwd pwd_; |
41 | 41 |
public: |
42 | 42 |
directory_impl (unistd &unistd, pwd &pwd) : unistd_ (unistd), pwd_ (pwd) {} |
43 |
- std::vector<user> find_user (const std::string &user_name) override |
|
43 |
+ std::vector<user> find_user (const std::string &user_name) const override |
|
44 | 44 |
{ |
45 | 45 |
std::vector<char> buffer (unistd_.sysconf (_SC_GETPW_R_SIZE_MAX)); |
46 | 46 |
passwd sys_passwd; |
... | ... |
@@ -50,7 +50,7 @@ class directory_ifc |
50 | 50 |
{ |
51 | 51 |
public: |
52 | 52 |
virtual ~directory_ifc() {} |
53 |
- virtual std::vector<user> find_user (const std::string &user_name) |
|
53 |
+ virtual std::vector<user> find_user (const std::string &user_name) const |
|
54 | 54 |
{ |
55 | 55 |
return std::vector<user>(); |
56 | 56 |
} |
... | ... |
@@ -66,7 +66,7 @@ public: |
66 | 66 |
directory (delegate delegate) : delegate_ |
67 | 67 |
(delegate) {} |
68 | 68 |
directory() : directory (delegate (new directory_ifc)) {} |
69 |
- std::vector<user> find_user (const std::string &user_name) |
|
69 |
+ std::vector<user> find_user (const std::string &user_name) const |
|
70 | 70 |
{ |
71 | 71 |
return delegate_->find_user (user_name); |
72 | 72 |
} |
... | ... |
@@ -23,7 +23,7 @@ public: |
23 | 23 |
fake_pwd (const std::string expected_user_name) : expected_user_name_ |
24 | 24 |
(expected_user_name), home_directory_ ("/somehome") {} |
25 | 25 |
int getpwnam_r (const char *user_name, passwd *out, char *buffer, |
26 |
- size_t buffer_sz, passwd **result) |
|
26 |
+ size_t buffer_sz, passwd **result) const override |
|
27 | 27 |
{ |
28 | 28 |
if (expected_user_name_ == user_name) { |
29 | 29 |
out->pw_dir = const_cast<char *> (home_directory_.c_str()); |
... | ... |
@@ -45,7 +45,7 @@ private: |
45 | 45 |
public: |
46 | 46 |
match_buffer_pwd (long int buffer_sz) : expected_buffer_sz_ (buffer_sz) {} |
47 | 47 |
int getpwnam_r (const char *user_name, passwd *out, char *buffer, |
48 |
- size_t buffer_sz, passwd **result) |
|
48 |
+ size_t buffer_sz, passwd **result) const override |
|
49 | 49 |
{ |
50 | 50 |
|
51 | 51 |
if (expected_buffer_sz_ == buffer_sz && buffer != 0) { |
... | ... |
@@ -64,7 +64,7 @@ class stub_pwnam_err_pwd : public pwd_ifc |
64 | 64 |
{ |
65 | 65 |
public: |
66 | 66 |
int getpwnam_r (const char *user_name, passwd *out, char *buffer, |
67 |
- size_t buffer_sz, passwd **result) |
|
67 |
+ size_t buffer_sz, passwd **result) const override |
|
68 | 68 |
{ |
69 | 69 |
*result = out; |
70 | 70 |
return 3; |
... | ... |
@@ -81,7 +81,7 @@ public: |
81 | 81 |
fake_unistd (int expected_name, long int return_value = 0) |
82 | 82 |
: expected_name_ (expected_name), |
83 | 83 |
return_value_ (return_value) {} |
84 |
- long int sysconf (int name) |
|
84 |
+ long int sysconf (int name) const override |
|
85 | 85 |
{ |
86 | 86 |
if (name == expected_name_) { |
87 | 87 |
return return_value_; |
... | ... |
@@ -27,7 +27,7 @@ public: |
27 | 27 |
} |
28 | 28 |
fake_directory() : user_name_ ("_NOT_A_USER") {} |
29 | 29 |
|
30 |
- std::vector<user> find_user (const std::string &user_name) override |
|
30 |
+ std::vector<user> find_user (const std::string &user_name) const override |
|
31 | 31 |
{ |
32 | 32 |
std::vector<user> result; |
33 | 33 |
|