git.fiddlerwoaroof.com
Browse code

implement with ifstream

Greg Wiley authored on 25/04/2017 23:34:44
Showing 8 changed files
... ...
@@ -2,7 +2,7 @@ CXXFLAGS += -fPIC -fno-stack-protector -std=c++14
2 2
 CFLAGS += -fPIC -fno-stack-protector
3 3
 
4 4
 OBJS = dual_control.o request.o dual_control_integrate.o validator.o conversation.o user.o \
5
-	   sys_unistd.o sys_pwd.o token.o file_reader.o
5
+	   sys_unistd.o sys_pwd.o token.o sys_fstream.o
6 6
 TESTS = dual_control_test validator_test conversation_test request_test user_test token_test
7 7
 TESTOBJS = $(patsubst %,%.o,$(TESTS))
8 8
 SRCS := $(OBJS:.o=.cc) $(TESTOBJS:.o=.cc)
9 9
deleted file mode 100644
... ...
@@ -1,28 +0,0 @@
1
-#include <fstream>
2
-
3
-#include "file_reader.h"
4
-
5
-namespace {
6
-class file_reader_impl : public file_reader_ifc
7
-{
8
-public:
9
-    bool open (std::ifstream &token_file, const std::string &file_path)
10
-    {
11
-        token_file.open (file_path);
12
-        return token_file.good();
13
-    }
14
-    std::string getline (std::ifstream &token_file, std::string &line)
15
-    {
16
-        std::getline (token_file, line);
17
-        return line;
18
-    }
19
-};
20
-
21
-}
22
-
23
-file_reader file_reader::create ()
24
-{
25
-    return file_reader (file_reader::delegate (new
26
-                        file_reader_impl ));
27
-}
28
-
29 0
deleted file mode 100644
... ...
@@ -1,58 +0,0 @@
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
-
12
-#ifndef _FILE_READER_H
13
-#define _FILE_READER_H
14
-
15
-#include <string>
16
-#include <memory>
17
-
18
-#include "user.h"
19
-
20
-
21
-class file_reader_ifc
22
-{
23
-public:
24
-    virtual bool open (std::ifstream &token_file, const std::string &file_path)
25
-    {
26
-        return false;
27
-    }
28
-    virtual std::string getline (std::ifstream &token_file,
29
-                                 std::string &line)
30
-    {
31
-        return "";
32
-    }
33
-};
34
-
35
-class file_reader : public file_reader_ifc
36
-{
37
-public:
38
-    typedef std::shared_ptr<file_reader_ifc> delegate;
39
-private:
40
-    delegate delegate_;
41
-public:
42
-    file_reader (delegate delegate) :
43
-        delegate_ (delegate) {}
44
-    file_reader() : delegate_ (delegate (new file_reader_ifc)) {}
45
-    bool open (std::ifstream &token_file, const std::string &file_path)
46
-    {
47
-        return delegate_->open (token_file, file_path);
48
-    }
49
-    std::string getline (std::ifstream &token_file, std::string &line)
50
-    {
51
-        return delegate_->getline (token_file, line);
52
-    }
53
-
54
-    static file_reader create ();
55
-};
56
-
57
-#endif
58
-
59 0
new file mode 100644
... ...
@@ -0,0 +1,23 @@
1
+#include <memory>
2
+#include <fstream>
3
+
4
+#include "sys_fstream.h"
5
+
6
+namespace {
7
+class impl : public fstreams_ifc {
8
+    public:
9
+        pstream open_fstream(const std::string &file_path) {
10
+            return pstream(new std::ifstream(file_path));
11
+        }
12
+};
13
+
14
+
15
+}
16
+
17
+
18
+fstreams fstreams::create()  {
19
+    static fstreams singleton(delegate(new impl));
20
+    return singleton;
21
+}
22
+
23
+
0 24
new file mode 100644
... ...
@@ -0,0 +1,31 @@
1
+#ifndef SYS_FSTREAM_H
2
+#define SYS_FSTREAM_H
3
+
4
+#include <memory>
5
+#include <sstream>
6
+
7
+class fstreams_ifc {
8
+    public:
9
+        typedef std::shared_ptr<std::istream> pstream;
10
+        virtual pstream open_fstream(const std::string &file_path) {
11
+            return pstream(new std::istringstream(""));
12
+        }
13
+};
14
+
15
+class fstreams : public fstreams_ifc {
16
+    public:
17
+        typedef std::shared_ptr<fstreams_ifc> delegate;
18
+    private:
19
+        delegate delegate_;
20
+    public:
21
+        fstreams(const delegate &delegate) : delegate_(delegate){}
22
+        fstreams() : fstreams(delegate(new fstreams_ifc)) {}
23
+        pstream open_fstream(const std::string &file_path) {
24
+            return delegate_->open_fstream(file_path);
25
+        }
26
+        static fstreams create();
27
+};
28
+
29
+
30
+#endif
31
+
... ...
@@ -16,35 +16,36 @@
16 16
 
17 17
 #include "token.h"
18 18
 #include "user.h"
19
+#include "sys_fstream.h"
19 20
 
20 21
 namespace
