tst-futimens1.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* vi: set sw=4 ts=4: */
  2. /* testcase
  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. for (i=0; i < (int) (sizeof(tests)/sizeof(tests[0])); ++i) {
  33. int err, fd;
  34. struct stat sb;
  35. name = tests[i].name;
  36. if (*name != '.')
  37. unlink(name);
  38. fd = open(name, tests[i].flags);
  39. if (fd < 0)
  40. abort();
  41. errno = 0;
  42. err = futimens(fd, tests[i].ts);
  43. if ((errno && !err) || (!errno && err)) {
  44. err = errno;
  45. printf("%s: FAILED test %d (errno and return value disagree)\n",
  46. argv[0], i);
  47. ++errors;
  48. } else
  49. err = errno;
  50. if (err != tests[i].err) {
  51. printf("%s: FAILED test %d (expected errno %d, got %d)\n",
  52. argv[0], i, tests[i].err, err);
  53. ++errors;
  54. continue;
  55. }
  56. if (stat(name, &sb) < 0) {
  57. printf("%s: FAILED test %d (verification)\n", argv[0], i);
  58. ++errors;
  59. continue;
  60. } else {
  61. unsigned wrong = tests[i].ts[0].tv_sec != sb.st_atim.tv_sec ||
  62. tests[i].ts[0].tv_nsec != sb.st_atim.tv_nsec ||
  63. tests[i].ts[1].tv_sec != sb.st_mtim.tv_sec ||
  64. tests[i].ts[1].tv_nsec != sb.st_mtim.tv_nsec;
  65. if (wrong) {
  66. ++errors;
  67. if (tests[i].ts[0].tv_sec != sb.st_atim.tv_sec)
  68. printf("%s: FAILED test %d (access time, sec: expected %ld, got %ld)\n",
  69. argv[0], i, tests[i].ts[0].tv_sec, sb.st_atim.tv_sec);
  70. if (tests[i].ts[0].tv_nsec != sb.st_atim.tv_nsec)
  71. printf("%s: FAILED test %d (access time, nsec: expected %ld, got %ld)\n",
  72. argv[0], i, tests[i].ts[0].tv_nsec, sb.st_atim.tv_nsec);
  73. if (tests[i].ts[1].tv_sec != sb.st_mtim.tv_sec)
  74. printf("%s: FAILED test %d (modification time, sec: expected %ld, got %ld)\n",
  75. argv[0], i, tests[i].ts[1].tv_sec, sb.st_mtim.tv_sec);
  76. if (tests[i].ts[1].tv_nsec != sb.st_mtim.tv_nsec)
  77. printf("%s: FAILED test %d (modification time, nsec: expected %ld, got %ld)\n",
  78. argv[0], i, tests[i].ts[1].tv_nsec, sb.st_mtim.tv_nsec);
  79. }
  80. }
  81. }
  82. if (*name != '.')
  83. unlink(name);
  84. printf("%d errors.\n", errors);
  85. return (!errors) ? EXIT_SUCCESS : EXIT_FAILURE;
  86. }
  87. #include <test-skeleton.c>