--- /dev/null
+/*
+ * 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;
+}