git.fiddlerwoaroof.com
Browse code

format

Greg Wiley authored on 22/04/2017 00:04:58
Showing 7 changed files
... ...
@@ -1,20 +1,35 @@
1
+/* Copyright (C) CJ Affiliate
2
+ *
3
+ * You may use, distribute and modify this code under  the
4
+ * terms of the  GNU General Public License  version 2  or
5
+ * later.
6
+ *
7
+ * You should have received a copy of the license with this
8
+ * file. If not, you will find a copy in the "LICENSE" file
9
+ * at https://github.com/cjdev/dual-control.
10
+ */
11
+
1 12
 #include "sys_pwd.h"
2 13
 
3 14
 #include <pwd.h>
4 15
 
5
-namespace {
6
-    class impl : public pwd_ifc {
7
-        public:
8
-        int getpwnam_r(const char *user_name, passwd *out, char *buffer,
9
-                size_t buffer_sz, passwd **result) {
10
-            return ::getpwnam_r(user_name, out, buffer, buffer_sz, result);
11
-        }
16
+namespace
17
+{
18
+class impl : public pwd_ifc
19
+{
20
+public:
21
+    int getpwnam_r (const char *user_name, passwd *out, char *buffer,
22
+                    size_t buffer_sz, passwd **result)
23
+    {
24
+        return ::getpwnam_r (user_name, out, buffer, buffer_sz, result);
25
+    }
12 26
 
13
-    };
14
-    static pwd system_pwd(pwd::delegate(new impl));
27
+};
28
+static pwd system_pwd (pwd::delegate (new impl));
15 29
 }
16 30
 
17
-pwd pwd::system() {
31
+pwd pwd::system()
32
+{
18 33
     return system_pwd;
19 34
 }
20 35
 
... ...
@@ -1,33 +1,49 @@
1
+/* Copyright (C) CJ Affiliate
2
+ *
3
+ * You may use, distribute and modify this code under  the
4
+ * terms of the  GNU General Public License  version 2  or
5
+ * later.
6
+ *
7
+ * You should have received a copy of the license with this
8
+ * file. If not, you will find a copy in the "LICENSE" file
9
+ * at https://github.com/cjdev/dual-control.
10
+ */
11
+
1 12
 #ifndef _SYS_PWD_H
2 13
 #define _SYS_PWD_H
3 14
 
4 15
 #include <memory>
5 16
 #include <pwd.h>
6 17
 
7
-class pwd_ifc {
8
-    public:
9
-        virtual int getpwnam_r(const char *user_name, passwd *out, char *buffer,
10
-                size_t buffer_sz, passwd **result) {
11
-            *result = 0;
12
-            return 0;
13
-        };
18
+class pwd_ifc
19
+{
20
+public:
21
+    virtual int getpwnam_r (const char *user_name, passwd *out, char *buffer,
22
+                            size_t buffer_sz, passwd **result)
23
+    {
24
+        *result = 0;
25
+        return 0;
26
+    };
14 27
 };
15 28
 
16
-class pwd : public pwd_ifc {
17
-    public:
18
-        typedef std::shared_ptr<pwd_ifc> delegate;
19
-
20
-    private:
21
-        delegate delegate_;
22
-
23
-    public:
24
-        pwd(const delegate delegate) : delegate_(delegate) {}
25
-        pwd() : delegate_(delegate(new pwd_ifc)) {}
26
-        int getpwnam_r(const char *user_name, passwd *out, char *buffer,
27
-                size_t buffer_sz, passwd **result) {
28
-            return delegate_-> getpwnam_r(user_name, out, buffer, buffer_sz, result);
29
-        }
30
-        static pwd system();
29
+class pwd : public pwd_ifc
30
+{
31
+public:
32
+    typedef std::shared_ptr<pwd_ifc> delegate;
33
+
34
+private:
35
+    delegate delegate_;
36
+
37
+public:
38
+    pwd (const delegate delegate) : delegate_ (delegate) {}
39
+    pwd() : delegate_ (delegate (new pwd_ifc)) {}
40
+    int getpwnam_r (const char *user_name, passwd *out, char *buffer,
41
+                    size_t buffer_sz, passwd **result)
42
+    {
43
+        return delegate_-> getpwnam_r (user_name, out, buffer, buffer_sz, result);
44
+    }
45
+    static pwd system();
31 46
 };
32 47
 
33 48
 #endif
49
+
... ...
@@ -1,16 +1,32 @@
1
+/* Copyright (C) CJ Affiliate
2
+ *
3
+ * You may use, distribute and modify this code under  the
4
+ * terms of the  GNU General Public License  version 2  or
5
+ * later.
6
+ *
7
+ * You should have received a copy of the license with this
8
+ * file. If not, you will find a copy in the "LICENSE" file
9
+ * at https://github.com/cjdev/dual-control.
10
+ */
11
+
1 12
 #include "sys_unistd.h"
2 13
 #include <unistd.h>
3 14
 
4
-namespace {
5
-    class impl : public unistd_ifc {
6
-        public:
7
-            long int sysconf(int name) {
8
-                return ::sysconf(name);
9
-            }
10
-    };
11
-    static unistd sys_unistd(unistd::delegate(new impl));
15
+namespace
16
+{
17
+class impl : public unistd_ifc
18
+{
19
+public:
20
+    long int sysconf (int name)
21
+    {
22
+        return ::sysconf (name);
23
+    }
24
+};
25
+static unistd sys_unistd (unistd::delegate (new impl));
12 26
 }
13 27
 
14
-unistd unistd::system() {
28
+unistd unistd::system()
29
+{
15 30
     return sys_unistd;
16 31
 }
32
+
... ...
@@ -1,3 +1,14 @@
1
+/* Copyright (C) CJ Affiliate
2
+ *
3
+ * You may use, distribute and modify this code under  the
4
+ * terms of the  GNU General Public License  version 2  or
5
+ * later.
6
+ *
7
+ * You should have received a copy of the license with this
8
+ * file. If not, you will find a copy in the "LICENSE" file
9
+ * at https://github.com/cjdev/dual-control.
10
+ */
11
+
1 12
 #ifndef _SYS_UNISTD_H_
2 13
 #define _SYS_UNISTD_H_
3 14
 
... ...
@@ -5,27 +16,32 @@
5 16
 
6 17
 #include <unistd.h>
7 18
 
8
-class unistd_ifc {
9
-    public:
10
-        virtual long int sysconf(int name) { return -1; }
19
+class unistd_ifc
20
+{
21
+public:
22
+    virtual long int sysconf (int name)
23
+    {
24
+        return -1;
25
+    }
11 26
 };
12 27
 
13
-class unistd : public unistd_ifc {
14
-    public:
15
-        typedef std::shared_ptr<unistd_ifc> delegate;
16
-
17
-    private:
18
-        delegate delegate_;
19
-
20
-    public:
21
-        unistd(delegate delegate): delegate_(delegate) {}
22
-        unistd() : delegate_(delegate(new unistd_ifc)) {}
23
-        long int sysconf(int name) {
24
-            return delegate_->sysconf(name);
25
-        }
26
-        static unistd system();
28
+class unistd : public unistd_ifc
29
+{
30
+public:
31
+    typedef std::shared_ptr<unistd_ifc> delegate;
32
+
33
+private:
34
+    delegate delegate_;
35
+
36
+public:
37
+    unistd (delegate delegate): delegate_ (delegate) {}
38
+    unistd() : delegate_ (delegate (new unistd_ifc)) {}
39
+    long int sysconf (int name)
40
+    {
41
+        return delegate_->sysconf (name);
42
+    }
43
+    static unistd system();
27 44
 };
28 45
 
29
-
30
-
31 46
 #endif
47
+
... ...
@@ -12,35 +12,40 @@
12 12
 #include <memory>
13 13
 #include <vector>
14 14
 
15
-
16 15
 #include "user.h"
17 16
 #include "sys_unistd.h"
18 17
 #include "sys_pwd.h"
19 18
 
20
-namespace {
21
-    class directory_impl : public directory_ifc {
22
-        private:
23
-            unistd unistd_;
24
-            pwd pwd_;
25
-        public:
26
-          directory_impl(unistd &unistd, pwd &pwd) : unistd_(unistd), pwd_(pwd) {}
27
-          std::vector<user> find_user (const std::string &user_name) {
28
-              std::vector<char> buffer (unistd_.sysconf (_SC_GETPW_R_SIZE_MAX));
29
-              passwd sys_passwd;
30
-              passwd *found_passwd(0);
31
-              int result = pwd_.getpwnam_r(user_name.c_str(), &sys_passwd,
32
-                      buffer.data(), buffer.size(), &found_passwd);
33
-              std::vector<user> return_value;
34
-              if (!result && found_passwd) {
35
-                  return_value.push_back(user());
36
-              }
37
-              return return_value;
38
-          }
39
-    };
19
+namespace
20
+{
21
+class directory_impl : public directory_ifc
22
+{
23
+private:
24
+    unistd unistd_;
25
+    pwd pwd_;
26
+public:
27
+    directory_impl (unistd &unistd, pwd &pwd) : unistd_ (unistd), pwd_ (pwd) {}
28
+    std::vector<user> find_user (const std::string &user_name)
29
+    {
30
+        std::vector<char> buffer (unistd_.sysconf (_SC_GETPW_R_SIZE_MAX));
31
+        passwd sys_passwd;
32
+        passwd *found_passwd (0);
33
+        int result = pwd_.getpwnam_r (user_name.c_str(), &sys_passwd,
34
+                                      buffer.data(), buffer.size(), &found_passwd);
35
+        std::vector<user> return_value;
36
+
37
+        if (!result && found_passwd) {
38
+            return_value.push_back (user());
39
+        }
40
+
41
+        return return_value;
42
+    }
43
+};
40 44
 }
41 45
 
42
-directory directory::create(unistd &unistd, pwd &pwd) {
43
-    return directory(delegate(new directory_impl(unistd, pwd)));
46
+directory directory::create (unistd &unistd, pwd &pwd)
47
+{
48
+    return directory (delegate (new directory_impl (unistd, pwd)));
44 49
 }
45 50
 
46 51
 /*
... ...
@@ -45,20 +45,20 @@ public:
45 45
 
46 46
 class directory : public directory_ifc
47 47
 {
48
-    public:
49
-        typedef std::shared_ptr<directory_ifc> delegate;
50
-    private:
51
-        delegate delegate_;
52
-    public:
48
+public:
49
+    typedef std::shared_ptr<directory_ifc> delegate;
50
+private:
51
+    delegate delegate_;
52
+public:
53 53
     directory (delegate delegate) : delegate_
54 54
         (delegate) {}
55
-    directory() : directory(delegate(new directory_ifc)) {}
55
+    directory() : directory (delegate (new directory_ifc)) {}
56 56
     std::vector<user> find_user (const std::string &user_name)
57 57
     {
58 58
         return delegate_->find_user (user_name);
59 59
     }
60 60
 
61
-    static directory create(unistd &unistd, pwd &pwd);
61
+    static directory create (unistd &unistd, pwd &pwd);
62 62
 };
63 63
 
64 64
 #endif
... ...
@@ -14,137 +14,158 @@
14 14
 #include "sys_pwd.h"
15 15
 #include "sys_unistd.h"
16 16
 
17
-class fake_pwd : public pwd_ifc {
18
-    private:
19
-        std::string expected_user_name_;
20
-    public:
21
-        fake_pwd(const std::string expected_user_name) : expected_user_name_(expected_user_name) {}
22
-        int getpwnam_r(const char *user_name, passwd *out, char *buffer,
23
-                       size_t buffer_sz, passwd **result) {
24
-            if (expected_user_name_ == user_name)  {
25
-                *result = out;
26
-            } else {
17
+class fake_pwd : public pwd_ifc
18
+{
19
+private:
20
+    std::string expected_user_name_;
21
+public:
22
+    fake_pwd (const std::string expected_user_name) : expected_user_name_
23
+        (expected_user_name) {}
24
+    int getpwnam_r (const char *user_name, passwd *out, char *buffer,
25
+                    size_t buffer_sz, passwd **result)
26
+    {
27
+        if (expected_user_name_ == user_name)  {
28
+            *result = out;
29
+        } else {
27 30
             *result = 0;
28
-            }
29
-            return 0;
30 31
         }
32
+
33
+        return 0;
34
+    }
31 35
 };
32 36
 
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 {
37
+class match_buffer_pwd : public pwd_ifc
38
+{
39
+private:
40
+    long int expected_buffer_sz_;
41
+public:
42
+    match_buffer_pwd (long int buffer_sz) : expected_buffer_sz_ (buffer_sz) {}
43
+    int getpwnam_r (const char *user_name, passwd *out, char *buffer,
44
+                    size_t buffer_sz, passwd **result)
45
+    {
46
+
47
+        if (expected_buffer_sz_ == buffer_sz && buffer != 0) {
48
+            *result = out;
49
+        } else {
44 50
             *result = 0;
45
-            }
46
-            return 0;
47 51
         }
52
+
53
+        return 0;
54
+    }
48 55
 };
49 56
 
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
+class stub_pwnam_err_pwd : public pwd_ifc
58
+{
59
+public:
60
+    int getpwnam_r (const char *user_name, passwd *out, char *buffer,
61
+                    size_t buffer_sz, passwd **result)
62
+    {
63
+        *result = out;
64
+        return 3;
65
+    }
57 66
 
58 67
 };
59 68
 
60
-class fake_unistd : public unistd_ifc {
61
-    private:
62
-        int expected_name_;
63
-        long int return_value_;
64
-    public:
65
-        fake_unistd(int expected_name, long int return_value = 0)
66
-            : expected_name_(expected_name),
67
-              return_value_(return_value) {}
68
-        long int sysconf(int name) {
69
-            if (name == expected_name_) {
69
+class fake_unistd : public unistd_ifc
70
+{
71
+private:
72
+    int expected_name_;
73
+    long int return_value_;
74
+public:
75
+    fake_unistd (int expected_name, long int return_value = 0)
76
+        : expected_name_ (expected_name),
77
+          return_value_ (return_value) {}
78
+    long int sysconf (int name)
79
+    {
80
+        if (name == expected_name_) {
70 81
             return return_value_;
71
-            }
72
-            return -1;
73 82
         }
83
+
84
+        return -1;
85
+    }
74 86
 };
75 87
 
76
-int find_user_happy() {
88
+int find_user_happy()
89
+{
77 90
     //given
78
-    std::string user_name("user");
79
-    pwd test_pwd(pwd::delegate(new fake_pwd(user_name)));
80
-    unistd test_unistd(unistd::delegate(new fake_unistd(_SC_GETPW_R_SIZE_MAX)));
81
-    directory directory(directory::create(test_unistd, test_pwd));
91
+    std::string user_name ("user");
92
+    pwd test_pwd (pwd::delegate (new fake_pwd (user_name)));
93
+    unistd test_unistd (unistd::delegate (new fake_unistd (
94
+            _SC_GETPW_R_SIZE_MAX)));
95
+    directory directory (directory::create (test_unistd, test_pwd));
82 96
 
83 97
     //when
84
-    std::vector<user> results = directory.find_user(user_name);
98
+    std::vector<user> results = directory.find_user (user_name);
85 99
 
86 100
     //then
87
-    check(!results.empty(), "user should have been found");
101
+    check (!results.empty(), "user should have been found");
88 102
     succeed();
89 103
 }
90 104
 
91
-int user_not_found() {
105
+int user_not_found()
106
+{
92 107
     //given
93
-    pwd test_pwd(pwd::delegate(new fake_pwd("user")));
94
-    unistd test_unistd(unistd::delegate(new fake_unistd(_SC_GETPW_R_SIZE_MAX)));
95
-    directory directory(directory::create(test_unistd, test_pwd));
108
+    pwd test_pwd (pwd::delegate (new fake_pwd ("user")));
109
+    unistd test_unistd (unistd::delegate (new fake_unistd (
110
+            _SC_GETPW_R_SIZE_MAX)));
111
+    directory directory (directory::create (test_unistd, test_pwd));
96 112
 
97 113
     //when
98
-    std::vector<user> results = directory.find_user("not_user");
114
+    std::vector<user> results = directory.find_user ("not_user");
99 115
 
100 116
     //then
101
-    check(results.empty(), "user should not have been found");
117
+    check (results.empty(), "user should not have been found");
102 118
     succeed();
103 119
 
104 120
 }
105 121
 
106
-int find_user_passes_buffer_and_size()  {
122
+int find_user_passes_buffer_and_size()
123
+{
107 124
     //given
108 125
     long int buffer_sz = 5976;
109
-    unistd test_unistd(unistd::delegate(new fake_unistd(_SC_GETPW_R_SIZE_MAX, buffer_sz)));
110
-    pwd match_pwd(pwd::delegate(new match_buffer_pwd(buffer_sz)));
111
-    directory directory(directory::create(test_unistd, match_pwd));
126
+    unistd test_unistd (unistd::delegate (new fake_unistd (_SC_GETPW_R_SIZE_MAX,
127
+                                          buffer_sz)));
128
+    pwd match_pwd (pwd::delegate (new match_buffer_pwd (buffer_sz)));
129
+    directory directory (directory::create (test_unistd, match_pwd));
112 130
 
113 131
     //when
114
-    std::vector<user> results = directory.find_user("does_not_matter");
115
-
132
+    std::vector<user> results = directory.find_user ("does_not_matter");
116 133
 
117 134
     // then
118
-    check(!results.empty(), "match failed");
135
+    check (!results.empty(), "match failed");
119 136
     succeed();
120 137
 }
121 138
 
122
-int find_user_fails_on_pwnam_r_error_and_result_ok()  {
139
+int find_user_fails_on_pwnam_r_error_and_result_ok()
140
+{
123 141
     //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));
142
+    unistd test_unistd (unistd::delegate (new fake_unistd (
143
+            _SC_GETPW_R_SIZE_MAX)));
144
+    pwd stub_pwd (pwd::delegate (new stub_pwnam_err_pwd));
145
+    directory directory (directory::create (test_unistd, stub_pwd));
127 146
 
128 147
     //when
129
-    std::vector<user> results = directory.find_user("does_not_matter");
148
+    std::vector<user> results = directory.find_user ("does_not_matter");
130 149
 
131 150
     // then
132
-    check(results.empty(), "did not check return");
151
+    check (results.empty(), "did not check return");
133 152
     succeed();
134 153
 }
135 154
 
136 155
 RESET_VARS_START
137 156
 RESET_VARS_END
138 157
 
139
-int run_tests() {
140
-    test(find_user_happy);
141
-    test(user_not_found);
142
-    test(find_user_passes_buffer_and_size);
143
-    test(find_user_fails_on_pwnam_r_error_and_result_ok);
158
+int run_tests()
159
+{
160
+    test (find_user_happy);
161
+    test (user_not_found);
162
+    test (find_user_passes_buffer_and_size);
163
+    test (find_user_fails_on_pwnam_r_error_and_result_ok);
144 164
     succeed();
145 165
 }
146 166
 
147
-int main(int argc, char **argv) {
167
+int main (int argc, char **argv)
168
+{
148 169
     return !run_tests();
149 170
 }
150 171