1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
//
// $Id$
//
//
// Original author: Darren Kessner <darren@proteowizard.org>
//
// Copyright 2007 Spielberg Family Center for Applied Proteomics
// Cedars Sinai Medical Center, Los Angeles, California 90048
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "Std.hpp"
#include "SHA1Calculator.hpp"
#include "unit.hpp"
#include <boost/filesystem/operations.hpp>
#include <cstring>
using namespace pwiz::util;
ostream* os_ = 0;
char verify_int_is_32_bits[(sizeof(int)==4)*2-1];
const char* hashEmpty_ = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
const char* textBrown_ = "The quick brown fox jumps over the lazy dog";
const char* hashBrown_ = "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12";
const char* textabc_ = "abc";
const char* hashabc_ = "a9993e364706816aba3e25717850c26c9cd0d89d";
const char* textabc2_ = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
const char* hashabc2_ = "84983e441c3bd26ebaae4aa1f95129e5e54670f1";
const char* hashMillion_ = "34aa973cd4c4daa4f61eeb2bdbad27316534016f"; // one million 'a'
void test()
{
SHA1Calculator sha1;
sha1.close();
string temp = sha1.hash();
if (os_) *os_ << "hash empty: " << temp << endl;
unit_assert(temp == hashEmpty_);
sha1.reset();
sha1.update((const unsigned char*)textBrown_, strlen(textBrown_));
sha1.close();
temp = sha1.hash();
if (os_) *os_ << "hash brown: " << temp << endl;
unit_assert(temp == hashBrown_);
}
void testStream()
{
istringstream is(textBrown_);
string hash = SHA1Calculator::hash(is);
if (os_) *os_ << "hash stream: " << hash << endl;
unit_assert(hash == "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
}
void testFile()
{
const char* filename = "sha1test.test.txt";
ofstream os(filename);
os << textBrown_;
os.close();
{
string hash = SHA1Calculator::hashFile(filename);
if (os_) *os_ << "hash file: " << hash << endl;
unit_assert(hash == "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
}
{
ifstream filestream(filename, ios::binary);
string hash = SHA1Calculator::hash(filestream);
if (os_) *os_ << "hash stream: " << hash << endl;
unit_assert(hash == "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
}
boost::filesystem::remove(filename);
}
void testStatic()
{
string temp = SHA1Calculator::hash(textBrown_);
unit_assert(temp == hashBrown_);
temp = SHA1Calculator::hash(textabc_);
if (os_) *os_ << "hash abc: " << temp << endl;
unit_assert(temp == hashabc_);
temp = SHA1Calculator::hash(textabc2_);
if (os_) *os_ << "hash abc2: " << temp << endl;
unit_assert(temp == hashabc2_);
}
void testMillion()
{
string a(10, 'a');
SHA1Calculator sha1;
for (int i=0; i<100000; i++)
sha1.update(a);
sha1.close();
string temp = sha1.hash();
if (os_) *os_ << "hash million: " << temp << endl;
unit_assert(temp == hashMillion_);
}
void testProjected()
{
SHA1Calculator sha1;
sha1.update((const unsigned char*)textBrown_, strlen(textBrown_));
string projected = sha1.hashProjected();
if (os_) *os_ << "projected: " << projected << endl;
unit_assert(projected == hashBrown_);
unit_assert(sha1.hashProjected() == hashBrown_); // doesn't change
sha1.close();
string final = sha1.hash();
unit_assert(final == hashBrown_);
unit_assert(sha1.hash() == hashBrown_); // doesn't change
}
int main(int argc, char* argv[])
{
TEST_PROLOG(argc, argv)
try
{
if (argc>1 && !strcmp(argv[1],"-v")) // verbose
os_ = &cout;
if (os_) *os_ << "sha1test\n";
test();
testStream();
testFile();
testStatic();
testMillion();
testProjected();
}
catch (exception& e)
{
TEST_FAILED(e.what())
}
catch (...)
{
TEST_FAILED("Caught unknown exception.")
}
TEST_EPILOG
}
|