chromium/chrome/browser/file_select_helper_unittest.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "chrome/browser/file_select_helper.h"

#include <stddef.h>

#include <string>
#include <utility>
#include <vector>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "build/build_config.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/file_select_listener.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/shell_dialogs/selected_file_info.h"

FileChooserParams;

#if BUILDFLAG(ENTERPRISE_CLOUD_CONTENT_ANALYSIS)
namespace {

// A listener that remembers the list of files chosen.  The |files| argument
// to the ctor must outlive the listener.
class TestFileSelectListener : public content::FileSelectListener {};

// Fill in the arguments to be passed to the ContentAnalysisCompletionCallback()
// method based on a list of paths and the desired result for each path.
// This function simulates a path either passing the deep scan (status of true)
// or failing (status of false).
void PrepareContentAnalysisCompletionCallbackArgs(
    std::vector<base::FilePath> paths,
    std::vector<bool> status,
    std::vector<blink::mojom::FileChooserFileInfoPtr>* orig_files,
    enterprise_connectors::ContentAnalysisDelegate::Data* data,
    enterprise_connectors::ContentAnalysisDelegate::Result* result) {}

}  // namespace
#endif  // BUILDFLAG(ENTERPRISE_CLOUD_CONTENT_ANALYSIS)

class FileSelectHelperTest : public testing::Test {};

TEST_F(FileSelectHelperTest, IsAcceptTypeValid) {}

#if BUILDFLAG(IS_MAC)
TEST_F(FileSelectHelperTest, ZipPackage) {
  // Zip the package.
  const char app_name[] = "CalculatorFake.app";
  base::FilePath src = data_dir_.Append(app_name);
  base::FilePath dest = FileSelectHelper::ZipPackage(src);
  ASSERT_FALSE(dest.empty());
  ASSERT_TRUE(base::PathExists(dest));

  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());

  // Unzip the package into a temporary directory.
  base::CommandLine cl(base::FilePath("/usr/bin/unzip"));
  cl.AppendArg(dest.value().c_str());
  cl.AppendArg("-d");
  cl.AppendArg(temp_dir.GetPath().value().c_str());
  std::string output;
  EXPECT_TRUE(base::GetAppOutput(cl, &output));

  // Verify that several key files haven't changed.
  const char* files_to_verify[] = {"Contents/Info.plist",
                                   "Contents/MacOS/Calculator",
                                   "Contents/_CodeSignature/CodeResources"};
  size_t file_count = std::size(files_to_verify);
  for (size_t i = 0; i < file_count; i++) {
    const char* relative_path = files_to_verify[i];
    base::FilePath orig_file = src.Append(relative_path);
    base::FilePath final_file =
        temp_dir.GetPath().Append(app_name).Append(relative_path);
    EXPECT_TRUE(base::ContentsEqual(orig_file, final_file));
  }
}
#endif  // BUILDFLAG(IS_MAC)

TEST_F(FileSelectHelperTest, GetSanitizedFileName) {}

TEST_F(FileSelectHelperTest, LastSelectedDirectory) {}

// The following tests depend on the enterprise cloud content analysis feature
// set.
#if BUILDFLAG(ENTERPRISE_CLOUD_CONTENT_ANALYSIS)

TEST_F(FileSelectHelperTest, ContentAnalysisCompletionCallback_NoFiles) {}

TEST_F(FileSelectHelperTest, ContentAnalysisCompletionCallback_OneOKFile) {}

TEST_F(FileSelectHelperTest, ContentAnalysisCompletionCallback_TwoOKFiles) {}

TEST_F(FileSelectHelperTest, ContentAnalysisCompletionCallback_TwoBadFiles) {}

TEST_F(FileSelectHelperTest, ContentAnalysisCompletionCallback_OKBadFiles) {}

TEST_F(FileSelectHelperTest,
       ContentAnalysisCompletionCallback_SystemFilesSkipped) {}

TEST_F(FileSelectHelperTest,
       ContentAnalysisCompletionCallback_SystemOKBadFiles) {}

TEST_F(FileSelectHelperTest,
       ContentAnalysisCompletionCallback_FolderUpload_OK) {}

TEST_F(FileSelectHelperTest,
       ContentAnalysisCompletionCallback_FolderUpload_Bad) {}

TEST_F(FileSelectHelperTest,
       ContentAnalysisCompletionCallback_FolderUpload_OKBad) {}

TEST_F(FileSelectHelperTest, GetFileTypesFromAcceptType) {}

// This test depends on platform-specific mappings from mime types to file
// extensions in PlatformMimeUtil. It would seem that Linux does not offer a way
// to get extensions, and our Windows implementation still needs to be updated.
#if BUILDFLAG(IS_MAC)
TEST_F(FileSelectHelperTest, MultipleFileExtensionsForMime) {
  content::BrowserTaskEnvironment task_environment;
  TestingProfile profile;
  scoped_refptr<FileSelectHelper> file_select_helper =
      new FileSelectHelper(&profile);

  std::vector<std::u16string> accept_types{u"application/vnd.ms-powerpoint"};
  std::unique_ptr<ui::SelectFileDialog::FileTypeInfo> file_type_info =
      file_select_helper->GetFileTypesFromAcceptType(accept_types);

  std::vector<base::FilePath::StringType> expected_extensions {
#if BUILDFLAG(IS_WIN)
    L"ppt", L"pot", L"pps"
  };
#else
    "ppt", "pot", "pps"
  };
#endif
  std::sort(expected_extensions.begin(), expected_extensions.end());

  ASSERT_EQ(file_type_info->extensions.size(), 1u);
  std::vector<base::FilePath::StringType> actual_extensions =
      file_type_info->extensions[0];
  std::sort(actual_extensions.begin(), actual_extensions.end());

  EXPECT_EQ(expected_extensions, actual_extensions);
}
#endif

#endif  // BUILDFLAG(ENTERPRISE_CLOUD_CONTENT_ANALYSIS)