tests/plover/test-log.c
author J. Ali Harlow <ali@juiblex.co.uk>
Wed Jun 22 17:32:44 2016 +0100 (2016-06-22)
changeset 45 a166277bc796
parent 38 a29623b68ca2
child 57 e0f448fb54a5
permissions -rw-r--r--
Remove obsolete file URI support
ali@38
     1
/*
ali@38
     2
 * Copyright (C) 2016  J. Ali Harlow <ali@juiblex.co.uk>
ali@38
     3
 *
ali@38
     4
 * This program is free software; you can redistribute it and/or modify
ali@38
     5
 * it under the terms of the GNU General Public License as published by
ali@38
     6
 * the Free Software Foundation; either version 2 of the License, or
ali@38
     7
 * (at your option) any later version.
ali@38
     8
 *
ali@38
     9
 * This program is distributed in the hope that it will be useful,
ali@38
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ali@38
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ali@38
    12
 * GNU General Public License for more details.
ali@38
    13
 *
ali@38
    14
 * You should have received a copy of the GNU General Public License along
ali@38
    15
 * with this program; if not, write to the Free Software Foundation, Inc.,
ali@38
    16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ali@38
    17
 */
ali@38
    18
ali@38
    19
#include <stdlib.h>
ali@38
    20
#include <sys/time.h>
ali@38
    21
#include <glib.h>
ali@38
    22
#include <razor.h>
ali@38
    23
#include <plover/plover.h>
ali@38
    24
ali@38
    25
static gchar *log_file_path(const char *directory,const char *name,
ali@38
    26
  const char *stamp)
ali@38
    27
{
ali@38
    28
    gchar *s,*path;
ali@38
    29
    if (name)
ali@38
    30
	s=g_strdup(name);
ali@38
    31
    else
ali@38
    32
	s=g_strconcat("test-",stamp,NULL);
ali@38
    33
    if (directory)
ali@38
    34
	path=g_build_filename(directory,s,NULL);
ali@38
    35
    else
ali@38
    36
	path=g_strdup(s);
ali@38
    37
    g_free(s);
ali@38
    38
    return path;
ali@38
    39
}
ali@38
    40
ali@38
    41
static GDateTime *date_days_ago(int age)
ali@38
    42
{
ali@38
    43
    GDateTime *now,*then;
ali@38
    44
    now=g_date_time_new_now_utc();
ali@38
    45
    then=g_date_time_add_days(now,-age);
ali@38
    46
    g_date_time_unref(now);
ali@38
    47
    return then;
ali@38
    48
}
ali@38
    49
ali@38
    50
static gchar *date_stamp(int age)
ali@38
    51
{
ali@38
    52
    gchar *stamp;
ali@38
    53
    GDateTime *then;
ali@38
    54
    then=date_days_ago(age);
ali@38
    55
    stamp=g_date_time_format(then,"%Y%m%d");
ali@38
    56
    g_date_time_unref(then);
ali@38
    57
    return stamp;
ali@38
    58
}
ali@38
    59
ali@38
    60
static void simulate_log_file(const char *directory,const char *name,int age)
ali@38
    61
{
ali@38
    62
    gchar *path,*contents,*stamp;
ali@38
    63
    GDateTime *then;
ali@38
    64
    GError *err=NULL;
ali@38
    65
    struct timeval times[2]={{0,},{0,}};
ali@38
    66
    stamp=date_stamp(age);
ali@38
    67
    path=log_file_path(directory,name,stamp);
ali@38
    68
    contents=g_strdup_printf("Log for %s\n",stamp);
ali@38
    69
    if (!g_file_set_contents(path,contents,-1,&err))
ali@38
    70
	g_error("%s: %s",path,err->message);
ali@38
    71
    g_free(contents);
ali@38
    72
    then=date_days_ago(age);
ali@38
    73
    times[0].tv_sec=times[1].tv_sec=g_date_time_to_unix(then);
ali@38
    74
    utimes(path,times);
ali@38
    75
    g_date_time_unref(then);
ali@38
    76
    g_free(path);
ali@38
    77
    g_free(stamp);
ali@38
    78
}
ali@38
    79
ali@38
    80
static void verify_log_file(const char *directory,const char *name,int age,
ali@38
    81
  gboolean should_exist)
ali@38
    82
{
ali@38
    83
    gchar *path,*contents,*stamp,*s;
ali@38
    84
    GError *err=NULL;
ali@38
    85
    stamp=date_stamp(age);
ali@38
    86
    path=log_file_path(directory,name,stamp);
ali@38
    87
    if (!should_exist)
ali@38
    88
	g_assert(!g_file_test(path,G_FILE_TEST_EXISTS));
ali@38
    89
    else
ali@38
    90
    {
ali@38
    91
	if (!g_file_get_contents(path,&contents,NULL,&err))
ali@38
    92
	    g_error("%s: %s",path,err->message);
ali@38
    93
	s=g_strdup_printf("Log for %s\n",stamp);
ali@38
    94
	g_assert_cmpstr(contents,==,s);
ali@38
    95
	g_free(s);
ali@38
    96
	g_free(contents);
ali@38
    97
    }
ali@38
    98
    g_free(path);
ali@38
    99
    g_free(stamp);
ali@38
   100
}
ali@38
   101
ali@38
   102
static int safe_log_open(const char *s)
ali@38
   103
{
ali@38
   104
    int fd1,fd2,result;
ali@38
   105
    fflush(stdout);
ali@38
   106
    fflush(stderr);
ali@38
   107
    fd1=dup(1);
ali@38
   108
    fd2=dup(2);
ali@38
   109
    result=plover_log_open(s);
ali@38
   110
    fflush(stdout);
ali@38
   111
    fflush(stderr);
ali@38
   112
    dup2(fd1,1);
ali@38
   113
    dup2(fd2,2);
ali@38
   114
    close(fd1);
ali@38
   115
    close(fd2);
ali@38
   116
    return result;
ali@38
   117
}
ali@38
   118
