]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Fix various issues in unit tests
authorAmos Jeffries <squid3@treenet.co.nz>
Thu, 27 Dec 2012 10:17:04 +0000 (03:17 -0700)
committerAmos Jeffries <squid3@treenet.co.nz>
Thu, 27 Dec 2012 10:17:04 +0000 (03:17 -0700)
* 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
src/tests/testConfigParser.cc
src/tests/testHttpRequest.cc
src/tests/test_http_range.cc

index 07cfc67a2204cad2d803fd2651a459a0170cb005..309e8cd7ffb8fc29701b49985224d3d2d5d8ee26 100644 (file)
@@ -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
index a3a58fe251516a07a4e5f164252bab240980a4a9..2ac31e67d1ffc2178b587b6511c887ed509b1a09 100644 (file)
@@ -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);
index b71b9148dda9909645d22ccdaa143f57cabddbb2..72f4569b375db3ce84f9c832a782b89f016f72f8 100644 (file)
@@ -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");
index a4e0b4406cf805683646211ea4608cd7a52ccb5a..3846cc47e22838879ce270c5974b47c2861e8456 100644 (file)
@@ -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;
 }