]> granicus.if.org Git - strace/blob - tests/xutimes.c
tests: parametrize utimes.c
[strace] / tests / xutimes.c
1 /*
2  * Check decoding of utimes/osf_utimes syscall.
3  *
4  * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef TEST_SYSCALL_NR
31 # error TEST_SYSCALL_NR must be defined
32 #endif
33
34 #ifndef TEST_SYSCALL_STR
35 # error TEST_SYSCALL_STR must be defined
36 #endif
37
38 #ifndef TEST_STRUCT
39 # error TEST_STRUCT must be defined
40 #endif
41
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <sys/time.h>
45 #include <unistd.h>
46
47 static void
48 print_tv(const TEST_STRUCT *const tv)
49 {
50         printf("{tv_sec=%ju, tv_usec=%ju}",
51                (uintmax_t) tv->tv_sec, (uintmax_t) tv->tv_usec);
52 }
53
54 static const char *errstr;
55
56 static long
57 k_utimes(const kernel_ulong_t pathname, const kernel_ulong_t times)
58 {
59         long rc = syscall(TEST_SYSCALL_NR, pathname, times);
60         errstr = sprintrc(rc);
61         return rc;
62 }
63
64 int
65 main(void)
66 {
67         static const char proto_fname[] = TEST_SYSCALL_STR "_sample";
68         static const char qname[] = "\"" TEST_SYSCALL_STR "_sample\"";
69
70         char *const fname = tail_memdup(proto_fname, sizeof(proto_fname));
71         const kernel_ulong_t kfname = (uintptr_t) fname;
72         TEST_STRUCT *const tv = tail_alloc(sizeof(*tv) * 2);
73
74         /* pathname */
75         k_utimes(0, 0);
76         printf("%s(NULL, NULL) = %s\n", TEST_SYSCALL_STR, errstr);
77
78         k_utimes(kfname + sizeof(proto_fname) - 1, 0);
79         printf("%s(\"\", NULL) = %s\n", TEST_SYSCALL_STR, errstr);
80
81         k_utimes(kfname, 0);
82         printf("%s(%s, NULL) = %s\n", TEST_SYSCALL_STR, qname, errstr);
83
84         fname[sizeof(proto_fname) - 1] = '+';
85         k_utimes(kfname, 0);
86         fname[sizeof(proto_fname) - 1] = '\0';
87         printf("%s(%p, NULL) = %s\n", TEST_SYSCALL_STR, fname, errstr);
88
89         if (F8ILL_KULONG_SUPPORTED) {
90                 k_utimes(f8ill_ptr_to_kulong(fname), 0);
91                 printf("%s(%#jx, NULL) = %s\n", TEST_SYSCALL_STR,
92                        (uintmax_t) f8ill_ptr_to_kulong(fname), errstr);
93         }
94
95         /* times */
96         k_utimes(kfname, (uintptr_t) (tv + 1));
97         printf("%s(%s, %p) = %s\n", TEST_SYSCALL_STR,
98                qname, tv + 1, errstr);
99
100         k_utimes(kfname, (uintptr_t) (tv + 2));
101         printf("%s(%s, %p) = %s\n", TEST_SYSCALL_STR,
102                qname, tv + 2, errstr);
103
104         tv[0].tv_sec = 1492358607;
105         tv[0].tv_usec = 345678912;
106         tv[1].tv_sec = 1492356078;
107         tv[1].tv_usec = 456789023;
108
109         k_utimes(kfname, (uintptr_t) tv);
110         printf("%s(%s, [", TEST_SYSCALL_STR, qname);
111         print_tv(&tv[0]);
112         printf(", ");
113         print_tv(&tv[1]);
114         printf("]) = %s\n", errstr);
115
116         tv[0].tv_usec = 345678;
117         tv[1].tv_usec = 456789;
118
119         k_utimes(kfname, (uintptr_t) tv);
120         printf("%s(%s, [", TEST_SYSCALL_STR, qname);
121         print_tv(&tv[0]);
122         printf(", ");
123         print_tv(&tv[1]);
124         printf("]) = %s\n", errstr);
125
126         if (F8ILL_KULONG_SUPPORTED) {
127                 k_utimes(kfname, f8ill_ptr_to_kulong(tv));
128                 printf("%s(%s, %#jx) = %s\n", TEST_SYSCALL_STR,
129                        qname, (uintmax_t) f8ill_ptr_to_kulong(tv), errstr);
130         }
131
132         puts("+++ exited with 0 +++");
133         return 0;
134 }