blob: 1b53210710fa375786ab294e3a0ef64b92856cf6 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Zeng Lingganga0a3bd92014-03-13 21:09:54 +08002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 * 07/2001 Ported by Wayne Boyer
4 *
Zeng Lingganga0a3bd92014-03-13 21:09:54 +08005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
Zeng Lingganga0a3bd92014-03-13 21:09:54 +080019/*
20 * DESCRIPTION
21 * 1. open a new file without O_CREAT, ENOENT should be returned.
Zeng Linggang8553a4b2014-03-13 21:10:48 +080022 * 2. open a file with O_RDONLY | O_NOATIME and the caller was not
23 * privileged, EPERM should be returned.
Zeng Lingganga0a3bd92014-03-13 21:09:54 +080024 */
25
Zeng Linggang8553a4b2014-03-13 21:10:48 +080026#define _GNU_SOURCE
27
plars865695b2001-08-27 22:15:12 +000028#include <stdio.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <errno.h>
32#include <sys/fcntl.h>
Zeng Linggang8553a4b2014-03-13 21:10:48 +080033#include <pwd.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080034#include "test.h"
Zeng Linggang8553a4b2014-03-13 21:10:48 +080035#include "safe_macros.h"
36#include "lapi/fcntl.h"
plars865695b2001-08-27 22:15:12 +000037
Zeng Lingganga0a3bd92014-03-13 21:09:54 +080038#define TEST_FILE "test_file"
Zeng Linggang8553a4b2014-03-13 21:10:48 +080039#define TEST_FILE2 "test_file2"
plars865695b2001-08-27 22:15:12 +000040
Zeng Lingganga0a3bd92014-03-13 21:09:54 +080041char *TCID = "open02";
plars865695b2001-08-27 22:15:12 +000042
Wanlong Gao001ce032013-02-04 17:11:18 +080043static void cleanup(void);
44static void setup(void);
plars865695b2001-08-27 22:15:12 +000045
Zeng Lingganga0a3bd92014-03-13 21:09:54 +080046static struct test_case_t {
47 char *filename;
Zeng Linggang8553a4b2014-03-13 21:10:48 +080048 int flag;
Zeng Lingganga0a3bd92014-03-13 21:09:54 +080049 int exp_errno;
50} test_cases[] = {
Zeng Linggang8553a4b2014-03-13 21:10:48 +080051 {TEST_FILE, O_RDWR, ENOENT},
52 {TEST_FILE2, O_RDONLY | O_NOATIME, EPERM},
Zeng Lingganga0a3bd92014-03-13 21:09:54 +080053};
54
55int TST_TOTAL = ARRAY_SIZE(test_cases);
56static void open_verify(const struct test_case_t *);
57
plars74948ad2002-11-14 16:16:14 +000058int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000059{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020060 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020061 const char *msg;
Zeng Lingganga0a3bd92014-03-13 21:09:54 +080062 int i;
plars865695b2001-08-27 22:15:12 +000063
Wanlong Gao001ce032013-02-04 17:11:18 +080064 msg = parse_opts(ac, av, NULL, NULL);
65 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080066 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000067
Wanlong Gao001ce032013-02-04 17:11:18 +080068 setup();
plars865695b2001-08-27 22:15:12 +000069
plars865695b2001-08-27 22:15:12 +000070 for (lc = 0; TEST_LOOPING(lc); lc++) {
Zeng Lingganga0a3bd92014-03-13 21:09:54 +080071 tst_count = 0;
72 for (i = 0; i < TST_TOTAL; i++)
73 open_verify(&test_cases[i]);
plars865695b2001-08-27 22:15:12 +000074 }
plars865695b2001-08-27 22:15:12 +000075
Wanlong Gao001ce032013-02-04 17:11:18 +080076 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080077 tst_exit();
plars865695b2001-08-27 22:15:12 +000078}
79
Wanlong Gao001ce032013-02-04 17:11:18 +080080static void setup(void)
plars865695b2001-08-27 22:15:12 +000081{
Zeng Linggang8553a4b2014-03-13 21:10:48 +080082 struct passwd *ltpuser;
83
84 tst_require_root(NULL);
85
plars865695b2001-08-27 22:15:12 +000086 tst_sig(NOFORK, DEF_HANDLER, cleanup);
87
plars865695b2001-08-27 22:15:12 +000088 TEST_PAUSE;
Zeng Linggang8553a4b2014-03-13 21:10:48 +080089
90 tst_tmpdir();
91
92 SAFE_TOUCH(cleanup, TEST_FILE2, 0644, NULL);
93
94 ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
95
96 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
Zeng Lingganga0a3bd92014-03-13 21:09:54 +080097}
plars865695b2001-08-27 22:15:12 +000098
Zeng Lingganga0a3bd92014-03-13 21:09:54 +080099static void open_verify(const struct test_case_t *test)
100{
Zeng Linggang8553a4b2014-03-13 21:10:48 +0800101 TEST(open(test->filename, test->flag, 0444));
plars865695b2001-08-27 22:15:12 +0000102
Zeng Lingganga0a3bd92014-03-13 21:09:54 +0800103 if (TEST_RETURN != -1) {
104 tst_resm(TFAIL, "open(%s) succeeded unexpectedly",
105 test->filename);
106 return;
107 }
108
Zeng Lingganga0a3bd92014-03-13 21:09:54 +0800109 if (TEST_ERRNO != test->exp_errno) {
110 tst_resm(TFAIL | TTERRNO,
111 "open() failed unexpectedly; expected: %d - %s",
112 test->exp_errno, strerror(test->exp_errno));
113 } else {
114 tst_resm(TPASS | TTERRNO, "open() failed as expected");
115 }
plars865695b2001-08-27 22:15:12 +0000116}
117
Wanlong Gao001ce032013-02-04 17:11:18 +0800118static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000119{
Zeng Linggang8553a4b2014-03-13 21:10:48 +0800120 if (seteuid(0))
121 tst_resm(TWARN | TERRNO, "seteuid(0) failed");
122
Zeng Linggang8553a4b2014-03-13 21:10:48 +0800123 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700124}