Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 17 | #include <fcntl.h> |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 18 | #include <sys/types.h> |
| 19 | #include <sys/xattr.h> |
| 20 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 21 | #include <android-base/file.h> |
| 22 | #include <gtest/gtest.h> |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 23 | |
| 24 | TEST(sys_xattr, setxattr) { |
| 25 | TemporaryFile tf; |
| 26 | char buf[10]; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 27 | ASSERT_EQ(0, setxattr(tf.path, "user.foo", "bar", 4, 0)); |
| 28 | ASSERT_EQ(4, getxattr(tf.path, "user.foo", buf, sizeof(buf))); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 29 | ASSERT_STREQ("bar", buf); |
| 30 | buf[0] = '\0'; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 31 | ASSERT_EQ(4, lgetxattr(tf.path, "user.foo", buf, sizeof(buf))); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 32 | ASSERT_STREQ("bar", buf); |
| 33 | } |
| 34 | |
| 35 | TEST(sys_xattr, fsetxattr) { |
| 36 | TemporaryFile tf; |
| 37 | char buf[10]; |
| 38 | ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "bar", 4, 0)); |
| 39 | ASSERT_EQ(4, fgetxattr(tf.fd, "user.foo", buf, sizeof(buf))); |
| 40 | ASSERT_STREQ("bar", buf); |
| 41 | } |
| 42 | |
| 43 | TEST(sys_xattr, fsetxattr_zerobuf) { |
| 44 | TemporaryFile tf; |
| 45 | char buf[10]; |
| 46 | ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "", 0, 0)); |
| 47 | ASSERT_EQ(0, fgetxattr(tf.fd, "user.foo", buf, sizeof(buf))); |
| 48 | } |
| 49 | |
| 50 | TEST(sys_xattr, fsetxattr_toosmallbuf) { |
| 51 | TemporaryFile tf; |
| 52 | char buf[10]; |
| 53 | ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "01234567890123456789", 21, 0)); |
| 54 | ASSERT_EQ(-1, fgetxattr(tf.fd, "user.foo", buf, sizeof(buf))); |
| 55 | ASSERT_EQ(ERANGE, errno); |
| 56 | } |
| 57 | |
Elliott Hughes | b82f5cf | 2021-03-08 14:09:43 -0800 | [diff] [blame] | 58 | TEST(sys_xattr, fsetxattr_invalid_fd) { |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 59 | char buf[10]; |
| 60 | errno = 0; |
Elliott Hughes | b82f5cf | 2021-03-08 14:09:43 -0800 | [diff] [blame] | 61 | ASSERT_EQ(-1, fsetxattr(-1, "user.foo", "0123", 5, 0)); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 62 | ASSERT_EQ(EBADF, errno); |
| 63 | errno = 0; |
Elliott Hughes | b82f5cf | 2021-03-08 14:09:43 -0800 | [diff] [blame] | 64 | ASSERT_EQ(-1, fgetxattr(-1, "user.foo", buf, sizeof(buf))); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 65 | ASSERT_EQ(EBADF, errno); |
| 66 | } |
| 67 | |
| 68 | TEST(sys_xattr, fsetxattr_with_opath) { |
| 69 | TemporaryFile tf; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 70 | int fd = open(tf.path, O_PATH); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 71 | ASSERT_NE(-1, fd); |
| 72 | |
| 73 | int res = fsetxattr(fd, "user.foo", "bar", 4, 0); |
| 74 | #if defined(__BIONIC__) |
| 75 | char buf[10]; |
| 76 | ASSERT_EQ(0, res); |
| 77 | ASSERT_EQ(4, fgetxattr(fd, "user.foo", buf, sizeof(buf))); |
| 78 | ASSERT_STREQ("bar", buf); |
| 79 | #else |
| 80 | ASSERT_EQ(-1, res); |
| 81 | ASSERT_EQ(EBADF, errno); |
| 82 | #endif |
Nick Kralevich | e1d0810 | 2015-06-06 11:23:26 -0700 | [diff] [blame] | 83 | close(fd); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST(sys_xattr, fsetxattr_with_opath_toosmall) { |
| 87 | TemporaryFile tf; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 88 | int fd = open(tf.path, O_PATH); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 89 | ASSERT_NE(-1, fd); |
| 90 | |
| 91 | int res = fsetxattr(fd, "user.foo", "01234567890123456789", 21, 0); |
| 92 | #if defined(__BIONIC__) |
| 93 | char buf[10]; |
| 94 | ASSERT_EQ(0, res); |
| 95 | ASSERT_EQ(-1, fgetxattr(fd, "user.foo", buf, sizeof(buf))); |
| 96 | ASSERT_EQ(ERANGE, errno); |
| 97 | #else |
| 98 | ASSERT_EQ(-1, res); |
| 99 | ASSERT_EQ(EBADF, errno); |
| 100 | #endif |
Nick Kralevich | e1d0810 | 2015-06-06 11:23:26 -0700 | [diff] [blame] | 101 | close(fd); |
| 102 | } |
| 103 | |
| 104 | TEST(sys_xattr, flistattr) { |
| 105 | TemporaryFile tf; |
| 106 | char buf[65536]; // 64kB is max possible xattr list size. See "man 7 xattr". |
| 107 | ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "bar", 4, 0)); |
| 108 | ssize_t result = flistxattr(tf.fd, buf, sizeof(buf)); |
| 109 | ASSERT_TRUE(result >= 9); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 110 | ASSERT_TRUE(memmem(buf, sizeof(buf), "user.foo", 9) != nullptr); |
Nick Kralevich | e1d0810 | 2015-06-06 11:23:26 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST(sys_xattr, flistattr_opath) { |
| 114 | TemporaryFile tf; |
| 115 | char buf[65536]; // 64kB is max possible xattr list size. See "man 7 xattr". |
| 116 | ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "bar", 4, 0)); |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 117 | int fd = open(tf.path, O_PATH); |
Nick Kralevich | e1d0810 | 2015-06-06 11:23:26 -0700 | [diff] [blame] | 118 | ASSERT_NE(-1, fd); |
| 119 | ssize_t res = flistxattr(fd, buf, sizeof(buf)); |
| 120 | #if defined(__BIONIC__) |
| 121 | ASSERT_TRUE(res >= 9); |
| 122 | ASSERT_TRUE(static_cast<size_t>(res) <= sizeof(buf)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 123 | ASSERT_TRUE(memmem(buf, res, "user.foo", 9) != nullptr); |
Nick Kralevich | e1d0810 | 2015-06-06 11:23:26 -0700 | [diff] [blame] | 124 | #else |
| 125 | ASSERT_EQ(-1, res); |
| 126 | ASSERT_EQ(EBADF, errno); |
| 127 | #endif |
| 128 | close(fd); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 129 | } |
Elliott Hughes | b82f5cf | 2021-03-08 14:09:43 -0800 | [diff] [blame] | 130 | |
| 131 | TEST(sys_xattr, flistattr_invalid_fd) { |
| 132 | char buf[65536]; // 64kB is max possible xattr list size. See "man 7 xattr". |
| 133 | errno = 0; |
| 134 | ASSERT_EQ(-1, flistxattr(-1, buf, sizeof(buf))); |
| 135 | ASSERT_EQ(EBADF, errno); |
| 136 | } |