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