Ensure that operator<< correctly prints strings with embedded '\0'

This commit is contained in:
fekir 2017-08-18 18:20:00 +02:00
parent 670d710f62
commit 21ead7c5a4
2 changed files with 25 additions and 1 deletions

View File

@ -116,7 +116,7 @@ int Column::getBytes() const noexcept // nothrow
// Standard std::ostream inserter
std::ostream& operator<<(std::ostream& aStream, const Column& aColumn)
{
aStream << aColumn.getText();
aStream.write(aColumn.getText(), aColumn.getBytes());
return aStream;
}

View File

@ -199,3 +199,27 @@ TEST(Column, getName) {
EXPECT_EQ("msg", oname1);
#endif
}
TEST(Column, stream) {
// Create a new database
SQLite::Database db(":memory:", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE);
EXPECT_EQ(0, db.exec("CREATE TABLE test (msg TEXT)"));
SQLite::Statement insert(db, "INSERT INTO test VALUES (?)");
// content to test
const char str_[] = "stringwith\0embedded";
std::string str(str_, sizeof(str_)-1);
insert.bind(1, str);
// Execute the one-step query to insert the row
EXPECT_EQ(1, insert.exec());
EXPECT_EQ(1, db.getTotalChanges());
SQLite::Statement query(db, "SELECT * FROM test");
query.executeStep();
std::stringstream ss;
auto col = query.getColumn(0);
ss << query.getColumn(0);
std::string content = ss.str();
EXPECT_EQ(content, str);
}