1 /*
2 * (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6
7 #include "tests.h"
8
9 #include <botan/symkey.h>
10
11 namespace Botan_Tests {
12
13 namespace {
14
test_from_rng()15 Test::Result test_from_rng()
16 {
17 Test::Result result("OctetString");
18
19 Botan::OctetString os(Test::rng(), 32);
20 result.test_eq("length is 32 bytes", os.size(), 32);
21
22 return result;
23 }
24
test_from_hex()25 Test::Result test_from_hex()
26 {
27 Test::Result result("OctetString");
28
29 Botan::OctetString os("0123456789ABCDEF");
30 result.test_eq("length is 8 bytes", os.size(), 8);
31
32 return result;
33 }
34
test_from_byte()35 Test::Result test_from_byte()
36 {
37 Test::Result result("OctetString");
38
39 auto rand_bytes = Test::rng().random_vec(8);
40 Botan::OctetString os(rand_bytes.data(), rand_bytes.size());
41 result.test_eq("length is 8 bytes", os.size(), 8);
42
43 return result;
44 }
45
test_odd_parity()46 Test::Result test_odd_parity()
47 {
48 Test::Result result("OctetString");
49
50 Botan::OctetString os("FFFFFFFFFFFFFFFF");
51 os.set_odd_parity();
52 Botan::OctetString expected("FEFEFEFEFEFEFEFE");
53 result.test_eq("odd parity set correctly", os, expected);
54
55 Botan::OctetString os2("EFCBDA4FAA997F63");
56 os2.set_odd_parity();
57 Botan::OctetString expected2("EFCBDA4FAB987F62");
58 result.test_eq("odd parity set correctly", os2, expected2);
59
60 return result;
61 }
62
test_to_string()63 Test::Result test_to_string()
64 {
65 Test::Result result("OctetString");
66
67 Botan::OctetString os("0123456789ABCDEF");
68 result.test_eq("OctetString::to_string() returns correct string", os.to_string(), "0123456789ABCDEF");
69
70 return result;
71 }
72
test_xor()73 Test::Result test_xor()
74 {
75 Test::Result result("OctetString");
76
77 Botan::OctetString os1("0000000000000000");
78 Botan::OctetString os2("FFFFFFFFFFFFFFFF");
79
80 Botan::OctetString xor_result = os1 ^ os2;
81 result.test_eq("OctetString XOR operations works as expected", xor_result, os2);
82
83 xor_result = os1;
84 xor_result ^= os2;
85 result.test_eq("OctetString XOR operations works as expected", xor_result, os2);
86
87 xor_result = os2 ^ os2;
88 result.test_eq("OctetString XOR operations works as expected", xor_result, os1);
89
90 Botan::OctetString os3("0123456789ABCDEF");
91 xor_result = os3 ^ os2;
92 Botan::OctetString expected("FEDCBA9876543210");
93 result.test_eq("OctetString XOR operations works as expected", xor_result, expected);
94
95 return result;
96 }
97
test_equality()98 Test::Result test_equality()
99 {
100 Test::Result result("OctetString");
101
102 const Botan::OctetString os1("0000000000000000");
103 const Botan::OctetString os1_copy = os1;
104 const Botan::OctetString os2("FFFFFFFFFFFFFFFF");
105 const Botan::OctetString os2_copy = os2;
106
107 result.confirm("OctetString equality operations works as expected", os1 == os1_copy);
108 result.confirm("OctetString equality operations works as expected", os2 == os2_copy);
109 result.confirm("OctetString equality operations works as expected", os1 != os2);
110
111 return result;
112 }
113
test_append()114 Test::Result test_append()
115 {
116 Test::Result result("OctetString");
117
118 Botan::OctetString os1("0000");
119 Botan::OctetString os2("FFFF");
120 Botan::OctetString expected("0000FFFF");
121
122 Botan::OctetString append_result = os1 + os2;
123
124 result.test_eq("OctetString append operations works as expected", append_result, expected);
125
126 return result;
127 }
128
129 class OctetString_Tests final : public Test
130 {
131 public:
run()132 std::vector<Test::Result> run() override
133 {
134 std::vector<Test::Result> results;
135
136 std::vector<std::function<Test::Result()>> fns =
137 {
138 test_from_rng,
139 test_from_hex,
140 test_from_byte,
141 test_odd_parity,
142 test_to_string,
143 test_xor,
144 test_equality,
145 test_append
146 };
147
148 for(size_t i = 0; i != fns.size(); ++i)
149 {
150 try
151 {
152 results.push_back(fns[ i ]());
153 }
154 catch(std::exception& e)
155 {
156 results.push_back(Test::Result::Failure("OctetString tests " + std::to_string(i), e.what()));
157 }
158 }
159
160 return results;
161 }
162 };
163
164 BOTAN_REGISTER_TEST("utils", "octetstring", OctetString_Tests);
165
166 }
167
168 }
169