blob: 184268220712a5f823721ae1020ccf75b9afaa2a [file] [log] [blame]
Nick Kralevich2825f102015-05-31 13:43:13 -07001/*
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
17#include <gtest/gtest.h>
18
19#include <sys/types.h>
20#include <sys/xattr.h>
21
22#include "TemporaryFile.h"
23
24TEST(sys_xattr, setxattr) {
25 TemporaryFile tf;
26 char buf[10];
27 ASSERT_EQ(0, setxattr(tf.filename, "user.foo", "bar", 4, 0));
28 ASSERT_EQ(4, getxattr(tf.filename, "user.foo", buf, sizeof(buf)));
29 ASSERT_STREQ("bar", buf);
30 buf[0] = '\0';
31 ASSERT_EQ(4, lgetxattr(tf.filename, "user.foo", buf, sizeof(buf)));
32 ASSERT_STREQ("bar", buf);
33}
34
35TEST(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
43TEST(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
50TEST(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
58TEST(sys_xattr, fsetxattr_invalidfd) {
59 char buf[10];
60 errno = 0;
61 ASSERT_EQ(-1, fsetxattr(65535, "user.foo", "0123", 5, 0));
62 ASSERT_EQ(EBADF, errno);
63 errno = 0;
64 ASSERT_EQ(-1, fgetxattr(65535, "user.foo", buf, sizeof(buf)));
65 ASSERT_EQ(EBADF, errno);
66}
67
68TEST(sys_xattr, fsetxattr_with_opath) {
69 TemporaryFile tf;
70 int fd = open(tf.filename, O_PATH);
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
83}
84
85TEST(sys_xattr, fsetxattr_with_opath_toosmall) {
86 TemporaryFile tf;
87 int fd = open(tf.filename, O_PATH);
88 ASSERT_NE(-1, fd);
89
90 int res = fsetxattr(fd, "user.foo", "01234567890123456789", 21, 0);
91#if defined(__BIONIC__)
92 char buf[10];
93 ASSERT_EQ(0, res);
94 ASSERT_EQ(-1, fgetxattr(fd, "user.foo", buf, sizeof(buf)));
95 ASSERT_EQ(ERANGE, errno);
96#else
97 ASSERT_EQ(-1, res);
98 ASSERT_EQ(EBADF, errno);
99#endif
100}