ali@38
   119
static void verify_log_open(const char *directory)
ali@38
   120
{
ali@38
   121
    int result;
ali@38
   122
    gchar *stamp,*s;
ali@38
   123
    simulate_log_file(directory,"test",1);
ali@38
   124
    simulate_log_file(directory,NULL,1);
ali@38
   125
    simulate_log_file(directory,NULL,2);
ali@38
   126
    simulate_log_file(directory,NULL,3);
ali@38
   127
    simulate_log_file(directory,NULL,4);
ali@38
   128
    if (directory)
ali@38
   129
	s=g_build_filename(directory,"test",NULL);
ali@38
   130
    else
ali@38
   131
	s=g_strdup("test");
ali@38
   132
    result=safe_log_open(s);
ali@38
   133
    g_free(s);
ali@38
   134
    g_assert_cmpint(result,==,0);
ali@38
   135
    stamp=date_stamp(1);
ali@38
   136
    s=g_strconcat("test-",stamp,"a",NULL);
ali@38
   137
    g_free(stamp);
ali@38
   138
    verify_log_file(directory,s,1,TRUE);
ali@38
   139
    g_free(s);
ali@38
   140
    verify_log_file(directory,NULL,1,TRUE);
ali@38
   141
    verify_log_file(directory,NULL,2,TRUE);
ali@38
   142
    verify_log_file(directory,NULL,3,TRUE);
ali@38
   143
    verify_log_file(directory,NULL,4,FALSE);
ali@38
   144
}
ali@38
   145
ali@38
   146
static void test_rotate(void)
ali@38
   147
{
ali@38
   148
    system("rm -rf test-log-rotate");
ali@38
   149
    g_assert(mkdir("test-log-rotate",0777)==0);
ali@38
   150
    verify_log_open("test-log-rotate");
ali@38
   151
}
ali@38
   152
ali@38
   153
static void test_rotate_cwd(void)
ali@38
   154
{
ali@38
   155
    system("rm -rf test-log-rotate");
ali@38
   156
    g_assert(mkdir("test-log-rotate",0777)==0);
ali@38
   157
    g_assert(chdir("test-log-rotate")==0);
ali@38
   158
    verify_log_open(NULL);
ali@38
   159
    g_assert(chdir("..")==0);
ali@38
   160
}
ali@38
   161
ali@38
   162
static void test_rotate_root(void)
ali@38
   163
{
ali@38
   164
    gchar *cwd,*s;
ali@38
   165
    system("rm -rf test-log-rotate");
ali@38
   166
    g_assert(mkdir("test-log-rotate",0777)==0);
ali@38
   167
    cwd=g_get_current_dir();
ali@44
   168
    s=g_strconcat("file:",cwd,"/",NULL);
ali@38
   169
    g_free(cwd);
ali@38
   170
    g_setenv("RAZOR_ROOT",s,TRUE);
ali@38
   171
    g_free(s);
ali@38
   172
    verify_log_open("test-log-rotate");
ali@38
   173
    g_unsetenv("RAZOR_ROOT");
ali@38
   174
}
ali@38
   175
ali@38
   176
static void test_readonly_dir(void)
ali@38
   177
{
ali@38
   178
    system("rm -rf test-log-rotate");
ali@38
   179
    g_assert(mkdir("test-log-rotate",0)==0);
ali@38
   180
    g_assert(safe_log_open("test-log-rotate/test")!=0);
ali@38
   181
}
ali@38
   182
ali@38
   183
static void test_directory(void)
ali@38
   184
{
ali@38
   185
    system("rm -rf test-log-rotate");
ali@38
   186
    g_assert(mkdir("test-log-rotate",0777)==0);
ali@38
   187
    g_assert(mkdir("test-log-rotate/test",0777)==0);
ali@38
   188
    g_assert(safe_log_open("test-log-rotate/test")!=0);
ali@38
   189
}
ali@38
   190
ali@38
   191
static void test_readonly_file(void)
ali@38
   192
{
ali@38
   193
    GError *err=NULL;
ali@38
   194
    system("rm -rf test-log-rotate");
ali@38
   195
    g_assert(mkdir("test-log-rotate",0777)==0);
ali@38
   196
    if (!g_file_set_contents("test-log-rotate/test","",0,&err))
ali@38
   197
	g_error("test-log-rotate/test: %s",err->message);
ali@38
   198
    g_assert(chmod("test-log-rotate/test",0)==0);
ali@38
   199
    g_assert(safe_log_open("test-log-rotate/test")!=0);
ali@38
   200
}
ali@38
   201
ali@38
   202
int main(int argc,char **argv)
ali@38
   203
{
ali@38
   204
    int retval;
ali@38
   205
    g_test_init(&argc,&argv,NULL);
ali@38
   206
    g_test_bug_base("mailto:ali@juiblex.co.uk");
ali@38
   207
    g_test_add_func("/log/rotate",test_rotate);
ali@38
   208
    g_test_add_func("/log/rotate-cwd",test_rotate_cwd);
ali@38
   209
    g_test_add_func("/log/rotate-root",test_rotate_root);
ali@38
   210
    g_test_add_func("/log/readonly-dir",test_readonly_dir);
ali@38
   211
    g_test_add_func("/log/directory",test_directory);
ali@38
   212
    g_test_add_func("/log/readonly-file",test_readonly_file);
ali@38
   213
    retval=g_test_run();
ali@38
   214
    return retval;
ali@38
   215
}