]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3831] Checkpoint: did dhcp4 UTs
authorFrancis Dupont <fdupont@isc.org>
Fri, 16 May 2025 09:20:15 +0000 (11:20 +0200)
committerFrancis Dupont <fdupont@isc.org>
Fri, 16 May 2025 21:08:02 +0000 (23:08 +0200)
src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc
src/bin/dhcp4/tests/http_control_socket_unittest.cc

index 06539f3dd882bd48eec470bd6c6f9841ee24efe8..d23cc2399c0933cb9012868ff7fe0ac334a88caa 100644 (file)
@@ -1529,6 +1529,9 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configWriteFilename) {
     createUnixChannelServer();
     std::string response;
 
+    // This is normally set by the command line -c parameter.
+    server_->setConfigFile("test1.json");
+
     sendUnixCommand("{ \"command\": \"config-write\", "
                     "\"arguments\": { \"filename\": \"test2.json\" } }", response);
 
@@ -1536,6 +1539,59 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configWriteFilename) {
     ::remove("test2.json");
 }
 
+// Tests if config-write can be called with a valid full path as parameter.
+TEST_F(CtrlChannelDhcpv4SrvTest, configWriteFullPath) {
+    createUnixChannelServer();
+    std::string response;
+
+
+    // This is normally set by the command line -c parameter.
+    server_->setConfigFile("/tmp/test1.json");
+
+
+    sendUnixCommand("{ \"command\": \"config-write\", "
+                    "\"arguments\": { \"filename\": \"/tmp/test2.json\" } }", response);
+
+    checkConfigWrite(response, CONTROL_RESULT_SUCCESS, "/tmp/test2.json");
+    ::remove("/tmp/test2.json");
+}
+
+// Tests if config-write raises an error with invalid path as parameter.
+TEST_F(CtrlChannelDhcpv4SrvTest, configWriteBadPath) {
+    createUnixChannelServer();
+    std::string response;
+
+    // This is normally set by the command line -c parameter.
+    server_->setConfigFile("test1.json");
+
+    sendUnixCommand("{ \"command\": \"config-write\", "
+                    "\"arguments\": { \"filename\": \"/tmp/test2.json\" } }", response);
+
+    string expected = "not allowed to write config into /tmp/test2.json: ";
+    expected += "file /tmp/test2.json must be in the same directory ";
+    expected += "as the config file (test1.json)";
+    checkConfigWrite(response, CONTROL_RESULT_ERROR, expected);
+    ::remove("/tmp/test2.json");
+}
+
+// Tests if config-write raises an error with invalid full path as parameter.
+TEST_F(CtrlChannelDhcpv4SrvTest, configWriteBadFullPath) {
+    createUnixChannelServer();
+    std::string response;
+
+    // This is normally set by the command line -c parameter.
+    server_->setConfigFile("/tmp/kea1/test.json");
+
+    sendUnixCommand("{ \"command\": \"config-write\", "
+                    "\"arguments\": { \"filename\": \"/tmp/kea2/test.json\" } }", response);
+
+    string expected = "not allowed to write config into /tmp/kea2/test.json: ";
+    expected += "file /tmp/kea2/test.json must be in the same directory ";
+    expected += "as the config file (/tmp/kea1/test.json)";
+    checkConfigWrite(response, CONTROL_RESULT_ERROR, expected);
+    ::remove("/tmp/kea2/test.json");
+}
+
 // Tests if config-reload attempts to reload a file and reports that the
 // file is missing.
 TEST_F(CtrlChannelDhcpv4SrvTest, configReloadMissingFile) {
index a947c683eb4d385d33d700e4e668bc845d1f9b8a..88c970e47f275b4ee0636968cf8f4e05ca7c2fd8 100644 (file)
@@ -389,6 +389,12 @@ public:
             ASSERT_TRUE(from_file);
         } else if (exp_status == CONTROL_RESULT_ERROR) {
 
+            // Errors can be in a list.
+            if (rsp->getType() == Element::list) {
+                ASSERT_EQ(1, rsp->size());
+                rsp = rsp->get(0);
+            }
+
             // Let's check if the reason for failure was given.
             ConstElementPtr text = rsp->get("text");
             ASSERT_TRUE(text);
@@ -503,6 +509,15 @@ public:
     // Tests if config-write can be called with a valid filename as parameter.
     void testConfigWriteFilename();
 
+    // Tests if config-write can be called with a valid full path as parameter.
+    void testConfigWriteFullPath();
+
+    // Tests if config-write raises an error with invalid path as parameter.
+    void testConfigWriteBadPath();
+
+    // Tests if config-write raises an error with invalid full path as parameter.
+    void testConfigWriteBadFullPath();
+
     // Tests if config-reload attempts to reload a file and reports that the
     // file is missing.
     void testConfigReloadMissingFile();
@@ -2323,6 +2338,9 @@ BaseCtrlChannelDhcpv4Test::testConfigWriteFilename() {
     createHttpChannelServer();
     std::string response;
 
+    // This is normally set by the command line -c parameter.
+    server_->setConfigFile("test1.json");
+
     sendHttpCommand("{ \"command\": \"config-write\", "
                     "\"arguments\": { \"filename\": \"test2.json\" } }",
                     response);
@@ -2339,6 +2357,87 @@ TEST_F(HttpsCtrlChannelDhcpv4Test, configWriteFilename) {
     testConfigWriteFilename();
 }
 
+// Tests if config-write can be called with a valid full path as parameter.
+void
+BaseCtrlChannelDhcpv4Test::testConfigWriteFullPath() {
+    createHttpChannelServer();
+    std::string response;
+
+    // This is normally set by the command line -c parameter.
+    server_->setConfigFile("/tmp/test1.json");
+
+    sendHttpCommand("{ \"command\": \"config-write\", "
+                    "\"arguments\": { \"filename\": \"/tmp/test2.json\" } }",
+                    response);
+
+    checkConfigWrite(response, CONTROL_RESULT_SUCCESS, "/tmp/test2.json");
+    ::remove("/tmp/test2.json");
+}
+
+TEST_F(HttpCtrlChannelDhcpv4Test, configWriteFullPath) {
+    testConfigWriteFullPath();
+}
+
+TEST_F(HttpsCtrlChannelDhcpv4Test, configWriteFullPath) {
+    testConfigWriteFullPath();
+}
+
+// Tests if config-write raises an error with invalid path as parameter.
+void
+BaseCtrlChannelDhcpv4Test::testConfigWriteBadPath() {
+    createHttpChannelServer();
+    std::string response;
+
+    // This is normally set by the command line -c parameter.
+    server_->setConfigFile("test1.json");
+
+    sendHttpCommand("{ \"command\": \"config-write\", "
+                    "\"arguments\": { \"filename\": \"/tmp/test2.json\" } }",
+                    response);
+
+    string expected = "not allowed to write config into /tmp/test2.json: ";
+    expected += "file /tmp/test2.json must be in the same directory ";
+    expected += "as the config file (test1.json)";
+    checkConfigWrite(response, CONTROL_RESULT_ERROR, expected);
+    ::remove("/tmp/test2.json");
+}
+
+TEST_F(HttpCtrlChannelDhcpv4Test, configWriteBadPath) {
+    testConfigWriteBadPath();
+}
+
+TEST_F(HttpsCtrlChannelDhcpv4Test, configWriteBadPath) {
+    testConfigWriteBadPath();
+}
+
+// Tests if config-write raises an error with invalid full path as parameter.
+void
+BaseCtrlChannelDhcpv4Test::testConfigWriteBadFullPath() {
+    createHttpChannelServer();
+    std::string response;
+
+    // This is normally set by the command line -c parameter.
+    server_->setConfigFile("/tmp/kea1/test.json");
+
+    sendHttpCommand("{ \"command\": \"config-write\", "
+                    "\"arguments\": { \"filename\": \"/tmp/kea2/test.json\" } }",
+                    response);
+
+    string expected = "not allowed to write config into /tmp/kea2/test.json: ";
+    expected += "file /tmp/kea2/test.json must be in the same directory ";
+    expected += "as the config file (/tmp/kea1/test.json)";
+    checkConfigWrite(response, CONTROL_RESULT_ERROR, expected);
+    ::remove("/tmp/kea2/test.json");
+}
+
+TEST_F(HttpCtrlChannelDhcpv4Test, configWriteBadFullPath) {
+    testConfigWriteBadFullPath();
+}
+
+TEST_F(HttpsCtrlChannelDhcpv4Test, configWriteBadFullPath) {
+    testConfigWriteBadFullPath();
+}
+
 // Tests if config-reload attempts to reload a file and reports that the
 // file is missing.
 void