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 | |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 24 | #include "utils.h" |
| 25 | |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 26 | TEST(sys_xattr, setxattr) { |
| 27 | TemporaryFile tf; |
| 28 | char buf[10]; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 29 | ASSERT_EQ(0, setxattr(tf.path, "user.foo", "bar", 4, 0)); |
| 30 | ASSERT_EQ(4, getxattr(tf.path, "user.foo", buf, sizeof(buf))); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 31 | ASSERT_STREQ("bar", buf); |
| 32 | buf[0] = '\0'; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 33 | ASSERT_EQ(4, lgetxattr(tf.path, "user.foo", buf, sizeof(buf))); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 34 | ASSERT_STREQ("bar", buf); |
| 35 | } |
| 36 | |
| 37 | TEST(sys_xattr, fsetxattr) { |
| 38 | TemporaryFile tf; |
| 39 | char buf[10]; |
| 40 | ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "bar", 4, 0)); |
| 41 | ASSERT_EQ(4, fgetxattr(tf.fd, "user.foo", buf, sizeof(buf))); |
| 42 | ASSERT_STREQ("bar", buf); |
| 43 | } |
| 44 | |
| 45 | TEST(sys_xattr, fsetxattr_zerobuf) { |
| 46 | TemporaryFile tf; |
| 47 | char buf[10]; |
| 48 | ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "", 0, 0)); |
| 49 | ASSERT_EQ(0, fgetxattr(tf.fd, "user.foo", buf, sizeof(buf))); |
| 50 | } |
| 51 | |
| 52 | TEST(sys_xattr, fsetxattr_toosmallbuf) { |
| 53 | TemporaryFile tf; |
| 54 | char buf[10]; |
| 55 | ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "01234567890123456789", 21, 0)); |
| 56 | ASSERT_EQ(-1, fgetxattr(tf.fd, "user.foo", buf, sizeof(buf))); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 57 | ASSERT_ERRNO(ERANGE); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Elliott Hughes | b82f5cf | 2021-03-08 14:09:43 -0800 | [diff] [blame] | 60 | TEST(sys_xattr, fsetxattr_invalid_fd) { |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 61 | char buf[10]; |
| 62 | errno = 0; |
Elliott Hughes | b82f5cf | 2021-03-08 14:09:43 -0800 | [diff] [blame] | 63 | ASSERT_EQ(-1, fsetxattr(-1, "user.foo", "0123", 5, 0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 64 | ASSERT_ERRNO(EBADF); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 65 | errno = 0; |
Elliott Hughes | b82f5cf | 2021-03-08 14:09:43 -0800 | [diff] [blame] | 66 | ASSERT_EQ(-1, fgetxattr(-1, "user.foo", buf, sizeof(buf))); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 67 | ASSERT_ERRNO(EBADF); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | TEST(sys_xattr, fsetxattr_with_opath) { |
| 71 | TemporaryFile tf; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 72 | int fd = open(tf.path, O_PATH); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 73 | ASSERT_NE(-1, fd); |
| 74 | |
| 75 | int res = fsetxattr(fd, "user.foo", "bar", 4, 0); |
| 76 | #if defined(__BIONIC__) |
| 77 | char buf[10]; |
| 78 | ASSERT_EQ(0, res); |
| 79 | ASSERT_EQ(4, fgetxattr(fd, "user.foo", buf, sizeof(buf))); |
| 80 | ASSERT_STREQ("bar", buf); |
| 81 | #else |
| 82 | ASSERT_EQ(-1, res); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 83 | ASSERT_ERRNO(EBADF); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 84 | #endif |
Nick Kralevich | e1d0810 | 2015-06-06 11:23:26 -0700 | [diff] [blame] | 85 | close(fd); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST(sys_xattr, fsetxattr_with_opath_toosmall) { |
| 89 | TemporaryFile tf; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 90 | int fd = open(tf.path, O_PATH); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 91 | ASSERT_NE(-1, fd); |
| 92 | |
| 93 | int res = fsetxattr(fd, "user.foo", "01234567890123456789", 21, 0); |
| 94 | #if defined(__BIONIC__) |
| 95 | char buf[10]; |
| 96 | ASSERT_EQ(0, res); |
| 97 | ASSERT_EQ(-1, fgetxattr(fd, "user.foo", buf, sizeof(buf))); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 98 | ASSERT_ERRNO(ERANGE); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 99 | #else |
| 100 | ASSERT_EQ(-1, res); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 101 | ASSERT_ERRNO(EBADF); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 102 | #endif |
Nick Kralevich | e1d0810 | 2015-06-06 11:23:26 -0700 | [diff] [blame] | 103 | close(fd); |
| 104 | } |
| 105 | |
| 106 | TEST(sys_xattr, flistattr) { |
| 107 | TemporaryFile tf; |
| 108 | char buf[65536]; // 64kB is max possible xattr list size. See "man 7 xattr". |
| 109 | ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "bar", 4, 0)); |
| 110 | ssize_t result = flistxattr(tf.fd, buf, sizeof(buf)); |
| 111 | ASSERT_TRUE(result >= 9); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 112 | ASSERT_TRUE(memmem(buf, sizeof(buf), "user.foo", 9) != nullptr); |
Nick Kralevich | e1d0810 | 2015-06-06 11:23:26 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TEST(sys_xattr, flistattr_opath) { |
| 116 | TemporaryFile tf; |
| 117 | char buf[65536]; // 64kB is max possible xattr list size. See "man 7 xattr". |
| 118 | ASSERT_EQ(0, fsetxattr(tf.fd, "user.foo", "bar", 4, 0)); |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 119 | int fd = open(tf.path, O_PATH); |
Nick Kralevich | e1d0810 | 2015-06-06 11:23:26 -0700 | [diff] [blame] | 120 | ASSERT_NE(-1, fd); |
| 121 | ssize_t res = flistxattr(fd, buf, sizeof(buf)); |
| 122 | #if defined(__BIONIC__) |
| 123 | ASSERT_TRUE(res >= 9); |
| 124 | ASSERT_TRUE(static_cast<size_t>(res) <= sizeof(buf)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 125 | ASSERT_TRUE(memmem(buf, res, "user.foo", 9) != nullptr); |
Nick Kralevich | e1d0810 | 2015-06-06 11:23:26 -0700 | [diff] [blame] | 126 | #else |
| 127 | ASSERT_EQ(-1, res); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 128 | ASSERT_ERRNO(EBADF); |
Nick Kralevich | e1d0810 | 2015-06-06 11:23:26 -0700 | [diff] [blame] | 129 | #endif |
| 130 | close(fd); |
Nick Kralevich | 2825f10 | 2015-05-31 13:43:13 -0700 | [diff] [blame] | 131 | } |
Elliott Hughes | b82f5cf | 2021-03-08 14:09:43 -0800 | [diff] [blame] | 132 | |
| 133 | TEST(sys_xattr, flistattr_invalid_fd) { |
| 134 | char buf[65536]; // 64kB is max possible xattr list size. See "man 7 xattr". |
| 135 | errno = 0; |
| 136 | ASSERT_EQ(-1, flistxattr(-1, buf, sizeof(buf))); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 137 | ASSERT_ERRNO(EBADF); |
Elliott Hughes | b82f5cf | 2021-03-08 14:09:43 -0800 | [diff] [blame] | 138 | } |