Initial build system and test framework
authorsangwan.kwon <sangwan.kwon@samsung.com>
Fri, 3 May 2019 07:20:13 +0000 (16:20 +0900)
committersangwan.kwon <sangwan.kwon@samsung.com>
Fri, 3 May 2019 07:25:40 +0000 (16:25 +0900)
Requires: gcc-c++, make, cmake
Optional: docker

Signed-off-by: sangwan.kwon <sangwan.kwon@samsung.com>
.gitignore [new file with mode: 0644]
CMakeLists.txt [new file with mode: 0644]
Makefile [new file with mode: 0644]
docker/Dockerfile [new file with mode: 0644]
include/osquery/status.h [new file with mode: 0644]
osquery/CMakeLists.txt [new file with mode: 0644]
osquery/core/CMakeLists.txt [new file with mode: 0644]
osquery/core/status_tests.cpp [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..3247a2b
--- /dev/null
@@ -0,0 +1,11 @@
+# cscope/ctag data
+/cscope.files
+/cscope.out
+/tags
+
+# Temporary files
+*.swp
+*~
+
+# Build
+/build 
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..42a6f74
--- /dev/null
@@ -0,0 +1,43 @@
+#  Copyright (c) 2019 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
+
+CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
+PROJECT(OSQUERY)
+
+INCLUDE(FindPkgConfig)
+INCLUDE(GNUInstallDirs)
+
+IF(NOT CMAKE_BUILD_TYPE)
+       SET(CMAKE_BUILD_TYPE "RELEASE")
+ENDIF(NOT CMAKE_BUILD_TYPE)
+
+SET(CMAKE_CXX_FLAGS_DEBUG   "-g -std=c++11 -O0 -ggdb -Wp,-U_FORTIFY_SOURCE")
+SET(CMAKE_CXX_FLAGS_RELEASE "-g -std=c++11 -O2 -DNDEBUG")
+
+SET(CMAKE_SHARED_LINKER_FLAGS  "-Wl,--as-needed")
+SET(CMAKE_EXE_LINKER_FLAGS     "-Wl,--as-needed")
+
+ADD_DEFINITIONS("-fPIC")
+ADD_DEFINITIONS("-Werror")
+ADD_DEFINITIONS("-Wall")
+ADD_DEFINITIONS("-Wextra")
+ADD_DEFINITIONS("-pedantic")
+ADD_DEFINITIONS("-pedantic-errors")
+
+INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}")
+INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/include")
+
+ENABLE_TESTING()
+
+ADD_SUBDIRECTORY(osquery)
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..0509e21
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,14 @@
+all:
+       mkdir -p build
+       cd build && cmake .. && make --no-print-directory $(MAKEFLAGS)
+
+clean:
+       rm -rf build
+
+docker_run:
+       docker build --network=host --tag tizen-osquery ./docker
+       docker run --rm -it --net=host -v $(shell pwd):/usr/src tizen-osquery
+
+%::
+       mkdir -p build
+       cd build && cmake .. && make --no-print-directory $@
diff --git a/docker/Dockerfile b/docker/Dockerfile
new file mode 100644 (file)
index 0000000..17c2991
--- /dev/null
@@ -0,0 +1,11 @@
+FROM ubuntu:16.04
+MAINTAINER sangwan.kwon@samsung.com
+
+RUN apt-get update && \
+    apt-get install -qq build-essential git cmake automake
+
+RUN cd /usr/src && \
+    git clone https://github.com/google/googletest.git
+
+RUN cd /usr/src/googletest && \
+    cmake . && make && make install
diff --git a/include/osquery/status.h b/include/osquery/status.h
new file mode 100644 (file)
index 0000000..87f506d
--- /dev/null
@@ -0,0 +1,94 @@
+// Copyright 2004-present Facebook. All Rights Reserved.
+
+#pragma once
+
+#include <string>
+
+namespace osquery {
+
+/**
+ * @brief A utility class which is used to express the state of operations.
+ *
+ * @code{.cpp}
+ *   osquery::Status foobar() {
+ *     auto na = doSomeWork();
+ *     if (na->itWorked()) {
+ *       return osquery::Status(0, "OK");
+ *     } else {
+ *       return osquery::Status(1, na->getErrorString());
+ *     }
+ *   }
+ * @endcode
+ */
+class Status {
+ public:
+  /**
+   * @brief Default constructor
+   *
+   * Note that the default constructor initialized an osquery::Status instance
+   * to a state such that a successful operation is indicated.
+   */
+  Status() : code_(0), message_("OK") {}
+
+  /**
+   * @brief A constructor which can be used to concisely express the status of
+   * an operation.
+   *
+   * @param c a status code. The idiom is that a zero status code indicates a
+   * successful operation and a non-zero status code indicates a failed
+   * operation.
+   * @param m a message indicating some extra detail regarding the operation.
+   * If all operations were successful, this message should be "OK".
+   * Otherwise, it doesn't matter what the string is, as long as both the
+   * setter and caller agree.
+   */
+  Status(int c, std::string m) : code_(c), message_(m) {}
+
+ public:
+  /**
+   * @brief A getter for the status code property
+   *
+   * @return an integer representing the status code of the operation.
+   */
+  int getCode() const { return code_; }
+
+  /**
+   * @brief A getter for the message property
+   *
+   * @return a string representing arbitrary additional information about the
+   * success or failure of an operation. On successful operations, the idiom
+   * is for the message to be "OK"
+   */
+  std::string getMessage() const { return message_; }
+
+  /**
+   * @brief A convenience method to check if the return code is 0
+   *
+   * @code{.cpp}
+   *   auto s = doSomething();
+   *   if (s.ok()) {
+   *     LOG(INFO) << "doing work";
+   *   } else {
+   *     LOG(ERROR) << s.toString();
+   *   }
+   * @endcode
+   *
+   * @return a boolean which is true if the status code is 0, false otherwise.
+   */
+  bool ok() const { return getCode() == 0; }
+
+  /**
+   * @brief A synonym for osquery::Status::getMessage()
+   *
+   * @see getMessage()
+   */
+  std::string toString() const { return getMessage(); }
+
+ private:
+  /// the internal storage of the status code
+  int code_;
+
+  /// the internal storage of the status message
+  std::string message_;
+};
+}
diff --git a/osquery/CMakeLists.txt b/osquery/CMakeLists.txt
new file mode 100644 (file)
index 0000000..c229249
--- /dev/null
@@ -0,0 +1,27 @@
+#  Copyright (c) 2019 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
+
+SET(${TARGET_OSQUERY_LIB} osquery)
+
+PKG_CHECK_MODULES(${TARGET_TANCHOR_LIB} REQUIRED gtest pthread)
+
+INCLUDE_DIRECTORIES(SYSTEM ${TARGET_OSQUERY_LIB}_INCLUDE_DIRS)
+
+MACRO(ADD_OSQUERY_TEST TEST_NAME SOURCE)
+       ADD_EXECUTABLE(${TEST_NAME} ${SOURCE})
+       TARGET_LINK_LIBRARIES(${TEST_NAME} gtest pthread)
+       ADD_TEST(${TEST_NAME} ${TEST_NAME})
+ENDMACRO(ADD_OSQUERY_TEST)
+
+ADD_SUBDIRECTORY(core)
diff --git a/osquery/core/CMakeLists.txt b/osquery/core/CMakeLists.txt
new file mode 100644 (file)
index 0000000..49c7a95
--- /dev/null
@@ -0,0 +1,3 @@
+# ADD_OSQUERY_LIBRARY(osquery_core init_osquery.cpp)
+
+ADD_OSQUERY_TEST(status_test status_tests.cpp)
diff --git a/osquery/core/status_tests.cpp b/osquery/core/status_tests.cpp
new file mode 100644 (file)
index 0000000..33c96f5
--- /dev/null
@@ -0,0 +1,39 @@
+// Copyright 2004-present Facebook. All Rights Reserved.
+
+#include "osquery/status.h"
+
+#include <gtest/gtest.h>
+
+namespace osquery {
+
+class StatusTests : public testing::Test {};
+
+TEST_F(StatusTests, test_constructor) {
+  auto s = Status(5, "message");
+  EXPECT_EQ(s.getCode(), 5);
+  EXPECT_EQ(s.getMessage(), "message");
+}
+
+TEST_F(StatusTests, test_constructor_2) {
+  Status s;
+  EXPECT_EQ(s.getCode(), 0);
+  EXPECT_EQ(s.getMessage(), "OK");
+}
+
+TEST_F(StatusTests, test_ok) {
+  auto s1 = Status(5, "message");
+  EXPECT_FALSE(s1.ok());
+  auto s2 = Status(0, "message");
+  EXPECT_TRUE(s2.ok());
+}
+
+TEST_F(StatusTests, test_to_string) {
+  auto s = Status(0, "foobar");
+  EXPECT_EQ(s.toString(), "foobar");
+}
+}
+
+int main(int argc, char* argv[]) {
+  testing::InitGoogleTest(&argc, argv);
+  return RUN_ALL_TESTS();
+}