summaryrefslogtreecommitdiff
path: root/chromium/content/common/quarantine/quarantine_unittest.cc
blob: 9e4f3bc51b9e279308acd92b93876899abd77791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/common/quarantine.h"

#include <iterator>
#include <string>

#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/macros.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace content {

namespace {

const char kTestData[] = "It's okay to have a trailing nul.";
const char kInternetURL[] = "http://example.com/some-url";
const char kInternetReferrerURL[] = "http://example.com/some-other-url";
const char kTestGUID[] = "69f8621d-c46a-4e88-b915-1ce5415cb008";

}  // namespace

TEST(QuarantineTest, FileCanBeOpenedForReadAfterAnnotation) {
  base::ScopedTempDir test_dir;
  ASSERT_TRUE(test_dir.CreateUniqueTempDir());

  base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.class");
  ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
            base::WriteFile(test_file, kTestData, arraysize(kTestData)));

  EXPECT_EQ(QuarantineFileResult::OK,
            QuarantineFile(test_file, GURL(kInternetURL),
                           GURL(kInternetReferrerURL), kTestGUID));

  std::string contents;
  EXPECT_TRUE(base::ReadFileToString(test_file, &contents));
  EXPECT_EQ(std::string(std::begin(kTestData), std::end(kTestData)), contents);
}

TEST(QuarantineTest, FileCanBeAnnotatedWithNoGUID) {
  base::ScopedTempDir test_dir;
  ASSERT_TRUE(test_dir.CreateUniqueTempDir());

  base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.class");
  ASSERT_EQ(static_cast<int>(arraysize(kTestData)),
            base::WriteFile(test_file, kTestData, arraysize(kTestData)));

  EXPECT_EQ(QuarantineFileResult::OK,
            QuarantineFile(test_file, GURL(kInternetURL),
                           GURL(kInternetReferrerURL), std::string()));
}

}  // namespace content