summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2020-11-23 23:31:36 +0100
committerCarl Hetherington <cth@carlh.net>2020-12-27 02:30:45 +0100
commitb318ce46f90b4f99f08749df8540f58b74962bee (patch)
tree9b046bb3978e04fb788c92a14b5d7aa77327ebe1 /src
parenta86a6cf14b3966348f4ea08571aacbba4e54013c (diff)
Allow predictable random number and date generation during testing.
Diffstat (limited to 'src')
-rwxr-xr-xsrc/KM_prng.cpp21
-rwxr-xr-xsrc/KM_prng.h1
-rwxr-xr-xsrc/KM_util.cpp18
-rwxr-xr-xsrc/KM_util.h5
4 files changed, 43 insertions, 2 deletions
diff --git a/src/KM_prng.cpp b/src/KM_prng.cpp
index d11a330..aebc755 100755
--- a/src/KM_prng.cpp
+++ b/src/KM_prng.cpp
@@ -64,6 +64,7 @@ public:
AES_KEY m_Context;
byte_t m_ctr_buf[RNG_BLOCK_SIZE];
Mutex m_Lock;
+ unsigned int m_cth_test_rng_state;
h__RNG()
{
@@ -97,6 +98,7 @@ public:
} // end AutoMutex context
set_key(rng_key);
+ reset();
}
//
@@ -138,7 +140,20 @@ public:
AES_encrypt(m_ctr_buf, tmp, &m_Context);
memcpy(buf + gen_count, tmp, len - gen_count);
}
+
+ if (cth_test)
+ {
+#ifdef __unix__
+ for (unsigned int i = 0; i < len; ++i)
+ buf[i] = rand_r(&m_cth_test_rng_state);
+#endif
+ }
}
+
+ void reset()
+ {
+ m_cth_test_rng_state = 1;
+ }
};
@@ -192,6 +207,12 @@ Kumu::FortunaRNG::FillRandom(Kumu::ByteString& Buffer)
return Buffer.Data();
}
+void
+Kumu::FortunaRNG::Reset()
+{
+ s_RNG->reset();
+}
+
//------------------------------------------------------------------------------------------
//
diff --git a/src/KM_prng.h b/src/KM_prng.h
index 172af2b..f4497ed 100755
--- a/src/KM_prng.h
+++ b/src/KM_prng.h
@@ -45,6 +45,7 @@ namespace Kumu
~FortunaRNG();
const byte_t* FillRandom(byte_t* buf, ui32_t len);
const byte_t* FillRandom(ByteString&);
+ void Reset();
};
diff --git a/src/KM_util.cpp b/src/KM_util.cpp
index da8d732..6d364cc 100755
--- a/src/KM_util.cpp
+++ b/src/KM_util.cpp
@@ -40,6 +40,8 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <map>
#include <string>
+bool Kumu::cth_test = false;
+
const char*
Kumu::Version()
{
@@ -589,6 +591,13 @@ Kumu::GenRandomValue(UUID& ID)
ID.Set(tmp_buf);
}
+void
+Kumu::ResetTestRNG()
+{
+ FortunaRNG RNG;
+ RNG.Reset();
+}
+
//
void
Kumu::GenRandomUUID(byte_t* buf)
@@ -745,7 +754,14 @@ Kumu::write_BER(byte_t* buf, ui64_t val, ui32_t ber_len)
//
Kumu::Timestamp::Timestamp() : m_TZOffsetMinutes(0) {
- m_Timestamp.now();
+ if (cth_test)
+ {
+ m_Timestamp.x = 42;
+ }
+ else
+ {
+ m_Timestamp.now();
+ }
}
Kumu::Timestamp::Timestamp(const Timestamp& rhs) {
diff --git a/src/KM_util.h b/src/KM_util.h
index 9dcf846..19ba385 100755
--- a/src/KM_util.h
+++ b/src/KM_util.h
@@ -40,6 +40,8 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace Kumu
{
+ extern bool cth_test;
+
// The version number declaration and explanation are in ../configure.ac
const char* Version();
@@ -421,7 +423,8 @@ namespace Kumu
void GenRandomUUID(byte_t* buf); // buf must be UUID_Length or longer
void GenRandomValue(UUID&);
-
+ void ResetTestRNG();
+
typedef ArchivableList<UUID> UUIDList;
// a self-wiping key container