ProteoWizard
Functions | Variables
SHA1_ostream_test.cpp File Reference
#include "Std.hpp"
#include "SHA1_ostream.hpp"
#include "unit.hpp"
#include "boost/iostreams/flush.hpp"
#include <boost/filesystem/operations.hpp>

Go to the source code of this file.

Functions

void test ()
 
void testFile ()
 
int main (int argc, char *argv[])
 

Variables

ostream * os_ = 0
 
const char * textBrown_ = "The quick brown fox jumps over the lazy dog"
 
const char * hashBrown_ = "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"
 

Function Documentation

§ test()

void test ( )

Definition at line 41 of file SHA1_ostream_test.cpp.

References pwiz::util::SHA1_ostream::explicitFlush(), pwiz::util::SHA1Calculator::hash(), pwiz::util::SHA1_ostream::hash(), hashBrown_, os_, textBrown_, and unit_assert.

Referenced by main().

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 }
const char * textBrown_
ostream * os_
ostream filter for calculating a SHA-1 hash of data on the fly
const char * hashBrown_
#define unit_assert(x)
Definition: unit.hpp:85

§ testFile()

void testFile ( )

Definition at line 69 of file SHA1_ostream_test.cpp.

References pwiz::util::SHA1Calculator::hashFile(), os_, textBrown_, and unit_assert.

Referenced by main().

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 }
const char * textBrown_
ostream * os_
ostream filter for calculating a SHA-1 hash of data on the fly
#define unit_assert(x)
Definition: unit.hpp:85

§ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 91 of file SHA1_ostream_test.cpp.

References os_, test(), TEST_EPILOG, TEST_FAILED, TEST_PROLOG, and testFile().

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 }
void test()
ostream * os_
#define TEST_EPILOG
Definition: unit.hpp:182
void testFile()
#define TEST_FAILED(x)
Definition: unit.hpp:176
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:174

Variable Documentation

§ os_

ostream* os_ = 0

Definition at line 34 of file SHA1_ostream_test.cpp.

Referenced by main(), test(), and testFile().

§ textBrown_

const char* textBrown_ = "The quick brown fox jumps over the lazy dog"

Definition at line 37 of file SHA1_ostream_test.cpp.

Referenced by test(), and testFile().

§ hashBrown_

const char* hashBrown_ = "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"

Definition at line 38 of file SHA1_ostream_test.cpp.

Referenced by test().