git.fiddlerwoaroof.com
Browse code

generates and writes tokens

Greg Wiley authored on 03/05/2017 18:54:28
Showing 17 changed files
1 1
Binary files a/conversation_test.out and b/conversation_test.out differ
2 2
Binary files a/dual_control_test.out and b/dual_control_test.out differ
3 3
Binary files a/logger_test.out and b/logger_test.out differ
4 4
Binary files a/request_test.out and b/request_test.out differ
5 5
Binary files a/session_test.out and b/session_test.out differ
... ...
@@ -11,6 +11,7 @@
11 11
 
12 12
 #include <memory>
13 13
 #include <fstream>
14
+#include <iostream>
14 15
 
15 16
 #include "sys_fstream.h"
16 17
 
... ...
@@ -19,10 +20,14 @@ namespace
19 20
 class impl : public fstreams_ifc
20 21
 {
21 22
 public:
22
-    pstream open_fstream (const std::string &file_path)
23
+    pstream open_fstream (const std::string &file_path) const override
23 24
     {
24 25
         return pstream (new std::ifstream (file_path));
25 26
     }
27
+    postream open_ofstream (const std::string &file_path, std::ios_base::open_mode mode) const override {
28
+        return postream(new std::ofstream(file_path, mode));
29
+    }
30
+
26 31
 };
27 32
 
28 33
 }
... ...
@@ -14,16 +14,21 @@
14 14
 
15 15
 #include <memory>
16 16
 #include <sstream>
17
+#include <iostream>
17 18
 
18 19
 class fstreams_ifc
19 20
 {
20 21
 public:
21 22
     typedef std::shared_ptr<std::istream> pstream;
23
+    typedef std::shared_ptr<std::ostream> postream;
22 24
     virtual ~fstreams_ifc() {}
23
-    virtual pstream open_fstream (const std::string &file_path)
25
+    virtual pstream open_fstream (const std::string &file_path) const
24 26
     {
25 27
         return pstream (new std::istringstream (""));
26 28
     }
29
+    virtual postream open_ofstream(const std::string &file_path, std::ios_base::openmode mode) const {
30
+        return postream(new std::ostringstream(""));
31
+    }
27 32
 };
28 33
 
29 34
 class fstreams
... ...
@@ -34,12 +39,17 @@ private:
34 39
     delegate delegate_;
35 40
 public:
36 41
     typedef fstreams_ifc::pstream pstream;
42
+    typedef fstreams_ifc::postream postream;
37 43
     fstreams (const delegate &delegate) : delegate_ (delegate) {}
38 44
     fstreams() : fstreams (delegate (new fstreams_ifc)) {}
39
-    pstream open_fstream (const std::string &file_path)
45
+    pstream open_fstream (const std::string &file_path) const
40 46
     {
41 47
         return delegate_->open_fstream (file_path);
42 48
     }
49
+    postream open_ofstream (const std::string &file_path, std::ios_base::openmode mode) const
50
+    {
51
+        return delegate_->open_ofstream (file_path, mode);
52
+    }
43 53
     static fstreams create();
44 54
 };
45 55
 
... ...
@@ -13,6 +13,8 @@
13 13
 #include <vector>
14 14
 #include <memory>
15 15
 #include <fstream>
16
+#include <iostream>
17
+#include <functional>
16 18
 
17 19
 #include "token.h"
18 20
 #include "user.h"
... ...
@@ -24,10 +26,12 @@ class tokens_impl : public tokens_ifc
24 26
 {
25 27
 private:
26 28
     fstreams fstreams_;
29
+    token_generator generate_token_;
27 30
 public:
28
-    tokens_impl (fstreams &fstreams) :
31
+    tokens_impl (const fstreams &fstreams, const token_generator &generate_token) :
32
+        generate_token_(generate_token),
29 33
         fstreams_ (fstreams) {}
30
-    std::string token (user &user)
34
+    std::string token (const user &user) const override
31 35
     {
32 36
         const std::string file_path (user.home_directory() + "/.dual_control");
33 37
         std::vector<char> line (7);
... ...
@@ -41,11 +45,17 @@ public:
41 45
         stream->getline (line.data(), line.size());
42 46
         return std::string (line.data());
43 47
     }
48
+    void create(const user &user) const override {
49
+        std::string generated_token(generate_token_());
50
+        std::string file_path(user.home_directory() + "/.dual_control");
51
+        fstreams::postream stream(fstreams_.open_ofstream(file_path, std::ios_base::trunc));
52
+        *stream << generated_token << std::endl;
53
+    }
44 54
 };
