int main(int, char*[]) { #if !defined(BOOST_UUID_TEST_RANDOM_MOCK) // Positive Testing boost::uuids::detail::random_provider provider; boost::array<unsigned int, 2> ints; // test generator() ints[0] = 0; ints[1] = 0; provider.generate(ints.begin(), ints.end()); BOOST_TEST_NE(ints[0], ints[1]); // test name() BOOST_TEST_GT(strlen(provider.name()), 4u); // test get_random_bytes() char buf1[64]; char buf2[64]; provider.get_random_bytes(buf1, 64); provider.get_random_bytes(buf2, 64); BOOST_TEST_NE(0, memcmp(buf1, buf2, 64)); #else // Negative Testing if (expectations_capable()) { // Test fail acquiring context if the provider supports it if (provider_acquires_context()) { expect_next_call_success(false); BOOST_TEST_THROWS(boost::uuids::detail::random_provider(), boost::uuids::entropy_error); BOOST_TEST(expectations_met()); } // Test fail acquiring entropy if (provider_acquires_context()) { expect_next_call_success(true); } expect_next_call_success(false); // 4 is important for the posix negative test (partial read) to work properly // as it sees a size of 4, returns 1, causing a 2nd loop to read 3 more bytes... char buf[4]; BOOST_TEST_THROWS(boost::uuids::detail::random_provider().get_random_bytes(buf, 4), boost::uuids::entropy_error); BOOST_TEST(expectations_met()); } #endif return boost::report_errors(); }
static void test_polymorphic_pointer_cast_fail() { Base * base = new Base; BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( base ), std::bad_cast ); delete base; BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( boost::shared_ptr<Base>(new Base) ), std::bad_cast ); #ifndef BOOST_NO_CXX11_SMART_PTR BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( std::shared_ptr<Base>(new Base) ), std::bad_cast ); #endif BOOST_TEST_THROWS( boost::polymorphic_pointer_cast<Derived>( boost::intrusive_ptr<Base>(new Base) ), std::bad_cast ); }
static void test_polymorphic_cast_fail() { Base * base = new Base; BOOST_TEST_THROWS( boost::polymorphic_cast<Derived*>( base ), std::bad_cast ); delete base; }
static void test_polymorphic_pointer_downcast_intrusive_fail() { boost::intrusive_ptr<Base> base (new Base); int old_count = assertion_failed_count; expect_assertion = true; BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion); // should assert BOOST_TEST_EQ( assertion_failed_count, old_count + 1 ); expect_assertion = false; }
int main() { int x = 0; // BOOST_TEST BOOST_TEST( x == 0 ); BOOST_TEST( ++x == 1 ); BOOST_TEST( x++ == 1 ); BOOST_TEST( x == 2? true: false ); BOOST_TEST( x == 2? &x: 0 ); // BOOST_TEST_EQ BOOST_TEST_EQ( x, 2 ); BOOST_TEST_EQ( ++x, 3 ); BOOST_TEST_EQ( x++, 3 ); int y = 4; BOOST_TEST_EQ( ++x, ++y ); BOOST_TEST_EQ( x++, y++ ); // BOOST_TEST_NE BOOST_TEST_NE( ++x, y ); BOOST_TEST_NE( &x, &y ); // BOOST_TEST_THROWS BOOST_TEST_THROWS( throw X(), X ); BOOST_TEST_THROWS( throw 1, int ); BOOST_TEST_THROWS( f(true), X ); BOOST_TEST_THROWS( f(false), int ); return boost::report_errors(); }
static void test_polymorphic_pointer_downcast_builtin_fail() { Base * base = new Base; int old_count = assertion_failed_count; expect_assertion = true; BOOST_TEST_THROWS( boost::polymorphic_pointer_downcast<Derived>( base ), expected_assertion ); // should assert BOOST_TEST_EQ( assertion_failed_count, old_count + 1 ); expect_assertion = false; delete base; }
void TestRegionList() { using RegionListIterCat = std::iterator_traits<hadesmem::RegionList::iterator>::iterator_category; HADESMEM_DETAIL_STATIC_ASSERT( std::is_base_of<std::input_iterator_tag, RegionListIterCat>::value); using RegionListConstIterCat = std::iterator_traits< hadesmem::RegionList::const_iterator>::iterator_category; HADESMEM_DETAIL_STATIC_ASSERT( std::is_base_of<std::input_iterator_tag, RegionListConstIterCat>::value); hadesmem::Process const process(::GetCurrentProcessId()); hadesmem::RegionList const region_list_1(process); hadesmem::RegionList region_list_2(region_list_1); hadesmem::RegionList region_list_3(std::move(region_list_2)); region_list_2 = std::move(region_list_3); BOOST_TEST(std::begin(region_list_2) != std::end(region_list_2)); auto iter = std::begin(region_list_1); hadesmem::Region const first_region(process, nullptr); BOOST_TEST(iter != std::end(region_list_1)); BOOST_TEST_EQ(*iter, first_region); hadesmem::Region const second_region( process, static_cast<char const* const>(first_region.GetBase()) + first_region.GetSize()); BOOST_TEST(++iter != std::end(region_list_1)); BOOST_TEST_EQ(*iter, second_region); hadesmem::Region last(process, nullptr); do { hadesmem::Region current = *iter; BOOST_TEST(current > last); BOOST_TEST(current >= last); BOOST_TEST(last < current); BOOST_TEST(last <= current); last = current; } while (++iter != std::end(region_list_1)); BOOST_TEST_THROWS( hadesmem::Region( process, static_cast<char const* const>(last.GetBase()) + last.GetSize()), hadesmem::Error); }
void TestThisThread() { hadesmem::Thread thread(::GetCurrentThreadId()); BOOST_TEST_EQ(thread, thread); hadesmem::Thread thread_new(::GetCurrentThreadId()); BOOST_TEST_EQ(thread, thread_new); hadesmem::Thread const thread_moved(std::move(thread_new)); hadesmem::Thread thread_copy(thread); BOOST_TEST_EQ(thread_copy, thread); thread = thread_copy; BOOST_TEST_EQ(thread, thread_copy); thread_new = std::move(thread_copy); BOOST_TEST_EQ(thread, thread_new); BOOST_TEST(thread >= thread); BOOST_TEST(thread <= thread); BOOST_TEST_EQ(thread.GetId(), ::GetCurrentThreadId()); std::stringstream thread_str_1; thread_str_1.imbue(std::locale::classic()); thread_str_1 << thread; DWORD thread_id_1; thread_str_1 >> thread_id_1; BOOST_TEST_EQ(thread.GetId(), thread_id_1); std::wstringstream thread_str_2; thread_str_2.imbue(std::locale::classic()); thread_str_2 << thread; DWORD thread_id_2; thread_str_2 >> thread_id_2; BOOST_TEST_EQ(thread.GetId(), thread_id_2); hadesmem::detail::SmartHandle quit_event( ::CreateEvent(nullptr, TRUE, FALSE, nullptr)); DWORD other_id = 0; hadesmem::detail::SmartHandle wait_thread( ::CreateThread(nullptr, 0, &WaitFunc, &quit_event, 0, &other_id)); // Disgusting hack to work around a potential race condition where we suspend // the thread before it's actually waiting on our event, and instead it's // still in its initialization routine and ends up holding a lock that the // main thread will need (e.g. from malloc). See thread_140224_144550.dmp. ::Sleep(5 * 1000); auto const cleanup_thread_func = [&]() { BOOST_TEST_NE(::SetEvent(quit_event.GetHandle()), 0); BOOST_TEST_EQ(::WaitForSingleObject(wait_thread.GetHandle(), INFINITE), WAIT_OBJECT_0); }; { ScopeExit<decltype(cleanup_thread_func)> cleanup_thread( cleanup_thread_func); hadesmem::Thread const other_thread(other_id); BOOST_TEST_EQ(hadesmem::SuspendThread(other_thread), 0UL); BOOST_TEST_EQ(hadesmem::SuspendThread(other_thread), 1UL); BOOST_TEST_EQ(hadesmem::ResumeThread(other_thread), 2UL); BOOST_TEST_EQ(hadesmem::ResumeThread(other_thread), 1UL); { hadesmem::SuspendedThread const suspend_thread(other_thread.GetId()); CONTEXT const full_context = hadesmem::GetThreadContext(other_thread, CONTEXT_FULL); CONTEXT const all_context = hadesmem::GetThreadContext(other_thread, CONTEXT_ALL); hadesmem::SetThreadContext(other_thread, full_context); hadesmem::SetThreadContext(other_thread, all_context); BOOST_TEST_THROWS(hadesmem::GetThreadContext(thread, CONTEXT_FULL), hadesmem::Error); } { hadesmem::SuspendedProcess const suspend_process(::GetCurrentProcessId()); } } }