git.fiddlerwoaroof.com
Browse code

system random init

Greg Wiley authored on 04/05/2017 21:11:56
Showing 7 changed files
... ...
@@ -3,8 +3,8 @@ CFLAGS += -fPIC -fno-stack-protector
3 3
 LDFLAGS = -lpam
4 4
 
5 5
 INTEGRATION_OBJS = sys_syslog.o sys_fstream.o sys_unistd.o sys_pwd.o sys_pam.o \
6
-                   sys_stdlib.o
7
-OBJS = dual_control.o request.o validator.o conversation.o user.o token.o logger.o session.o installer.o
6
+                   sys_stdlib.o sys_time.o
7
+OBJS = dual_control.o request.o validator.o conversation.o user.o token.o logger.o session.o installer.o system.o
8 8
 TEST_SOURCES := $(wildcard *_test.cc)
9 9
 TESTS := $(patsubst %.cc,%.out,$(TEST_SOURCES))
10 10
 TESTRUNS := $(patsubst %.out,RUN_%,$(TESTS))
11 11
Binary files a/dual_control and b/dual_control differ
12 12
new file mode 100644
... ...
@@ -0,0 +1,18 @@
1
+#include <memory>
2
+#include <ctime>
3
+
4
+#include "sys_time.h"
5
+
6
+namespace {
7
+    class impl : public time_ifc {
8
+        public:
9
+            time_t time(time_t *timer) const override{
10
+                return ::time(timer);
11
+            }
12
+    };
13
+}
14
+
15
+const time_t &time::get() {
16
+    static time singleton(std::make_shared<impl>());
17
+    return singleton;
18
+}
0 19
new file mode 100644
... ...
@@ -0,0 +1,29 @@
1
+#ifndef SYS_TIME_H_
2
+#define SYS_TIME_H_
3
+
4
+#include <ctime>
5
+#include <memory>
6
+
7
+class sys_time_ifc {
8
+    public:
9
+        virtual time_t time(time_t *timer) const {
10
+            return 0;
11
+        }
12
+};
13
+
14
+class sys_time {
15
+    public:
16
+        using delegate = std::shared_ptr<sys_time_ifc>;
17
+    private:
18
+        delegate delegate_;
19
+    public:
20
+        sys_time(const delegate &delegate) : delegate_(delegate) {}
21
+        time_t time(time_t *timer) const {
22
+            return delegate_->time(timer);
23
+        }
24
+   static const time_t &get();
25
+
26
+};
27
+
28
+#endif
29
+
0 30
new file mode 100644
... ...
@@ -0,0 +1,10 @@
1
+
2
+#include "system.h"
3
+#include "sys_stdlib.h"
4
+#include "sys_time.h"
5
+
6
+system::system(const stdlib &stdlib, const sys_time &time) {
7
+    unsigned seed = static_cast<unsigned>(time.time(nullptr));
8
+    stdlib.srand(seed);
9
+}
10
+
0 11
new file mode 100644
... ...
@@ -0,0 +1,15 @@
1
+#ifndef SYSTEM_H_
2
+#define SYSTEM_H_
3
+
4
+#include <memory>
5
+
6
+#include "sys_time.h"
7
+#include "sys_stdlib.h"
8
+
9
+class system {
10
+    public:
11
+        system(const stdlib &stdlib, const sys_time &time);
12
+};
13
+
14
+
15
+#endif
0 16
new file mode 100644
... ...
@@ -0,0 +1,55 @@
1
+#include <memory>
2
+#include <ctime>
3
+
4
+#include "test_util.h"
5
+#include "system.h"
6
+
7
+#include "sys_time.h"
8
+#include "sys_stdlib.h"
9
+
10
+class mock_stdlib : public stdlib_ifc {
11
+    public:
12
+    mutable unsigned captured_seed {0};
13
+    void srand(unsigned int seed) const {
14
+        captured_seed = seed;
15
+    }
16
+};
17
+
18
+
19
+class fake_sys_time : public sys_time_ifc {
20
+     private:
21
+         unsigned value_;
22
+     public:
23
+         fake_sys_time(unsigned value) : value_(value) {}
24
+         time_t time(time_t *timer) const override {
25
+             return static_cast<time_t>(value_);
26
+         }
27
+};
28
+
29
+int initializes_random () {
30
+    // given
31
+    unsigned seed(20392);
32
+    auto test_stdlib = std::make_shared<mock_stdlib>();
33
+    stdlib stdlib(test_stdlib);
34
+    auto test_time = std::make_shared<fake_sys_time>(seed);
35
+    sys_time time(test_time);
36
+
37
+    // when
38
+    system system(stdlib, time);
39
+
40
+
41
+    // then
42
+    check(test_stdlib->captured_seed == seed, "seed not cpatured");
43
+    succeed();
44
+}
45
+int run_tests()
46
+{
47
+    test (initializes_random);
48
+    succeed();
49
+}
50
+
51
+int main (int argc, char *argv[])
52
+{
53
+    return !run_tests();
54
+}
55
+