2016-06-20 23:38:29 +03:00
|
|
|
#!/usr/bin/env python3
|
2019-06-20 02:58:25 +03:00
|
|
|
# Copyright (c) Facebook, Inc. and its affiliates.
|
2016-05-12 23:43:17 +03:00
|
|
|
#
|
2019-06-20 02:58:25 +03:00
|
|
|
# This software may be used and distributed according to the terms of the
|
|
|
|
# GNU General Public License version 2.
|
2016-05-12 23:43:17 +03:00
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import os
|
2018-10-18 23:59:34 +03:00
|
|
|
from typing import Dict
|
2016-05-12 23:43:17 +03:00
|
|
|
|
2018-10-18 23:59:34 +03:00
|
|
|
from .lib import testcase
|
2018-05-10 07:33:49 +03:00
|
|
|
|
2016-05-26 18:22:23 +03:00
|
|
|
|
2018-11-07 01:50:10 +03:00
|
|
|
def getallxattrs(abspath: str) -> Dict[str, bytes]:
|
2018-10-18 23:59:34 +03:00
|
|
|
result = {}
|
|
|
|
for xattr in os.listxattr(abspath):
|
|
|
|
result[xattr] = os.getxattr(abspath, xattr)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def sha1(value: bytes) -> bytes:
|
|
|
|
return hashlib.sha1(value).hexdigest().encode("ascii")
|
2016-05-26 18:22:23 +03:00
|
|
|
|
|
|
|
|
2016-07-08 21:25:45 +03:00
|
|
|
@testcase.eden_repo_test
|
2018-04-05 03:31:25 +03:00
|
|
|
class XattrTest(testcase.EdenRepoTest):
|
2018-04-05 03:31:28 +03:00
|
|
|
def populate_repo(self) -> None:
|
2018-05-10 07:33:49 +03:00
|
|
|
self.repo.write_file("hello", "hola\n")
|
2018-10-18 23:59:34 +03:00
|
|
|
self.repo.write_file("subdir/file", "contents")
|
2018-05-10 07:33:49 +03:00
|
|
|
self.repo.commit("Initial commit.")
|
2016-07-08 21:25:39 +03:00
|
|
|
|
2018-04-05 03:31:28 +03:00
|
|
|
def test_get_sha1_xattr(self) -> None:
|
2018-05-10 07:33:49 +03:00
|
|
|
filename = os.path.join(self.mount, "hello")
|
2018-10-18 23:59:34 +03:00
|
|
|
xattr = os.getxattr(filename, "user.sha1")
|
2018-05-10 07:33:49 +03:00
|
|
|
contents = open(filename, "rb").read()
|
2016-05-26 18:22:23 +03:00
|
|
|
expected_sha1 = sha1(contents)
|
2016-05-12 23:43:17 +03:00
|
|
|
self.assertEqual(expected_sha1, xattr)
|
|
|
|
|
2016-05-26 18:22:23 +03:00
|
|
|
# and test what happens as we replace the file contents.
|
2018-05-10 07:33:49 +03:00
|
|
|
with open(filename, "w") as f:
|
|
|
|
f.write("foo")
|
2016-05-26 18:22:23 +03:00
|
|
|
f.flush()
|
2018-10-18 23:59:34 +03:00
|
|
|
self.assertEqual(sha1(b"foo"), os.getxattr(filename, "user.sha1"))
|
2016-05-26 18:22:23 +03:00
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
f.write("bar")
|
2016-05-26 18:22:23 +03:00
|
|
|
f.flush()
|
2018-10-18 23:59:34 +03:00
|
|
|
self.assertEqual(sha1(b"foobar"), os.getxattr(filename, "user.sha1"))
|
2016-05-26 18:22:23 +03:00
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
f.write("baz")
|
2016-05-26 18:22:23 +03:00
|
|
|
|
2018-10-18 23:59:34 +03:00
|
|
|
self.assertEqual(sha1(b"foobarbaz"), os.getxattr(filename, "user.sha1"))
|
2016-05-26 18:22:23 +03:00
|
|
|
|
2018-04-05 03:31:28 +03:00
|
|
|
def test_listxattr(self) -> None:
|
2018-10-18 23:59:34 +03:00
|
|
|
# Assert that listxattr on a directory is empty and does not break
|
|
|
|
# future listxattr calls.
|
|
|
|
self.assertEqual([], os.listxattr(os.path.join(self.mount, "subdir")))
|
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
filename = os.path.join(self.mount, "hello")
|
2018-10-18 23:59:34 +03:00
|
|
|
xattrs = getallxattrs(filename)
|
2018-05-10 07:33:49 +03:00
|
|
|
contents = open(filename, "rb").read()
|
2016-05-26 18:22:23 +03:00
|
|
|
expected_sha1 = sha1(contents)
|
2019-09-03 18:02:16 +03:00
|
|
|
self.assertEqual({}, xattrs)
|
2018-10-18 23:59:34 +03:00
|
|
|
|
|
|
|
def test_get_sha1_xattr_succeeds_after_querying_xattr_on_dir(self) -> None:
|
|
|
|
with self.assertRaises(OSError):
|
|
|
|
os.getxattr(self.mount, "does_not_exist")
|
|
|
|
|
|
|
|
filename = os.path.join(self.mount, "hello")
|
|
|
|
xattr = os.getxattr(filename, "user.sha1")
|
|
|
|
contents = open(filename, "rb").read()
|
|
|
|
expected_sha1 = sha1(contents)
|
|
|
|
self.assertEqual(expected_sha1, xattr)
|