Skip to content

Commit

Permalink
#142: added unit test
Browse files Browse the repository at this point in the history
- new unit test to evaluate handling of invalid receive / save date .
  • Loading branch information
jrse committed May 22, 2018
1 parent 946c62a commit 938cffd
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 21 deletions.
28 changes: 20 additions & 8 deletions src/librmb/rados-mail-object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ std::string RadosMailObject::to_string(const string &padding) {
string from_envelope = get_metadata(RBOX_METADATA_FROM_ENVELOPE);

time_t ts = -1;

if (!recv_time_str.empty()) {
ts = static_cast<time_t>(stol(recv_time_str));
try {
ts = static_cast<time_t>(stol(recv_time_str));
} catch (std::exception &ex) {
ts = -1;
}
}
ostringstream ss;

Expand All @@ -63,13 +66,22 @@ std::string RadosMailObject::to_string(const string &padding) {
ss << padding << " "
<< "oid = " << oid << endl;
string recv_time;
RadosUtils::convert_time_t_to_str(ts, &recv_time);
ss << padding << " " << static_cast<char>(RBOX_METADATA_RECEIVED_TIME) << "(receive_time)=" << recv_time
<< "\n";
if (RadosUtils::convert_time_t_to_str(ts, &recv_time) >= 0) {
ss << padding << " " << static_cast<char>(RBOX_METADATA_RECEIVED_TIME) << "(receive_time)=" << recv_time
<< "\n";
} else {
ss << padding << " " << static_cast<char>(RBOX_METADATA_RECEIVED_TIME) << "(receive_time)= INVALID DATE : '"
<< recv_time_str << "'"
<< "\n";
}
string save_time;
RadosUtils::convert_time_t_to_str(save_date_rados, &save_time);
ss << padding << " "
<< "save_time=" << save_time << "\n";
if (RadosUtils::convert_time_t_to_str(save_date_rados, &save_time) >= 0) {
ss << padding << " "
<< "save_time=" << save_time << "\n";
} else {
ss << padding << " "
<< "save_time= INVALID DATE '" << save_date_rados << "'\n";
}
ss << padding << " " << static_cast<char>(RBOX_METADATA_PHYSICAL_SIZE) << "(phy_size)=" << p_size << " "
<< static_cast<char>(RBOX_METADATA_VIRTUAL_SIZE) << "(v_size) = " << v_size << " stat_size=" << object_size
<< endl;
Expand Down
4 changes: 4 additions & 0 deletions src/librmb/rados-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ bool RadosUtils::is_date_attribute(const rbox_metadata_key &key) {

int RadosUtils::convert_time_t_to_str(const time_t &t, std::string *ret_val) {
char buffer[256];
if (t == -1) {
*ret_val = "invalid date";
return -1;
}
struct tm timeinfo;
localtime_r(&t, &timeinfo);
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &timeinfo);
Expand Down
108 changes: 95 additions & 13 deletions src/tests/rmb/test_rmb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,6 @@ TEST(rmb1, rmb_command_filter_result) {
obj1.set_oid("oid_1");
obj1.set_mail_size(200);
obj1.set_rados_save_date(time(NULL));
/*std::map<std::string, ceph::bufferlist> *metadata = obj1.get_metadata();
(*metadata)[std::string("I")] = librados::bufferlist("V0.1"); // Version
(*metadata)["G"] = librados::bufferlist("8eed840764b05359f12718004d2485ee"); // Mail guid
(*metadata)["R"] = librados::bufferlist("12345627"); // Receive date
(*metadata)["S"] = librados::bufferlist("22222222"); // save date
(*metadata)["P"] = librados::bufferlist("1"); // pop3 uidl
(*metadata)["O"] = librados::bufferlist("0"); // Pop3 order
(*metadata)["M"] = librados::bufferlist("9999840764b05359f12718004d2485ee"); // Mailbox GUID
(*metadata)["Z"] = librados::bufferlist("200"); // Physical Size
(*metadata)["V"] = librados::bufferlist("250"); // virtual size
(*metadata)["U"] = librados::bufferlist("1"); // Mail uid
(*metadata)["A"] = librados::bufferlist(""); // From Envelope
(*metadata)[std::string("F")] = librados::bufferlist("01"); // Mail Flags*/
{
std::string key = "M";
std::string val = "8eed840764b05359f12718004d2485ee";
Expand Down Expand Up @@ -250,6 +237,101 @@ TEST(rmb1, rmb_command_filter_result) {
EXPECT_EQ(0, rmb_cmd.query_mail_storage(&mails, &parser, false));
}

TEST(rmb1, rmb_command_filter_result2) {
librmbtest::RadosStorageMock storage_mock;
librmbtest::RadosClusterMock cluster_mock;
std::map<std::string, std::string> opts;
opts["ls"] = "-";
librmb::CmdLineParser parser(opts["ls"]);
librmb::RmbCommands rmb_cmd(&storage_mock, &cluster_mock, &opts);
std::vector<librmb::RadosMailObject *> mails;
librmb::RadosMailObject obj1;
obj1.set_oid("oid_1");
obj1.set_mail_size(200);
obj1.set_rados_save_date(time(NULL));
{
std::string key = "M";
std::string val = "8eed840764b05359f12718004d2485ee";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
{
std::string key = "I";
std::string val = "v0.1";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
{
std::string key = "G";
std::string val = "8eed840764b05359f12718004d2485ee";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
{
std::string key = "R";
std::string val = "aafsadfasdfasdf";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
{
std::string key = "S";
std::string val = "adfhasdfhsfdkahsdf";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
{
std::string key = "P";
std::string val = "1";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
{
std::string key = "O";
std::string val = "0";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
{
std::string key = "Z";
std::string val = "";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
{
std::string key = "V";
std::string val = "250";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
{
std::string key = "U";
std::string val = "1";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
{
std::string key = "A";
std::string val = "";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
{
std::string key = "F";
std::string val = "01";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
{
std::string key = "B";
std::string val = "DRAFTS";
librmb::RadosMetadata m(key, val);
obj1.add_metadata(m);
}
mails.push_back(&obj1);

EXPECT_EQ(0, rmb_cmd.query_mail_storage(&mails, &parser, false));
}

int main(int argc, char **argv) {
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
Expand Down

0 comments on commit 938cffd

Please sign in to comment.