Add unit tests 26/161926/7
authorJunghoon Park <jh9216.park@samsung.com>
Tue, 28 Nov 2017 07:50:46 +0000 (16:50 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Tue, 28 Nov 2017 10:52:07 +0000 (19:52 +0900)
Change-Id: I54bdfca90c792f69471f6644adf50e9961ae6ab9
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
CMakeLists.txt
packaging/rpc-port.spec
src/parcel-internal.cc
src/parcel-internal.h
unit_tests/CMakeLists.txt [new file with mode: 0644]
unit_tests/src/main.cc [new file with mode: 0644]
unit_tests/src/rpc_port_parcel_test.cc [new file with mode: 0644]

index 5b10bcd..0c183bc 100644 (file)
@@ -60,3 +60,5 @@ INSTALL(TARGETS ${this_target} DESTINATION ${LIB_INSTALL_DIR})
 
 INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION include/rpc-port FILES_MATCHING PATTERN "*.h")
 INSTALL(DIRECTORY ${LIBRARY_OUTPUT_PATH}/ DESTINATION ${LIB_INSTALL_DIR} FILES_MATCHING PATTERN "*.so*")
+
+ADD_SUBDIRECTORY(unit_tests)
index ecb09f4..18b8126 100755 (executable)
@@ -14,6 +14,7 @@ BuildRequires:  pkgconfig(aul)
 BuildRequires:  pkgconfig(pkgmgr)
 BuildRequires:  pkgconfig(pkgmgr-info)
 BuildRequires:  pkgconfig(openssl)
+BuildRequires:  pkgconfig(gmock)
 
 Requires(post): /sbin/ldconfig
 Requires(post): coreutils
