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.

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}
const char * hashBrown_
const char * textBrown_
const char * hashEmpty_
ostream * os_
void close()
finish the hash
void reset()
resets hash
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
#define unit_assert(x)
Definition unit.hpp:85

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().

◆ testStream()

void testStream ( )

Definition at line 71 of file SHA1CalculatorTest.cpp.

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}

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

Referenced by main().

◆ testFile()

void testFile ( )

Definition at line 80 of file SHA1CalculatorTest.cpp.

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}
static std::string hashFile(const std::string &filename)
static function to calculate hash of a file

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

Referenced by main().

◆ testStatic()

void testStatic ( )

Definition at line 104 of file SHA1CalculatorTest.cpp.

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}
const char * textabc_
const char * textabc2_
const char * hashabc2_
const char * hashabc_

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

Referenced by main().

◆ testMillion()

void testMillion ( )

Definition at line 119 of file SHA1CalculatorTest.cpp.

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}
const char * hashMillion_

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

Referenced by main().

◆ testProjected()

void testProjected ( )

Definition at line 134 of file SHA1CalculatorTest.cpp.

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}
std::string hashProjected() const
returns projected final hash value as if close() were called first; hash remains open and update() ma...

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().

◆ main()

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

Definition at line 152 of file SHA1CalculatorTest.cpp.

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();
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 testFile()
void testStream()
void testMillion()
void test()
void testProjected()
void testStatic()
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175

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

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().