Back to index

courier  0.68.2
testmaildiraclt.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2003 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "maildiraclt.h"
00007 #include      "maildirmisc.h"
00008 
00009 #if    HAVE_UNISTD_H
00010 #include      <unistd.h>
00011 #endif
00012 #include      <string.h>
00013 #include      <errno.h>
00014 #include      <stdio.h>
00015 #include      <stdlib.h>
00016 #include      <fcntl.h>
00017 #if    HAVE_UTIME_H
00018 #include      <utime.h>
00019 #endif
00020 #if TIME_WITH_SYS_TIME
00021 #include      <sys/time.h>
00022 #include      <time.h>
00023 #else
00024 #if HAVE_SYS_TIME_H
00025 #include      <sys/time.h>
00026 #else
00027 #include      <time.h>
00028 #endif
00029 #endif
00030 
00031 
00032 #define CHK(x) if (!(x)) { printf("%s(%d): sanity check failed.\n", \
00033                      __FILE__, __LINE__); exit(1);}
00034 
00035 static int cb_enum(const char *identifier,
00036                  const maildir_aclt *acl,
00037                  void *b)
00038 {
00039        char *cb=(char *)b;
00040 
00041        if (strlen(cb) > 7000)
00042               return -1;
00043 
00044        strcat(strcat(strcat(strcat(b, identifier), "."),
00045                     maildir_aclt_ascstr(acl)), ".");
00046        return 0;
00047 }
00048 
00049 int main()
00050 {
00051        maildir_aclt a, b;
00052        maildir_aclt_list l;
00053        char buf[8192];
00054        int i;
00055        struct stat stat_buf;
00056 
00057        CHK(maildir_aclt_init(&a, "rw", NULL) == 0);
00058        CHK(maildir_aclt_init(&b, NULL, &a) == 0);
00059 
00060        CHK(strcmp(maildir_aclt_ascstr(&a), "rw") == 0);
00061        CHK(strcmp(maildir_aclt_ascstr(&b), "rw") == 0);
00062 
00063        maildir_aclt_destroy(&b);
00064        CHK(maildir_aclt_init(&b, "arx", NULL) == 0);
00065        CHK(maildir_aclt_add(&a, NULL, &b) == 0);
00066        CHK(strcmp(maildir_aclt_ascstr(&a), "arwx") == 0);
00067        CHK(maildir_aclt_add(&a, "bwz", NULL) == 0);
00068        CHK(strcmp(maildir_aclt_ascstr(&a), "abrwxz") == 0);
00069 
00070        maildir_aclt_destroy(&b);
00071        CHK(maildir_aclt_init(&b, "wry", NULL) == 0);
00072        CHK(maildir_aclt_del(&a, NULL, &b) == 0);
00073        CHK(strcmp(maildir_aclt_ascstr(&a), "abxz") == 0);
00074        CHK(maildir_aclt_del(&a, "abc", NULL) == 0);
00075        CHK(strcmp(maildir_aclt_ascstr(&a), "xz") == 0);
00076 
00077        maildir_aclt_list_init(&l);
00078        CHK(maildir_aclt_list_add(&l, "owner", NULL, &a) == 0);
00079        CHK(maildir_aclt_list_add(&l, "user1", NULL, &a) == 0);
00080        CHK(maildir_aclt_list_add(&l, "user2", NULL, &b) == 0);
00081        CHK(maildir_aclt_list_add(&l, "owner", NULL, &b) == 0);
00082        CHK(maildir_aclt_list_del(&l, "user1") == 0);
00083        CHK(maildir_aclt_list_del(&l, "user3") == 0);
00084        CHK(maildir_aclt_list_find(&l, "owner") != NULL &&
00085            strcmp(maildir_aclt_ascstr(maildir_aclt_list_find(&l, "owner")),
00086                  "rwy") == 0);
00087        maildir_aclt_destroy(&a);
00088        maildir_aclt_destroy(&b);
00089 
00090        buf[0]=0;
00091 
00092        CHK(maildir_aclt_list_enum(&l, cb_enum, buf) == 0 &&
00093            strcmp(buf, "owner.rwy.user2.rwy.") == 0);
00094        maildir_aclt_list_destroy(&l);
00095 
00096        CHK(maildir_del("confmdtest") == 0);
00097        CHK(maildir_make("confmdtest", 0700, 0700, 0) == 0);
00098 
00099        for (i=0; i<150; i++)
00100        {
00101               int fd;
00102 
00103               sprintf(buf, "confmdtest/new/msg%d", i);
00104               CHK((fd=open(buf, O_RDWR|O_CREAT|O_TRUNC, 0600)) >= 0);
00105               close(fd);
00106        }
00107        CHK(maildir_del("confmdtest") == 0);
00108        CHK(maildir_make("confmdtest", 0700, 0700, 0) == 0);
00109        CHK(maildir_make("confmdtest/.foo.bar", 0700, 0700, 0) == 0);
00110        CHK(maildir_acl_read(&l, "confmdtest", ".") == 0);
00111 
00112        buf[0]=0;
00113        CHK(maildir_aclt_list_enum(&l, cb_enum, buf) == 0 &&
00114            strcmp(buf, "owner.aceilrstwx.administrators.aceilrstwx.") == 0);
00115 
00116        CHK(maildir_aclt_list_add(&l, "anyone", "lr", NULL) == 0);
00117        CHK(maildir_acl_write(&l, "confmdtest", ".", NULL, NULL) == 0);
00118        maildir_aclt_list_destroy(&l);
00119        CHK(maildir_acl_read(&l, "confmdtest", ".") == 0);
00120 
00121        buf[0]=0;
00122        CHK(maildir_aclt_list_enum(&l, cb_enum, buf) == 0 &&
00123            strcmp(buf, "owner.aceilrstwx.administrators.aceilrstwx.anyone.lr.") == 0);
00124 
00125        maildir_aclt_list_destroy(&l);
00126        CHK(maildir_acl_read(&l, "confmdtest", ".foo.bar") == 0);
00127 
00128        buf[0]=0;
00129        CHK(maildir_aclt_list_enum(&l, cb_enum, buf) == 0 &&
00130            strcmp(buf, "owner.aceilrstwx.administrators.aceilrstwx.anyone.lr.") == 0);
00131 
00132        maildir_aclt_list_destroy(&l);
00133        CHK(maildir_acl_read(&l, "confmdtest", ".foo") == 0);
00134 
00135        buf[0]=0;
00136        CHK(maildir_aclt_list_enum(&l, cb_enum, buf) == 0 &&
00137            strcmp(buf, "owner.aceilrstwx.administrators.aceilrstwx.anyone.lr.") == 0);
00138 
00139        CHK(maildir_acl_write(&l, "confmdtest", ".foobar", NULL, NULL)==0);
00140 
00141        maildir_acl_reset("confmdtest");
00142 
00143        CHK(stat("confmdtest/" ACLHIERDIR "/foo", &stat_buf) == 0 &&
00144            stat("confmdtest/" ACLHIERDIR "/foobar", &stat_buf) == 0);
00145 
00146 #if    HAVE_UTIME
00147 
00148        {
00149               struct utimbuf ub;
00150 
00151               ub.actime=ub.modtime=time(NULL)-60*60*2;
00152               utime("confmdtest/" ACLHIERDIR "/foobar", &ub);
00153 
00154               maildir_acl_reset("confmdtest");
00155 
00156               CHK(stat("confmdtest/" ACLHIERDIR "/foo", &stat_buf) == 0 &&
00157                   stat("confmdtest/" ACLHIERDIR "/foobar", &stat_buf) < 0 &&
00158                   errno == ENOENT);
00159 
00160        }
00161 #else
00162 #if    HAVE_UTIMES
00163 
00164        {
00165               struct timeval       tv;
00166 
00167               tv.tv_sec=time(NULL)-60*60*2;
00168               tv.tv_usec=0;
00169               utimes("confmdtest/" ACLHIERDIR "/foobar", &tv);
00170 
00171               maildir_acl_reset("confmdtest");
00172 
00173               CHK(stat("confmdtest/" ACLHIERDIR "/foo", &stat_buf) == 0 &&
00174                   stat("confmdtest/" ACLHIERDIR "/foobar", &stat_buf) < 0 &&
00175                   errno == ENOENT);
00176        }
00177 #endif
00178 #endif
00179 
00180 #if 1
00181        CHK(maildir_del("confmdtest") == 0);
00182 #endif
00183 
00184        maildir_aclt_list_destroy(&l);
00185        CHK(maildir_aclt_list_add(&l, "owner", "swite", NULL) == 0);
00186        CHK(maildir_aclt_list_add(&l, "anyone", "lr", NULL) == 0);
00187        CHK(maildir_aclt_list_add(&l, "-user1", "r", NULL) == 0);
00188 
00189        {
00190               static const char *id[3];
00191 
00192               id[0]="owner";
00193               id[1]="user0";
00194               id[2]=NULL;
00195 
00196               CHK(maildir_acl_compute_array(&a, &l, id) == 0 &&
00197                   strcmp(maildir_aclt_ascstr(&a), "aeilrstw") == 0);
00198               maildir_aclt_destroy(&a);
00199 
00200               id[0]="user1";
00201               id[1]=NULL;
00202               CHK(maildir_acl_compute_array(&a, &l, id) == 0 &&
00203                   strcmp(maildir_aclt_ascstr(&a), "l") == 0);
00204               maildir_aclt_destroy(&a);
00205        }
00206 
00207        exit(0);
00208        return (0);
00209 }