ftpfs-ls_unittest.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. FTP file system
  3. Copyright (C) 2006 Robson Braga Araujo <robsonbraga@gmail.com>
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. */
  7. #define _XOPEN_SOURCE 600 /* glibc2 needs this */
  8. #include <time.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #include "ftpfs.h"
  14. #include "ftpfs-ls.h"
  15. struct ftpfs ftpfs;
  16. #define check(sbuf, dev, ino, mode, nlink, uid, gid, \
  17. rdev, size, blksize, blocks, date) \
  18. do { \
  19. struct tm tm; \
  20. time_t tt; \
  21. memset(&tm, 0, sizeof(tm)); \
  22. strptime(date, "%H:%M:%S %d/%m/%Y", &tm); \
  23. tt = mktime(&tm); \
  24. assert(sbuf.st_dev == (dev)); \
  25. assert(sbuf.st_ino == (ino)); \
  26. assert(sbuf.st_mode == (mode)); \
  27. assert(sbuf.st_nlink == (nlink)); \
  28. assert(sbuf.st_uid == (uid)); \
  29. assert(sbuf.st_gid == (gid)); \
  30. assert(sbuf.st_rdev == (rdev)); \
  31. assert(sbuf.st_size == (size)); \
  32. assert(sbuf.st_blksize == (blksize)); \
  33. assert(sbuf.st_blocks == (blocks)); \
  34. assert(sbuf.st_atime == tt); \
  35. assert(sbuf.st_ctime == tt); \
  36. assert(sbuf.st_mtime == tt); \
  37. } while (0);
  38. int main(int argc, char **argv) {
  39. const char *list;
  40. char line[256];
  41. struct fuse_cache_operations dummy_oper;
  42. struct stat sbuf;
  43. int err;
  44. struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
  45. char linkbuf[1024];
  46. char date[20];
  47. time_t tt;
  48. struct tm tm;
  49. struct tm test_tm;
  50. ftpfs.debug = 1;
  51. tt = time(NULL);
  52. gmtime_r(&tt, &tm);
  53. ftpfs.blksize = 4096;
  54. memset(&dummy_oper, 0, sizeof(dummy_oper));
  55. err = cache_parse_options(&args);
  56. cache_init(&dummy_oper);
  57. list = "05-22-03 12:13PM <DIR> chinese_pr\r\n";
  58. err = parse_dir(list, "/", "chinese_pr", &sbuf, NULL, 0, NULL, NULL);
  59. assert(err == 0);
  60. check(sbuf, 0, 0, S_IFDIR, 1, 0, 0, 0, 0, 0, 0, "12:13:00 22/05/2003");
  61. err = parse_dir(list, "/", "hinese_pr", &sbuf, NULL, 0, NULL, NULL);
  62. assert(err == 1);
  63. list = "05-14-03 02:49PM 40448 PR_AU13_CH.doc\r\n";
  64. err = parse_dir(list, "/", "PR_AU13_CH.doc", &sbuf, NULL, 0, NULL, NULL);
  65. assert(err == 0);
  66. check(sbuf, 0, 0, S_IFREG, 1, 0, 0, 0, 40448, 4096, 80, "14:49:00 14/05/2003");
  67. list = "11-25-04 09:17AM 20075882 242_310_Condor_en_ok.pdf\r\n";
  68. err = parse_dir(list, "/", "242_310_Condor_en_ok.pdf", &sbuf, NULL, 0, NULL, NULL);
  69. assert(err == 0);
  70. check(sbuf, 0, 0, S_IFREG, 1, 0, 0, 0, 20075882, 4096, 39216, "09:17:00 25/11/2004");
  71. list = "lrwxrwxrwx 1 1 17 Nov 24 2002 lg -> cidirb/documentos\r\n";
  72. err = parse_dir(list, "/", "lg", &sbuf, linkbuf, 1024, NULL, NULL);
  73. assert(err == 0);
  74. assert(!strcmp(linkbuf, "cidirb/documentos"));
  75. check(sbuf, 0, 0, S_IFLNK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH, 1, 0, 0, 0, 17, 4096, 8, "00:00:00 24/11/2002");
  76. list = "lrwxrwxrwx 1 1137 1100 14 Mar 12 2004 molbio -> Science/molbio\r\n";
  77. err = parse_dir(list, "/", "molbio", &sbuf, linkbuf, 1024, NULL, NULL);
  78. assert(err == 0);
  79. assert(!strcmp(linkbuf, "Science/molbio"));
  80. check(sbuf, 0, 0, S_IFLNK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH, 1, 0, 0, 0, 14, 4096, 8, "00:00:00 12/03/2004");
  81. // Test a date six months in the past
  82. test_tm = tm;
  83. test_tm.tm_mon -= 6;
  84. if (test_tm.tm_mon < 0) {
  85. test_tm.tm_mon += 12;
  86. test_tm.tm_year--;
  87. }
  88. strftime(line, 256,
  89. "drwxr-xr-x 4 robson users 4096 %b %d 00:00 tests\r\n", &test_tm);
  90. err = parse_dir(line, "/", "tests", &sbuf, NULL, 0, NULL, NULL);
  91. assert(err == 0);
  92. strftime(date, 20, "00:00:00 %d/%m/%Y", &test_tm);
  93. check(sbuf, 0, 0, S_IFDIR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, 4, 0, 0, 0, 4096, 4096, 8, date);
  94. list = "dr-xr-xr-x 2 root 512 Apr 8 1994 etc\r\n";
  95. err = parse_dir(list, "/", "etc", &sbuf, NULL, 0, NULL, NULL);
  96. assert(err == 0);
  97. check(sbuf, 0, 0, S_IFDIR|S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, 2, 0, 0, 0, 512, 4096, 8, "00:00:00 08/04/1994");
  98. // Test a date a little bit in the past
  99. test_tm = tm;
  100. strftime(line, 256,
  101. "---------- 1 robson users 1803128 %b %d 00:00 ls-lR.Z\r\n",
  102. &test_tm);
  103. err = parse_dir(line, "/", "ls-lR.Z", &sbuf, NULL, 0, NULL, NULL);
  104. assert(err == 0);
  105. strftime(date, 20, "00:00:00 %d/%m/%Y", &test_tm);
  106. check(sbuf, 0, 0, S_IFREG, 1, 0, 0, 0, 1803128, 4096, 3528, date);
  107. // Test a file with space
  108. list = "-rw-r--r-- 1 robson users 1803128 Jan 01 2001 test\r\n";
  109. err = parse_dir(list, "/", " test", &sbuf, NULL, 0, NULL, NULL);
  110. assert(err == 0);
  111. check(sbuf, 0, 0, S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, 1, 0, 0, 0, 1803128, 4096, 3528, "00:00:00 01/01/2001");
  112. list = "drwxrwsr-x+ 14 cristol molvis 1024 Feb 17 2000 v2\r\n";
  113. err = parse_dir(list, "/", "v2", &sbuf, NULL, 0, NULL, NULL);
  114. assert(err == 0);
  115. check(sbuf, 0, 0, S_IFDIR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IXOTH, 14, 0, 0, 0, 1024, 4096, 8, "00:00:00 17/02/2000");
  116. list = "drwxrwsr-x+144 cristol molvis 10240 Dec 31 2005 v11\r\n";
  117. err = parse_dir(list, "/", "v11", &sbuf, NULL, 0, NULL, NULL);
  118. assert(err == 0);
  119. check(sbuf, 0, 0, S_IFDIR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IXOTH, 144, 0, 0, 0, 10240, 4096, 24, "00:00:00 31/12/2005");
  120. list = "-rw------- 1 6700 2000 6561177600 Oct 15 2005 home.backup.tar\r\n";
  121. err = parse_dir(list, "/", "home.backup.tar", &sbuf, NULL, 0, NULL, NULL);
  122. assert(err == 0);
  123. check(sbuf, 0, 0, S_IFREG|S_IRUSR|S_IWUSR, 1, 0, 0, 0, 6561177600LL, 4096, 12814800, "00:00:00 15/10/2005");
  124. return 0;
  125. }