]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/tests/testRock.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / tests / testRock.cc
index da9b757cd53be3961268b2d1c9ae7c8072539e77..55d5ed0e08367023eb97f7bed4b818e102b23fe5 100644 (file)
@@ -1,34 +1,40 @@
-#define SQUID_UNIT_TEST 1
-#include "squid.h"
+/*
+ * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
 
+#include "squid.h"
 #include "ConfigParser.h"
 #include "DiskIO/DiskIOModule.h"
+#include "fde.h"
 #include "fs/rock/RockSwapDir.h"
 #include "globals.h"
 #include "HttpHeader.h"
 #include "HttpReply.h"
-#include "Mem.h"
 #include "MemObject.h"
 #include "RequestFlags.h"
 #include "SquidConfig.h"
 #include "Store.h"
+#include "store/Disk.h"
+#include "store/Disks.h"
 #include "StoreFileSystem.h"
 #include "StoreSearch.h"
-#include "SwapDir.h"
 #include "testRock.h"
 #include "testStoreSupport.h"
+#include "unitTestMain.h"
 
+#include <stdexcept>
 #if HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
-#if HAVE_STDEXCEPT
-#include <stdexcept>
-#endif
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 
-#define TESTDIR "testRock_Store"
+#define TESTDIR "tr"
 
 CPPUNIT_TEST_SUITE_REGISTRATION( testRock );
 
@@ -39,7 +45,7 @@ static char cwd[MAXPATHLEN];
 static void
 addSwapDir(testRock::SwapDirPointer aStore)
 {
-    allocate_new_swapdir(&Config.cacheSwap);
+    allocate_new_swapdir(Config.cacheSwap);
     Config.cacheSwap.swapDirs[Config.cacheSwap.n_configured] = aStore.getRaw();
     ++Config.cacheSwap.n_configured;
 }
@@ -52,12 +58,15 @@ testRock::setUp()
     if (0 > system ("rm -rf " TESTDIR))
         throw std::runtime_error("Failed to clean test work directory");
 
+    Config.memShared.defaultTo(false);
+    Config.shmLocking.defaultTo(false);
+
     // use current directory for shared segments (on path-based OSes)
     Ipc::Mem::Segment::BasePath = getcwd(cwd,MAXPATHLEN);
     if (Ipc::Mem::Segment::BasePath == NULL)
         Ipc::Mem::Segment::BasePath = ".";
 
-    Store::Root(new StoreController);
+    Store::Init();
 
     store = new Rock::SwapDir();
 
@@ -82,7 +91,7 @@ testRock::setUp()
     store->create();
 
     rr = new Rock::SwapDirRr;
-    rr->run(rrAfterConfig);
+    rr->useConfig();
 }
 
 void
@@ -90,13 +99,14 @@ testRock::tearDown()
 {
     CPPUNIT_NS::TestFixture::tearDown();
 
-    Store::Root(NULL);
+    Store::FreeMemory();
 
     store = NULL;
 
     free_cachedir(&Config.cacheSwap);
 
-    delete rr;
+    rr->finishShutdown(); // deletes rr
+    rr = NULL;
 
     // TODO: do this once, or each time.
     // safe_free(Config.replPolicy->type);
@@ -133,11 +143,11 @@ testRock::commonInit()
 
     Mem::Init();
 
-    comm_init();
+    fde::Init();
 
-    httpHeaderInitModule();    /* must go before any header processing (e.g. the one in errorInitialize) */
+    comm_init();
 
-    httpReplyInitModule();     /* must go before accepting replies */
+    httpHeaderInitModule(); /* must go before any header processing (e.g. the one in errorInitialize) */
 
     mem_policy = createRemovalPolicy(Config.replPolicy);
 
@@ -182,8 +192,8 @@ testRock::createEntry(const int i)
     flags.cachable = true;
     StoreEntry *const pe =
         storeCreateEntry(storeId(i), "dummy log url", flags, Http::METHOD_GET);
-    HttpReply *const rep = const_cast<HttpReply *>(pe->getReply());
-    rep->setHeaders(Http::scOkay, "dummy test object", "x-squid-internal/test", 0, -1, squid_curtime + 100000);
+    auto &rep = pe->mem().adjustableBaseReply();
+    rep.setHeaders(Http::scOkay, "dummy test object", "x-squid-internal/test", 0, -1, squid_curtime + 100000);
 
     pe->setPublicKey();
 
@@ -196,14 +206,7 @@ testRock::addEntry(const int i)
     StoreEntry *const pe = createEntry(i);
 
     pe->buffer();
-    /* TODO: remove this when the metadata is separated */
-    {
-        Packer p;
-        packerToStoreInit(&p, pe);
-        pe->getReply()->packHeadersInto(&p);
-        packerClean(&p);
-    }
-
+    pe->mem().freshestReply().packHeadersUsingSlowPacker(*pe);
     pe->flush();
     pe->timestampsSet();
     pe->complete();
@@ -259,16 +262,28 @@ testRock::testRockSwapOut()
 
     // try to swap out entry to a used unlocked slot
     {
-        StoreEntry *const pe = addEntry(4);
+        // without marking the old entry as deleted
+        StoreEntry *const pe = addEntry(3);
 
-        CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING, pe->swap_status);
-        CPPUNIT_ASSERT_EQUAL(0, pe->swap_dirn);
-        CPPUNIT_ASSERT(pe->swap_filen >= 0);
+        CPPUNIT_ASSERT_EQUAL(SWAPOUT_NONE, pe->swap_status);
+        CPPUNIT_ASSERT_EQUAL(-1, pe->swap_dirn);
+        CPPUNIT_ASSERT_EQUAL(-1, pe->swap_filen);
+        pe->unlock("testRock::testRockSwapOut e#3");
+
+        // after marking the old entry as deleted
+        StoreEntry *const pe2 = getEntry(4);
+        CPPUNIT_ASSERT(pe2 != nullptr);
+        pe2->release();
+
+        StoreEntry *const pe3 = addEntry(4);
+        CPPUNIT_ASSERT_EQUAL(SWAPOUT_WRITING, pe3->swap_status);
+        CPPUNIT_ASSERT_EQUAL(0, pe3->swap_dirn);
+        CPPUNIT_ASSERT(pe3->swap_filen >= 0);
 
         StockEventLoop loop;
         loop.run();
 
-        CPPUNIT_ASSERT_EQUAL(SWAPOUT_DONE, pe->swap_status);
+        CPPUNIT_ASSERT_EQUAL(SWAPOUT_DONE, pe3->swap_status);
 
         pe->unlock("testRock::testRockSwapOut e#4");
     }
@@ -318,3 +333,4 @@ testRock::testRockSwapOut()
         CPPUNIT_ASSERT_EQUAL(static_cast<StoreEntry *>(NULL), pe2);
     }
 }
+