@@ -63,3 +64,17 @@ rm -rf %{buildroot}
 %{_includedir}/rpc-port/*.h
 %{_libdir}/pkgconfig/*.pc
 %{_libdir}/lib%{name}.so
+
+#################################################
+# gtest-rpc-port
+#################################################
+%package -n gtest-rpc-port
+Summary:    GTest for rpc-port
+Group:      Development/Libraries
+Requires:   %{name}
+
+%description -n gtest-rpc-port
+GTest for rpc-port
+
+%files -n gtest-rpc-port
+%{_bindir}/gtest-rpc-port
index 9868c57..2f1ce25 100644 (file)
@@ -20,9 +20,7 @@
 namespace rpc_port {
 
 Parcel::Parcel(const unsigned char* buf, unsigned int size)
-    : data_(buf, buf + size) {
-  reader_ = data_.begin();
-}
+    : data_(buf, buf + size) {}
 
 void Parcel::WriteByte(char b) {
   Write<char>(b);
@@ -100,7 +98,7 @@ double Parcel::ReadDouble() {
 std::string Parcel::ReadString() {
   int l = Read<int>();
 
-  std::string str(reader_, reader_ + l);
+  std::string str(reinterpret_cast<const char*>(&data_[reader_]));
   reader_ += l;
 
   return str;
@@ -113,7 +111,7 @@ bool Parcel::ReadBool() {
 bundle* Parcel::ReadBundle() {
   int l = Read<int>();
 
-  std::string str(reader_, reader_ + l);
+  std::string str(reinterpret_cast<const char*>(&data_[reader_]));
   reader_ += l;
 
   bundle* b = bundle_decode(
index 645cf36..43a7819 100644 (file)
@@ -66,14 +66,14 @@ class Parcel {
     T d;
     unsigned char* p = reinterpret_cast<unsigned char*>(&d);
 
-    std::copy(reader_, reader_ + sizeof(T), p);
+    std::copy(&data_[reader_], &data_[reader_] + sizeof(T), p);
     reader_ += sizeof(T);
     return d;
   }
 
  private:
   std::vector<unsigned char> data_;
-  std::vector<unsigned char>::iterator reader_;
+  unsigned int reader_ = 0;
 };
 
 }  // namespace rpc_port
diff --git a/unit_tests/CMakeLists.txt b/unit_tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..ecbe1b6
--- /dev/null
@@ -0,0 +1,30 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(gtest-rpc-port CXX)
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(gtest-rpc-port REQUIRED
+    glib-2.0
+    gmock
+    aul
+)
+
+FOREACH(flag ${gtest-rpc-port_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror -Winline")
+
+SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++11")
+SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
+SET(CMAKE_CXX_FLAGS_RELEASE "-O2")
+SET(SOURCES "")
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include)
+
+AUX_SOURCE_DIRECTORY(src SOURCES)
+ADD_EXECUTABLE(${PROJECT_NAME}
+       ${SOURCES}
+)
+
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${gtest-rpc-port_LDFLAGS} rpc-port)
+
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin/)
diff --git a/unit_tests/src/main.cc b/unit_tests/src/main.cc
new file mode 100644 (file)
index 0000000..52756d2
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 <gtest/gtest.h>
+#include <gmock/gmock.h>
+
+int main(int argc, char** argv){
+  testing::InitGoogleTest(&argc, argv);
+  return RUN_ALL_TESTS();
+}
diff --git a/unit_tests/src/rpc_port_parcel_test.cc b/unit_tests/src/rpc_port_parcel_test.cc
new file mode 100644 (file)
index 0000000..ee8d43c
--- /dev/null
@@ -0,0 +1,225 @@
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * 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 <gtest/gtest.h>
+#include <gmock/gmock.h>
+#include <iostream>
+#include <stdbool.h>
+#include <stdexcept>
+
+#include "rpc-port-parcel.h"
+
+using namespace std;
+using ::testing::AtLeast;
+
+class ParcelTest : public ::testing::Test {
+ public:
+  virtual void SetUp() {
+    int ret = rpc_port_parcel_create(&handle_);
+
+    ASSERT_NE(handle_, nullptr);
+    ASSERT_EQ(ret, 0);
+  }
+
+  virtual void TearDown() {
+    int ret = rpc_port_parcel_destroy(handle_);
+    ASSERT_EQ(ret, 0);
+  }
+
+  static void WriteParcelable(rpc_port_parcel_h h, void* data) {
+    const char str[] = "abcdef";
+    int ret = rpc_port_parcel_write_string(h, str);
+    ASSERT_EQ(ret, 0);
+
+    ret = rpc_port_parcel_write_double(h, 123.456f);
+    ASSERT_EQ(ret, 0);
+
+    ret = rpc_port_parcel_write_int32(h, 123456);
+    ASSERT_EQ(ret, 0);
+
+    ParcelTest* p = static_cast<ParcelTest*>(data);
+    ASSERT_NE(p, nullptr);
+    p->touch_to_ = true;
+  }
+
+  static void ReadParcelable(rpc_port_parcel_h h, void* data) {
+    char* s = nullptr;
+    int ret = rpc_port_parcel_read_string(h, &s);
+    ASSERT_EQ(ret, 0);
+    ASSERT_STREQ(s, "abcdef");
+
+    double b;
+    ret = rpc_port_parcel_read_double(h, &b);
+    ASSERT_EQ(ret, 0);
+    ASSERT_EQ(b, 123.456f);
+
+    int i;
+    ret = rpc_port_parcel_read_int32(h, &i);
+    ASSERT_EQ(ret, 0);
+    ASSERT_EQ(i, 123456);
+
+    ParcelTest* p = static_cast<ParcelTest*>(data);
+    ASSERT_NE(p, nullptr);
+    p->touch_from_ = true;
+  }
+
+  rpc_port_parcel_h handle_;
+  bool touch_from_ = false;
+  bool touch_to_ = false;
+};
+
+TEST(rpc_port_parcel, rpc_port_parcel_create_destroy) {
+  rpc_port_parcel_h handle;
+
+  int ret = rpc_port_parcel_create(&handle);
+
+  ASSERT_NE(handle, nullptr);
+  ASSERT_EQ(ret, 0);
+
+  ret = rpc_port_parcel_destroy(handle);
+  ASSERT_EQ(ret, 0);
+}
+
+TEST_F(ParcelTest, read_write_byte) {
+  int ret = rpc_port_parcel_write_byte(handle_, 'k');
+  ASSERT_EQ(ret, 0);
+
+  char b;
+  ret = rpc_port_parcel_read_byte(handle_, &b);
+  ASSERT_EQ(ret, 0);
+  ASSERT_EQ(b, 'k');
+}
+
+TEST_F(ParcelTest, read_write_int16) {
+  int ret = rpc_port_parcel_write_int16(handle_, 123);
+  ASSERT_EQ(ret, 0);
+
+  short b;
+  ret = rpc_port_parcel_read_int16(handle_, &b);
+  ASSERT_EQ(ret, 0);
+  ASSERT_EQ(b, 123);
+}
+
+TEST_F(ParcelTest, read_write_int32) {
+  int ret = rpc_port_parcel_write_int32(handle_, 123456);
+  ASSERT_EQ(ret, 0);
+
+  int b;
+  ret = rpc_port_parcel_read_int32(handle_, &b);
+  ASSERT_EQ(ret, 0);
+  ASSERT_EQ(b, 123456);
+}
+
+TEST_F(ParcelTest, read_write_int64) {
+  int ret = rpc_port_parcel_write_int64(handle_, 12345678);
+  ASSERT_EQ(ret, 0);
+
+  long long b;
+  ret = rpc_port_parcel_read_int64(handle_, &b);
+  ASSERT_EQ(ret, 0);
+  ASSERT_EQ(b, 12345678);
+}
+
+TEST_F(ParcelTest, read_write_float) {
+  int ret = rpc_port_parcel_write_float(handle_, 123.456f);
+  ASSERT_EQ(ret, 0);
+
+  float b;
+  ret = rpc_port_parcel_read_float(handle_, &b);
+  ASSERT_EQ(ret, 0);
+  ASSERT_EQ(b, 123.456f);
+}
+
+TEST_F(ParcelTest, read_write_double) {
+  int ret = rpc_port_parcel_write_double(handle_, 123.456f);
+  ASSERT_EQ(ret, 0);
+
+  double b;
+  ret = rpc_port_parcel_read_double(handle_, &b);
+  ASSERT_EQ(ret, 0);
+  ASSERT_EQ(b, 123.456f);
+}
+
+TEST_F(ParcelTest, read_write_string) {
+  const char str[] = "abcdef";
+  int ret = rpc_port_parcel_write_string(handle_, str);
+  ASSERT_EQ(ret, 0);
+
+  char* s = nullptr;
+  ret = rpc_port_parcel_read_string(handle_, &s);
+  ASSERT_EQ(ret, 0);
+  ASSERT_STREQ(s, str);
+  free(s);
+}
+
+TEST_F(ParcelTest, read_write_bool) {
+  int ret = rpc_port_parcel_write_bool(handle_, true);
+  ASSERT_EQ(ret, 0);
+
+  bool b = false;
+  ret = rpc_port_parcel_read_bool(handle_, &b);
+  ASSERT_EQ(ret, 0);
+  ASSERT_TRUE(b);
+}
+
+TEST_F(ParcelTest, read_write_bundle) {
+  bundle* b = bundle_create();
+  ASSERT_NE(b, nullptr);
+
+  int ret = bundle_add_str(b, "Test", "Value");
+  ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
+
+  ret = rpc_port_parcel_write_bundle(handle_, b);
+  ASSERT_EQ(ret, 0);
+
+  bundle_free(b);
+  b = nullptr;
+
+  ret = rpc_port_parcel_read_bundle(handle_, &b);
+  ASSERT_EQ(ret, 0);
+
+  char* str;
+  ret = bundle_get_str(b, "Test", &str);
+  ASSERT_EQ(ret, BUNDLE_ERROR_NONE);
+  ASSERT_STREQ("Value", str);
+
+  bundle_free(b);
+}
+
+TEST_F(ParcelTest, read_write_array_count) {
+  int ret = rpc_port_parcel_write_array_count(handle_, 123);
+  ASSERT_EQ(ret, 0);
+
+  int c;
+  ret = rpc_port_parcel_read_array_count(handle_, &c);
+  ASSERT_EQ(ret, 0);
+  ASSERT_EQ(c, 123);
+}
+
+TEST_F(ParcelTest, read_write) {
+  rpc_port_parcelable_t p = {
+    .to = WriteParcelable,
+    .from = ReadParcelable
+  };
+
+  int ret = rpc_port_parcel_write(handle_, &p, this);
+  ASSERT_EQ(ret, 0);
+  ASSERT_TRUE(touch_to_);
+
+  ret = rpc_port_parcel_read(handle_, &p, this);
+  ASSERT_EQ(ret, 0);
+  ASSERT_TRUE(touch_from_);
+}