]> git.stg.codes - stg.git/blob - tests/test_bfstream.cpp
Some minor changes in bf stream.
[stg.git] / tests / test_bfstream.cpp
1 #include "tut/tut.hpp"
2
3 #include "stg/bfstream.h"
4 #include "stg/os_int.h"
5
6 #include <algorithm>
7 #include <string>
8 #include <cstring>
9
10 namespace
11 {
12
13 class TRACKER
14 {
15     public:
16         TRACKER() : m_lastSize(0), m_callCount(0), m_lastBlock(NULL) {}
17         ~TRACKER() { delete[] m_lastBlock; }
18         void Call(const void * block, size_t size)
19         {
20         delete[] m_lastBlock;
21         if (size > 0)
22             {
23             m_lastBlock = new char[size];
24             memcpy(m_lastBlock,  block, size);
25             }
26         else
27             m_lastBlock = NULL;
28         m_lastSize = size;
29         ++m_callCount;
30         }
31         size_t LastSize() const { return m_lastSize; }
32         size_t CallCount() const { return m_callCount; }
33         const void * LastBlock() const { return m_lastBlock; }
34
35     private:
36         size_t m_lastSize;
37         size_t m_callCount;
38         char * m_lastBlock;
39 };
40
41 void Callback(const void * block, size_t size, void * data)
42 {
43 TRACKER & tracker = *static_cast<TRACKER *>(data);
44 tracker.Call(block, size);
45 }
46
47 }
48
49 namespace tut
50 {
51     struct bfstream_data {
52     };
53
54     typedef test_group<bfstream_data> tg;
55     tg bfstream_test_group("BFStream tests group");
56
57     typedef tg::object testobject;
58
59     template<>
60     template<>
61     void testobject::test<1>()
62     {
63         set_test_name("Check bfstream mechanics");
64
65         TRACKER tracker;
66         STG::ENCRYPT_STREAM stream("pr7Hhen", Callback, &tracker);
67         ensure_equals("CallCount() == 0 after construction", tracker.CallCount(), 0);
68
69         uint32_t block[2] = {0x12345678, 0x87654321};
70         stream.Put(&block[0], sizeof(block[0]));
71         ensure_equals("CallCount() == 0 after first put", tracker.CallCount(), 0);
72         stream.Put(&block[1], sizeof(block[1]));
73         ensure_equals("CallCount() == 1 after second put", tracker.CallCount(), 1);
74
75         uint32_t block2[4] = {0x12345678, 0x87654321, 0x12345678, 0x87654321};
76         stream.Put(&block2[0], sizeof(block2[0]) * 3);
77         ensure_equals("CallCount() == 2 after third put", tracker.CallCount(), 2);
78         stream.Put(&block2[3], sizeof(block2[3]));
79         ensure_equals("CallCount() == 3 after fourth put", tracker.CallCount(), 3);
80     }
81
82     template<>
83     template<>
84     void testobject::test<2>()
85     {
86         set_test_name("Check bfstream encryption");
87
88         TRACKER tracker;
89         STG::ENCRYPT_STREAM stream("pr7Hhen", Callback, &tracker);
90
91         uint32_t block[2] = {0x12345678, 0x87654321};
92         stream.Put(&block[0], sizeof(block[0]));
93         ensure_equals("LastSize() == 0 after first put", tracker.LastSize(), 0);
94         ensure_equals("LastBlock() == NULL after first put", tracker.LastBlock(), static_cast<const void *>(NULL));
95         stream.Put(&block[1], sizeof(block[1]));
96         ensure_equals("LastSize() == 8 after second put", tracker.LastSize(), 8);
97         const uint32_t * ptr = static_cast<const uint32_t *>(tracker.LastBlock());
98         ensure_equals("ptr[0] == 0xd3988cd after second put", ptr[0], 0xd3988cd);
99         ensure_equals("ptr[1] == 0x7996c6d6 after second put", ptr[1], 0x7996c6d6);
100
101         uint32_t block2[4] = {0x12345678, 0x87654321, 0x12345678, 0x87654321};
102         stream.Put(&block2[0], sizeof(block2[0]) * 3);
103         ensure_equals("LastSize() == 8 after third put", tracker.LastSize(), 8);
104         ptr = static_cast<const uint32_t *>(tracker.LastBlock());
105         ensure_equals("ptr[0] == 0xd3988cd after third put", ptr[0], 0xd3988cd);
106         ensure_equals("ptr[1] == 0x7996c6d6 after third put", ptr[1], 0x7996c6d6);
107
108         stream.Put(&block2[3], sizeof(block2[3]));
109         ensure_equals("LastSize() == 8 after fourth put", tracker.LastSize(), 8);
110         ptr = static_cast<const uint32_t *>(tracker.LastBlock());
111         ensure_equals("ptr[0] == 0xd3988cd after fourth put", ptr[0], 0xd3988cd);
112         ensure_equals("ptr[1] == 0x7996c6d6 after fourth put", ptr[1], 0x7996c6d6);
113     }
114
115     template<>
116     template<>
117     void testobject::test<3>()
118     {
119         set_test_name("Check bfstream long string processing");
120
121         TRACKER tracker;
122         STG::ENCRYPT_STREAM estream("pr7Hhen", Callback, &tracker);
123         std::string source = "This is a test long string for checking stream encryption/decryption. \"abcdefghijklmnopqrstuvwxyz 0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ\"";
124         char buffer[source.length() + 9];
125         memset(buffer, 0, sizeof(buffer));
126
127         estream.Put(source.c_str(), source.length() + 1, true);
128         ensure("Encryption long string LastSize()", tracker.LastSize() >= source.length() + 1);
129         ensure("Encryption long string LastBlock() != NULL", tracker.LastBlock() != NULL);
130         memcpy(buffer, tracker.LastBlock(), std::min(tracker.LastSize(), sizeof(buffer)));
131
132         STG::DECRYPT_STREAM dstream("pr7Hhen", Callback, &tracker);
133         dstream.Put(buffer, sizeof(buffer), true);
134         ensure("Decryption long string LastSize() decryption", tracker.LastSize() >= sizeof(buffer));
135         ensure("Decryption long string LastBlock() != NULL", tracker.LastBlock() != NULL);
136         memcpy(buffer, tracker.LastBlock(), std::min(tracker.LastSize(), sizeof(buffer)));
137
138         ensure_equals("Decrypt(Encrypt(source)) == source", std::string(buffer), source);
139     }
140
141 }