gparted/tests/test_PipeCapture.cc

290 lines
9.1 KiB
C++
Raw Normal View History

/* Copyright (C) 2017 Mike Fleetwood
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/* Test PipeCapture
*
* All the tests work by creating a pipe(3) and using a separate thread to write data into
* the pipe with PipeCapture running in the initial thread. Captured data is then checked
* that it matches the input.
*/
#include "PipeCapture.h"
#include "gtest/gtest.h"
#include <stddef.h>
#include <stdio.h>
Add binary data string difference reporting to PipeCapture tests (#777973) Google Test string comparison asserts are only designed of C style strings containing printable text of one or more lines with a terminating NUL character. GParted is crashing when PipeCapture is reading the binary file names being reported by fsck.fat from a very corrupted FAT file system. Therefore need to be able to compare and report differences of binary data stored in C++ std::string and Glib::ustrings. Write a specific assertion to handle this. Now these sample tests: TEST_F( PipeCaptureTest, BinaryStringFailure ) { inputstr = "AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCC"; capturedstr = "AAAAAAAAAAAAAAAABBBBBBBBBBbbbb"; EXPECT_BINARYSTRINGEQ( inputstr, capturedstr.raw() ); } TEST_F( PipeCaptureTest, LeadingBinaryStringFailure ) { inputstr = "The quick brown fox jumps over the lazy dog"; capturedstr = "The quick brown fox\n"; EXPECT_BINARYSTRINGEQ( inputstr.substr( 0, capturedstr.raw().length() ), capturedstr.raw() ); } report failure like this: $ ./test_PipeCapture ... [ RUN ] PipeCaptureTest.BinaryStringFailure test_PipeCapture.cc:270: Failure Expected: inputstr Of length: 48 To be equal to: capturedstr.raw() Of length: 30 With first binary difference: < 0x00000010 "BBBBBBBBBBBBBBBB" 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 -- > 0x00000010 "BBBBBBBBBBbbbb" 42 42 42 42 42 42 42 42 42 42 62 62 62 62 [ FAILED ] PipeCaptureTest.BinaryStringFailure (1 ms) [ RUN ] PipeCaptureTest.LeadingBinaryStringFailure test_PipeCapture.cc:278: Failure Expected: inputstr.substr( 0, capturedstr.raw().length() ) Of length: 20 To be equal to: capturedstr.raw() Of length: 20 With first binary difference: < 0x00000010 "fox " 66 6F 78 20 -- > 0x00000010 "fox." 66 6F 78 0A [ FAILED ] PipeCaptureTest.LeadingBinaryStringFailure (0 ms) ... Bug 777973 - Segmentation fault on bad disk
2017-05-23 09:35:51 -06:00
#include <sstream>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <string>
#include <sigc++/sigc++.h>
#include <glib.h>
#include <glibmm.h>
namespace GParted
{
// Repeat a C++ string count times, where count >= 0.
static std::string repeat( const std::string & str, size_t count )
{
std::string result = "";
while ( count -- > 0 )
result += str;
return result;
}
Add binary data string difference reporting to PipeCapture tests (#777973) Google Test string comparison asserts are only designed of C style strings containing printable text of one or more lines with a terminating NUL character. GParted is crashing when PipeCapture is reading the binary file names being reported by fsck.fat from a very corrupted FAT file system. Therefore need to be able to compare and report differences of binary data stored in C++ std::string and Glib::ustrings. Write a specific assertion to handle this. Now these sample tests: TEST_F( PipeCaptureTest, BinaryStringFailure ) { inputstr = "AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCC"; capturedstr = "AAAAAAAAAAAAAAAABBBBBBBBBBbbbb"; EXPECT_BINARYSTRINGEQ( inputstr, capturedstr.raw() ); } TEST_F( PipeCaptureTest, LeadingBinaryStringFailure ) { inputstr = "The quick brown fox jumps over the lazy dog"; capturedstr = "The quick brown fox\n"; EXPECT_BINARYSTRINGEQ( inputstr.substr( 0, capturedstr.raw().length() ), capturedstr.raw() ); } report failure like this: $ ./test_PipeCapture ... [ RUN ] PipeCaptureTest.BinaryStringFailure test_PipeCapture.cc:270: Failure Expected: inputstr Of length: 48 To be equal to: capturedstr.raw() Of length: 30 With first binary difference: < 0x00000010 "BBBBBBBBBBBBBBBB" 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 -- > 0x00000010 "BBBBBBBBBBbbbb" 42 42 42 42 42 42 42 42 42 42 62 62 62 62 [ FAILED ] PipeCaptureTest.BinaryStringFailure (1 ms) [ RUN ] PipeCaptureTest.LeadingBinaryStringFailure test_PipeCapture.cc:278: Failure Expected: inputstr.substr( 0, capturedstr.raw().length() ) Of length: 20 To be equal to: capturedstr.raw() Of length: 20 With first binary difference: < 0x00000010 "fox " 66 6F 78 20 -- > 0x00000010 "fox." 66 6F 78 0A [ FAILED ] PipeCaptureTest.LeadingBinaryStringFailure (0 ms) ... Bug 777973 - Segmentation fault on bad disk
2017-05-23 09:35:51 -06:00
// Number of bytes of binary data to compare and report.
const size_t BinaryStringDiffSize = 16;
// Format up to 16 bytes of binary data ready for printing as:
// Hex offset ASCII text Hex bytes
// "0x000000000 \"ABCDEFGHabcdefgh\" 41 42 43 44 45 46 47 48 61 62 63 64 65 66 67 68"
std::string BinaryStringToPrint( size_t offset, const char * s, size_t len )
{
std::ostringstream result;
result << "0x";
result.fill( '0' );
result << std::setw( 8 ) << std::hex << std::uppercase << offset << " \"";
size_t i;
for ( i = 0 ; i < BinaryStringDiffSize && i < len ; i ++ )
result.put( ( isprint( s[i] ) ) ? s[i] : '.' );
result.put( '\"' );
if ( len > 0 )
{
for ( ; i < BinaryStringDiffSize ; i ++ )
result.put( ' ' );
result.put( ' ' );
for ( i = 0 ; i < BinaryStringDiffSize && i < len ; i ++ )
result << " "
<< std::setw( 2 ) << std::hex << std::uppercase
<< (unsigned int)(unsigned char)s[i];
}
return result.str();
}
// Helper to construct and return message for equality assertion of C++ strings containing
// binary data used in:
// EXPECT_BINARYSTRINGEQ( str1, str2 )
::testing::AssertionResult CompareHelperBinaryStringEQ( const char * lhs_expr, const char * rhs_expr,
const std::string & lhs, const std::string & rhs )
{
// Loop comparing binary data in 16 byte amounts, stopping and reporting the first
// difference encountered.
bool diff = false;
const char * p1 = lhs.data();
const char * p2 = rhs.data();
size_t len1 = lhs.length();
size_t len2 = rhs.length();
while ( len1 > 0 || len2 > 0 )
{
size_t cmp_span = BinaryStringDiffSize;
cmp_span = ( len1 < cmp_span ) ? len1 : cmp_span;
cmp_span = ( len2 < cmp_span ) ? len2 : cmp_span;
if ( cmp_span < BinaryStringDiffSize && len1 != len2 )
{
diff = true;
break;
}
if ( memcmp( p1, p2, cmp_span ) != 0 )
{
diff = true;
break;
}
p1 += cmp_span;
p2 += cmp_span;
len1 -= cmp_span;
len2 -= cmp_span;
}
if ( ! diff )
return ::testing::AssertionSuccess();
else
{
size_t offset = p1 - lhs.data();
return ::testing::AssertionFailure()
<< " Expected: " << lhs_expr << "\n"
<< " Of length: " << lhs.length() << "\n"
<< "To be equal to: " << rhs_expr << "\n"
<< " Of length: " << rhs.length() << "\n"
<< "With first binary difference:\n"
<< "< " << BinaryStringToPrint( offset, p1, len1 ) << "\n"
<< "--\n"
<< "> " << BinaryStringToPrint( offset, p2, len2 );
}
}
// Nonfatal assertion that binary data in C++ strings are equal.
#define EXPECT_BINARYSTRINGEQ(str1, str2) \
EXPECT_PRED_FORMAT2(CompareHelperBinaryStringEQ, str1, str2)
// Explicit test fixture class with common variables and methods used in each test.
// Reference:
// Google Test, Primer, Test Fixtures: Using the Same Data Configuration for Multiple Tests
class PipeCaptureTest : public ::testing::Test
{
protected:
PipeCaptureTest() : eof_signalled( false ) {};
virtual void SetUp();
virtual void TearDown();
static gboolean main_loop_quit( gpointer data );
void writer_thread( const std::string & str );
void run_writer_thread();
static const size_t ReaderFD = 0;
static const size_t WriterFD = 1;
std::string inputstr;
Glib::ustring capturedstr;
bool eof_signalled;
int pipefds[2];
Glib::RefPtr<Glib::MainLoop> glib_main_loop;
public:
void eof_callback() { eof_signalled = true; };
};
// Further setup PipeCaptureTest fixture before running each test. Create pipe and Glib
// main loop object.
void PipeCaptureTest::SetUp()
{
ASSERT_TRUE( pipe( pipefds ) == 0 ) << "Failed to create pipe. errno="
<< errno << "," << strerror( errno );
glib_main_loop = Glib::MainLoop::create();
}
// Tear down fixture after running each test. Close reading end of the pipe. Also
// re-closed the writing end of the pipe, just in case something went wrong in the test.
void PipeCaptureTest::TearDown()
{
ASSERT_TRUE( close( pipefds[ReaderFD] ) == 0 ) << "Failed to close reading end of pipe. errno="
<< errno << "," << strerror( errno );
close( pipefds[WriterFD] );
}
// Callback used to end the currently running Glib main loop.
gboolean PipeCaptureTest::main_loop_quit( gpointer data )
{
static_cast<PipeCaptureTest *>( data )->glib_main_loop->quit();
return false; // One shot g_idle_add() callback
}
// Write the string into the pipe and close the pipe for writing. Registers callback to
// end the currently running Glib main loop.
void PipeCaptureTest::writer_thread( const std::string & str )
{
const size_t BlockSize = 4096;
const char * writebuf = str.data();
size_t remaining_size = str.length();
while ( remaining_size > 0 )
{
size_t write_size = ( remaining_size > BlockSize ) ? BlockSize : remaining_size;
ssize_t written = write( pipefds[WriterFD], writebuf, write_size );
if ( written <= 0 )
{
ADD_FAILURE() << __func__ << "(): Failed to write to pipe. errno="
<< errno << "," << strerror( errno );
break;
}
remaining_size -= written;
writebuf += written;
}
ASSERT_TRUE( close( pipefds[WriterFD] ) == 0 ) << "Failed to close writing end of pipe. errno="
<< errno << "," << strerror( errno );
g_idle_add( main_loop_quit, this );
}
// Create writer thread and run the Glib main loop.
void PipeCaptureTest::run_writer_thread()
{
Glib::Thread::create( sigc::bind( sigc::mem_fun( *this, &PipeCaptureTest::writer_thread ),
inputstr ),
false );
glib_main_loop->run();
}
TEST_F( PipeCaptureTest, EmptyPipe )
{
// Test capturing 0 bytes with no on EOF callback registered.
inputstr = "";
PipeCapture pc( pipefds[ReaderFD], capturedstr );
pc.connect_signal();
run_writer_thread();
Add binary data string difference reporting to PipeCapture tests (#777973) Google Test string comparison asserts are only designed of C style strings containing printable text of one or more lines with a terminating NUL character. GParted is crashing when PipeCapture is reading the binary file names being reported by fsck.fat from a very corrupted FAT file system. Therefore need to be able to compare and report differences of binary data stored in C++ std::string and Glib::ustrings. Write a specific assertion to handle this. Now these sample tests: TEST_F( PipeCaptureTest, BinaryStringFailure ) { inputstr = "AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCC"; capturedstr = "AAAAAAAAAAAAAAAABBBBBBBBBBbbbb"; EXPECT_BINARYSTRINGEQ( inputstr, capturedstr.raw() ); } TEST_F( PipeCaptureTest, LeadingBinaryStringFailure ) { inputstr = "The quick brown fox jumps over the lazy dog"; capturedstr = "The quick brown fox\n"; EXPECT_BINARYSTRINGEQ( inputstr.substr( 0, capturedstr.raw().length() ), capturedstr.raw() ); } report failure like this: $ ./test_PipeCapture ... [ RUN ] PipeCaptureTest.BinaryStringFailure test_PipeCapture.cc:270: Failure Expected: inputstr Of length: 48 To be equal to: capturedstr.raw() Of length: 30 With first binary difference: < 0x00000010 "BBBBBBBBBBBBBBBB" 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 -- > 0x00000010 "BBBBBBBBBBbbbb" 42 42 42 42 42 42 42 42 42 42 62 62 62 62 [ FAILED ] PipeCaptureTest.BinaryStringFailure (1 ms) [ RUN ] PipeCaptureTest.LeadingBinaryStringFailure test_PipeCapture.cc:278: Failure Expected: inputstr.substr( 0, capturedstr.raw().length() ) Of length: 20 To be equal to: capturedstr.raw() Of length: 20 With first binary difference: < 0x00000010 "fox " 66 6F 78 20 -- > 0x00000010 "fox." 66 6F 78 0A [ FAILED ] PipeCaptureTest.LeadingBinaryStringFailure (0 ms) ... Bug 777973 - Segmentation fault on bad disk
2017-05-23 09:35:51 -06:00
EXPECT_BINARYSTRINGEQ( inputstr, capturedstr.raw() );
EXPECT_FALSE( eof_signalled );
}
TEST_F( PipeCaptureTest, EmptyPipeWithEOF )
{
// Test capturing 0 bytes and registered on EOF callback occurs.
inputstr = "";
PipeCapture pc( pipefds[ReaderFD], capturedstr );
pc.signal_eof.connect( sigc::mem_fun( *this, &PipeCaptureTest::eof_callback ) );
pc.connect_signal();
run_writer_thread();
Add binary data string difference reporting to PipeCapture tests (#777973) Google Test string comparison asserts are only designed of C style strings containing printable text of one or more lines with a terminating NUL character. GParted is crashing when PipeCapture is reading the binary file names being reported by fsck.fat from a very corrupted FAT file system. Therefore need to be able to compare and report differences of binary data stored in C++ std::string and Glib::ustrings. Write a specific assertion to handle this. Now these sample tests: TEST_F( PipeCaptureTest, BinaryStringFailure ) { inputstr = "AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCC"; capturedstr = "AAAAAAAAAAAAAAAABBBBBBBBBBbbbb"; EXPECT_BINARYSTRINGEQ( inputstr, capturedstr.raw() ); } TEST_F( PipeCaptureTest, LeadingBinaryStringFailure ) { inputstr = "The quick brown fox jumps over the lazy dog"; capturedstr = "The quick brown fox\n"; EXPECT_BINARYSTRINGEQ( inputstr.substr( 0, capturedstr.raw().length() ), capturedstr.raw() ); } report failure like this: $ ./test_PipeCapture ... [ RUN ] PipeCaptureTest.BinaryStringFailure test_PipeCapture.cc:270: Failure Expected: inputstr Of length: 48 To be equal to: capturedstr.raw() Of length: 30 With first binary difference: < 0x00000010 "BBBBBBBBBBBBBBBB" 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 -- > 0x00000010 "BBBBBBBBBBbbbb" 42 42 42 42 42 42 42 42 42 42 62 62 62 62 [ FAILED ] PipeCaptureTest.BinaryStringFailure (1 ms) [ RUN ] PipeCaptureTest.LeadingBinaryStringFailure test_PipeCapture.cc:278: Failure Expected: inputstr.substr( 0, capturedstr.raw().length() ) Of length: 20 To be equal to: capturedstr.raw() Of length: 20 With first binary difference: < 0x00000010 "fox " 66 6F 78 20 -- > 0x00000010 "fox." 66 6F 78 0A [ FAILED ] PipeCaptureTest.LeadingBinaryStringFailure (0 ms) ... Bug 777973 - Segmentation fault on bad disk
2017-05-23 09:35:51 -06:00
EXPECT_BINARYSTRINGEQ( inputstr, capturedstr.raw() );
EXPECT_TRUE( eof_signalled );
}
TEST_F( PipeCaptureTest, ShortASCIIText )
{
// Test capturing small amount of ASCII text.
inputstr = "The quick brown fox jumps over the lazy dog";
PipeCapture pc( pipefds[ReaderFD], capturedstr );
pc.signal_eof.connect( sigc::mem_fun( *this, &PipeCaptureTest::eof_callback ) );
pc.connect_signal();
run_writer_thread();
Add binary data string difference reporting to PipeCapture tests (#777973) Google Test string comparison asserts are only designed of C style strings containing printable text of one or more lines with a terminating NUL character. GParted is crashing when PipeCapture is reading the binary file names being reported by fsck.fat from a very corrupted FAT file system. Therefore need to be able to compare and report differences of binary data stored in C++ std::string and Glib::ustrings. Write a specific assertion to handle this. Now these sample tests: TEST_F( PipeCaptureTest, BinaryStringFailure ) { inputstr = "AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCC"; capturedstr = "AAAAAAAAAAAAAAAABBBBBBBBBBbbbb"; EXPECT_BINARYSTRINGEQ( inputstr, capturedstr.raw() ); } TEST_F( PipeCaptureTest, LeadingBinaryStringFailure ) { inputstr = "The quick brown fox jumps over the lazy dog"; capturedstr = "The quick brown fox\n"; EXPECT_BINARYSTRINGEQ( inputstr.substr( 0, capturedstr.raw().length() ), capturedstr.raw() ); } report failure like this: $ ./test_PipeCapture ... [ RUN ] PipeCaptureTest.BinaryStringFailure test_PipeCapture.cc:270: Failure Expected: inputstr Of length: 48 To be equal to: capturedstr.raw() Of length: 30 With first binary difference: < 0x00000010 "BBBBBBBBBBBBBBBB" 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 -- > 0x00000010 "BBBBBBBBBBbbbb" 42 42 42 42 42 42 42 42 42 42 62 62 62 62 [ FAILED ] PipeCaptureTest.BinaryStringFailure (1 ms) [ RUN ] PipeCaptureTest.LeadingBinaryStringFailure test_PipeCapture.cc:278: Failure Expected: inputstr.substr( 0, capturedstr.raw().length() ) Of length: 20 To be equal to: capturedstr.raw() Of length: 20 With first binary difference: < 0x00000010 "fox " 66 6F 78 20 -- > 0x00000010 "fox." 66 6F 78 0A [ FAILED ] PipeCaptureTest.LeadingBinaryStringFailure (0 ms) ... Bug 777973 - Segmentation fault on bad disk
2017-05-23 09:35:51 -06:00
EXPECT_BINARYSTRINGEQ( inputstr, capturedstr.raw() );
EXPECT_TRUE( eof_signalled );
}
TEST_F( PipeCaptureTest, LongASCIIText )
{
// Test capturing 1 MiB of ASCII text (requiring multiple reads in PipeCapture).
inputstr = repeat( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_\n", 16384 );
PipeCapture pc( pipefds[ReaderFD], capturedstr );
pc.signal_eof.connect( sigc::mem_fun( *this, &PipeCaptureTest::eof_callback ) );
pc.connect_signal();
run_writer_thread();
Add binary data string difference reporting to PipeCapture tests (#777973) Google Test string comparison asserts are only designed of C style strings containing printable text of one or more lines with a terminating NUL character. GParted is crashing when PipeCapture is reading the binary file names being reported by fsck.fat from a very corrupted FAT file system. Therefore need to be able to compare and report differences of binary data stored in C++ std::string and Glib::ustrings. Write a specific assertion to handle this. Now these sample tests: TEST_F( PipeCaptureTest, BinaryStringFailure ) { inputstr = "AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCC"; capturedstr = "AAAAAAAAAAAAAAAABBBBBBBBBBbbbb"; EXPECT_BINARYSTRINGEQ( inputstr, capturedstr.raw() ); } TEST_F( PipeCaptureTest, LeadingBinaryStringFailure ) { inputstr = "The quick brown fox jumps over the lazy dog"; capturedstr = "The quick brown fox\n"; EXPECT_BINARYSTRINGEQ( inputstr.substr( 0, capturedstr.raw().length() ), capturedstr.raw() ); } report failure like this: $ ./test_PipeCapture ... [ RUN ] PipeCaptureTest.BinaryStringFailure test_PipeCapture.cc:270: Failure Expected: inputstr Of length: 48 To be equal to: capturedstr.raw() Of length: 30 With first binary difference: < 0x00000010 "BBBBBBBBBBBBBBBB" 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 -- > 0x00000010 "BBBBBBBBBBbbbb" 42 42 42 42 42 42 42 42 42 42 62 62 62 62 [ FAILED ] PipeCaptureTest.BinaryStringFailure (1 ms) [ RUN ] PipeCaptureTest.LeadingBinaryStringFailure test_PipeCapture.cc:278: Failure Expected: inputstr.substr( 0, capturedstr.raw().length() ) Of length: 20 To be equal to: capturedstr.raw() Of length: 20 With first binary difference: < 0x00000010 "fox " 66 6F 78 20 -- > 0x00000010 "fox." 66 6F 78 0A [ FAILED ] PipeCaptureTest.LeadingBinaryStringFailure (0 ms) ... Bug 777973 - Segmentation fault on bad disk
2017-05-23 09:35:51 -06:00
EXPECT_BINARYSTRINGEQ( inputstr, capturedstr.raw() );
EXPECT_TRUE( eof_signalled );
}
} // namespace GParted
// Custom Google Test main() which also initialises the Glib threading system for
// distributions with glib/glibmm before version 2.32.
// References:
// * Google Test, Primer, Writing the main() Function
// * Deprecated thread API, g_thread_init()
// https://developer.gnome.org/glib/stable/glib-Deprecated-Thread-APIs.html#g-thread-init
int main( int argc, char **argv )
{
printf("Running main() from %s\n", __FILE__ );
testing::InitGoogleTest( &argc, argv );
Glib::thread_init();
return RUN_ALL_TESTS();
}