summaryrefslogtreecommitdiff
path: root/content/browser/download/file_metadata_unittest_linux.cc
blob: 9b7ceb531b99820955aaba270f144bc68e0716cb (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2012 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 <errno.h>
#include <sys/types.h>
#include <sys/xattr.h>

#include <algorithm>
#include <sstream>
#include <string>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/strings/string_split.h"
#include "content/browser/download/file_metadata_linux.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace content {
namespace {

using std::istringstream;
using std::string;
using std::vector;

class FileMetadataLinuxTest : public testing::Test {
 public:
  FileMetadataLinuxTest()
      : source_url_("http://www.source.com"),
        referrer_url_("http://www.referrer.com"),
        is_xattr_supported_(false) {}

  const base::FilePath& test_file() const {
    return test_file_;
  }

  const GURL& source_url() const {
    return source_url_;
  }

  const GURL& referrer_url() const {
    return referrer_url_;
  }

  bool is_xattr_supported() const {
    return is_xattr_supported_;
  }

 protected:
  virtual void SetUp() OVERRIDE {
    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &test_file_));
    int result = setxattr(test_file_.value().c_str(),
                          "user.test", "test", 4, 0);
    is_xattr_supported_ = (!result) || (errno != ENOTSUP);
    if (!is_xattr_supported_) {
      VLOG(0) << "Test will be skipped because extended attributes are not "
                << "supported on this OS/file system.";
    }
  }

  void CheckExtendedAttributeValue(const string attr_name,
                                   const string expected_value) const {
    ssize_t len = getxattr(test_file().value().c_str(), attr_name.c_str(),
                           NULL, 0);
    if (len <= static_cast<ssize_t>(0)) {
      FAIL() << "Attribute '" << attr_name << "' does not exist";
    }
    char* buffer = new char[len];
    len = getxattr(test_file().value().c_str(), attr_name.c_str(), buffer, len);
    EXPECT_EQ(expected_value.size(), static_cast<size_t>(len));
    string real_value(buffer, len);
    delete[] buffer;
    EXPECT_EQ(expected_value, real_value);
  }

  void GetExtendedAttributeNames(vector<string>* attr_names) const {
    ssize_t len = listxattr(test_file().value().c_str(), NULL, 0);
    if (len <= static_cast<ssize_t>(0)) return;
    char* buffer = new char[len];
    len = listxattr(test_file().value().c_str(), buffer, len);
    attr_names->clear();
    base::SplitString(string(buffer, len), '\0', attr_names);
    delete[] buffer;
  }

  void VerifyAttributesAreSetCorrectly() const {
    vector<string> attr_names;
    GetExtendedAttributeNames(&attr_names);

    // Check if the attributes are set on the file
    vector<string>::const_iterator pos = find(attr_names.begin(),
        attr_names.end(), kSourceURLAttrName);
    EXPECT_NE(pos, attr_names.end());
    pos = find(attr_names.begin(), attr_names.end(), kReferrerURLAttrName);
    EXPECT_NE(pos, attr_names.end());

    // Check if the attribute values are set correctly
    CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec());
    CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec());
  }

 private:
  base::ScopedTempDir temp_dir_;
  base::FilePath test_file_;
  GURL source_url_;
  GURL referrer_url_;
  bool is_xattr_supported_;
};

TEST_F(FileMetadataLinuxTest, CheckMetadataSetCorrectly) {
  if (!is_xattr_supported()) return;
  AddOriginMetadataToFile(test_file(), source_url(), referrer_url());
  VerifyAttributesAreSetCorrectly();
}

TEST_F(FileMetadataLinuxTest, SetMetadataMultipleTimes) {
  if (!is_xattr_supported()) return;
  GURL dummy_url("http://www.dummy.com");
  AddOriginMetadataToFile(test_file(), dummy_url, dummy_url);
  AddOriginMetadataToFile(test_file(), source_url(), referrer_url());
  VerifyAttributesAreSetCorrectly();
}

TEST_F(FileMetadataLinuxTest, InvalidSourceURLTest) {
  if (!is_xattr_supported()) return;
  GURL invalid_url;
  vector<string> attr_names;
  AddOriginMetadataToFile(test_file(), invalid_url, referrer_url());
  GetExtendedAttributeNames(&attr_names);
  EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
      kSourceURLAttrName));
  CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec());
}

TEST_F(FileMetadataLinuxTest, InvalidReferrerURLTest) {
  if (!is_xattr_supported()) return;
  GURL invalid_url;
  vector<string> attr_names;
  AddOriginMetadataToFile(test_file(), source_url(), invalid_url);
  GetExtendedAttributeNames(&attr_names);
  EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
      kReferrerURLAttrName));
  CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec());
}

TEST_F(FileMetadataLinuxTest, InvalidURLsTest) {
  if (!is_xattr_supported()) return;
  GURL invalid_url;
  vector<string> attr_names;
  AddOriginMetadataToFile(test_file(), invalid_url, invalid_url);
  GetExtendedAttributeNames(&attr_names);
  EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
      kSourceURLAttrName));
  EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
      kReferrerURLAttrName));
}

}  // namespace
}  // namespace content