summaryrefslogtreecommitdiff
path: root/src/KM_util.cpp
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2016-01-14 22:27:20 +0000
committerCarl Hetherington <cth@carlh.net>2016-01-14 23:02:44 +0000
commit379fb7a2ef1aaa0e92d0c5c76ea78b86f8554808 (patch)
treef7e6e15682a358d446a8a1d4297acef0f72f1486 /src/KM_util.cpp
parent664f5734adfbf3bae1cbfd84c3b648bdf70f0490 (diff)
Patches for testing to allow predictable random number and date generation.
Diffstat (limited to 'src/KM_util.cpp')
-rwxr-xr-xsrc/KM_util.cpp44
1 files changed, 30 insertions, 14 deletions
diff --git a/src/KM_util.cpp b/src/KM_util.cpp
index e6d04a7..e7585ea 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()
{
@@ -320,7 +322,7 @@ Kumu::base64encode(const byte_t* buf, ui32_t buf_len, char* strbuf, ui32_t strbu
diff = buf_len - i;
assert(diff > 0);
assert(diff < 3);
-
+
strbuf[out_char++] = base64_chars[( buf[0] >> 2 )];
if ( diff == 1 )
@@ -575,7 +577,7 @@ Kumu::bin2UUIDhex(const byte_t* bin_buf, ui32_t bin_len, char* str_buf, ui32_t s
// add in the hyphens and trainling null
for ( i = 8; i < 24; i += 5 )
str_buf[i] = '-';
-
+
str_buf[36] = 0;
return str_buf;
}
@@ -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)
@@ -647,7 +656,7 @@ bool
Kumu::read_BER(const byte_t* buf, ui64_t* val)
{
ui8_t ber_size, i;
-
+
if ( buf == 0 || val == 0 )
return false;
@@ -671,7 +680,7 @@ Kumu::read_BER(const byte_t* buf, ui64_t* val)
static const ui64_t ber_masks[9] =
- { ui64_C(0xffffffffffffffff), ui64_C(0xffffffffffffff00),
+ { ui64_C(0xffffffffffffffff), ui64_C(0xffffffffffffff00),
ui64_C(0xffffffffffff0000), ui64_C(0xffffffffff000000),
ui64_C(0xffffffff00000000), ui64_C(0xffffff0000000000),
ui64_C(0xffff000000000000), ui64_C(0xff00000000000000),
@@ -716,7 +725,7 @@ Kumu::write_BER(byte_t* buf, ui64_t val, ui32_t ber_len)
DefaultLogSink().Error("BER integer length %u exceeds maximum size of 9\n", ber_len);
return false;
}
-
+
if ( ( val & ber_masks[ber_len - 1] ) != 0 )
{
ui64Printer tmp_i(val);
@@ -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) {
@@ -830,7 +846,7 @@ Kumu::Timestamp::SetTZOffsetMinutes(const i32_t& minutes)
return true;
}
-//
+//
const char*
Kumu::Timestamp::EncodeString(char* str_buf, ui32_t buf_len) const
{
@@ -859,7 +875,7 @@ Kumu::Timestamp::EncodeString(char* str_buf, ui32_t buf_len) const
if ( m_TZOffsetMinutes < 0 )
direction = '-';
}
-
+
// 2004-05-01T13:20:00+00:00
snprintf(str_buf, buf_len,
"%04hu-%02hhu-%02hhuT%02hhu:%02hhu:%02hhu%c%02u:%02u",
@@ -889,7 +905,7 @@ Kumu::Timestamp::DecodeString(const char* datestr)
YMDhms.date.year = strtol(datestr, 0, 10);
YMDhms.date.month = strtol(datestr + 5, 0, 10);
YMDhms.date.day = strtol(datestr + 8, 0, 10);
-
+
if ( datestr[10] == 'T' )
{
if ( ! ( isdigit(datestr[11]) && isdigit(datestr[12]) )
@@ -962,7 +978,7 @@ Kumu::Timestamp::DecodeString(const char* datestr)
datestr, char_count);
return false;
}
-
+
m_Timestamp = YMDhms;
m_TZOffsetMinutes = YMDhms.offset;
return true;
@@ -1004,7 +1020,7 @@ Kumu::Timestamp::Archive(MemIOWriter* Writer) const
ui8_t month, day, hour, minute, second, tick = 0;
GetComponents(year, month, day, hour, minute, second);
- if ( ! Writer->WriteUi16BE(year) ) return false;
+ if ( ! Writer->WriteUi16BE(year) ) return false;
if ( ! Writer->WriteUi8(month) ) return false;
if ( ! Writer->WriteUi8(day) ) return false;
if ( ! Writer->WriteUi8(hour) ) return false;
@@ -1140,7 +1156,7 @@ Kumu::ByteString::Capacity(ui32_t cap_size)
else
free(m_Data);
}
-
+
if ( ( m_Data = (byte_t*)malloc(cap_size) ) == 0 )
return RESULT_ALLOC;
@@ -1150,7 +1166,7 @@ Kumu::ByteString::Capacity(ui32_t cap_size)
memcpy(m_Data, tmp_data, m_Length);
free(tmp_data);
}
-
+
m_Capacity = cap_size;
return RESULT_OK;
}
@@ -1241,7 +1257,7 @@ Kumu::km_token_split(const std::string& str, const std::string& separator)
pstr = r + separator.size();
r = strstr(pstr, separator.c_str());
}
-
+
components.push_back(std::string(pstr));
return components;
}