Browse code
buffer size from sysonf is used
Greg Wiley authored on 21/04/2017 23:52:30
Showing 1 changed files
Showing 1 changed files
... | ... |
@@ -16,15 +16,32 @@ |
16 | 16 |
|
17 | 17 |
class fake_pwd : public pwd_ifc { |
18 | 18 |
private: |
19 |
- passwd passwd_; |
|
20 | 19 |
std::string expected_user_name_; |
21 | 20 |
public: |
22 | 21 |
fake_pwd(const std::string expected_user_name) : expected_user_name_(expected_user_name) {} |
23 | 22 |
int getpwnam_r(const char *user_name, passwd *out, char *buffer, |
24 | 23 |
size_t buffer_sz, passwd **result) { |
25 | 24 |
if (expected_user_name_ == user_name) { |
26 |
- *out = passwd_; |
|
27 | 25 |
*result = out; |
26 |
+ } else { |
|
27 |
+ *result = 0; |
|
28 |
+ } |
|
29 |
+ return 0; |
|
30 |
+ } |
|
31 |
+}; |
|
32 |
+ |
|
33 |
+class match_buffer_pwd : public pwd_ifc { |
|
34 |
+ private: |
|
35 |
+ long int expected_buffer_sz_; |
|
36 |
+ public: |
|
37 |
+ match_buffer_pwd(long int buffer_sz) : expected_buffer_sz_(buffer_sz) {} |
|
38 |
+ int getpwnam_r(const char *user_name, passwd *out, char *buffer, |
|
39 |
+ size_t buffer_sz, passwd **result) { |
|
40 |
+ |
|
41 |
+ if (expected_buffer_sz_ == buffer_sz && buffer != 0) { |
|
42 |
+ *result = out; |
|
43 |
+ } else { |
|
44 |
+ *result = 0; |
|
28 | 45 |
} |
29 | 46 |
return 0; |
30 | 47 |
} |
... | ... |
@@ -33,11 +50,14 @@ class fake_pwd : public pwd_ifc { |
33 | 50 |
class fake_unistd : public unistd_ifc { |
34 | 51 |
private: |
35 | 52 |
int expected_name_; |
53 |
+ long int return_value_; |
|
36 | 54 |
public: |
37 |
- fake_unistd(int expected_name) : expected_name_(expected_name) {} |
|
55 |
+ fake_unistd(int expected_name, long int return_value = 0) |
|
56 |
+ : expected_name_(expected_name), |
|
57 |
+ return_value_(return_value) {} |
|
38 | 58 |
long int sysconf(int name) { |
39 | 59 |
if (name == expected_name_) { |
40 |
- return 0; |
|
60 |
+ return return_value_; |
|
41 | 61 |
} |
42 | 62 |
return -1; |
43 | 63 |
} |
... | ... |
@@ -73,80 +93,33 @@ int user_not_found() { |
73 | 93 |
|
74 | 94 |
} |
75 | 95 |
|
76 |
-RESET_VARS_START |
|
77 |
-RESET_VARS_END |
|
78 |
- |
|
79 |
-int run_tests() { |
|
80 |
- test(find_user_happy); |
|
81 |
- succeed(); |
|
82 |
-} |
|
83 |
- |
|
84 |
-int main(int argc, char **argv) { |
|
85 |
- return !run_tests(); |
|
86 |
-} |
|
87 |
- |
|
88 |
-/* |
|
89 |
-std::shared_ptr<struct passwd> fake_passwd; |
|
90 |
-int fake_getpwnam_r (const char *nam, struct passwd *pwd, char *buffer, |
|
91 |
- size_t bufsize, struct passwd **result) |
|
92 |
-{ |
|
93 |
- if (fake_passwd) { |
|
94 |
- *pwd = *fake_passwd; |
|
95 |
- *result = pwd; |
|
96 |
- return 0; |
|
97 |
- } |
|
98 |
- |
|
99 |
- return -1; |
|
100 |
-} |
|
101 |
- |
|
102 |
-bool create_user_succeeds() |
|
103 |
-{ |
|
104 |
- // given |
|
105 |
- std::string username ("msmith"); |
|
106 |
- std::string home_directory ("this is my home"); |
|
107 |
- fake_passwd.reset (new struct passwd); |
|
108 |
- fake_passwd->pw_dir = const_cast<char *> (home_directory.c_str()); |
|
109 |
- |
|
110 |
- // when |
|
111 |
- std::shared_ptr<user> user (create_user (username)); |
|
112 |
- |
|
113 |
- // then |
|
114 |
- check (user, "user should be returned"); |
|
115 |
- check (user->home_directory() == home_directory, |
|
116 |
- "home directory does not match"); |
|
117 |
- |
|
118 |
- succeed(); |
|
119 |
- |
|
120 |
-} |
|
96 |
+int find_user_passes_buffer_and_size() { |
|
97 |
+ //given |
|
98 |
+ long int buffer_sz = 5976; |
|
99 |
+ unistd test_unistd(unistd::delegate(new fake_unistd(_SC_GETPW_R_SIZE_MAX, buffer_sz))); |
|
100 |
+ pwd match_pwd(pwd::delegate(new match_buffer_pwd(buffer_sz))); |
|
101 |
+ directory directory(directory::create(test_unistd, match_pwd)); |
|
121 | 102 |
|
122 |
-bool create_user_nonexistent() |
|
123 |
-{ |
|
124 |
- // given |
|
125 |
- std::string username ("msmith"); |
|
103 |
+ //when |
|
104 |
+ std::vector<user> results = directory.find_user("does_not_matter"); |
|
126 | 105 |
|
127 |
- // when |
|
128 |
- std::shared_ptr<user> user (create_user (username)); |
|
129 | 106 |
|
130 | 107 |
// then |
131 |
- check (!user, "no user should be returned"); |
|
132 |
- |
|
108 |
+ check(!results.empty(), "match failed"); |
|
133 | 109 |
succeed(); |
134 | 110 |
} |
135 | 111 |
|
136 | 112 |
RESET_VARS_START |
137 |
-fake_passwd.reset ((struct passwd *)0); |
|
138 | 113 |
RESET_VARS_END |
139 | 114 |
|
140 |
-int run_tests() |
|
141 |
-{ |
|
142 |
- test (gets_home_directory); |
|
143 |
- test (create_user_succeeds); |
|
144 |
- test (create_user_nonexistent); |
|
115 |
+int run_tests() { |
|
116 |
+ test(find_user_happy); |
|
117 |
+ test(user_not_found); |
|
118 |
+ test(find_user_passes_buffer_and_size); |
|
145 | 119 |
succeed(); |
146 | 120 |
} |
147 |
-int main (int argc, char *argv[]) |
|
148 |
-{ |
|
121 |
+ |
|
122 |
+int main(int argc, char **argv) { |
|
149 | 123 |
return !run_tests(); |
150 | 124 |
} |
151 |
-*/ |
|
152 | 125 |
|