Add check that --font-dir is actually a directory

Hoisted myself on this today when I was actually passing the path to
the font file I wanted to use, not the directory.

BUG=pdfium:1008,pdfium:1020

Change-Id: I4a68a7d96633e951a92125d83397ff457288b1e0
Reviewed-on: https://pdfium-review.googlesource.com/32636
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc
index 347982c..c7c1a3a 100644
--- a/samples/pdfium_test.cc
+++ b/samples/pdfium_test.cc
@@ -33,6 +33,7 @@
 #include "samples/pdfium_test_event_helper.h"
 #include "samples/pdfium_test_write_helper.h"
 #include "testing/test_support.h"
+#include "testing/utils/path_service.h"
 #include "third_party/base/logging.h"
 #include "third_party/base/optional.h"
 
@@ -325,7 +326,7 @@
         return false;
       }
       options->output_format = OUTPUT_SKP;
-#endif
+#endif  // PDF_ENABLE_SKIA
     } else if (cur_arg.size() > 11 &&
                cur_arg.compare(0, 11, "--font-dir=") == 0) {
       if (!options->font_directory.empty()) {
@@ -338,6 +339,13 @@
         fprintf(stderr, "Failed to expand --font-dir, %s\n", path.c_str());
         return false;
       }
+
+      if (!PathService::DirectoryExists(expanded_path.value())) {
+        fprintf(stderr, "--font-dir, %s, appears to not be a directory\n",
+                path.c_str());
+        return false;
+      }
+
       options->font_directory = expanded_path.value();
 
 #ifdef _WIN32
@@ -763,7 +771,7 @@
 void ShowConfig() {
   std::string config;
   std::string maybe_comma;
-#if PDF_ENABLE_V8
+#ifdef PDF_ENABLE_V8
   config.append(maybe_comma);
   config.append("V8");
   maybe_comma = ",";
diff --git a/testing/utils/path_service.cpp b/testing/utils/path_service.cpp
index 2ded0ed..5e1ce39 100644
--- a/testing/utils/path_service.cpp
+++ b/testing/utils/path_service.cpp
@@ -35,7 +35,10 @@
 }
 #endif
 
-bool DirectoryExists(const std::string& path) {
+}  // namespace
+
+// static
+bool PathService::DirectoryExists(const std::string& path) {
 #ifdef _WIN32
   DWORD fileattr = GetFileAttributesA(path.c_str());
   if (fileattr != INVALID_FILE_ATTRIBUTES)
@@ -49,8 +52,6 @@
 #endif
 }
 
-}  // namespace
-
 // static
 bool PathService::EndsWithSeparator(const std::string& path) {
   return path.size() > 1 && path[path.size() - 1] == PATH_SEPARATOR;
@@ -127,7 +128,7 @@
   potential_path.append("testing");
   potential_path.push_back(PATH_SEPARATOR);
   potential_path.append("resources");
-  if (DirectoryExists(potential_path)) {
+  if (PathService::DirectoryExists(potential_path)) {
     *path = potential_path;
     return true;
   }
@@ -140,7 +141,7 @@
   potential_path.append("testing");
   potential_path.push_back(PATH_SEPARATOR);
   potential_path.append("resources");
-  if (DirectoryExists(potential_path)) {
+  if (PathService::DirectoryExists(potential_path)) {
     *path = potential_path;
     return true;
   }
diff --git a/testing/utils/path_service.h b/testing/utils/path_service.h
index fc0042c..63df808 100644
--- a/testing/utils/path_service.h
+++ b/testing/utils/path_service.h
@@ -16,6 +16,9 @@
 // Get the various file directory and path information.
 class PathService {
  public:
+  // Return true when the path is a directory that exists.
+  static bool DirectoryExists(const std::string& path);
+
   // Return true when the path ends with a path separator.
   static bool EndsWithSeparator(const std::string& path);