git.fiddlerwoaroof.com
Browse code

check for weird pwnam result

Greg Wiley authored on 22/04/2017 00:04:23
Showing 2 changed files
... ...
@@ -31,7 +31,7 @@ namespace {
31 31
               int result = pwd_.getpwnam_r(user_name.c_str(), &sys_passwd,
32 32
                       buffer.data(), buffer.size(), &found_passwd);
33 33
               std::vector<user> return_value;
34
-              if (found_passwd) {
34
+              if (!result && found_passwd) {
35 35
                   return_value.push_back(user());
36 36
               }
37 37
               return return_value;
... ...
@@ -47,6 +47,16 @@ class match_buffer_pwd : public pwd_ifc {
47 47
         }
48 48
 };
49 49
 
50
+class stub_pwnam_err_pwd : public pwd_ifc {
51
+    public:
52
+        int getpwnam_r(const char *user_name, passwd *out, char *buffer,
53
+                       size_t buffer_sz, passwd **result) {
54
+            *result = out;
55
+            return 3;
56
+        }
57
+
58
+};
59
+
50 60
 class fake_unistd : public unistd_ifc {
51 61
     private:
52 62
         int expected_name_;
... ...
@@ -109,6 +119,20 @@ int find_user_passes_buffer_and_size()  {
109 119
     succeed();
110 120
 }
111 121
 
122
+int find_user_fails_on_pwnam_r_error_and_result_ok()  {
123
+    //given
124
+    unistd test_unistd(unistd::delegate(new fake_unistd(_SC_GETPW_R_SIZE_MAX)));
125
+    pwd stub_pwd(pwd::delegate(new stub_pwnam_err_pwd));
126
+    directory directory(directory::create(test_unistd, stub_pwd));
127
+
128
+    //when
129
+    std::vector<user> results = directory.find_user("does_not_matter");
130
+
131
+    // then
132
+    check(results.empty(), "did not check return");
133
+    succeed();
134
+}
135
+
112 136
 RESET_VARS_START
113 137
 RESET_VARS_END
114 138
 
... ...
@@ -116,6 +140,7 @@ int run_tests() {
116 140
     test(find_user_happy);
117 141
     test(user_not_found);
118 142
     test(find_user_passes_buffer_and_size);
143
+    test(find_user_fails_on_pwnam_r_error_and_result_ok);
119 144
     succeed();
120 145
 }
121 146