Merge "Extract base::UnixSocketRaw"
diff --git a/BUILD.gn b/BUILD.gn
index 117d5b2..46aec09 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -13,6 +13,7 @@
 # limitations under the License.
 
 import("gn/perfetto.gni")
+import("gn/test.gni")
 
 if (perfetto_build_standalone || perfetto_build_with_android) {
   import("//gn/standalone/android.gni")
@@ -44,13 +45,13 @@
 group("all") {
   testonly = true  # allow to build also test targets
   deps = [
+    ":perfetto_unittests",
     "src/protozero/protoc_plugin($host_toolchain)",
   ]
   if (perfetto_build_standalone || perfetto_build_with_android) {
     deps += [
       ":perfetto",
       ":perfetto_integrationtests",
-      ":perfetto_unittests",
       ":traced",
       ":traced_probes",
       "protos/perfetto/config:merged_config",  # For syntax-checking the proto.
@@ -105,8 +106,7 @@
   }
 }
 
-group("perfetto_unittests_sources") {
-  testonly = true
+test("perfetto_unittests") {
   deps = [
     "gn:default_deps",
     "gn:gtest_main",
@@ -137,16 +137,7 @@
 }
 
 if (perfetto_build_standalone || perfetto_build_with_android) {
-  executable("perfetto_unittests") {
-    testonly = true
-    deps = [
-      ":perfetto_unittests_sources",
-      "gn:default_deps",
-    ]
-  }
-
-  executable("perfetto_integrationtests") {
-    testonly = true
+  test("perfetto_integrationtests") {
     deps = [
       "gn:default_deps",
       "gn:gtest_main",
diff --git a/gn/BUILD.gn b/gn/BUILD.gn
index 75137f9..7d4bf5e 100644
--- a/gn/BUILD.gn
+++ b/gn/BUILD.gn
@@ -66,6 +66,10 @@
     public_deps = [
       "//buildtools:gtest_main",
     ]
+  } else if (build_with_chromium) {
+    public_deps = [
+      "//base/test:run_all_unittests",
+    ]
   } else {
     public_deps = [
       "//testing/gtest:gtest_main",
diff --git a/gn/standalone/BUILD.gn b/gn/standalone/BUILD.gn
index 7c1e56d..e7aabf0 100644
--- a/gn/standalone/BUILD.gn
+++ b/gn/standalone/BUILD.gn
@@ -21,7 +21,10 @@
     "-Wall",
     "-Wextra",
   ]
-  if (is_clang) {
+
+  # Disable Weverything on fuzzers to avoid breakages when new versions of clang
+  # are rolled into OSS-fuzz.
+  if (is_clang && !is_fuzzer) {
     cflags += [
       "-Weverything",
       "-Wno-c++98-compat-pedantic",
diff --git a/gn/test.gni b/gn/test.gni
new file mode 100644
index 0000000..db48b4b
--- /dev/null
+++ b/gn/test.gni
@@ -0,0 +1,26 @@
+# Copyright (C) 2019 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.
+
+import("perfetto.gni")
+
+if (perfetto_build_standalone || perfetto_build_with_android) {
+  template("test") {
+    executable(target_name) {
+      forward_variables_from(invoker, "*")
+      testonly = true
+    }
+  }
+} else {
+  import("//testing/test.gni")
+}
diff --git a/src/base/file_utils.cc b/src/base/file_utils.cc
index 5a39933..7dc003c 100644
--- a/src/base/file_utils.cc
+++ b/src/base/file_utils.cc
@@ -82,6 +82,7 @@
 }
 
 bool FlushFile(int fd) {
+  PERFETTO_DCHECK(fd != 0);
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
   return !PERFETTO_EINTR(fdatasync(fd));
diff --git a/src/tracing/core/service_impl_unittest.cc b/src/tracing/core/service_impl_unittest.cc
index def0d88..cbf2da6 100644
--- a/src/tracing/core/service_impl_unittest.cc
+++ b/src/tracing/core/service_impl_unittest.cc
@@ -378,10 +378,6 @@
   producer->WaitForDataSourceStop("data_source");
   consumer->WaitForTracingDisabled();
 
-  // Writing to the file may happen concurrently to the read below, which can
-  // cause flakiness. Flush forcefully to ensure all data was written.
-  base::FlushFile(*tmp_file);
-
   // Verify the contents of the file.
   std::string trace_raw;
   ASSERT_TRUE(base::ReadFile(tmp_file.path().c_str(), &trace_raw));
diff --git a/src/tracing/core/tracing_service_impl.cc b/src/tracing/core/tracing_service_impl.cc
index 7edd776..92a8f93 100644
--- a/src/tracing/core/tracing_service_impl.cc
+++ b/src/tracing/core/tracing_service_impl.cc
@@ -943,6 +943,8 @@
     PERFETTO_DLOG("Draining into file, written: %" PRIu64 " KB, stop: %d",
                   (total_wr_size + 1023) / 1024, stop_writing_into_file);
     if (stop_writing_into_file) {
+      // Ensure all data was written to the file before we close it.
+      base::FlushFile(fd);
       tracing_session->write_into_file.reset();
       tracing_session->write_period_ms = 0;
       if (tracing_session->state == TracingSession::STARTED)