21 22
 {
22 23
 class user_token_supplier_impl : public user_token_supplier_ifc
23 24
 {
24 25
 private:
25
-    file_reader file_reader_;
26
+    fstreams fstreams_;
26 27
 public:
27
-    user_token_supplier_impl (file_reader &file_reader) : file_reader_
28
-        (file_reader) {}
28
+    user_token_supplier_impl (fstreams &fstreams) :
29
+        fstreams_(fstreams) {}
29 30
     std::string token (user &user)
30 31
     {
31 32
         const std::string file_path (user.home_directory() + "/.dual_control");
32
-        std::ifstream token_file;
33
-        std::string fetched_token;
33
+        std::vector<char> line(7);
34 34
 
35
-        bool token_file_opened = file_reader_.open (token_file, file_path);
35
+        fstreams::pstream stream(fstreams_.open_fstream(file_path));
36 36
 
37
-        if (!token_file_opened) {
37
+        if (!stream->good()) {
38 38
             return "";
39 39
         }
40 40
 
41
-        return file_reader_.getline (token_file, fetched_token);
41
+        stream->getline(line.data(), line.size());
42
+        return std::string(line.data());
42 43
     }
43 44
 };
44 45
 }
45
-user_token_supplier user_token_supplier::create (file_reader &file_reader)
46
+user_token_supplier user_token_supplier::create (fstreams &fstreams)
46 47
 {
47 48
     return user_token_supplier (user_token_supplier::delegate
48
-                                (new user_token_supplier_impl (file_reader)));
49
+                                (new user_token_supplier_impl (fstreams)));
49 50
 }
50 51
 
... ...
@@ -16,7 +16,7 @@
16 16
 #include <memory>
17 17
 
18 18
 #include "user.h"
19
-#include "file_reader.h"
19
+#include "sys_fstream.h"
20 20
 
21 21
 class user_token_supplier_ifc
22 22
 {
... ...
@@ -43,7 +43,7 @@ public:
43 43
     {
44 44
         return delegate_->token (user);
45 45
     }
46
-    static user_token_supplier create (file_reader &file_reader);
46
+    static user_token_supplier create (fstreams &fstreams);
47 47
 };
48 48
 
49 49
 #endif
... ...
@@ -15,36 +15,12 @@
15 15
 #include <cstdio>
16 16
 #include <sys/stat.h>
17 17
 #include <fstream>
18
+#include <sstream>
18 19
 
19 20
 #include "token.h"
20 21
 #include "test_util.h"
21 22
 #include "user.h"
22
-#include "file_reader.h"
23
-
24
-class fake_file_reader : public file_reader_ifc
25
-{
26
-private:
27
-    std::string file_path_;
28
-public:
29
-    bool open (std::ifstream &token_file, const std::string &file_path)
30
-    {
31
-        file_path_ = file_path;
32
-        return true;
33
-    }
34
-    std::string getline (std::ifstream &token_file, std::string &fetched_token)
35
-    {
36
-        return file_path_;
37
-    }
38
-};
39
-
40
-class file_reader_with_open_fail : public file_reader_ifc
41
-{
42
-public:
43
-    bool open (std::ifstream &token_file, std::string &fetched_token)
44
-    {
45
-        return false;
46
-    }
47
-};
23
+#include "sys_fstream.h"
48 24
 
49 25
 class fake_user : public user_ifc
50 26
 {
... ...
@@ -62,32 +38,57 @@ public:
62 38
     }
63 39
 };
64 40
 
41
+class fake_fstreams : public fstreams_ifc {
42
+    private:
43
+        std::string expected_file_path_;
44
+        std::string file_contents_;
45
+    public:
46
+        fake_fstreams(const std::string &expected_file_path, const std::string &file_contents)
47
+            : expected_file_path_(expected_file_path),
48
+              file_contents_(file_contents) {}
49
+        pstream open_fstream(const std::string &file_path) {
50
+            if (file_path == expected_file_path_) {
51
+                return fstreams::pstream(new std::istringstream(file_contents_));
52
+            } else {
53
+                return fstreams_ifc::open_fstream(file_path);
54
+            }
55
+
56
+        }
57
+};
58
+
65 59
 int reads_from_the_right_file ()
66 60
 {
67 61
     //given
68 62
     std::string home_directory = "/somedir";
69
-    file_reader test_file_reader (file_reader::delegate (new fake_file_reader));
63
+    // hardcoded file name is .dual_control in the user's home directory
64
+    std::string token_file = home_directory + "/.dual_control";
65
+    std::string token("123456");
66
+    fstreams test_streams(fstreams::delegate(new fake_fstreams(token_file, token)));
67
+
68
+    //file_reader test_file_reader (file_reader::delegate (new fake_file_reader));
70 69
     user test_user (user::delegate (new fake_user (home_directory)));
71 70
     user_token_supplier supplier (user_token_supplier::create (
72
-                                      test_file_reader));
71
+                                      test_streams));
73 72
 
74 73
     //when
75 74
     std::string actual = supplier.token (test_user);
76 75
 
77 76
     //then
78
-    std::string expected(home_directory + "/.dual_control");
79
-    check (actual == expected, "read wrong file");
77
+    check (actual == token, "token does not match");
80 78
     succeed();
81 79
 }
82 80
 
83 81
 int returns_empty_string_if_file_open_fail()
84 82
 {
85 83
     //given
86
-    file_reader test_file_reader (file_reader::delegate (new
87
-                                  file_reader_with_open_fail));
88
-    user test_user (user::delegate (new fake_user));
84
+    std::string home_directory = "/somedir";
85
+    // hardcoded file name is .dual_control in the user's home directory
86
+    std::string token_file = home_directory + "/.not_dual_control";
87
+    fstreams test_streams(fstreams::delegate(new fake_fstreams(token_file, "654321")));
88
+    user test_user (user::delegate (new fake_user (home_directory)));
89 89
     user_token_supplier supplier (user_token_supplier::create (
90
-                                      test_file_reader));
90
+                                      test_streams));
91
+
91 92
 
92 93
     //when
93 94
     std::string actual = supplier.token (test_user);