Browse code
create user from system
Greg Wiley authored on 11/04/2017 23:08:29
Showing 3 changed files
Showing 3 changed files
... | ... |
@@ -3,16 +3,42 @@ |
3 | 3 |
#include <vector> |
4 | 4 |
#include <string> |
5 | 5 |
#include <pwd.h> |
6 |
- |
|
6 |
+#include <unistd.h> |
|
7 |
+#include "test_support.h" |
|
7 | 8 |
|
8 | 9 |
user::user(struct passwd *sys_info) : info(sys_info) { |
9 | 10 |
} |
10 | 11 |
|
11 |
-bool user::valid() { |
|
12 |
- return info; |
|
13 |
-} |
|
14 |
- |
|
15 | 12 |
std::string user::home_directory() { |
16 | 13 |
return info->pw_dir; |
17 | 14 |
} |
18 | 15 |
|
16 |
+// concrete user implementation |
|
17 |
+ |
|
18 |
+class concrete_user : public user { |
|
19 |
+ private: |
|
20 |
+ std::vector<char> buffer_; |
|
21 |
+ std::shared_ptr<struct passwd> store_; |
|
22 |
+ public: |
|
23 |
+ concrete_user(const std::vector<char> &buffer, const std::shared_ptr<struct passwd> &store); |
|
24 |
+}; |
|
25 |
+ |
|
26 |
+concrete_user::concrete_user(const std::vector<char> &buffer, const std::shared_ptr<struct passwd> &store) : |
|
27 |
+ buffer_(buffer), |
|
28 |
+ store_(store), |
|
29 |
+ user(store.get()) { |
|
30 |
+} |
|
31 |
+ |
|
32 |
+const std::shared_ptr<user> create_user(const std::string &user_name) { |
|
33 |
+ std::vector<char> buffer(sysconf(_SC_GETPW_R_SIZE_MAX)); |
|
34 |
+ std::shared_ptr<struct passwd> sys_passwd; |
|
35 |
+ struct passwd *found_passwd(0); |
|
36 |
+ |
|
37 |
+ getpwnam_r(user_name.c_str(), sys_passwd.get(), buffer.data(), buffer.size(), &found_passwd); |
|
38 |
+ |
|
39 |
+ return std::shared_ptr<user>(new concrete_user(buffer, sys_passwd)); |
|
40 |
+} |
|
41 |
+ |
|
42 |
+ |
|
43 |
+ |
|
44 |
+ |
... | ... |
@@ -2,6 +2,7 @@ |
2 | 2 |
#define _USER_H |
3 | 3 |
#include <vector> |
4 | 4 |
#include <string> |
5 |
+#include <memory> |
|
5 | 6 |
#include <pwd.h> |
6 | 7 |
|
7 | 8 |
class user { |
... | ... |
@@ -10,18 +11,11 @@ class user { |
10 | 11 |
|
11 | 12 |
public: |
12 | 13 |
user(struct passwd *sys_info); |
13 |
- bool valid(); |
|
14 | 14 |
std::string home_directory(); |
15 | 15 |
}; |
16 | 16 |
|
17 |
-class concrete_user : public user { |
|
18 |
- private: |
|
19 |
- std::vector<char> buffer; |
|
20 |
- std::shared_ptr<struct passwd> store; |
|
21 |
- public: |
|
22 |
- concrete_user(const std::string &user_name); |
|
23 |
-}; |
|
24 | 17 |
|
18 |
+const std::shared_ptr<user> create_user(const std::string &user_name); |
|
25 | 19 |
|
26 | 20 |
#endif |
27 | 21 |
|
... | ... |
@@ -1,6 +1,7 @@ |
1 |
+#include <memory> |
|
2 |
+ |
|
1 | 3 |
#include "user.h" |
2 | 4 |
#include "test_util.h" |
3 |
- |
|
4 | 5 |
int gets_home_directory() { |
5 | 6 |
//given |
6 | 7 |
const char *expected_home_directory = "home/msmith"; |
... | ... |
@@ -16,6 +17,30 @@ int gets_home_directory() { |
16 | 17 |
return expected_home_directory == actual_home_directory; |
17 | 18 |
} |
18 | 19 |
|
20 |
+std::string fake_home_directory(""); |
|
21 |
+int fake_getpwnam_r(const char *nam, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result) { |
|
22 |
+ pwd->pw_dir = const_cast<char *>(fake_home_directory.c_str()); |
|
23 |
+ result = &pwd; |
|
24 |
+ return 0; |
|
25 |
+} |
|
26 |
+ |
|
27 |
+int initialize_concrete_user() { |
|
28 |
+ // given |
|
29 |
+ std::string username("msmith"); |
|
30 |
+ std::string home_directory("this is my home"); |
|
31 |
+ fake_home_directory = home_directory; |
|
32 |
+ |
|
33 |
+ // when |
|
34 |
+ std::shared_ptr<user> user(create_user(username)); |
|
35 |
+ |
|
36 |
+ // then |
|
37 |
+ check(user, "user should be returned"); |
|
38 |
+ check(user->home_directory() == home_directory, "home directory does not match"); |
|
39 |
+ |
|
40 |
+ succeed(); |
|
41 |
+ |
|
42 |
+} |
|
43 |
+ |
|
19 | 44 |
RESET_VARS_START |
20 | 45 |
RESET_VARS_END |
21 | 46 |
|