Возвращаясь к теме про фаззеры.


??? ???, ??????? ? ????????? ??? ????????? ?????? ?????? ? ???????? ? ????? ????????. ????? EntropyGenerator – ??????? ?????? ?????????? ????????? ?????.

class EntropyGenerator
{
public:
    EntropyGenerator():
        m_replay_index(std::numeric_limits<size_t>::max())
    {
        m_log.reserve(1024 * 64);
    }
    
    void
    replay()
    {
        m_replay_index = 0;
    }
    
    size_t
    generate(
        size_t up_to
        )
    {
        size_t r;
        
        if (m_replay_index == std::numeric_limits<size_t>::max())
        {
            if (up_to > 0)
            {
                errno_t err = rand_s(&r);
                BOOST_REQUIRE(err == 0);
                
                r /= std::numeric_limits<size_t>::max() / up_to;
            }
            else
            {
                r = 0;
            }
            
            m_log.push_back(r);
        }
        else
        {
            r = m_log[m_replay_index++];
            
            if (m_replay_index == m_log.size())
            {
                // This is the last generated number
                __debugbreak();
            }
        }
        
        BOOST_REQUIRE((r == 0 && up_to == 0) || (0 <= r && r < up_to));
        return r;
    }
    
protected:
    std::vector<size_t> m_log;
    size_t m_replay_index;
};

EntropyGenerator ???????????? ??? ?????? ??????:

  1. ??? ?????? ??????? ?? ?????????? ????????? ????? ? ???????? ????????? ? ????????? ?????? ??????????????? ????? ? ???;
  2. ??? ?????? ? ????????? ????????, EntropyGenerator ????????????? ? ????? ??????????????? ??????????????? ?????. ??? ?????? ?????????? ???????????????? ????? ??????????? ????? ????????, ???????? ??????????, ??? ?????????? ? ?????? ???? ? ?????????.

??????? runStress ????????? ?????????? ????, ???????? ? ?????? ???? ???????? ???? ??????? ?????? ?????????? ???. ????? ????????? ????? ??????????? ??????????. ? ????????? Boost.Test, ? ??? ??????????? ??????? (BOOST_REQUIRE ? Ko) ??????? ??????????.

template <typename TestT>
runStress(
    TestT* test,
void
    size_t replay_count
    )
{
    EntropyGenerator rnd;
    
    for (size_t i = 0; i <= replay_count; )
    {
        try
        {
            // Run test
            (*test)(rnd);
            return;
        }
        catch (...)
        {
            if (i < replay_count)
            {
                ++i;
                rnd.replay();
            }
            else
                throw;
        }
    }
}

?????? ?????:

// 64 chars
char randomData[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ[]";


void
stressLoadCells(
    EntropyGenerator& rnd
    )
{
    for (size_t repeat = 20; repeat > 0; --repeat)
    {
        //  Fill the source with random data of random length
        Source source;
        source.m_data.assign(rnd.generate(128), ' ');
        
        for (
            std::string::iterator i = source.m_data.begin(), i_end = source.m_data.end();
            i != i_end;
            ++i)
        {
            *i = randomData[rnd.generate(64)];
        }
        
        // Create buffer with a cell of random size
        Buffer buffer(&source, rnd.generate(15) + 1);
        
        for (size_t i = 0; i < 128; ++i)
        {
            size_t j = rnd.generate(static_cast<size_t>(source.size()));
            BOOST_REQUIRE(buffer[j] == source.m_data[j]);
        }
    }
}


void
stressTestLoadCells()
{
    runStress(&stressLoadCells, 1);
}

????? ????? ? ???? – ??????? stressTestLoadCells. ???????? ??? ?????????? ? ??????? stressLoadCells, ??????? ? ?????????? ???? (? ?????? ??????) ??? ????????? (? ?????? ????) ???.

???? ??????? ??? ??????. ??????? ????????? ????????? ????????, ????? ? ???? ????? ???. ??? ??? ????? ???????? ????????? ????????? ????? ?????????? rnd.generate ? ?????? ??????????. ?????? ? ???????? ??????????, ???? ???? ??? ??? ?????? ???????????. ??? ??????????? ????????? ??????? ?????????????????? ????? ??? ????, ????? ?? ????? ?????????????. ??? ??????????? ?????????? ? ???????? ?? ?? ????????? ?????, ? ?? N ??????? rnd.generate.

Cross-posted from blog.not-a-kernel-guy.com.

Skip to main content