1 /*
2  * Copyright (c) 2011, Peter Thorson. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above copyright
9  *       notice, this list of conditions and the following disclaimer in the
10  *       documentation and/or other materials provided with the distribution.
11  *     * Neither the name of the WebSocket++ Project nor the
12  *       names of its contributors may be used to endorse or promote products
13  *       derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 //#define BOOST_TEST_DYN_LINK
28 #define BOOST_TEST_MODULE sha1
29 #include <boost/test/unit_test.hpp>
30 
31 #include <iostream>
32 #include <string>
33 
34 #include <websocketpp/sha1/sha1.hpp>
35 #include <websocketpp/utilities.hpp>
36 
37 BOOST_AUTO_TEST_SUITE ( sha1 )
38 
BOOST_AUTO_TEST_CASE(sha1_test_a)39 BOOST_AUTO_TEST_CASE( sha1_test_a ) {
40     unsigned char hash[20];
41     unsigned char reference[20] = {0xa9, 0x99, 0x3e, 0x36, 0x47,
42                                    0x06, 0x81, 0x6a, 0xba, 0x3e,
43                                    0x25, 0x71, 0x78, 0x50, 0xc2,
44                                    0x6c, 0x9c, 0xd0, 0xd8, 0x9d};
45 
46     websocketpp::sha1::calc("abc",3,hash);
47 
48     BOOST_CHECK_EQUAL_COLLECTIONS(hash, hash+20, reference, reference+20);
49 }
50 
BOOST_AUTO_TEST_CASE(sha1_test_b)51 BOOST_AUTO_TEST_CASE( sha1_test_b ) {
52     unsigned char hash[20];
53     unsigned char reference[20] = {0x84, 0x98, 0x3e, 0x44, 0x1c,
54                                    0x3b, 0xd2, 0x6e, 0xba, 0xae,
55                                    0x4a, 0xa1, 0xf9, 0x51, 0x29,
56                                    0xe5, 0xe5, 0x46, 0x70, 0xf1};
57 
58     websocketpp::sha1::calc(
59         "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",56,hash);
60 
61     BOOST_CHECK_EQUAL_COLLECTIONS(hash, hash+20, reference, reference+20);
62 }
63 
BOOST_AUTO_TEST_CASE(sha1_test_c)64 BOOST_AUTO_TEST_CASE( sha1_test_c ) {
65     std::string input;
66     unsigned char hash[20];
67     unsigned char reference[20] = {0x34, 0xaa, 0x97, 0x3c, 0xd4,
68                                    0xc4, 0xda, 0xa4, 0xf6, 0x1e,
69                                    0xeb, 0x2b, 0xdb, 0xad, 0x27,
70                                    0x31, 0x65, 0x34, 0x01, 0x6f};
71 
72     for (int i = 0; i < 1000000; i++) {
73         input += 'a';
74     }
75 
76     websocketpp::sha1::calc(input.c_str(),input.size(),hash);
77 
78     BOOST_CHECK_EQUAL_COLLECTIONS(hash, hash+20, reference, reference+20);
79 }
80 
81 BOOST_AUTO_TEST_SUITE_END()
82