OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/public/common/quarantine.h" | 5 #include "content/public/common/quarantine.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/files/file.h" | 10 #include "base/files/file.h" |
11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 const char kTestData[] = "It's okay to have a trailing nul."; | 21 const char kTestData[] = "It's okay to have a trailing nul."; |
22 const char kInternetURL[] = "http://example.com/some-url"; | 22 const char kInternetURL[] = "http://example.com/some-url"; |
23 const char kInternetReferrerURL[] = "http://example.com/some-other-url"; | 23 const char kInternetReferrerURL[] = "http://example.com/some-other-url"; |
24 const char kTestGUID[] = "69f8621d-c46a-4e88-b915-1ce5415cb008"; | 24 const char kTestGUID[] = "69f8621d-c46a-4e88-b915-1ce5415cb008"; |
25 | 25 |
26 } // namespace | 26 } // namespace |
27 | 27 |
28 TEST(QuarantineTest, FileCanBeOpenedForReadAfterAnnotation) { | 28 TEST(QuarantineTest, FileCanBeOpenedForReadAfterAnnotation) { |
| 29 if (!IsQuarantineSupportedForTesting()) { |
| 30 LOG(WARNING) << "Test will be skipped because quarantining is " |
| 31 "not supported on this OS/file system."; |
| 32 return; |
| 33 } |
| 34 |
29 base::ScopedTempDir test_dir; | 35 base::ScopedTempDir test_dir; |
30 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 36 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
31 | 37 |
32 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.class"); | 38 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.class"); |
33 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), | 39 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), |
34 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | 40 base::WriteFile(test_file, kTestData, arraysize(kTestData))); |
35 | 41 |
36 EXPECT_EQ(QuarantineFileResult::OK, | 42 EXPECT_EQ(QuarantineFileResult::OK, |
37 QuarantineFile(test_file, GURL(kInternetURL), | 43 QuarantineFile(test_file, GURL(kInternetURL), |
38 GURL(kInternetReferrerURL), kTestGUID)); | 44 GURL(kInternetReferrerURL), kTestGUID)); |
39 | 45 |
40 std::string contents; | 46 std::string contents; |
41 EXPECT_TRUE(base::ReadFileToString(test_file, &contents)); | 47 EXPECT_TRUE(base::ReadFileToString(test_file, &contents)); |
42 EXPECT_EQ(std::string(std::begin(kTestData), std::end(kTestData)), contents); | 48 EXPECT_EQ(std::string(std::begin(kTestData), std::end(kTestData)), contents); |
43 } | 49 } |
44 | 50 |
45 TEST(QuarantineTest, FileCanBeAnnotatedWithNoGUID) { | 51 TEST(QuarantineTest, FileCanBeAnnotatedWithNoGUID) { |
| 52 if (!IsQuarantineSupportedForTesting()) { |
| 53 LOG(WARNING) << "Test will be skipped because quarantining is " |
| 54 "not supported on this OS/file system."; |
| 55 return; |
| 56 } |
| 57 |
46 base::ScopedTempDir test_dir; | 58 base::ScopedTempDir test_dir; |
47 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); | 59 ASSERT_TRUE(test_dir.CreateUniqueTempDir()); |
48 | 60 |
49 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.class"); | 61 base::FilePath test_file = test_dir.GetPath().AppendASCII("foo.class"); |
50 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), | 62 ASSERT_EQ(static_cast<int>(arraysize(kTestData)), |
51 base::WriteFile(test_file, kTestData, arraysize(kTestData))); | 63 base::WriteFile(test_file, kTestData, arraysize(kTestData))); |
52 | 64 |
53 EXPECT_EQ(QuarantineFileResult::OK, | 65 EXPECT_EQ(QuarantineFileResult::OK, |
54 QuarantineFile(test_file, GURL(kInternetURL), | 66 QuarantineFile(test_file, GURL(kInternetURL), |
55 GURL(kInternetReferrerURL), std::string())); | 67 GURL(kInternetReferrerURL), std::string())); |
56 } | 68 } |
57 | 69 |
58 } // namespace content | 70 } // namespace content |
OLD | NEW |