1//
2// HexBinaryTest.cpp
3//
4// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "HexBinaryTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/HexBinaryEncoder.h"
15#include "Poco/HexBinaryDecoder.h"
16#include "Poco/Exception.h"
17#include <sstream>
18
19
20using Poco::HexBinaryEncoder;
21using Poco::HexBinaryDecoder;
22using Poco::DataFormatException;
23
24
25HexBinaryTest::HexBinaryTest(const std::string& rName): CppUnit::TestCase(rName)
26{
27}
28
29
30HexBinaryTest::~HexBinaryTest()
31{
32}
33
34
35void HexBinaryTest::testEncoder()
36{
37 {
38 std::ostringstream str;
39 HexBinaryEncoder encoder(str);
40 encoder << std::string("\00\01\02\03\04\05", 6);
41 encoder.close();
42 assertTrue (str.str() == "000102030405");
43 }
44 {
45 std::ostringstream str;
46 HexBinaryEncoder encoder(str);
47 encoder << std::string("\00\01\02\03", 4);
48 encoder.close();
49 assertTrue (str.str() == "00010203");
50 }
51 {
52 std::ostringstream str;
53 HexBinaryEncoder encoder(str);
54 encoder << "ABCDEF";
55 encoder << char(0xaa) << char(0xbb);
56 encoder.close();
57 assertTrue (str.str() == "414243444546aabb");
58 }
59 {
60 std::ostringstream str;
61 HexBinaryEncoder encoder(str);
62 encoder.rdbuf()->setUppercase();
63 encoder << "ABCDEF";
64 encoder << char(0xaa) << char(0xbb);
65 encoder.close();
66 assertTrue (str.str() == "414243444546AABB");
67 }
68}
69
70
71void HexBinaryTest::testDecoder()
72{
73 {
74 std::istringstream istr("000102030405");
75 HexBinaryDecoder decoder(istr);
76 assertTrue (decoder.good() && decoder.get() == 0);
77 assertTrue (decoder.good() && decoder.get() == 1);
78 assertTrue (decoder.good() && decoder.get() == 2);
79 assertTrue (decoder.good() && decoder.get() == 3);
80 assertTrue (decoder.good() && decoder.get() == 4);
81 assertTrue (decoder.good() && decoder.get() == 5);
82 assertTrue (decoder.good() && decoder.get() == -1);
83 }
84 {
85 std::istringstream istr("0001020304");
86 HexBinaryDecoder decoder(istr);
87 assertTrue (decoder.good() && decoder.get() == 0);
88 assertTrue (decoder.good() && decoder.get() == 1);
89 assertTrue (decoder.good() && decoder.get() == 2);
90 assertTrue (decoder.good() && decoder.get() == 3);
91 assertTrue (decoder.good() && decoder.get() == 4);
92 assertTrue (decoder.good() && decoder.get() == -1);
93 }
94 {
95 std::istringstream istr("0a0bcdef");
96 HexBinaryDecoder decoder(istr);
97 assertTrue (decoder.good() && decoder.get() == 0x0a);
98 assertTrue (decoder.good() && decoder.get() == 0x0b);
99 assertTrue (decoder.good() && decoder.get() == 0xcd);
100 assertTrue (decoder.good() && decoder.get() == 0xef);
101 assertTrue (decoder.good() && decoder.get() == -1);
102 }
103 {
104 std::istringstream istr("0A0BCDEF");
105 HexBinaryDecoder decoder(istr);
106 assertTrue (decoder.good() && decoder.get() == 0x0a);
107 assertTrue (decoder.good() && decoder.get() == 0x0b);
108 assertTrue (decoder.good() && decoder.get() == 0xcd);
109 assertTrue (decoder.good() && decoder.get() == 0xef);
110 assertTrue (decoder.good() && decoder.get() == -1);
111 }
112 {
113 std::istringstream istr("00 01 02 03");
114 HexBinaryDecoder decoder(istr);
115 assertTrue (decoder.good() && decoder.get() == 0);
116 assertTrue (decoder.good() && decoder.get() == 1);
117 assertTrue (decoder.good() && decoder.get() == 2);
118 assertTrue (decoder.good() && decoder.get() == 3);
119 assertTrue (decoder.good() && decoder.get() == -1);
120 }
121 {
122 std::istringstream istr("414243444546");
123 HexBinaryDecoder decoder(istr);
124 std::string s;
125 decoder >> s;
126 assertTrue (s == "ABCDEF");
127 assertTrue (decoder.eof());
128 assertTrue (!decoder.fail());
129 }
130 {
131 std::istringstream istr("4041\r\n4243\r\n4445");
132 HexBinaryDecoder decoder(istr);
133 std::string s;
134 decoder >> s;
135 assertTrue (s == "@ABCDE");
136 assertTrue (decoder.eof());
137 assertTrue (!decoder.fail());
138 }
139 {
140 std::istringstream istr("AABB#CCDD");
141 HexBinaryDecoder decoder(istr);
142 std::string s;
143 try
144 {
145 decoder >> s;
146 assertTrue (decoder.bad());
147 }
148 catch (DataFormatException&)
149 {
150 }
151 assertTrue (!decoder.eof());
152 }
153}
154
155
156void HexBinaryTest::testEncodeDecode()
157{
158 {
159 std::stringstream str;
160 HexBinaryEncoder encoder(str);
161 encoder << "The quick brown fox ";
162 encoder << "jumped over the lazy dog.";
163 encoder.close();
164 HexBinaryDecoder decoder(str);
165 std::string s;
166 int c = decoder.get();
167 while (c != -1) { s += char(c); c = decoder.get(); }
168 assertTrue (s == "The quick brown fox jumped over the lazy dog.");
169 }
170 {
171 std::string src;
172 for (int i = 0; i < 255; ++i) src += char(i);
173 std::stringstream str;
174 HexBinaryEncoder encoder(str);
175 encoder.write(src.data(), (std::streamsize) src.size());
176 encoder.close();
177 HexBinaryDecoder decoder(str);
178 std::string s;
179 int c = decoder.get();
180 while (c != -1) { s += char(c); c = decoder.get(); }
181 assertTrue (s == src);
182 }
183}
184
185
186void HexBinaryTest::setUp()
187{
188}
189
190
191void HexBinaryTest::tearDown()
192{
193}
194
195
196CppUnit::Test* HexBinaryTest::suite()
197{
198 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HexBinaryTest");
199
200 CppUnit_addTest(pSuite, HexBinaryTest, testEncoder);
201 CppUnit_addTest(pSuite, HexBinaryTest, testDecoder);
202 CppUnit_addTest(pSuite, HexBinaryTest, testEncodeDecode);
203
204 return pSuite;
205}
206