Browse Source

Fuzz: random valid RLP generation

cl-refactor
Dimitry Khokhlov 10 years ago
committed by Dimitry
parent
commit
2a40053f07
  1. 25
      test/fuzzTesting/createRandomTest.cpp
  2. 112
      test/fuzzTesting/fuzzHelper.cpp
  3. 4
      test/fuzzTesting/fuzzHelper.h

25
test/fuzzTesting/createRandomTest.cpp

@ -34,6 +34,7 @@ extern std::string const c_testExampleStateTest;
extern std::string const c_testExampleTransactionTest;
extern std::string const c_testExampleVMTest;
extern std::string const c_testExampleBlockchainTest;
extern std::string const c_testExampleRLPTest;
//Main Test functinos
void fillRandomTest(std::function<void(json_spirit::mValue&, bool)> _doTests, std::string const& _testString, bool _debug = false);
@ -62,7 +63,8 @@ int main(int argc, char *argv[])
if (arg == "-t" && i + 1 < argc)
{
testSuite = argv[i + 1];
if (testSuite != "BlockChainTests" && testSuite != "TransactionTests" && testSuite != "StateTests" && testSuite != "VMTests")
if (testSuite != "BlockChainTests" && testSuite != "TransactionTests" && testSuite != "StateTests"
&& testSuite != "VMTests" && testSuite != "RLPTests")
testSuite = "";
}
else
@ -139,6 +141,14 @@ int main(int argc, char *argv[])
else
fillRandomTest(dev::test::doVMTests, (filltest) ? testFillString : c_testExampleVMTest, filldebug);
}
else
if (testSuite == "RLPTests")
{
if (checktest)
return checkRandomTest(dev::test::doStateTests, testmValue, debug);
else
fillRandomTest(dev::test::doStateTests, (filltest) ? testFillString : c_testExampleStateTest, filldebug);
}
}
return 0;
@ -239,6 +249,9 @@ void parseTestWithTypes(std::string& _test)
std::size_t pos = _test.find(types.at(i));
while (pos != std::string::npos)
{
if (types.at(i) == "[RLP]")
_test.replace(pos, 5, dev::test::RandomCode::generate(10, options));
else
if (types.at(i) == "[CODE]")
_test.replace(pos, 6, "0x"+dev::test::RandomCode::generate(10, options));
else
@ -276,7 +289,7 @@ void parseTestWithTypes(std::string& _test)
std::vector<std::string> getTypes()
{
return {"[CODE]", "[HEX]", "[HASH20]", "[HASH32]", "[0xHASH32]", "[V]", "[GASLIMIT]"};
return {"[RLP]", "[CODE]", "[HEX]", "[HASH20]", "[HASH32]", "[0xHASH32]", "[V]", "[GASLIMIT]"};
}
std::string const c_testExampleTransactionTest = R"(
@ -377,6 +390,14 @@ std::string const c_testExampleVMTest = R"(
}
)";
std::string const c_testExampleRLPTest = R"(
{
"randomRLPTest" : {
"out" : "[RLP]"
}
}
)";
std::string const c_testExampleBlockchainTest = R"(
{
"randomBlockTest" : {

112
test/fuzzTesting/fuzzHelper.cpp

@ -42,11 +42,121 @@ boostIntGenerator RandomCode::randOpLengGen = boostIntGenerator(gen, opLengDist)
boostIntGenerator RandomCode::randUniIntGen = boostIntGenerator(gen, uniIntDist);
boostUInt64Generator RandomCode::randUInt64Gen = boostUInt64Generator(gen, uInt64Dist);
std::string RandomCode::rndByteSequence(int _length, SizeStrictness _sizeType)
int RandomCode::recursiveRLP(std::string &result, int depth)
{
bool genvalidrlp = true;
if (depth > 1)
{
//create rlp blocks
int size = 2 + randUniIntGen() % 4;
for (auto i = 0; i < size; i++)
{
std::string blockstr;
recursiveRLP(blockstr, depth - 1);
result += blockstr;
}
//make rlp header
int length = result.size()/2;
std::string header;
int rnd = randUniIntGen() % 100;
if (rnd < 50)
{
//make header as array
if (length <= 55)
header = toCompactHex(128 + length);
else
{
std::string hexlength = toCompactHex(length);
header = toCompactHex(183 + hexlength.size()/2) + hexlength;
}
}
else
{
//make header as list
if (length <= 55)
header = toCompactHex(192 + length);
else
{
std::string hexlength = toCompactHex(length, HexPrefix::DontAdd, 1);
header = toCompactHex(247 + hexlength.size()/2) + hexlength;
}
}
result = header + result;
return result.size()/2;
}
if (depth == 1)
{
int rnd = randUniIntGen() % 5;
switch (rnd)
{
case 0:
//single byte [0x00, 0x7f]
result.insert(0, toCompactHex(randUniIntGen() % 128, HexPrefix::DontAdd, 1));
return 1;
case 1:
{
//string 0-55 [0x80, 0xb7] + string
int len = randUniIntGen() % 55;
std::string hex = rndByteSequence(len);
if (len == 1)
if (genvalidrlp && fromHex(hex)[0] < 128)
hex = toCompactHex((u64)128);
result.insert(0, toCompactHex(128 + len) + hex);
return len + 1;
}
case 2:
{
//string more 55 [0xb8, 0xbf] + length + string
int len = randUniIntGen() % 100;
if (len < 56 && genvalidrlp)
len = 56;
std::string hex = rndByteSequence(len);
std::string hexlen = toCompactHex(len, HexPrefix::DontAdd, 1);
std::string rlpblock = toCompactHex(183 + hexlen.size()/2) + hexlen + hex;
result.insert(0, rlpblock);
return rlpblock.size()/2;
}
case 3:
{
//list 0-55 [0xc0, 0xf7] + data
int len = randUniIntGen() % 55;
std::string hex = rndByteSequence(len);
result.insert(0, toCompactHex(192 + len) + hex);
return len + 1;
}
case 4:
{
//list more 55 [0xf8, 0xff] + length + data
int len = randUniIntGen() % 100;
if (len < 56 && genvalidrlp)
len = 56;
std::string hexlen = toCompactHex(len, HexPrefix::DontAdd, 1);
std::string rlpblock = toCompactHex(247 + hexlen.size()/2) + hexlen + rndByteSequence(len);
result.insert(0, rlpblock);
return rlpblock.size()/2;
}
}
}
return 0;
}
std::string RandomCode::rndRLPSequence(int _length, SizeStrictness _sizeType)
{
refreshSeed();
std::string hash;
_length = (_sizeType == SizeStrictness::Strict) ? std::max(1, _length) : randomUniInt() % _length;
recursiveRLP(hash, _length);
return hash;
}
std::string RandomCode::rndByteSequence(int _length, SizeStrictness _sizeType)
{
refreshSeed();
std::string hash = "";
_length = (_sizeType == SizeStrictness::Strict) ? std::max(0, _length) : randomUniInt() % _length;
for (auto i = 0; i < _length; i++)
{
uint8_t byte = randOpCodeGen();

4
test/fuzzTesting/fuzzHelper.h

@ -75,6 +75,9 @@ public:
/// Generate random byte string of a given length
static std::string rndByteSequence(int _length = 1, SizeStrictness _sizeType = SizeStrictness::Strict);
/// Gemerate random rlp byte sequence of a given length
static std::string rndRLPSequence(int _length = 1, SizeStrictness _sizeType = SizeStrictness::Strict);
/// Generate random int64
static std::string randomUniIntHex(u256 _maxVal = 0);
static int randomUniInt();
@ -83,6 +86,7 @@ private:
static std::string fillArguments(dev::eth::Instruction _opcode, RandomCodeOptions const& _options);
static std::string getPushCode(int _value);
static std::string getPushCode(std::string const& _hex);
static int recursiveRLP(std::string &result, int depth);
static void refreshSeed();
static boost::random::mt19937 gen; ///< Random generator

Loading…
Cancel
Save