start a util unittest
am: 32c399278b

Change-Id: Ic8d070d328df2c1cf2ffb3813cfdaceb2e693800
diff --git a/.gitignore b/.gitignore
index a121c9e..8713a1e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,6 +12,7 @@
 /minijail0
 /syscall_filter_unittest
 /system_unittest
+/util_unittest
 /parse_seccomp_policy
 
 # common.mk TEST().
diff --git a/Android.bp b/Android.bp
index 03b0530..9dd17ea 100644
--- a/Android.bp
+++ b/Android.bp
@@ -236,6 +236,34 @@
     },
 }
 
+// Utility functionality unit tests using gtest.
+//
+// For a device, run with:
+// adb shell /data/nativetest/util_unittest_gtest/util_unittest_gtest
+//
+// For host, run with:
+// out/host/linux-x86/nativetest(64)/util_unittest_gtest/util_unittest_gtest
+// =========================================================
+cc_test {
+    name: "util_unittest_gtest",
+    defaults: ["libminijail_flags"],
+    host_supported: true,
+
+    srcs: [
+        "util.c",
+        "util_unittest.cc",
+    ] + unittestSrcFiles,
+
+    static_libs: ["libminijail_generated"],
+    shared_libs: minijailCommonLibraries,
+
+    target: {
+        android: {
+            test_suites: ["device-tests"],
+        },
+    },
+}
+
 // libminijail_test executable for brillo_Minijail test.
 // =========================================================
 cc_test {
diff --git a/Makefile b/Makefile
index b0f0509..7bbb0b4 100644
--- a/Makefile
+++ b/Makefile
@@ -55,7 +55,8 @@
 
 tests: TEST(CXX_BINARY(libminijail_unittest)) \
 	TEST(CXX_BINARY(syscall_filter_unittest)) \
-	TEST(CXX_BINARY(system_unittest))
+	TEST(CXX_BINARY(system_unittest)) \
+	TEST(CXX_BINARY(util_unittest)) \
 
 
 CC_BINARY(minijail0): LDLIBS += -lcap -ldl
@@ -105,6 +106,16 @@
 clean: CLEAN(system_unittest)
 
 
+CXX_BINARY(util_unittest): CXXFLAGS += $(GTEST_CXXFLAGS)
+CXX_BINARY(util_unittest): LDLIBS += -lcap $(GTEST_LIBS)
+ifeq ($(USE_SYSTEM_GTEST),no)
+CXX_BINARY(util_unittest): $(GTEST_LIBS)
+endif
+CXX_BINARY(util_unittest): util_unittest.o \
+		$(CORE_OBJECT_FILES) testrunner.o
+clean: CLEAN(util_unittest)
+
+
 CXX_BINARY(parse_seccomp_policy): parse_seccomp_policy.o syscall_filter.o \
 		bpf.o util.o libconstants.gen.o libsyscalls.gen.o
 clean: CLEAN(parse_seccomp_policy)
diff --git a/util_unittest.cc b/util_unittest.cc
new file mode 100644
index 0000000..b5cdff7
--- /dev/null
+++ b/util_unittest.cc
@@ -0,0 +1,67 @@
+// util_unittest.cpp
+// Copyright (C) 2018 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.
+//
+// Test system.[ch] module code using gtest.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <gtest/gtest.h>
+
+#include "util.h"
+
+// Sanity check for the strip func.
+TEST(strip, basic) {
+  char str[] = " foo\t";
+  ASSERT_EQ("foo", std::string(strip(str)));
+}
+
+// Make sure we don't crash with various "null"-like inputs.
+TEST(tokenize, null_stringp) {
+  ASSERT_EQ(nullptr, tokenize(nullptr, nullptr));
+  ASSERT_EQ(nullptr, tokenize(nullptr, ""));
+  ASSERT_EQ(nullptr, tokenize(nullptr, ","));
+
+  char *p = nullptr;
+  ASSERT_EQ(nullptr, tokenize(&p, nullptr));
+}
+
+// Make sure we don't crash with various "null"-like inputs.
+TEST(tokenize, null_delim) {
+  char str[] = "a,b,c";
+  char *p = str;
+  ASSERT_EQ(str, tokenize(&p, nullptr));
+  ASSERT_EQ(nullptr, p);
+  ASSERT_EQ(str, std::string("a,b,c"));
+
+  p = str;
+  ASSERT_EQ(str, tokenize(&p, ""));
+  ASSERT_EQ(nullptr, p);
+  ASSERT_EQ(str, std::string("a,b,c"));
+}
+
+// Sanity check for the tokenize func.
+TEST(tokenize, basic) {
+  char str[] = "a,b,c";
+  char *p = str;
+  ASSERT_EQ("a", std::string(tokenize(&p, ",")));
+  ASSERT_EQ("b", std::string(tokenize(&p, ",")));
+  ASSERT_EQ("c", std::string(tokenize(&p, ",")));
+  ASSERT_EQ(nullptr, p);
+  ASSERT_EQ(nullptr, tokenize(&p, ","));
+}