2016-06-20 23:38:29 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
2017-01-21 09:02:33 +03:00
|
|
|
# Copyright (c) 2016-present, Facebook, Inc.
|
2016-05-12 23:43:17 +03:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# This source code is licensed under the BSD-style license found in the
|
|
|
|
# LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
# of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import os
|
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
from .lib import fs, testcase
|
|
|
|
|
2016-05-26 18:22:23 +03:00
|
|
|
|
2018-04-05 03:31:28 +03:00
|
|
|
def sha1(value: bytes) -> str:
|
2016-05-26 18:22:23 +03:00
|
|
|
return hashlib.sha1(value).hexdigest()
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
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")
|
|
|
|
xattr = fs.getxattr(filename, "user.sha1")
|
|
|
|
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-05-10 07:33:49 +03:00
|
|
|
self.assertEqual(sha1(b"foo"), fs.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-05-10 07:33:49 +03:00
|
|
|
self.assertEqual(sha1(b"foobar"), fs.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-05-10 07:33:49 +03:00
|
|
|
self.assertEqual(sha1(b"foobarbaz"), fs.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-05-10 07:33:49 +03:00
|
|
|
filename = os.path.join(self.mount, "hello")
|
2016-05-12 23:43:17 +03:00
|
|
|
xattrs = fs.listxattr(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)
|
2018-05-10 07:33:49 +03:00
|
|
|
self.assertEqual({"user.sha1": expected_sha1}, xattrs)
|