git.fiddlerwoaroof.com
Browse code

Refactor random source to match conventions

Ed Langley authored on 13/06/2017 22:42:25
Showing 4 changed files
... ...
@@ -9,8 +9,8 @@ else
9 9
 endif
10 10
 
11 11
 INTEGRATION_OBJS = sys_syslog.o sys_fstream.o sys_unistd.o sys_pwd.o sys_pam.o \
12
-                   sys_stdlib.o sys_time.o
13
-OBJS = dual_control.o request.o validator.o conversation.o user.o token.o logger.o session.o installer.o system.o generator.o base32.o
12
+                   sys_stdlib.o sys_time.o random_source.o
13
+OBJS = dual_control.o request.o validator.o conversation.o user.o token.o logger.o session.o installer.o system.o generator.o base32.o random_source.o
14 14
 TEST_SOURCES := $(wildcard *_test.cc)
15 15
 TESTS := $(patsubst %.cc,%.out,$(TEST_SOURCES))
16 16
 TESTRUNS := $(patsubst %.out,RUN_%,$(TESTS))
... ...
@@ -38,12 +38,14 @@ installer init_installer()
38 38
 {
39 39
     fstreams fstreams (fstreams::create());
40 40
 
41
-    random_source_ifc foo (fstreams);
42
-    auto bytes = foo.get_random_bytes(16);
41
+    random_source foo (random_source::create(fstreams));
42
+    auto bytes = foo.get_random_bytes (16);
43 43
     std::cout << "I'm random: ";
44
+
44 45
     for (auto byte: bytes) {
45
-        std::cout << static_cast<unsigned int>(byte) << ", ";
46
+        std::cout << static_cast<unsigned int> (byte) << ", ";
46 47
     }
48
+
47 49
     std::cout << std::endl;
48 50
 
49 51
     pwd pwd (pwd::create());
50 52
new file mode 100644
... ...
@@ -0,0 +1,38 @@
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
+#include "random_source.h"
13
+#include "sys_fstream.h"
14
+
15
+namespace
16
+{
17
+class impl : public random_source_ifc
18
+{
19
+private:
20
+    fstreams fstreams_;
21
+public:
22
+    impl (fstreams fstreams)
23
+        : fstreams_ (fstreams)
24
+    {}
25
+    std::vector<uint8_t> get_random_bytes (int length) const override {
26
+        std::string file_path = "/dev/urandom";
27
+        fstreams::pstream random_source = fstreams_.open_fstream (file_path);
28
+
29
+        std::vector<uint8_t> result (length);
30
+        random_source->read (reinterpret_cast<char *> (result.data()), length);
31
+        return result;
32
+    }
33
+};
34
+}
35
+
36
+random_source random_source::create(fstreams &fstreams) {
37
+    return random_source(random_source::delegate (new impl (fstreams)));
38
+};
... ...
@@ -22,36 +22,29 @@
22 22
 
23 23
 class random_source_ifc
24 24
 {
25
- private:
26
-    fstreams fstreams_;
27
- public:
28
-    random_source_ifc (fstreams fstreams)
29
-        : fstreams_ (fstreams)
30
-    {}
31
-    virtual std::vector<uint8_t> get_random_bytes (int length) const {
32
-        std::string file_path = "/dev/urandom";
33
-        fstreams::pstream random_source = fstreams_.open_fstream(file_path);
34
-
35
-        std::vector<uint8_t> result(length);
36
-        random_source->read(reinterpret_cast<char *>(result.data()), length);
37
-        return result;
38
-        /* return {}; */
25
+public:
26
+    virtual std::vector<uint8_t> get_random_bytes (int length) const
27
+    {
28
+        return {};
39 29
     }
40 30
 };
41 31
 
42 32
 class random_source
43 33
 {
44
- public:
34
+public:
45 35
     using delegate = std::shared_ptr<random_source_ifc>;
46
- private:
36
+private:
47 37
     delegate delegate_;
48
- public:
38
+public:
49 39
     random_source (delegate delegate)
50 40
         : delegate_ (delegate)
51 41
     {}
52
-    std::vector<uint8_t> get_random_bytes (int length) const {
53
-        return delegate_->get_random_bytes(length);
42
+    std::vector<uint8_t> get_random_bytes (int length) const
43
+    {
44
+        return delegate_->get_random_bytes (length);
54 45
     }
46
+
47
+    static random_source create(fstreams &fstreams);
55 48
 };
56 49
 
57 50
 #endif