From 0a4e9efe53182c010f7a9c2a8ae6775c54ee767c Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Tue, 1 Jan 2013 21:07:07 -0700 Subject: [PATCH] Fix various issues in unit tests * Define MemObject stub constructor to initialize teh stub object properly apparently store unit tests needs one defined. Best to make it work and set base values than leave garbage in the object fields. * Buffer overrun on config parser if test is ever given a too-long string input. * Memory leak in HttpRequest testing. One instance of a short array. * Range: header testing may thor exceptions which were not caught by the test binary. Could lead to difficulty debugging exception errors. Detected by Coverity Scan. Issues 740523, 740482, 740440, 740498 --- src/tests/stub_MemObject.cc | 20 +++++++++++++++++++- src/tests/testConfigParser.cc | 4 +++- src/tests/testHttpRequest.cc | 1 + src/tests/test_http_range.cc | 30 +++++++++++++++++++----------- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/tests/stub_MemObject.cc b/src/tests/stub_MemObject.cc index 07cfc67a22..309e8cd7ff 100644 --- a/src/tests/stub_MemObject.cc +++ b/src/tests/stub_MemObject.cc @@ -21,7 +21,25 @@ MemObject::endOffset() const void MemObject::trimSwappable() STUB void MemObject::trimUnSwappable() STUB int64_t MemObject::policyLowestOffsetToKeep(bool swap) const STUB_RETVAL(-1) -MemObject::MemObject(char const *, char const *) {} // NOP due to Store +MemObject::MemObject(char const *, char const *) : + url(NULL), + inmem_lo(0), + nclients(0), + request(NULL), + ping_reply_callback(NULL), + ircb_data(NULL), + log_url(NULL), + id(0), + object_sz(-1), + swap_hdr_sz(0), + vary_headers(NULL), + _reply(NULL) +{ + memset(&clients, 0, sizeof(clients)); + memset(&start_ping, 0, sizeof(start_ping)); + memset(&abort, 0, sizeof(abort)); +} // NOP instead of elided due to Store + HttpReply const * MemObject::getReply() const { // XXX: required by testStore diff --git a/src/tests/testConfigParser.cc b/src/tests/testConfigParser.cc index a3a58fe251..2ac31e67d1 100644 --- a/src/tests/testConfigParser.cc +++ b/src/tests/testConfigParser.cc @@ -30,8 +30,10 @@ bool testConfigParser::doParseQuotedTest(const char *s, const char *expectInterp fprintf(stderr, "Invalid config line: %s\n", s); return false; } + // Keep the initial value on cfgparam. The ConfigParser methods will write on cfgline - strcpy(cfgparam, tmp+1); + strncpy(cfgparam, tmp+1, sizeof(cfgparam)-1); + cfgparam[sizeof(cfgparam)-1] = '\0'; // Initialize parser to point to the start of quoted string strtok(cfgline, w_space); diff --git a/src/tests/testHttpRequest.cc b/src/tests/testHttpRequest.cc index d7e32398f2..414dcc9b0c 100644 --- a/src/tests/testHttpRequest.cc +++ b/src/tests/testHttpRequest.cc @@ -57,6 +57,7 @@ testHttpRequest::testCreateFromUrlAndMethod() CPPUNIT_ASSERT_EQUAL(String("/bar"), aRequest->urlpath); CPPUNIT_ASSERT_EQUAL(AnyP::PROTO_HTTP, aRequest->protocol); CPPUNIT_ASSERT_EQUAL(String("http://foo/bar"), String(url)); + xfree(url); /* a connect url with non-CONNECT data */ url = xstrdup(":foo/bar"); diff --git a/src/tests/test_http_range.cc b/src/tests/test_http_range.cc index a4e0b4406c..3846cc47e2 100644 --- a/src/tests/test_http_range.cc +++ b/src/tests/test_http_range.cc @@ -187,17 +187,25 @@ testRangeCanonization() } int -main (int argc, char **argv) +main(int argc, char **argv) { - Mem::Init(); - /* enable for debugging to console */ - // _db_init (NULL, NULL); - // Debug::Levels[64] = 9; - testRangeParser ("bytes=0-3"); - testRangeParser ("bytes=-3"); - testRangeParser ("bytes=1-"); - testRangeParser ("bytes=0-3, 1-, -2"); - testRangeIter (); - testRangeCanonization(); + try{ + Mem::Init(); + /* enable for debugging to console */ + // _db_init (NULL, NULL); + // Debug::Levels[64] = 9; + testRangeParser("bytes=0-3"); + testRangeParser("bytes=-3"); + testRangeParser("bytes=1-"); + testRangeParser("bytes=0-3, 1-, -2"); + testRangeIter(); + testRangeCanonization(); + } catch (const std::exception &e) { + printf("Error: dying from an unhandled exception: %s\n", e.what()); + return 1; + } catch (...) { + printf("Error: dying from an unhandled exception.\n"); + return 1; + } return 0; } -- 2.47.2