1 /*
2 Copyright (c) Marshall Clow 2011-2012.
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 For more information, see http://www.boost.org
8 */
9
10 #include <boost/config.hpp>
11 #include <boost/algorithm/hex.hpp>
12 #include <boost/algorithm/string/case_conv.hpp>
13
14 #define BOOST_TEST_MAIN
15 #include <boost/test/unit_test.hpp>
16
17 #include <string>
18 #include <iostream>
19
20
21 template<typename String>
test_to_hex(const typename String::value_type ** tests)22 void test_to_hex ( const typename String::value_type ** tests ) {
23 for ( const typename String::value_type **p = tests; *p; p++ ) {
24 String arg, argh, one, two, three, four;
25 arg.assign ( *p );
26 boost::algorithm::hex ( *p, std::back_inserter ( one ));
27 boost::algorithm::hex ( arg, std::back_inserter ( two ));
28 boost::algorithm::hex ( arg.begin (), arg.end (), std::back_inserter ( three ));
29 four = boost::algorithm::hex ( arg );
30 BOOST_CHECK ( one == two );
31 BOOST_CHECK ( one == three );
32 BOOST_CHECK ( one == four );
33 argh = one;
34 one.clear (); two.clear (); three.clear (); four.clear ();
35 boost::algorithm::unhex ( argh.c_str (), std::back_inserter ( one ));
36 boost::algorithm::unhex ( argh, std::back_inserter ( two ));
37 boost::algorithm::unhex ( argh.begin (), argh.end (), std::back_inserter ( three ));
38 four = boost::algorithm::unhex ( argh );
39 BOOST_CHECK ( one == two );
40 BOOST_CHECK ( one == three );
41 BOOST_CHECK ( one == four );
42 BOOST_CHECK ( one == arg );
43 }
44 }
45
46 template<typename String>
test_to_hex_lower(const typename String::value_type ** tests)47 void test_to_hex_lower ( const typename String::value_type ** tests ) {
48 for ( const typename String::value_type **p = tests; *p; p++ ) {
49 String arg, argh, one, two, three, four;
50 arg.assign ( *p );
51 boost::algorithm::hex_lower ( *p, std::back_inserter ( one ));
52 boost::algorithm::hex_lower ( arg, std::back_inserter ( two ));
53 boost::algorithm::hex_lower ( arg.begin (), arg.end (), std::back_inserter ( three ));
54 four = boost::algorithm::hex_lower ( arg );
55 BOOST_CHECK ( one == two );
56 BOOST_CHECK ( one == three );
57 BOOST_CHECK ( one == four );
58 argh = one;
59 one.clear (); two.clear (); three.clear (); four.clear ();
60 boost::algorithm::unhex ( argh.c_str (), std::back_inserter ( one ));
61 boost::algorithm::unhex ( argh, std::back_inserter ( two ));
62 boost::algorithm::unhex ( argh.begin (), argh.end (), std::back_inserter ( three ));
63 four = boost::algorithm::unhex ( argh );
64 BOOST_CHECK ( one == two );
65 BOOST_CHECK ( one == three );
66 BOOST_CHECK ( one == four );
67 BOOST_CHECK ( one == arg );
68 }
69 }
70
71
72 template<typename String>
test_from_hex_success(const typename String::value_type ** tests)73 void test_from_hex_success ( const typename String::value_type ** tests ) {
74 for ( const typename String::value_type **p = tests; *p; p++ ) {
75 String arg, argh, one, two, three, four;
76 arg.assign ( *p );
77 boost::algorithm::unhex ( *p, std::back_inserter ( one ));
78 boost::algorithm::unhex ( arg, std::back_inserter ( two ));
79 boost::algorithm::unhex ( arg.begin (), arg.end (), std::back_inserter ( three ));
80 four = boost::algorithm::unhex ( arg );
81 BOOST_CHECK ( one == two );
82 BOOST_CHECK ( one == three );
83 BOOST_CHECK ( one == four );
84 argh = one;
85 one.clear (); two.clear (); three.clear (); four.clear ();
86 boost::algorithm::hex ( argh.c_str (), std::back_inserter ( one ));
87 boost::algorithm::hex ( argh, std::back_inserter ( two ));
88 boost::algorithm::hex ( argh.begin (), argh.end (), std::back_inserter ( three ));
89 four = boost::algorithm::hex ( argh );
90 boost::algorithm::to_lower( arg );
91 boost::algorithm::to_lower( one );
92 boost::algorithm::to_lower( two );
93 boost::algorithm::to_lower( three );
94 boost::algorithm::to_lower( four );
95 BOOST_CHECK ( one == two );
96 BOOST_CHECK ( one == three );
97 BOOST_CHECK ( one == four );
98 BOOST_CHECK ( one == arg );
99 }
100 }
101
102 template<typename String>
test_from_hex_failure(const typename String::value_type ** tests)103 void test_from_hex_failure ( const typename String::value_type ** tests ) {
104 int num_catches;
105 for ( const typename String::value_type **p = tests; *p; p++ ) {
106 String arg, one;
107 arg.assign ( *p );
108 num_catches = 0;
109
110 try { boost::algorithm::unhex ( *p, std::back_inserter ( one )); }
111 catch ( const boost::algorithm::hex_decode_error & /*ex*/ ) { num_catches++; }
112 try { boost::algorithm::unhex ( arg, std::back_inserter ( one )); }
113 catch ( const boost::algorithm::hex_decode_error & /*ex*/ ) { num_catches++; }
114 try { boost::algorithm::unhex ( arg.begin (), arg.end (), std::back_inserter ( one )); }
115 catch ( const boost::algorithm::hex_decode_error & /*ex*/ ) { num_catches++; }
116 BOOST_CHECK ( num_catches == 3 );
117 }
118 }
119
120
121
122 const char *tohex [] = {
123 "",
124 "a",
125 "\001",
126 "12",
127 "asdfadsfsad",
128 "01234567890ABCDEF",
129 NULL // End of the list
130 };
131
132
133 const wchar_t *tohex_w [] = {
134 L"",
135 L"a",
136 L"\001",
137 L"12",
138 L"asdfadsfsad",
139 L"01234567890ABCDEF",
140 NULL // End of the list
141 };
142
143
144 const char *fromhex [] = {
145 "20",
146 "2122234556FF",
147 "2122234556ff",
148 NULL // End of the list
149 };
150
151
152 const wchar_t *fromhex_w [] = {
153 L"00101020",
154 L"2122234556FF3456",
155 L"2122234556ff3456",
156 NULL // End of the list
157 };
158
159
160 const char *fromhex_fail [] = {
161 "2",
162 "H",
163 "234",
164 "21222G4556FF",
165 "h",
166 "21222g4556ff",
167 NULL // End of the list
168 };
169
170
171 const wchar_t *fromhex_fail_w [] = {
172 L"2",
173 L"12",
174 L"H",
175 L"234",
176 L"21222G4556FF",
177 L"h",
178 L"21222g4556ff",
179 NULL // End of the list
180 };
181
182
BOOST_AUTO_TEST_CASE(test_main)183 BOOST_AUTO_TEST_CASE( test_main )
184 {
185 test_to_hex<std::string> ( tohex );
186 test_to_hex_lower<std::string> ( tohex );
187 test_from_hex_success<std::string> ( fromhex );
188 test_from_hex_failure<std::string> ( fromhex_fail );
189
190 test_to_hex<std::wstring> ( tohex_w );
191 test_to_hex_lower<std::wstring> ( tohex_w );
192 test_from_hex_success<std::wstring> ( fromhex_w );
193 test_from_hex_failure<std::wstring> ( fromhex_fail_w );
194 }
195