ProteoWizard
Functions | Variables
SHA1CalculatorTest.cpp File Reference
#include "Std.hpp"
#include "SHA1Calculator.hpp"
#include "unit.hpp"
#include <boost/filesystem/operations.hpp>
#include <cstring>

Go to the source code of this file.

Functions

void test ()
 
void testStream ()
 
void testFile ()
 
void testStatic ()
 
void testMillion ()
 
void testProjected ()
 
int main (int argc, char *argv[])
 

Variables

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"
 

Function Documentation

§ test()

void test ( )

Definition at line 54 of file SHA1CalculatorTest.cpp.

References pwiz::util::SHA1Calculator::close(), pwiz::util::SHA1Calculator::hash(), hashBrown_, hashEmpty_, os_, pwiz::util::SHA1Calculator::reset(), textBrown_, unit_assert, and pwiz::util::SHA1Calculator::update().

Referenced by main().

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 }
void reset()
resets hash
void close()
finish the hash
ostream * os_
std::string hash() const
returns the current hash value note: close() must be called first to retrieve final hash value ...
void update(const unsigned char *buffer, size_t bufferSize)
update hash with buffer of bytes
const char * hashBrown_
const char * textBrown_
#define unit_assert(x)
Definition: unit.hpp:85
const char * hashEmpty_

§ testStream()

void testStream ( )

Definition at line 71 of file SHA1CalculatorTest.cpp.

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

Referenced by main().

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 }
ostream * os_
const char * textBrown_
#define unit_assert(x)
Definition: unit.hpp:85

§ testFile()

void testFile ( )

Definition at line 80 of file SHA1CalculatorTest.cpp.

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

Referenced by main().

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 }
ostream * os_
const char * textBrown_
#define unit_assert(x)
Definition: unit.hpp:85

§ testStatic()

void testStatic ( )

Definition at line 104 of file SHA1CalculatorTest.cpp.

References pwiz::util::SHA1Calculator::hash(), hashabc2_, hashabc_, hashBrown_, os_, textabc2_, textabc_, textBrown_, and unit_assert.

Referenced by main().

105 {
106  string temp = SHA1Calculator::hash(textBrown_);
107  unit_assert(temp == hashBrown_);
108 
109  temp = SHA1Calculator::hash(textabc_);
110  if (os_) *os_ << "hash abc: " << temp << endl;
111  unit_assert(temp == hashabc_);
112 
113  temp = SHA1Calculator::hash(textabc2_);
114  if (os_) *os_ << "hash abc2: " << temp << endl;
115  unit_assert(temp == hashabc2_);
116 }
ostream * os_
const char * hashabc2_
const char * textabc2_
const char * hashabc_
const char * hashBrown_
const char * textBrown_
#define unit_assert(x)
Definition: unit.hpp:85
const char * textabc_

§ testMillion()

void testMillion ( )

Definition at line 119 of file SHA1CalculatorTest.cpp.

References pwiz::util::SHA1Calculator::close(), pwiz::util::SHA1Calculator::hash(), hashMillion_, os_, unit_assert, and pwiz::util::SHA1Calculator::update().

Referenced by main().

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 }
void close()
finish the hash
ostream * os_
std::string hash() const
returns the current hash value note: close() must be called first to retrieve final hash value ...
void update(const unsigned char *buffer, size_t bufferSize)
update hash with buffer of bytes
const char * hashMillion_
#define unit_assert(x)
Definition: unit.hpp:85

§ testProjected()

void testProjected ( )

Definition at line 134 of file SHA1CalculatorTest.cpp.

References pwiz::util::SHA1Calculator::close(), pwiz::util::SHA1Calculator::hash(), hashBrown_, pwiz::util::SHA1Calculator::hashProjected(), os_, textBrown_, unit_assert, and pwiz::util::SHA1Calculator::update().

Referenced by main().

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 }
void close()
finish the hash
ostream * os_
std::string hashProjected() const
returns projected final hash value as if close() were called first; hash remains open and update() ma...
std::string hash() const
returns the current hash value note: close() must be called first to retrieve final hash value ...
void update(const unsigned char *buffer, size_t bufferSize)
update hash with buffer of bytes
const char * hashBrown_
const char * textBrown_
#define unit_assert(x)
Definition: unit.hpp:85

§ main()

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

Definition at line 152 of file SHA1CalculatorTest.cpp.

References os_, test(), TEST_EPILOG, TEST_FAILED, TEST_PROLOG, testFile(), testMillion(), testProjected(), testStatic(), and testStream().

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

Variable Documentation

§ os_

ostream* os_ = 0

§ verify_int_is_32_bits

char verify_int_is_32_bits[(sizeof(int)==4) *2-1]

Definition at line 37 of file SHA1CalculatorTest.cpp.

§ hashEmpty_

const char* hashEmpty_ = "da39a3ee5e6b4b0d3255bfef95601890afd80709"

Definition at line 40 of file SHA1CalculatorTest.cpp.

Referenced by test().

§ textBrown_

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

Definition at line 42 of file SHA1CalculatorTest.cpp.

Referenced by test(), testFile(), testProjected(), testStatic(), and testStream().

§ hashBrown_

const char* hashBrown_ = "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"

Definition at line 43 of file SHA1CalculatorTest.cpp.

Referenced by test(), testProjected(), and testStatic().

§ textabc_

const char* textabc_ = "abc"

Definition at line 45 of file SHA1CalculatorTest.cpp.

Referenced by testStatic().

§ hashabc_

const char* hashabc_ = "a9993e364706816aba3e25717850c26c9cd0d89d"

Definition at line 46 of file SHA1CalculatorTest.cpp.

Referenced by testStatic().

§ textabc2_

const char* textabc2_ = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"

Definition at line 48 of file SHA1CalculatorTest.cpp.

Referenced by testStatic().

§ hashabc2_

const char* hashabc2_ = "84983e441c3bd26ebaae4aa1f95129e5e54670f1"

Definition at line 49 of file SHA1CalculatorTest.cpp.

Referenced by testStatic().

§ hashMillion_

const char* hashMillion_ = "34aa973cd4c4daa4f61eeb2bdbad27316534016f"

Definition at line 51 of file SHA1CalculatorTest.cpp.

Referenced by testMillion().