testflock.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include "testflock.h"
  17. #include "testutil.h"
  18. #include "fspr_pools.h"
  19. #include "fspr_thread_proc.h"
  20. #include "fspr_file_io.h"
  21. #include "fspr_file_info.h"
  22. #include "fspr_general.h"
  23. #include "fspr_strings.h"
  24. static int launch_reader(abts_case *tc)
  25. {
  26. fspr_proc_t proc = {0};
  27. fspr_procattr_t *procattr;
  28. const char *args[2];
  29. fspr_status_t rv;
  30. fspr_exit_why_e why;
  31. int exitcode;
  32. rv = fspr_procattr_create(&procattr, p);
  33. APR_ASSERT_SUCCESS(tc, "Couldn't create procattr", rv);
  34. rv = fspr_procattr_io_set(procattr, APR_NO_PIPE, APR_NO_PIPE,
  35. APR_NO_PIPE);
  36. APR_ASSERT_SUCCESS(tc, "Couldn't set io in procattr", rv);
  37. rv = fspr_procattr_error_check_set(procattr, 1);
  38. APR_ASSERT_SUCCESS(tc, "Couldn't set error check in procattr", rv);
  39. args[0] = "tryread" EXTENSION;
  40. args[1] = NULL;
  41. rv = fspr_proc_create(&proc, "./tryread" EXTENSION, args, NULL, procattr, p);
  42. APR_ASSERT_SUCCESS(tc, "Couldn't launch program", rv);
  43. ABTS_ASSERT(tc, "wait for child process",
  44. fspr_proc_wait(&proc, &exitcode, &why, APR_WAIT) == APR_CHILD_DONE);
  45. ABTS_ASSERT(tc, "child terminated normally", why == APR_PROC_EXIT);
  46. return exitcode;
  47. }
  48. static void test_withlock(abts_case *tc, void *data)
  49. {
  50. fspr_file_t *file;
  51. fspr_status_t rv;
  52. int code;
  53. rv = fspr_file_open(&file, TESTFILE, APR_WRITE|APR_CREATE,
  54. APR_OS_DEFAULT, p);
  55. APR_ASSERT_SUCCESS(tc, "Could not create file.", rv);
  56. ABTS_PTR_NOTNULL(tc, file);
  57. rv = fspr_file_lock(file, APR_FLOCK_EXCLUSIVE);
  58. APR_ASSERT_SUCCESS(tc, "Could not lock the file.", rv);
  59. ABTS_PTR_NOTNULL(tc, file);
  60. code = launch_reader(tc);
  61. ABTS_INT_EQUAL(tc, FAILED_READ, code);
  62. (void) fspr_file_close(file);
  63. }
  64. static void test_withoutlock(abts_case *tc, void *data)
  65. {
  66. int code;
  67. code = launch_reader(tc);
  68. ABTS_INT_EQUAL(tc, SUCCESSFUL_READ, code);
  69. }
  70. static void remove_lockfile(abts_case *tc, void *data)
  71. {
  72. APR_ASSERT_SUCCESS(tc, "Couldn't remove lock file.",
  73. fspr_file_remove(TESTFILE, p));
  74. }
  75. abts_suite *testflock(abts_suite *suite)
  76. {
  77. suite = ADD_SUITE(suite)
  78. abts_run_test(suite, test_withlock, NULL);
  79. abts_run_test(suite, test_withoutlock, NULL);
  80. abts_run_test(suite, remove_lockfile, NULL);
  81. return suite;
  82. }