aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_octetstring.cpp
blob: ff689518e9b6a676ad5e51ddc6138dd8473b059b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#include <botan/symkey.h>

namespace Botan_Tests {

namespace {

using Botan::OctetString;

Test::Result test_from_rng()
   {
   Test::Result result("OctetString");

   OctetString os(Test::rng(), 32);
   result.test_eq("length is 32 bytes", os.size(), 32);

   return result;
   }

Test::Result test_from_hex()
   {
   Test::Result result("OctetString");

   OctetString os("0123456789ABCDEF");
   result.test_eq("length is 8 bytes", os.size(), 8);

   return result;
   }

Test::Result test_from_byte()
   {
   Test::Result result("OctetString");

   auto rand_bytes = Test::rng().random_vec(8);
   OctetString os(rand_bytes.data(), rand_bytes.size());
   result.test_eq("length is 8 bytes", os.size(), 8);

   return result;
   }

Test::Result test_odd_parity()
   {
   Test::Result result("OctetString");

   OctetString os("FFFFFFFFFFFFFFFF");
   os.set_odd_parity();
   OctetString expected("FEFEFEFEFEFEFEFE");
   result.test_eq("odd parity set correctly", os, expected);

   OctetString os2("EFCBDA4FAA997F63");
   os2.set_odd_parity();
   OctetString expected2("EFCBDA4FAB987F62");
   result.test_eq("odd parity set correctly", os2, expected2);

   return result;
   }

Test::Result test_as_string()
   {
   Test::Result result("OctetString");

   OctetString os("0123456789ABCDEF");
   result.test_eq("OctetString::as_string() returns correct string", os.as_string(), "0123456789ABCDEF");

   return result;
   }

Test::Result test_xor()
   {
   Test::Result result("OctetString");

   OctetString os1("0000000000000000");
   OctetString os2("FFFFFFFFFFFFFFFF");

   OctetString xor_result = os1 ^ os2;
   result.test_eq("OctetString XOR operations works as expected", xor_result, os2);

   xor_result = os1;
   xor_result ^= os2;
   result.test_eq("OctetString XOR operations works as expected", xor_result, os2);

   xor_result = os2 ^ os2;
   result.test_eq("OctetString XOR operations works as expected", xor_result, os1);

   OctetString os3("0123456789ABCDEF");
   xor_result = os3 ^ os2;
   OctetString expected("FEDCBA9876543210");
   result.test_eq("OctetString XOR operations works as expected", xor_result, expected);

   return result;
   }

Test::Result test_equality()
   {
   Test::Result result("OctetString");

   OctetString os1("0000000000000000");
   OctetString os2("FFFFFFFFFFFFFFFF");

   result.confirm("OctetString equality operations works as expected", os1 == os1);
   result.confirm("OctetString equality operations works as expected", os2 == os2);
   result.confirm("OctetString equality operations works as expected", os1 != os2);

   return result;
   }

Test::Result test_append()
   {
   Test::Result result("OctetString");

   OctetString os1("0000");
   OctetString os2("FFFF");
   OctetString expected("0000FFFF");

   OctetString append_result = os1 + os2;

   result.test_eq("OctetString append operations works as expected", append_result, expected);

   return result;
   }

class OctetString_Tests : public Test
   {
   public:
      std::vector<Test::Result> run() override
         {
         std::vector<Test::Result> results;

         std::vector<std::function<Test::Result()>> fns =
            {
            test_from_rng,
            test_from_hex,
            test_from_byte,
            test_odd_parity,
            test_as_string,
            test_xor,
            test_equality,
            test_append
            };

         for(size_t i = 0; i != fns.size(); ++i)
            {
            try
               {
               results.push_back(fns[ i ]());
               }
            catch(std::exception& e)
               {
               results.push_back(Test::Result::Failure("OctetString tests " + std::to_string(i), e.what()));
               }
            }

         return results;
         }
   };

BOTAN_REGISTER_TEST("octetstring", OctetString_Tests);

}

}