1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. 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 "testflock.h"
18 #include "testutil.h"
19 #include "apr_pools.h"
20 #include "apr_thread_proc.h"
21 #include "apr_file_io.h"
22 #include "apr_file_info.h"
23 #include "apr_general.h"
24 #include "apr_strings.h"
25
launch_reader(abts_case * tc)26 static int launch_reader(abts_case *tc)
27 {
28 apr_proc_t proc = {0};
29 apr_procattr_t *procattr;
30 const char *args[2];
31 apr_status_t rv;
32 apr_exit_why_e why;
33 int exitcode;
34
35 rv = apr_procattr_create(&procattr, p);
36 APR_ASSERT_SUCCESS(tc, "Couldn't create procattr", rv);
37
38 rv = apr_procattr_io_set(procattr, APR_NO_PIPE, APR_NO_PIPE,
39 APR_NO_PIPE);
40 APR_ASSERT_SUCCESS(tc, "Couldn't set io in procattr", rv);
41
42 rv = apr_procattr_cmdtype_set(procattr, APR_PROGRAM_ENV);
43 APR_ASSERT_SUCCESS(tc, "Couldn't set copy environment", rv);
44
45 rv = apr_procattr_error_check_set(procattr, 1);
46 APR_ASSERT_SUCCESS(tc, "Couldn't set error check in procattr", rv);
47
48 args[0] = "tryread" EXTENSION;
49 args[1] = NULL;
50 rv = apr_proc_create(&proc, TESTBINPATH "tryread" EXTENSION, args, NULL, procattr, p);
51 APR_ASSERT_SUCCESS(tc, "Couldn't launch program", rv);
52
53 ABTS_ASSERT(tc, "wait for child process",
54 apr_proc_wait(&proc, &exitcode, &why, APR_WAIT) == APR_CHILD_DONE);
55
56 ABTS_ASSERT(tc, "child terminated normally", why == APR_PROC_EXIT);
57 return exitcode;
58 }
59
test_withlock(abts_case * tc,void * data)60 static void test_withlock(abts_case *tc, void *data)
61 {
62 apr_file_t *file;
63 apr_status_t rv;
64 int code;
65
66 rv = apr_file_open(&file, TESTFILE, APR_FOPEN_WRITE|APR_FOPEN_CREATE,
67 APR_OS_DEFAULT, p);
68 APR_ASSERT_SUCCESS(tc, "Could not create file.", rv);
69 ABTS_PTR_NOTNULL(tc, file);
70
71 rv = apr_file_lock(file, APR_FLOCK_EXCLUSIVE);
72 APR_ASSERT_SUCCESS(tc, "Could not lock the file.", rv);
73 ABTS_PTR_NOTNULL(tc, file);
74
75 code = launch_reader(tc);
76 ABTS_INT_EQUAL(tc, FAILED_READ, code);
77
78 (void) apr_file_close(file);
79 }
80
test_withoutlock(abts_case * tc,void * data)81 static void test_withoutlock(abts_case *tc, void *data)
82 {
83 int code;
84
85 code = launch_reader(tc);
86 ABTS_INT_EQUAL(tc, SUCCESSFUL_READ, code);
87 }
88
remove_lockfile(abts_case * tc,void * data)89 static void remove_lockfile(abts_case *tc, void *data)
90 {
91 APR_ASSERT_SUCCESS(tc, "Couldn't remove lock file.",
92 apr_file_remove(TESTFILE, p));
93 }
94
testflock(abts_suite * suite)95 abts_suite *testflock(abts_suite *suite)
96 {
97 suite = ADD_SUITE(suite)
98
99 abts_run_test(suite, test_withlock, NULL);
100 abts_run_test(suite, test_withoutlock, NULL);
101 abts_run_test(suite, remove_lockfile, NULL);
102
103 return suite;
104 }
105