1 | // |
2 | // DigestStreamTest.cpp |
3 | // |
4 | // |
5 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
6 | // and Contributors. |
7 | // |
8 | // SPDX-License-Identifier: BSL-1.0 |
9 | // |
10 | |
11 | |
12 | #include "DigestStreamTest.h" |
13 | #include "Poco/CppUnit/TestCaller.h" |
14 | #include "Poco/CppUnit/TestSuite.h" |
15 | #include "Poco/DigestStream.h" |
16 | #include "Poco/MD5Engine.h" |
17 | #include <sstream> |
18 | |
19 | |
20 | using Poco::DigestInputStream; |
21 | using Poco::DigestOutputStream; |
22 | using Poco::DigestEngine; |
23 | using Poco::MD5Engine; |
24 | |
25 | |
26 | DigestStreamTest::DigestStreamTest(const std::string& rName): CppUnit::TestCase(rName) |
27 | { |
28 | } |
29 | |
30 | |
31 | DigestStreamTest::~DigestStreamTest() |
32 | { |
33 | } |
34 | |
35 | |
36 | void DigestStreamTest::testInputStream() |
37 | { |
38 | std::istringstream istr("abcdefghijklmnopqrstuvwxyz" ); |
39 | MD5Engine md5; |
40 | DigestInputStream ds(md5, istr); |
41 | std::string s; |
42 | ds >> s; |
43 | assertTrue (DigestEngine::digestToHex(md5.digest()) == "c3fcd3d76192e4007dfb496cca67e13b" ); |
44 | assertTrue (s == "abcdefghijklmnopqrstuvwxyz" ); |
45 | } |
46 | |
47 | |
48 | void DigestStreamTest::testOutputStream1() |
49 | { |
50 | MD5Engine md5; |
51 | DigestOutputStream ds(md5); |
52 | ds << "abcdefghijklmnopqrstuvwxyz" ; |
53 | ds.close(); |
54 | assertTrue (DigestEngine::digestToHex(md5.digest()) == "c3fcd3d76192e4007dfb496cca67e13b" ); |
55 | |
56 | ds << "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; |
57 | ds << "abcdefghijklmnopqrstuvwxyz0123456789" ; |
58 | ds.close(); |
59 | assertTrue (DigestEngine::digestToHex(md5.digest()) == "d174ab98d277d9f5a5611c2c9f419d9f" ); |
60 | } |
61 | |
62 | |
63 | void DigestStreamTest::testOutputStream2() |
64 | { |
65 | MD5Engine md5; |
66 | std::ostringstream ostr; |
67 | DigestOutputStream ds(md5, ostr); |
68 | ds << "abcdefghijklmnopqrstuvwxyz" ; |
69 | ds.close(); |
70 | assertTrue (DigestEngine::digestToHex(md5.digest()) == "c3fcd3d76192e4007dfb496cca67e13b" ); |
71 | assertTrue (ostr.str() == "abcdefghijklmnopqrstuvwxyz" ); |
72 | } |
73 | |
74 | |
75 | void DigestStreamTest::testToFromHex() |
76 | { |
77 | std::string digest("c3fcd3d76192e4007dfb496cca67e13b" ); |
78 | Poco::DigestEngine::Digest dig = DigestEngine::digestFromHex(digest); |
79 | std::string digest2 = DigestEngine::digestToHex(dig); |
80 | assertTrue (digest == digest2); |
81 | } |
82 | |
83 | |
84 | void DigestStreamTest::setUp() |
85 | { |
86 | } |
87 | |
88 | |
89 | void DigestStreamTest::tearDown() |
90 | { |
91 | } |
92 | |
93 | |
94 | CppUnit::Test* DigestStreamTest::suite() |
95 | { |
96 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("DigestStreamTest" ); |
97 | |
98 | CppUnit_addTest(pSuite, DigestStreamTest, testInputStream); |
99 | CppUnit_addTest(pSuite, DigestStreamTest, testOutputStream1); |
100 | CppUnit_addTest(pSuite, DigestStreamTest, testOutputStream2); |
101 | CppUnit_addTest(pSuite, DigestStreamTest, testToFromHex); |
102 | |
103 | return pSuite; |
104 | } |
105 | |