Add unit test 67/240867/3
authorDaehyeon Jung <darrenh.jung@samsung.com>
Wed, 12 Aug 2020 07:57:07 +0000 (16:57 +0900)
committerDaehyeon Jung <darrenh.jung@samsung.com>
Wed, 19 Aug 2020 11:01:00 +0000 (20:01 +0900)
Change-Id: I600fb5dcd448dde1bb6ed4b21c562f3c81492ae2
Signed-off-by: Daehyeon Jung <darrenh.jung@samsung.com>
CMakeLists.txt
packaging/capi-content-mime-type.spec
unit-tests/CMakeLists.txt [new file with mode: 0644]
unit-tests/test.cc [new file with mode: 0644]
unit-tests/test.png [new file with mode: 0644]
unit-tests/test_in_accessible.png [new file with mode: 0644]

index 365123c..bec26dd 100755 (executable)
@@ -2,7 +2,7 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
 SET(fw_name "capi-content-mime-type")
 
-PROJECT(${fw_name})
+PROJECT(${fw_name} C CXX)
 
 SET(CMAKE_INSTALL_PREFIX /usr)
 SET(PREFIX ${CMAKE_INSTALL_PREFIX})
@@ -94,3 +94,4 @@ ADD_CUSTOM_COMMAND(
 
 ENDIF(UNIX)
 
+ADD_SUBDIRECTORY(unit-tests)
\ No newline at end of file
index df5ca9b..405be1f 100755 (executable)
@@ -7,9 +7,15 @@ License:    Apache-2.0
 Source0:    %{name}-%{version}.tar.gz
 Source1001:    capi-content-mime-type.manifest
 BuildRequires:  cmake
+BuildRequires:  shared-mime-info
 BuildRequires:  pkgconfig(dlog)
+BuildRequires:  pkgconfig(gmock)
 BuildRequires:  pkgconfig(capi-base-common)
 BuildRequires:  pkgconfig(xdgmime)
+%if 0%{?gcov:1}
+BuildRequires:  lcov
+BuildRequires:  zip
+%endif
 
 %description
 
@@ -62,6 +68,19 @@ mkdir -p %{buildroot}%{_datadir}/gcov/obj
 install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
 %endif
 
+%check
+cd unit-tests
+chmod -r test_in_accessible.png
+LD_LIBRARY_PATH=.. ctest -V
+chmod +r test_in_accessible.png
+cd ..
+%if 0%{?gcov:1}
+lcov -c --ignore-errors graph --no-external -d . -o mime-type.info
+genhtml mime-type.info -o mime-type.out
+zip -r mime-type.zip mime-type.out mime-type.info
+install -m 0644 mime-type.zip %{buildroot}%{_datadir}/gcov/
+%endif
+
 %post -p /sbin/ldconfig
 
 %postun -p /sbin/ldconfig
@@ -81,4 +100,6 @@ install -m 0644 gcov-obj/* %{buildroot}%{_datadir}/gcov/obj
 %if 0%{?gcov:1}
 %files gcov
 %{_datadir}/gcov/obj/*
+%{_datadir}/gcov/mime-type.zip
 %endif
+
diff --git a/unit-tests/CMakeLists.txt b/unit-tests/CMakeLists.txt
new file mode 100644 (file)
index 0000000..399efa9
--- /dev/null
@@ -0,0 +1,12 @@
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} TEST_SRCS)
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../include)
+
+ENABLE_TESTING()
+
+SET(TARGET_TEST "mimetype-test")
+
+ADD_EXECUTABLE(${TARGET_TEST} ${TEST_SRCS})
+
+ADD_TEST(${TARGET_TEST} ${TARGET_TEST})
+TARGET_LINK_LIBRARIES(${TARGET_TEST} gmock capi-content-mime-type)
\ No newline at end of file
diff --git a/unit-tests/test.cc b/unit-tests/test.cc
new file mode 100644 (file)
index 0000000..ad78476
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+ * mimetype-unittest
+ *
+ * Copyright (c) 2020 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 <gmock/gmock.h>
+#include <mime_type.h>
+
+namespace mimetype {
+
+class MimeTypeEnvironment : public ::testing::Environment {
+ public:
+  void SetUp() override {}
+
+  void TearDown() override {}
+};
+
+class MimeTypeTest : public ::testing::Test {
+ protected:
+  void SetUp() override {}
+
+  void TearDown() override {}
+};
+
+TEST_F(MimeTypeTest, GetMimeType) {
+  int r;
+  char* type;
+
+  r = mime_type_get_mime_type(nullptr, nullptr);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_INVALID_PARAMETER));
+
+  r = mime_type_get_mime_type("", &type);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_INVALID_PARAMETER));
+
+  r = mime_type_get_mime_type(".", &type);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_INVALID_PARAMETER));
+
+  r = mime_type_get_mime_type("txt", &type);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_NONE));
+  ASSERT_THAT(type, testing::StrEq("text/plain"));
+  free(type);
+
+  r = mime_type_get_mime_type("png", &type);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_NONE));
+  ASSERT_THAT(type, testing::StrEq("image/png"));
+  free(type);
+}
+
+TEST_F(MimeTypeTest, GetFileExtension) {
+  int r;
+  char** file_extension;
+  int len;
+
+  r = mime_type_get_file_extension(nullptr, nullptr, nullptr);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_INVALID_PARAMETER));
+
+  r = mime_type_get_file_extension("", &file_extension, &len);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_INVALID_PARAMETER));
+
+  r = mime_type_get_file_extension("image/png", &file_extension, &len);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_NONE));
+  ASSERT_THAT(file_extension[0], testing::StrEq("png"));
+  ASSERT_THAT(len, testing::Eq(1));
+  free(file_extension[0]);
+  free(file_extension);
+}
+
+TEST_F(MimeTypeTest, GetMimeTypeForData) {
+  int r;
+  char* mime_type;
+  const char png[] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
+     0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
+     0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
+     0x01, 0x00, 0x00, 0x00, 0x00, 0x37, 0x6e, 0xf9,
+     0x24, 0x00, 0x00, 0x00, 0x10, 0x49, 0x44, 0x41,
+     0x54, 0x78, 0x9c, 0x62, 0x60, 0x01, 0x00, 0x00,
+     0x00, 0xff, 0xff, 0x03, 0x00, 0x00, 0x06, 0x00,
+     0x05, 0x57, 0xbf, 0xab, 0xd4, 0x00, 0x00, 0x00,
+     0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60,
+     0x82
+  };
+
+  r = mime_type_get_mime_type_for_data(nullptr, 0, nullptr);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_INVALID_PARAMETER));
+
+  r = mime_type_get_mime_type_for_data(png, sizeof(png), &mime_type);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_NONE));
+  ASSERT_THAT(mime_type, testing::StrEq("image/png"));
+  free(mime_type);
+}
+
+TEST_F(MimeTypeTest, GetMimeTypeForFile) {
+  int r;
+  char* mime_type;
+
+  r = mime_type_get_mime_type_for_file(nullptr, nullptr);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_INVALID_PARAMETER));
+
+  r = mime_type_get_mime_type_for_file("", &mime_type);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_INVALID_PARAMETER));
+
+  r = mime_type_get_mime_type_for_file("test_in_accessible.png", &mime_type);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_PERMISSION_DENIED));
+
+  r = mime_type_get_mime_type_for_file("test.png", &mime_type);
+  ASSERT_THAT(r, testing::Eq(MIME_TYPE_ERROR_NONE));
+  ASSERT_THAT(mime_type, testing::StrEq("image/png"));
+  free(mime_type);
+}
+
+} // namespace mimetype
+
+const ::testing::Environment* env = nullptr;
+
+int main(int argc, char* argv[]) {
+  int ret = 0;
+  try {
+    ::testing::InitGoogleTest(&argc, argv);
+    ::env = testing::AddGlobalTestEnvironment(new mimetype::MimeTypeEnvironment);
+  } catch(...) {
+    std::cout << "Exception occured" << std::endl;
+    return 1;
+  }
+  try {
+    return RUN_ALL_TESTS();
+  } catch(const ::testing::internal::GoogleTestFailureException& e) {
+    std::cout << "GoogleTestFailureException occured:" << e.what() << std::endl;
+    ret = 1;
+  }
+  return ret;
+}
diff --git a/unit-tests/test.png b/unit-tests/test.png
new file mode 100644 (file)
index 0000000..7371c99
Binary files /dev/null and b/unit-tests/test.png differ
diff --git a/unit-tests/test_in_accessible.png b/unit-tests/test_in_accessible.png
new file mode 100644 (file)
index 0000000..7371c99
Binary files /dev/null and b/unit-tests/test_in_accessible.png differ