tst-futimens1.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* vi: set sw=4 ts=4: */
  2. /* testcase for futimens(2)
  3. * Copyright (C) 2009 Bernhard Reutner-Fischer <uClibc@uClibc.org>
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <errno.h>
  9. #include <stdlib.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <unistd.h>
  13. struct
  14. {
  15. char *name; /* name of file to open */
  16. int flags; /* flags for file descriptor */
  17. const struct timespec ts[2];
  18. int err; /* expected errno */
  19. } tests [] =
  20. {
  21. {"futimens.tst", (O_CREAT|O_TRUNC), {{0,0},{0,0}}, 0},
  22. {"futimens.tst", (O_CREAT|O_TRUNC), {{99,0},{0,0}}, 0},
  23. {"futimens.tst", (O_CREAT|O_TRUNC), {{0,99},{0,0}}, 0},
  24. {"futimens.tst", (O_CREAT|O_TRUNC), {{0,0},{99,0}}, 0},
  25. {"futimens.tst", (O_CREAT|O_TRUNC), {{0,0},{0,99}}, 0},
  26. {"futimens.tst", (O_CREAT|O_TRUNC), {{11,2},{3,4}}, 0},
  27. };
  28. int do_test(int argc, char **argv) {
  29. char *name;
  30. int i, errors;
  31. errors = argc - argc + 0;
  32. unsigned has_stat_nsec = 0;
  33. {
  34. struct stat probe;
  35. /* Let's attempt an educated guess if this filesystem supports
  36. * nanosecond mtime. */
  37. if ((!stat(".", &probe)) && probe.st_mtim.tv_nsec)
  38. has_stat_nsec = 1;
  39. else if ((!stat(argv[0], &probe)) && probe.st_mtim.tv_nsec)
  40. has_stat_nsec = 1;
  41. }
  42. for (i=0; i < (int) (sizeof(tests)/sizeof(tests[0])); ++i) {
  43. int err, fd;
  44. struct stat sb;
  45. name = tests[i].name;
  46. if (*name != '.')
  47. unlink(name);
  48. fd = open(name, tests[i].flags);
  49. if (fd < 0)
  50. abort();
  51. errno = 0;
  52. err = futimens(fd, tests[i].ts);
  53. if ((errno && !err) || (!errno && err)) {
  54. err = errno;
  55. printf("FAILED test %d (errno and return value disagree)\n", i);
  56. ++errors;
  57. } else
  58. err = errno;
  59. if (err != tests[i].err) {
  60. printf("FAILED test %d (expected errno %d, got %d)\n",
  61. i, tests[i].err, err);
  62. ++errors;
  63. continue;
  64. }
  65. if (stat(name, &sb) < 0) {
  66. printf("FAILED test %d (verification)\n", i);
  67. ++errors;
  68. continue;
  69. } else {
  70. unsigned wrong = tests[i].ts[0].tv_sec != sb.st_atim.tv_sec ||
  71. tests[i].ts[0].tv_nsec != sb.st_atim.tv_nsec ||
  72. tests[i].ts[1].tv_sec != sb.st_mtim.tv_sec ||
  73. tests[i].ts[1].tv_nsec != sb.st_mtim.tv_nsec;
  74. if (wrong) {
  75. if (tests[i].ts[0].tv_sec != sb.st_atim.tv_sec) {
  76. printf("FAILED test %d (access time, sec: expected %ld, got %ld)\n",
  77. i, tests[i].ts[0].tv_sec, sb.st_atim.tv_sec);
  78. ++errors;
  79. }
  80. if (tests[i].ts[0].tv_nsec != sb.st_atim.tv_nsec) {
  81. printf("FAILED test %d (access time, nsec: expected %ld, got %ld)\n",
  82. i, tests[i].ts[0].tv_nsec, sb.st_atim.tv_nsec);
  83. errors += has_stat_nsec;
  84. }
  85. if (tests[i].ts[1].tv_sec != sb.st_mtim.tv_sec) {
  86. printf("FAILED test %d (modification time, sec: expected %ld, got %ld)\n",
  87. i, tests[i].ts[1].tv_sec, sb.st_mtim.tv_sec);
  88. ++errors;
  89. }
  90. if (tests[i].ts[1].tv_nsec != sb.st_mtim.tv_nsec) {
  91. printf("FAILED test %d (modification time, nsec: expected %ld, got %ld)\n",
  92. i, tests[i].ts[1].tv_nsec, sb.st_mtim.tv_nsec);
  93. errors += has_stat_nsec;
  94. }
  95. }
  96. }
  97. }
  98. if (*name != '.')
  99. unlink(name);
  100. printf("%d errors.\n", errors);
  101. return (!errors) ? EXIT_SUCCESS : EXIT_FAILURE;
  102. }
  103. #include <test-skeleton.c>