ProteoWizard
SHA1_ostream_test.cpp
Go to the documentation of this file.
1 //
2 // $Id: SHA1_ostream_test.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 "SHA1_ostream.hpp"
26 #include "unit.hpp"
27 #include "boost/iostreams/flush.hpp"
28 #include <boost/filesystem/operations.hpp>
29 
30 
31 using namespace pwiz::util;
32 
33 
34 ostream* os_ = 0;
35 
36 
37 const char* textBrown_ = "The quick brown fox jumps over the lazy dog";
38 const char* hashBrown_ = "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12";
39 
40 
41 void test()
42 {
43  ostringstream oss;
44  SHA1_ostream sha1os(oss);
45 
46  sha1os << textBrown_ << flush;
47  string hash = sha1os.hash();
48  sha1os.explicitFlush();
49 
50  if (os_) *os_ << "str: " << oss.str() << endl
51  << "hash: " << hash << endl;
52 
53  unit_assert(hash == hashBrown_);
54  unit_assert(sha1os.hash() == hashBrown_);
55 
56  sha1os << textBrown_ << flush;
57  sha1os.explicitFlush();
58 
59  hash = sha1os.hash();
60 
61  if (os_) *os_ << "str: " << oss.str() << endl
62  << "hash: " << hash << endl;
63 
64  string hash2 = SHA1Calculator::hash(string(textBrown_) + textBrown_);
65  unit_assert(sha1os.hash() == hash2);
66 }
67 
68 
69 void testFile()
70 {
71  string filename = "SHA1_ostream.temp.txt";
72  ofstream ofs(filename.c_str(), ios::binary); // binary necessary on Windows to avoid \n -> \r\n translation
73  SHA1_ostream sha1os(ofs);
74 
75  sha1os << textBrown_ << '\n' << textBrown_ << flush;
76  string hashStream = sha1os.hash();
77 
78  sha1os.explicitFlush();
79  string hashFile = SHA1Calculator::hashFile(filename);
80 
81  if (os_) *os_ << "stream: " << hashStream << endl
82  << "file : " << hashFile << endl;
83 
84  unit_assert(hashStream == hashFile);
85  unit_assert(hashStream == "a159e6cde4e50e51713700d1fe4d0ce553eace87");
86  ofs.close();
87  boost::filesystem::remove(filename);
88 }
89 
90 
91 int main(int argc, char* argv[])
92 {
93  TEST_PROLOG(argc, argv)
94 
95  try
96  {
97  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
98  test();
99  testFile();
100  }
101  catch (exception& e)
102  {
103  TEST_FAILED(e.what())
104  }
105  catch (...)
106  {
107  TEST_FAILED("Caught unknown exception.")
108  }
109 
111 }
112