Add unit tests for Watchdog API 06/279206/1
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 3 Aug 2022 10:22:50 +0000 (19:22 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 3 Aug 2022 10:22:50 +0000 (19:22 +0900)
To refactor watchdog API, unit tests are added.

Change-Id: Id3918f6334b14ec29b48553226979c1b03f41520
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
test/unit_tests/test_watchdog.cc [new file with mode: 0644]

diff --git a/test/unit_tests/test_watchdog.cc b/test/unit_tests/test_watchdog.cc
new file mode 100644 (file)
index 0000000..ce702fa
--- /dev/null
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <aul.h>
+#include <aul_watchdog.h>
+#include <aul_cmd.h>
+#include <aul_sock.h>
+#include <gtest/gtest.h>
+#include <stdlib.h>
+
+#include <bundle_cpp.h>
+
+#include <iostream>
+#include <memory>
+
+#include "mock/mock_hook.h"
+#include "mock/socket_mock.h"
+#include "mock/test_fixture.h"
+#include "src/launch.h"
+
+using ::testing::_;
+using ::testing::DoAll;
+using ::testing::Return;
+using ::testing::SetArgPointee;
+using ::testing::Invoke;
+using ::testing::SaveArg;
+
+namespace {
+
+class Mocks : virtual public ::testing::NiceMock<SocketMock> {};
+
+}  // namespace
+
+class WatchdogTest : public TestFixture {
+ public:
+  WatchdogTest() : TestFixture(std::make_unique<::Mocks>()) {}
+
+  virtual void SetUp() {
+    if (aul_is_initialized())
+      return;
+
+    EXPECT_CALL(GetMock<SocketMock>(), send(_, _, _, _))
+      .WillOnce(Invoke([&](int fd, const void* buf, size_t n, int flags)
+          -> ssize_t {
+        return n;
+      }));
+    aul_launch_init(nullptr, nullptr);
+  }
+
+  virtual void TearDown() {
+    aul_launch_fini();
+  }
+};
+
+TEST_F(WatchdogTest, aul_watchdog_enable_P) {
+  int cmd = -1;
+
+  EXPECT_CALL(GetMock<SocketMock>(), send(_, _, _, _))
+      .Times(2)
+      .WillRepeatedly(Invoke([&](int fd, const void* buf, size_t n, int flags)
+          -> ssize_t {
+        const app_pkt_t* header = reinterpret_cast<const app_pkt_t*>(buf);
+        cmd = header->cmd;
+        return n;
+      }));
+
+  int ret = aul_watchdog_enable();
+  EXPECT_EQ(ret, AUL_R_OK);
+  EXPECT_EQ(cmd, WATCHDOG_ENABLE);
+
+  // Post: Disable watchdog
+  aul_watchdog_disable();
+}
+
+TEST_F(WatchdogTest, aul_watchdog_disable_P) {
+  int cmd = -1;
+
+  EXPECT_CALL(GetMock<SocketMock>(), send(_, _, _, _))
+      .Times(2)
+      .WillRepeatedly(Invoke([&](int fd, const void* buf, size_t n, int flags)
+          -> ssize_t {
+        const app_pkt_t* header = reinterpret_cast<const app_pkt_t*>(buf);
+        cmd = header->cmd;
+        return n;
+      }));
+
+  // Pre: Enable watchdog
+  int ret = aul_watchdog_enable();
+  EXPECT_EQ(ret, AUL_R_OK);
+
+  ret = aul_watchdog_disable();
+  EXPECT_EQ(ret, AUL_R_OK);
+  EXPECT_EQ(cmd, WATCHDOG_DISABLE);
+}
+
+TEST_F(WatchdogTest, aul_watchdog_disable_N) {
+  int ret = aul_watchdog_disable();
+  EXPECT_EQ(ret, AUL_R_ERROR);
+}
+
+TEST_F(WatchdogTest, aul_watchdog_kick_P) {
+  int cmd = -1;
+
+  EXPECT_CALL(GetMock<SocketMock>(), send(_, _, _, _))
+      .Times(3)
+      .WillRepeatedly(Invoke([&](int fd, const void* buf, size_t n, int flags)
+          -> ssize_t {
+        const app_pkt_t* header = reinterpret_cast<const app_pkt_t*>(buf);
+        cmd = header->cmd;
+        return n;
+      }));
+
+  // Pre: Enable watchdog
+  int ret = aul_watchdog_enable();
+  EXPECT_EQ(ret, AUL_R_OK);
+
+  ret = aul_watchdog_kick();
+  EXPECT_EQ(ret, AUL_R_OK);
+  EXPECT_EQ(cmd, WATCHDOG_KICK);
+
+  // Post: Disable watchdog
+  aul_watchdog_disable();
+}
+
+TEST_F(WatchdogTest, aul_watchdog_kick_N) {
+  int ret = aul_watchdog_kick();
+  EXPECT_EQ(ret, AUL_R_ERROR);
+}