meta: add makefile, test runner, skeleton MessageBuffer

Add the very beginnings of MessageBuffer, which will
manage a buffer of log messages. Along the way, add
the support files that we need to build and test
MessageBuffer.

Note that the core functionality of MessageBuffer will be
added in later CLs. (The rest of the functionality is too
big to fit in this CL.)

Bug: 31653003
Test: ./runtests.sh (on bullhead)

Change-Id: I6c8a1bf453d02438988c955d70196e67f8173d1d
diff --git a/Android.mk b/Android.mk
new file mode 100644
index 0000000..851eb04
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,57 @@
+# Copyright (C) 2016 The Android Open Source Project
+#
+# 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.
+
+LOCAL_PATH := $(call my-dir)
+wifilogd_cpp_flags := -Wall -Wextra -Weffc++ -Weverything \
+    -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded -Werror
+wifilogd_gtest_cpp_flags := -Wno-undef -Wno-missing-noreturn \
+    -Wno-shift-sign-overflow -Wno-used-but-marked-unused -Wno-deprecated \
+    -Wno-weak-vtables -Wno-sign-conversion -Wno-global-constructors \
+    -Wno-covered-switch-default
+
+wifilogd_parent_dir := $(LOCAL_PATH)/../
+wifilogd_includes := \
+    $(wifilogd_parent_dir)
+
+###
+### wifilogd static library
+###
+include $(CLEAR_VARS)
+LOCAL_MODULE := libwifilogd
+LOCAL_CPPFLAGS := $(wifilogd_cpp_flags)
+LOCAL_C_INCLUDES := $(wifilogd_includes)
+LOCAL_SRC_FILES := \
+    message_buffer.cpp
+LOCAL_SHARED_LIBRARIES := \
+    libbase \
+    liblog
+include $(BUILD_STATIC_LIBRARY)
+
+###
+### wifilogd unit tests.
+###
+include $(CLEAR_VARS)
+LOCAL_MODULE := wifilogd_unit_test
+LOCAL_CPPFLAGS := $(wifilogd_cpp_flags) $(wifilogd_gtest_cpp_flags)
+LOCAL_C_INCLUDES := $(wifilogd_includes)
+LOCAL_SRC_FILES := \
+    tests/message_buffer_unittest.cpp \
+    tests/main.cpp
+LOCAL_STATIC_LIBRARIES := \
+    libgtest \
+    libwifilogd
+LOCAL_SHARED_LIBRARIES := \
+    libbase \
+    liblog
+include $(BUILD_NATIVE_TEST)
diff --git a/message_buffer.cpp b/message_buffer.cpp
new file mode 100644
index 0000000..f89210f
--- /dev/null
+++ b/message_buffer.cpp
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * 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 <android-base/logging.h>
+
+#include "wifilogd/message_buffer.h"
+
+namespace android {
+namespace wifilogd {
+
+MessageBuffer::MessageBuffer(size_t size) : data_(new uint8_t[size]) {
+  CHECK(size > GetHeaderSize());
+}
+
+}  // namespace wifilogd
+}  // namespace android
diff --git a/message_buffer.h b/message_buffer.h
new file mode 100644
index 0000000..ee16bda
--- /dev/null
+++ b/message_buffer.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * 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.
+ */
+
+#ifndef MESSAGE_BUFFER_H_
+#define MESSAGE_BUFFER_H_
+
+#include <cstdint>
+#include <memory>
+
+#include "android-base/macros.h"
+
+namespace android {
+namespace wifilogd {
+
+// A fixed-size buffer, which provides FIFO access to read and write
+// a sequence of messages.
+class MessageBuffer {
+ public:
+  // Constructs the buffer. |size| must be greater than GetHeaderSize().
+  explicit MessageBuffer(size_t size);
+
+  // Returns the size of MessageBuffer's per-message header.
+  static constexpr size_t GetHeaderSize() { return sizeof(LengthHeader); }
+
+ private:
+  struct LengthHeader {
+    uint16_t payload_len;
+  };
+
+  std::unique_ptr<uint8_t[]> data_;
+
+  // MessageBuffer is a value type, so it would be semantically reasonable to
+  // support copy and assign. Performance-wise, though, we should avoid
+  // copies. Remove the copy constructor and the assignment operator, to
+  // ensure that we don't accidentally make copies.
+  DISALLOW_COPY_AND_ASSIGN(MessageBuffer);
+};
+
+}  // namespace wifilogd
+}  // namespace android
+
+#endif  // MESSAGE_BUFFER_H_
diff --git a/runtests.sh b/runtests.sh
new file mode 100755
index 0000000..9d6701a
--- /dev/null
+++ b/runtests.sh
@@ -0,0 +1,39 @@
+#!/usr/bin/env bash
+
+# Copyright (C) 2016 The Android Open Source Project
+#
+# 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.
+
+if [ -z $ANDROID_BUILD_TOP ]; then
+  echo "You need to source and lunch before you can use this script"
+  exit 1
+fi
+
+echo "Running tests"
+set -e # fail early
+
+# NOTE We can't actually run these commands, since they rely on functions added by
+#      build/envsetup.sh to the bash shell environment.
+echo "+ mmma -j32 $ANDROID_BUILD_TOP/system/connectivity/wifilogd"
+make -j32 -C $ANDROID_BUILD_TOP -f build/core/main.mk \
+    MODULES-IN-system-connectivity-wifilogd
+
+set -x # print commands
+
+adb root
+adb wait-for-device
+adb remount
+adb sync
+
+adb shell /data/nativetest/wifilogd_unit_test/wifilogd_unit_test
+adb shell /data/nativetest64/wifilogd_unit_test/wifilogd_unit_test
diff --git a/tests/main.cpp b/tests/main.cpp
new file mode 100644
index 0000000..3866b51
--- /dev/null
+++ b/tests/main.cpp
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * 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 "android-base/logging.h"
+#include "gtest/gtest.h"
+
+int main(int argc, char** argv) {
+  ::testing::InitGoogleTest(&argc, argv);
+  // Force ourselves to always log to stderr.
+  ::android::base::InitLogging(argv, android::base::StderrLogger);
+  return RUN_ALL_TESTS();
+}
diff --git a/tests/message_buffer_unittest.cpp b/tests/message_buffer_unittest.cpp
new file mode 100644
index 0000000..557918e
--- /dev/null
+++ b/tests/message_buffer_unittest.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2016, The Android Open Source Project
+ *
+ * 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 "wifilogd/message_buffer.h"
+
+namespace android {
+namespace wifilogd {
+namespace {
+
+constexpr size_t kBufferSizeBytes = 1024;
+constexpr size_t kHeaderSizeBytes = MessageBuffer::GetHeaderSize();
+
+class MessageBufferTest : public ::testing::Test {
+ public:
+  MessageBufferTest() : buffer_{kBufferSizeBytes} {}
+
+ protected:
+  MessageBuffer buffer_;
+};
+
+}  // namespace
+
+// Per
+// github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#death-tests),
+// death tests should be specially named.
+using MessageBufferDeathTest = MessageBufferTest;
+
+TEST_F(MessageBufferDeathTest, ConstructionOfUselesslySmallBufferCausesDeath) {
+  EXPECT_DEATH(MessageBuffer{kHeaderSizeBytes}, "Check failed");
+}
+
+}  // namespace wifilogd
+}  // namespace android