45 55
 }
46
-tokens tokens::create (fstreams &fstreams)
56
+tokens tokens::create (const fstreams &fstreams, const tokens_impl::token_generator &generate_token)
47 57
 {
48 58
     return tokens (tokens::delegate
49
-                                (new tokens_impl (fstreams)));
59
+                                (new tokens_impl (fstreams, generate_token)));
50 60
 }
51 61
 
... ...
@@ -14,6 +14,7 @@
14 14
 
15 15
 #include <string>
16 16
 #include <memory>
17
+#include <functional>
17 18
 
18 19
 #include "user.h"
19 20
 #include "sys_fstream.h"
... ...
@@ -21,11 +22,13 @@
21 22
 class tokens_ifc
22 23
 {
23 24
 public:
25
+    using token_generator = std::function<std::string()>;
24 26
     virtual ~tokens_ifc() {}
25
-    virtual std::string token (user &user)
27
+    virtual std::string token (const user &user) const
26 28
     {
27 29
         return "";
28 30
     }
31
+    virtual void create(const user &user) const {}
29 32
 };
30 33
 
31 34
 class tokens
... ...
@@ -39,11 +42,14 @@ public:
39 42
         delegate_ (delegate) {}
40 43
     tokens() : tokens (
41 44
             delegate (new tokens_ifc)) {}
42
-    std::string token (user &user)
45
+    std::string token (const user &user) const
43 46
     {
44 47
         return delegate_->token (user);
45 48
     }
46
-    static tokens create (fstreams &fstreams);
49
+    void create(const user &user) const {
50
+        return delegate_->create(user);
51
+    }
52
+    static tokens create (const fstreams &fstreams, const tokens_ifc::token_generator &generate_token);
47 53
 };
48 54
 
49 55
 #endif
... ...
@@ -32,12 +32,30 @@ public:
32 32
         home_directory_ (home_directory)
33 33
     {
34 34
     }
35
-    std::string home_directory()
35
+    std::string home_directory() const override
36 36
     {
37 37
         return home_directory_;
38 38
     }
39 39
 };
40 40
 
41
+class mock_write_fstreams: public fstreams_ifc {
42
+    private:
43
+        mutable std::string captured_filename_;
44
+        mutable std::shared_ptr<std::ostringstream> capture_stream_;
45
+    public:
46
+        postream open_ofstream (const std::string &file_path, std::ios_base::open_mode mode) const override {
47
+            captured_filename_ = file_path;
48
+            capture_stream_ = std::make_shared<std::ostringstream>();
49
+            return capture_stream_;
50
+        }
51
+        std::string captured_filename() {
52
+            return captured_filename_;
53
+        }
54
+        std::string captured_written() {
55
+            return capture_stream_->str();
56
+        }
57
+};
58
+
41 59
 class fake_fstreams : public fstreams_ifc
42 60
 {
43 61
 private:
... ...
@@ -48,7 +66,7 @@ public:
48 66
                    const std::string &file_contents)
49 67
         : expected_file_path_ (expected_file_path),
50 68
           file_contents_ (file_contents) {}
51
-    pstream open_fstream (const std::string &file_path)
69
+    pstream open_fstream (const std::string &file_path) const override
52 70
     {
53 71
         if (file_path == expected_file_path_) {
54 72
             return fstreams::pstream (new std::istringstream (file_contents_));
... ...
@@ -72,7 +90,7 @@ int reads_from_the_right_file ()
72 90
     //file_reader test_file_reader (file_reader::delegate (new fake_file_reader));
73 91
     user test_user (user::delegate (new fake_user (home_directory)));
74 92
     tokens supplier (tokens::create (
75
-                                      test_streams));
93
+                                      test_streams,[]{return"";}));
76 94
 
77 95
     //when
78 96
     std::string actual = supplier.token (test_user);
