Adding a unit test using boost Unit Test Framework
diff --git a/model/sync-digest.cc b/model/sync-digest.cc
index 5ff9418..621b52b 100644
--- a/model/sync-digest.cc
+++ b/model/sync-digest.cc
@@ -120,6 +120,12 @@
EVP_MD_CTX_destroy (m_context);
}
+bool
+Digest::empty () const
+{
+ return m_buffer == 0;
+}
+
void
Digest::reset ()
{
@@ -154,7 +160,8 @@
if (m_buffer == 0)
finalize ();
- BOOST_ASSERT (sizeof (std::size_t) <= m_hashLength);
+ if (sizeof (std::size_t) > m_hashLength)
+ throw DigestCalculationError () << errinfo_at_line (__LINE__);
// just getting first sizeof(std::size_t) bytes
// not ideal, but should work pretty well
@@ -162,13 +169,10 @@
}
bool
-Digest::operator == (Digest &digest)
+Digest::operator == (const Digest &digest) const
{
- if (m_buffer == 0)
- finalize ();
-
- if (digest.m_buffer == 0)
- digest.finalize ();
+ if (m_buffer == 0 || digest.m_buffer == 0)
+ throw DigestCalculationError () << errinfo_at_line (__LINE__);
BOOST_ASSERT (m_hashLength == digest.m_hashLength);
@@ -221,6 +225,10 @@
{
string str;
is >> str; // read string first
+
+ if (str.size () == 0)
+ throw DigestCalculationError () << errinfo_at_line (__LINE__);
+
// uint8_t padding = (3 - str.size () % 3) % 3;
// for (uint8_t i = 0; i < padding; i++) str.push_back ('=');
diff --git a/model/sync-digest.h b/model/sync-digest.h
index f5c83f6..91b331e 100644
--- a/model/sync-digest.h
+++ b/model/sync-digest.h
@@ -42,6 +42,12 @@
Digest ();
/**
+ * @brief Check if digest is empty
+ */
+ bool
+ empty () const;
+
+ /**
* @brief Reset digest to the initial state
*/
void
@@ -66,7 +72,12 @@
* Side effect: Finalize will be called on `this' and `digest'
*/
bool
- operator == (Digest &digest);
+ operator == (const Digest &digest) const;
+
+ bool
+ operator != (const Digest &digest) const
+ { return ! (*this == digest); }
+
/**
* @brief Add existing digest to digest calculation
diff --git a/model/sync-seq-no.h b/model/sync-seq-no.h
index fb9e6b8..a6005af 100644
--- a/model/sync-seq-no.h
+++ b/model/sync-seq-no.h
@@ -55,7 +55,7 @@
, m_seq (seq)
{ }
- inline Digest
+ inline const Digest&
getDigest () const;
/**
@@ -124,11 +124,10 @@
m_digest << m_session << m_seq;
}
-Digest
+const Digest&
SeqNo::getDigest () const
{
- Digest digest;
- return digest;
+ return m_digest;
}