mirror of
https://github.com/zeek/zeek.git
synced 2025-10-06 16:48:19 +00:00
Add new unit test for opaque serialization.
One Bro begins a computation, another finishes it. (This commit also fixes a problem with the serialization OpaqueVals.)
This commit is contained in:
parent
b9d05f56d0
commit
69d8d29fbd
6 changed files with 99 additions and 20 deletions
|
@ -53,7 +53,11 @@ StringVal* HashVal::DoGet()
|
|||
return new StringVal("");
|
||||
}
|
||||
|
||||
HashVal::HashVal(OpaqueType* t) : OpaqueVal(t), valid(false) { }
|
||||
HashVal::HashVal(OpaqueType* t)
|
||||
: OpaqueVal(t)
|
||||
{
|
||||
valid = false;
|
||||
}
|
||||
|
||||
IMPLEMENT_SERIAL(HashVal, SER_HASH_VAL);
|
||||
|
||||
|
@ -113,7 +117,7 @@ bool MD5Val::DoInit()
|
|||
bool MD5Val::DoFeed(const void* data, size_t size)
|
||||
{
|
||||
if ( ! IsValid() )
|
||||
return new StringVal("");
|
||||
return false;
|
||||
|
||||
md5_update(&ctx, data, size);
|
||||
return true;
|
||||
|
@ -220,7 +224,7 @@ bool SHA1Val::DoInit()
|
|||
bool SHA1Val::DoFeed(const void* data, size_t size)
|
||||
{
|
||||
if ( ! IsValid() )
|
||||
return new StringVal("");
|
||||
return false;
|
||||
|
||||
sha1_update(&ctx, data, size);
|
||||
return true;
|
||||
|
@ -331,7 +335,7 @@ bool SHA256Val::DoInit()
|
|||
bool SHA256Val::DoFeed(const void* data, size_t size)
|
||||
{
|
||||
if ( ! IsValid() )
|
||||
return new StringVal("");
|
||||
return false;
|
||||
|
||||
sha256_update(&ctx, data, size);
|
||||
return true;
|
||||
|
|
17
src/Val.cc
17
src/Val.cc
|
@ -3114,16 +3114,13 @@ void VectorVal::ValDescribe(ODesc* d) const
|
|||
d->Add("]");
|
||||
}
|
||||
|
||||
OpaqueVal::OpaqueVal(OpaqueType* t) : opaque_type(t) { }
|
||||
OpaqueVal::OpaqueVal(OpaqueType* t)
|
||||
: Val(t)
|
||||
{
|
||||
}
|
||||
|
||||
OpaqueVal::~OpaqueVal()
|
||||
{
|
||||
Unref(opaque_type);
|
||||
}
|
||||
|
||||
bool OpaqueVal::IsValid() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
IMPLEMENT_SERIAL(OpaqueVal, SER_OPAQUE_VAL);
|
||||
|
@ -3131,15 +3128,13 @@ IMPLEMENT_SERIAL(OpaqueVal, SER_OPAQUE_VAL);
|
|||
bool OpaqueVal::DoSerialize(SerialInfo* info) const
|
||||
{
|
||||
DO_SERIALIZE(SER_OPAQUE_VAL, Val);
|
||||
assert(opaque_type);
|
||||
return opaque_type->Serialize(info);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OpaqueVal::DoUnserialize(UnserialInfo* info)
|
||||
{
|
||||
DO_UNSERIALIZE(Val);
|
||||
opaque_type = static_cast<OpaqueType*>(BroType::Unserialize(info));
|
||||
return opaque_type != 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1019,16 +1019,11 @@ public:
|
|||
OpaqueVal(OpaqueType* t);
|
||||
virtual ~OpaqueVal();
|
||||
|
||||
// Determines whether the opaque value is in a valid state.
|
||||
virtual bool IsValid() const;
|
||||
|
||||
protected:
|
||||
friend class Val;
|
||||
OpaqueVal() { }
|
||||
|
||||
DECLARE_SERIAL(OpaqueVal);
|
||||
|
||||
OpaqueType* opaque_type;
|
||||
};
|
||||
|
||||
|
||||
|
|
4
testing/btest/Baseline/istate.opaque/expected.log
Normal file
4
testing/btest/Baseline/istate.opaque/expected.log
Normal file
|
@ -0,0 +1,4 @@
|
|||
acbd18db4cc2f85cedef654fccc4a4d8
|
||||
0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
|
||||
2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
|
||||
[entropy=0.918296, chi_square=423.666667, mean=108.0, monte_carlo_pi=nan, serial_correlation=-0.5]
|
4
testing/btest/Baseline/istate.opaque/output.log
Normal file
4
testing/btest/Baseline/istate.opaque/output.log
Normal file
|
@ -0,0 +1,4 @@
|
|||
acbd18db4cc2f85cedef654fccc4a4d8
|
||||
0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
|
||||
2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
|
||||
[entropy=0.918296, chi_square=423.666667, mean=108.0, monte_carlo_pi=nan, serial_correlation=-0.5]
|
77
testing/btest/istate/opaque.bro
Normal file
77
testing/btest/istate/opaque.bro
Normal file
|
@ -0,0 +1,77 @@
|
|||
#
|
||||
# @TEST-EXEC: bro -r $TRACES/empty.trace write.bro
|
||||
# @TEST-EXEC: bro read.bro
|
||||
# @TEST-EXEC: btest-diff expected.log
|
||||
# @TEST-EXEC: btest-diff output.log
|
||||
# @TEST-EXEC: cmp output.log expected.log
|
||||
|
||||
@TEST-START-FILE read.bro
|
||||
|
||||
global md5_handle: opaque of md5 &persistent &synchronized;
|
||||
global sha1_handle: opaque of sha1 &persistent &synchronized;
|
||||
global sha256_handle: opaque of sha256 &persistent &synchronized;
|
||||
global entropy_handle: opaque of entropy &persistent &synchronized;
|
||||
|
||||
event bro_done()
|
||||
{
|
||||
local out = open("output.log");
|
||||
|
||||
# Finish incremental operations started by a previous Bro.
|
||||
if ( md5_hash_update(md5_handle, "oo") )
|
||||
print out, md5_hash_finish(md5_handle);
|
||||
else
|
||||
print out, "md5_hash_update() failed";
|
||||
|
||||
if ( sha1_hash_update(sha1_handle, "oo") )
|
||||
print out, sha1_hash_finish(sha1_handle);
|
||||
else
|
||||
print out, "sha1_hash_update() failed";
|
||||
|
||||
if ( sha256_hash_update(sha256_handle, "oo") )
|
||||
print out, sha256_hash_finish(sha256_handle);
|
||||
else
|
||||
print out, "sha256_hash_update() failed";
|
||||
|
||||
if ( entropy_test_add(entropy_handle, "oo") )
|
||||
print out, entropy_test_finish(entropy_handle);
|
||||
else
|
||||
print out, "entropy_test_add() failed";
|
||||
}
|
||||
|
||||
@TEST-END-FILE
|
||||
|
||||
@TEST-START-FILE write.bro
|
||||
|
||||
global md5_handle: opaque of md5 &persistent &synchronized;
|
||||
global sha1_handle: opaque of sha1 &persistent &synchronized;
|
||||
global sha256_handle: opaque of sha256 &persistent &synchronized;
|
||||
global entropy_handle: opaque of entropy &persistent &synchronized;
|
||||
|
||||
event bro_init()
|
||||
{
|
||||
local out = open("expected.log");
|
||||
print out, md5_hash("foo");
|
||||
print out, sha1_hash("foo");
|
||||
print out, sha256_hash("foo");
|
||||
print out, find_entropy("foo");
|
||||
|
||||
# Begin incremental operations. Our goal is to feed the data string "foo" to
|
||||
# the computation, but split into "f" and "oo" in two instances..
|
||||
md5_handle = md5_hash_init();
|
||||
if ( ! md5_hash_update(md5_handle, "f") )
|
||||
print out, "md5_hash_update() failed";
|
||||
|
||||
sha1_handle = sha1_hash_init();
|
||||
if ( ! sha1_hash_update(sha1_handle, "f") )
|
||||
print out, "sha1_hash_update() failed";
|
||||
|
||||
sha256_handle = sha256_hash_init();
|
||||
if ( ! sha256_hash_update(sha256_handle, "f") )
|
||||
print out, "sha256_hash_update() failed";
|
||||
|
||||
entropy_handle = entropy_test_init();
|
||||
if ( ! entropy_test_add(entropy_handle, "f") )
|
||||
print out, "entropy_test_add() failed";
|
||||
}
|
||||
|
||||
@TEST-END-FILE
|
Loading…
Add table
Add a link
Reference in a new issue