... ...
@@ -92,7 +110,7 @@ int returns_empty_string_if_file_open_fail()
92 110
                            "654321")));
93 111
     user test_user (user::delegate (new fake_user (home_directory)));
94 112
     tokens supplier (tokens::create (
95
-                                      test_streams));
113
+                                      test_streams, []{return "";}));
96 114
 
97 115
     //when
98 116
     std::string actual = supplier.token (test_user);
... ...
@@ -102,13 +120,34 @@ int returns_empty_string_if_file_open_fail()
102 120
     succeed();
103 121
 }
104 122
 
105
-RESET_VARS_START
106
-RESET_VARS_END
123
+int writes_the_token () {
124
+    // given
125
+    std::string home_directory("/somedir");
126
+    user test_user(user::delegate(new fake_user(home_directory)));
127
+    mock_write_fstreams *mockfs(new mock_write_fstreams);
128
+    fstreams test_streams{fstreams::delegate(mockfs)};
129
+    std::string token("token");
130
+    tokens tokens(tokens::create(test_streams, [&]{return token;}));
131
+
132
+    //when
133
+    tokens.create(test_user);
134
+
135
+   // then
136
+    std::ostringstream temp;
137
+    temp << token << std::endl;
138
+    std::string expected_written = temp.str();
139
+    std::string expected_filename(home_directory + "/.dual_control");
140
+    check(mockfs->captured_filename() == expected_filename, "filename does not match");
141
+    check(mockfs->captured_written() == expected_written, "token does not match");
142
+   succeed();
143
+}
144
+
107 145
 
108 146
 int run_tests()
109 147
 {
110 148
     test (reads_from_the_right_file);
111 149
     test (returns_empty_string_if_file_open_fail);
150
+    test (writes_the_token);
112 151
     succeed();
113 152
 }
114 153
 
115 154
Binary files a/token_test.out and b/token_test.out differ
... ...
@@ -27,7 +27,7 @@ public:
27 27
     user_impl (const passwd user_info) :
28 28
         home_directory_ (std::string (user_info.pw_dir)),
29 29
         user_name_ (std::string (user_info.pw_name)) {}
30
-    std::string home_directory()
30
+    std::string home_directory() const override
31 31
     {
32 32
         return home_directory_;
33 33
     }
... ...
@@ -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)
43
+    std::vector<user> find_user (const std::string &user_name) override
44 44
     {
45 45
         std::vector<char> buffer (unistd_.sysconf (_SC_GETPW_R_SIZE_MAX));
46 46
         passwd sys_passwd;
... ...
@@ -22,9 +22,9 @@ class user_ifc
22 22
 {
23 23
 public:
24 24
     virtual ~user_ifc() {}
25
-    virtual std::string home_directory()
25
+    virtual std::string home_directory() const
26 26
     {
27
-        return "virtual";
27
+        return "";
28 28
     }
29 29
 };
30 30
 
... ...
@@ -39,7 +39,7 @@ public:
39 39
     {
40 40
     }
41 41
     user() : user (delegate (new user_ifc)) {}
42
-    std::string home_directory()
42
+    std::string home_directory() const
43 43
     {
44 44
         return delegate_-> home_directory();
45 45
     }
46 46
Binary files a/user_test.out and b/user_test.out differ
... ...
@@ -28,7 +28,7 @@ public:
28 28
         tokens_ (tokens) {}
29 29
     bool validate (const std::string &requester_user_name,
30 30
                    const std::string &authorizer_user_name,
31
-                   const std::string &token)
31
+                   const std::string &token) override
32 32
     {
33 33
         std::vector<user> found_user = directory_.find_user (authorizer_user_name);
34 34
 
... ...
@@ -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)
30
+    std::vector<user> find_user (const std::string &user_name) override
31 31
     {
32 32
         std::vector<user> result;
33 33
 
... ...
@@ -46,7 +46,7 @@ private:
46 46
 public:
47 47
     fake_tokens (const std::string &token) : token_ (token) {}
48 48
     fake_tokens() : token_ ("_NOT_A_TOKEN") {}
49
-    std::string token (user &user)
49
+    std::string token (const user &user) const override
50 50
     {
51 51
         return token_;
52 52
     }
53 53
Binary files a/validator_test.out and b/validator_test.out differ