File: test

package info (click to toggle)
logrotate 3.11.0-0.1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,512 kB
  • ctags: 554
  • sloc: ansic: 4,339; sh: 3,500; makefile: 66
file content (1878 lines) | stat: -rwxr-xr-x 39,502 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
#!/bin/bash

ACL_TESTS=1
SELINUX_TESTS=0
SYSLOG_TESTS=0
LOGROTATE=../logrotate
M="-m ./mailer"
S="-s state"
RLR="$LOGROTATE $M $S"

# -- ACL - BEGIN --------------------------------
# We test if the ACLs tests should be done or not
echo 1 > test.x
setfacl -m u:nobody:rwx test.x 2>/dev/null
if [ $? != 0 ]; then
	ACL_TESTS=0
	echo "setfacl failed on this system. ACL tests will not be executed."
fi
rm -f test.x
if [ $ACL_TESTS = 1 ]; then
  # It seems we can run the ACL tests, but was logrotate compiled WITH_ACL=yes ?
  # See the Makefile, "pretest" part, for more information
  if [ -f ./test.ACL ]; then
    ACL_TESTS=`cat ./test.ACL`
    if [ $ACL_TESTS = 0 ]; then
      echo "logrotate was NOT compiled with 'WITH_ACL=yes'. ACL tests will not be executed."
    fi
  fi
fi
# -- ACL - END ----------------------------------

# -- SELINUX - BEGIN --------------------------------
# We test if the ACLs tests should be done or not
if type "selinuxenabled" >/dev/null 2>&1 && selinuxenabled; then
	SELINUX_TESTS=1
else
	echo "SELinux disabled. SELinux tests will not be executed."
fi

if [ $SELINUX_TESTS = 1 ]; then
  # It seems we can run the ACL tests, but was logrotate compiled WITH_ACL=yes ?
  # See the Makefile, "pretest" part, for more information
  if [ -f ./test.SELINUX ]; then
    SELINUX_TESTS=`cat ./test.SELINUX`
    if [ $SELINUX_TESTS = 0 ]; then
      echo "logrotate was NOT compiled with 'WITH_SELINUX=yes'. SELINUX tests will not be executed."
    fi
  fi
fi

if [ $SELINUX_TESTS = 1 ]; then
  # if logrotate_tmp_t, we can't continue with SELinux tests...
  touch .selinuxtest
  chcon --type=logrotate_tmp_t .selinuxtest 2>/dev/null
  if [ $? != 0 ]; then
    SELINUX_TESTS=0
    echo "SELinux context 'logrotate_tmp_t' does not exist. SELinux tests will not be executed."
  fi
  rm -f .selinuxtest
fi
# -- SELINUX - END ----------------------------------

# -- SYSLOG - BEGIN
logger syslog_test 2>/dev/null
if [ $? = 0 ]; then
	journalctl -n 50 2>/dev/null | grep syslog_test 2>/dev/null >/dev/null
	if [ $? = 0 ]; then
		SYSLOG_TESTS=1
	fi
fi
# -- SYSLOG - END

cleanup() {
    rm -f test*.log* anothertest*.log* state test-config. scriptout mail-out compress-args compress-env different*.log*
    rm -f $(ls | egrep '^test-config.[0-9]+$')

    [ -n "$1" ] && echo "Running test $1"
    return 0
}

genconfig() {
    input=test-config.$1.in
    output=test-config.$1
    user=$(id -u -n)
    group=$(id -g -n)
    sed "s,&DIR&,$PWD,g" < $input | sed "s,&USER&,$user,g" | sed "s,&GROUP&,$group,g" > $output
    config_crc=$(md5sum $output)
}

createlog() {
    num=$1
    file=$2
    cl_compressed=$3

    case $num in
	0)
	    what=zero
	    ;;
	1)
	    what=first
	    ;;
	2)
	    what=second
	    ;;
	3)
	    what=third
	    ;;
	4)
	    what=fourth
	    ;;
	5)
	    what=fifth
	    ;;
	6)
	    what=sixth
	    ;;
	7)
	    what=seventh
	    ;;
	8)
	    what=eight
	    ;;
	9)
	    what=ninth
	    ;;
	*)
	    exit 1
	    ;;
    esac

    echo $what > $file
    [ -n "$cl_compressed" ] && gzip -9 $file
}

createlogs() {
    base=$1
    numlogs=$2
    cls_compressed=$3

    rm -f ${base}*

    num=0
    while [ $num != $numlogs ]; do
	if [ $num = 0 ]; then
	    createlog 0 $base
	else
	    createlog $num ${base}.$num $cls_compressed
	fi

	num=`expr $num + 1`
    done
}

checkmail() {
    (echo -s $PWD/$1 user@myhost.org; echo $2) | diff -u - mail-out
    if [ $? != 0 ]; then
        exit 5
    fi
}

checkoutput() {
    while read line; do
	set $line
	file=$1
	co_compressed=$2
	if [ "$#" -le 2 ]; then
		shift $#
	else
		shift 2
	fi

	fileother=`echo $line | awk '{print $1}'`
	expected=`echo $line | cut -s -d\  -f3-`

	if [ $file != $fileother ]; then
	    echo "unexpected file $file'" >&2
	    exit 2
	fi

	if [ ! -f $file ]; then
	    echo "file $file does not exist"
	fi

	if [ -n "$co_compressed" ] && [ "$co_compressed" != 0 ]; then
		contents=`gunzip -c $file`
	else
		contents=`cat $file | tr -d '\000'`
	fi
	if [ "$contents" != "$expected" ]; then
	    echo "file $file does not contain expected results (compressed $co_compressed, args $*)" >&2
	    echo contains: \'$contents\'
	    echo expected: \'$expected\'
	    exit 2
	fi
	echo "$config_crc" | md5sum -c - 2>&1 > /dev/null
	if [ $? != 0 ]; then
		echo "config file $output has been altered: MD5 sum mismatch"
		exit 3
	fi
    done
}

preptest() {
    base=$1
    confignum=$2
    numlogs=$3
    pt_compressed=$4

    rm -f $base*
    rm -f state

    genconfig $confignum
    createlogs $base $numlogs $pt_compressed
}

# we don't want any stuff left from previous runs
cleanup 1

# ------------------------------- Test 1 -------------------------------------
# Without a log file, no rotations should occur
preptest test.log 1 2
$RLR test-config.1 

checkoutput <<EOF
test.log 0 zero
test.log.1 0 first
EOF

# Put in place a state file that will force a rotation
cat > state <<EOF
logrotate state -- version 1
"$PWD/test.log" 2000-1-1
EOF

# Now force the rotation
$RLR test-config.1
checkoutput <<EOF
test.log 0
test.log.1 0 zero
test.log.2 0 first
EOF

# rerun it to make sure nothing happens
$RLR test-config.1 

checkoutput <<EOF
test.log
test.log.1 0 zero
test.log.2 0 first
EOF

cleanup 2

# ------------------------------- Test 2 -------------------------------------
preptest test.log 2 3
$RLR test-config.2 --force

checkoutput <<EOF
test.log.1 0 zero
test.log.2 0 first
EOF

checkmail test.log.3 second

if [ -f test.log ]; then
    echo "erroneously created test.log"
fi

cleanup 3

# ------------------------------- Test 3 -------------------------------------

preptest test.log 3 1
$RLR test-config.3 --force

checkoutput <<EOF
test.log 0
test.log.1 0 zero
scriptout 0 foo
EOF

cleanup

preptest test.log 3 1
preptest test2.log 3 1
$RLR test-config.3 --force

checkoutput <<EOF
test.log 0
test.log.1 0 zero
test2.log 0
test2.log.1 0 zero
scriptout 0 foo foo
EOF

cleanup 4

# ------------------------------- Test 4 -------------------------------------
preptest test.log 4 1
preptest test2.log 4 1
$RLR test-config.4 --force 

checkoutput <<EOF
test.log 0
test.log.1 0 zero
test2.log 0
test2.log.1 0 zero
scriptout 0 foo
EOF

cleanup 5

# ------------------------------- Test 5 -------------------------------------
preptest test.log 5 1
preptest anothertest.log 5 1
$RLR test-config.5 --force 

checkoutput <<EOF
test.log 0
test.log.1 0 zero
anothertest.log 0
anothertest.log.1 0 zero
scriptout 0 foo
EOF

cleanup 6

# ------------------------------- Test 6 -------------------------------------
preptest test.log 6 1
preptest anothertest.log 6 1
if [ $SELINUX_TESTS = 1 ]; then
	chcon --type=logrotate_tmp_t test.log
else
	echo "Skipping SELinux part of test 6"
fi
$RLR test-config.6 --force

if [ $SELINUX_TESTS = 1 ]; then
	ls -Z test.log.0|grep logrotate_tmp_t >/dev/null
	if [ $? != 0 ]; then
		echo "test.log.0 should have selinux context logrotate_tmp_t."
		exit 3
	fi

	ls -Z anothertest.log.0|grep logrotate_tmp_t >/dev/null
	if [ $? = 0 ]; then
		echo "anothertest.log.0 should not have selinux context logrotate_tmp_t."
		exit 3
	fi
fi

checkoutput <<EOF
test.log 0
test.log.0 0 zero
anothertest.log 0
anothertest.log.0 0 zero
scriptout 0 foo
EOF

cleanup 7

# ------------------------------- Test 7 -------------------------------------
preptest test.log 7 1
preptest anothertest.log 7 1

$RLR test-config.7 --force

checkoutput <<EOF
test.log 0
test.log.6 0 zero
anothertest.log 0
anothertest.log.6 0 zero
scriptout 0 foo
EOF

cleanup 8

# ------------------------------- Test 8 -------------------------------------
preptest test.log 8 1 1
$RLR test-config.8 --force

checkoutput <<EOF
test.log 0
test.log.1.gz 1 zero
scriptout 0 foo
EOF

checkmail test.log zero

cleanup 9

# ------------------------------- Test 9 -------------------------------------
preptest test.log 9 1 1
$RLR test-config.9 --force

checkoutput <<EOF
test.log 0
scriptout 0 foo
EOF

checkmail test.log zero

cleanup 10

# ------------------------------- Test 10 ------------------------------------
preptest test.log 10 1

if [ $SELINUX_TESTS = 1 ]; then
	chcon --type=logrotate_tmp_t test.log
else
	echo "Skipping SELinux part of test 10"
fi

$RLR test-config.10 --force

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

echo "newfile" > test.log

$RLR test-config.10 --force

if [ $SELINUX_TESTS = 1 ]; then
	ls -Z test.log.2.gz|grep logrotate_tmp_t >/dev/null
	if [ $? != 0 ]; then
		echo "test.log.2.gz should have selinux context logrotate_tmp_t."
		ls -Z test.log.2.gz
		exit 3
	fi

	ls -Z test.log.1|grep logrotate_tmp_t >/dev/null
	if [ $? != 0 ]; then
		echo "test.log.1 should have selinux context logrotate_tmp_t."
		ls -Z test.log.1
		exit 3
	fi
fi

checkoutput <<EOF
test.log 0
test.log.1 0 newfile
test.log.2.gz 1 zero
EOF

checkmail test.log.1 newfile

cleanup 11

# ------------------------------- Test 11 ------------------------------------
preptest test.log 11 2 1
$RLR test-config.11 --force

checkoutput <<EOF
test.log 0
scriptout 0 foo
EOF

checkmail test.log.2.gz first

# check rotation into a directory given as a relative pathname
cleanup 12

# ------------------------------- Test 12 ------------------------------------
preptest test.log 12 1 0
rm -rf testdir
mkdir testdir
$RLR test-config.12 --force

checkoutput <<EOF
test.log 0
testdir/test.log.1 0 zero
EOF

rm -rf testdir

# check rotation into a directory given as an absolute  pathname
cleanup 13

# ------------------------------- Test 13 ------------------------------------
preptest test.log 13 1 0
rm -rf testdir
$RLR test-config.13 --force

ls -l|grep testdir|grep "drwx------." 2>/dev/null >/dev/null
if [ $? != 0 ]; then
	echo "testdir should have mode 2700, but it has:"
	ls -l|grep testdir
	exit 3
fi

checkoutput <<EOF
test.log 0
testdir/test.log.1 0 zero
EOF

rm -rf testdir

# sanity rotation check using dateext and dateformat
cleanup 14

# ------------------------------- Test 14 ------------------------------------
preptest test.log 14 1 0

$RLR test-config.14 --force

DATESTRING=$(/bin/date +%Y-%m-%d)

checkoutput <<EOF
test.log 0
test.log.$DATESTRING 0 zero
EOF

rm -rf testdir

# shred test
cleanup 15

# ------------------------------- Test 15 ------------------------------------
preptest test.log 15 1 0
$RLR test-config.15 --force

# this rotation should use shred
$RLR test-config.15 --force

checkoutput <<EOF
test.log 0
test.log.1 0
EOF

cleanup 16

# ------------------------------- Test 16 ------------------------------------
preptest test.log 16 1 0
# log with 1 byte should not be rotated
echo "a" > test.log
$RLR test-config.16

if [ -f test.log.1 ]; then
	echo "file $file does exist!"
	exit 2
fi

# log with 4 bytes should be rotated
echo "zero" > test.log
$RLR test-config.16

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

cleanup 17

# ------------------------------- Test 17 ------------------------------------
preptest test.log 17 1 0
# log with 1 byte should not be rotated
$RLR test-config.17 -l logrotate.log 2>error.log

grep "unexpected } (missing previous '{')" error.log >/dev/null
if [ $? != 0 ]; then
	echo "No error printed, but there should be one."
	exit 3
fi

rm error.log

grep "reading config file test-config.17" logrotate.log >/dev/null
if [ $? != 0 ]; then
	echo "There is no log output in logrotate.log"
	exit 3
fi

rm -f logrotate.log

checkoutput <<EOF
test.log 0 zero
EOF

cleanup 18

# ------------------------------- Test 18 ------------------------------------
preptest test.log 18 1
$RLR test-config.18 -l syslog --force

checkoutput <<EOF
test.log 0
test.log.1.Z 1 zero
EOF

(echo "gzip -f -9") | diff -u - compress-args
egrep -q '^LOGROTATE_COMPRESSED_FILENAME=.+/test/test.log.1$' compress-env
if [ $? != 0 ]; then
      echo "LOGROTATE_COMPRESSED_FILENAME environment variable not found."
      cat compress-env++      exit 3
fi

if [ $SYSLOG_TESTS = 1 ]; then
	journalctl -n 50 2>/dev/null|grep $PWD/test.log.1 2>/dev/null >/dev/null
	if [ $? != 0 ]; then
		echo "syslog message not found"
		exit 1
	fi
fi

cleanup 19

# ------------------------------- Test 19 ------------------------------------
preptest test.log 19 1
$RLR test-config.19 --force 2>error.log
if [ $? = 0 ]; then
	echo "Logrotate exited with 0 exit code, but it should not"
fi

grep "error running non-shared postrotate script for" error.log >/dev/null
if [ $? != 0 ]; then
	echo "No error printed, but there should be one."
	exit 3
fi

cleanup 20

# ------------------------------- Test 20 ------------------------------------
preptest test.log 20 1
$RLR test-config.20 --force 2>error.log

if [ $? = 0 ]; then
	echo "Logrotate exited with 0 exit code, but it should not"
fi

grep "error running shared postrotate script for" error.log >/dev/null
if [ $? != 0 ]; then
	echo "No error printed, but there should be one."
	exit 3
fi

cleanup 21

# ------------------------------- Test 21 ------------------------------------
# different base name, so it should not find the file
preptest differenttest.log 21 1
$RLR test-config.21 --force 2>error.log

if [ $? != 0 ]; then
	echo "Logrotate exited with non-zero exit code, but it should not"
fi

cat error.log

# grep "error running shared postrotate script for" error.log >/dev/null
# if [ $? != 0 ]; then
# 	echo "No error printed, but there should be one."
# 	exit 3
# fi

cleanup 22

# ------------------------------- Test 22 ------------------------------------
# different base name, so it should not find the file
preptest differenttest.log 22 1
$RLR test-config.22 --force 2>error.log

if [ $? = 0 ]; then
	echo "Logrotate exited with zero exit code, but it should not"
fi

grep "error: stat of" error.log >/dev/null
if [ $? != 0 ]; then
	echo "No error printed, but there should be one."
	exit 3
fi

cleanup 23

# ------------------------------- Test 23 ------------------------------------
# symlinks - symlinks rotation is not allowed for security reasons.
preptest test.log.original 23 1
ln -s test.log.original test.log
$RLR test-config.23 --force 2>error.log 

checkoutput <<EOF
test.log 0 zero
test.log.original 0 zero
EOF

rm -f test.log 2>/dev/null || true

cleanup 24

# ------------------------------- Test 24 ------------------------------------
# symlinks 2 - now copytruncate is used, but symlinks rotation is not allowed for
# security reasons.
# since logrotate-3.8.2, we don't support symlinks rotation officially.
preptest test.log.original 24 1
ln -s test.log.original test.log
$RLR test-config.24 --force 2>error.log

checkoutput <<EOF
test.log 0 zero
test.log.original 0 zero
EOF

rm -f test.log 2>/dev/null || true

cleanup 25

# ------------------------------- Test 25 ------------------------------------
# If there is no '{' character after log files definition, error should be printed
# and config file should be skipped

preptest test.log 25 1 0
# log with 1 byte should not be rotated
$RLR test-config.25 2>error.log

grep "missing '{' after log files definition" error.log >/dev/null
if [ $? != 0 ]; then
	echo "No error printed, but there should be one."
	exit 3
fi

rm error.log

checkoutput <<EOF
test.log 0 zero
EOF

cleanup 26

# ------------------------------- Test 26 ------------------------------------
# If there is error in config file, log should not be rotated and original log
# should be untouched

preptest test.log 26 1 0
# log with 1 byte should not be rotated
$RLR test-config.26 2>error.log

grep "unknown option" error.log >/dev/null
if [ $? != 0 ]; then
	echo "No error printed, but there should be one."
	exit 3
fi

rm error.log

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

cleanup 27

# ------------------------------- Test 27 ------------------------------------
# logrotate fails to find the correct file to mail, when using "mailfirst" in
# combination with "delaycompress" and "dateext" option.
preptest test.log 27 1 0

DATESTRING=$(/bin/date +%Y%m%d)

$RLR test-config.27 --force
checkoutput <<EOF
test.log 0
test.log-$DATESTRING 0 zero
EOF

checkmail test.log-$DATESTRING zero


cleanup 28

# ------------------------------- Test 28 ------------------------------------
# { on new line

preptest test.log 28 1 0
# log with 1 byte should not be rotated
$RLR test-config.28

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

cleanup 29

# ------------------------------- Test 29 ------------------------------------
# { } on the same line

preptest test.log 29 1 0
# log with 1 byte should not be rotated
$RLR test-config.29 --force

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

cleanup 30

# ------------------------------- Test 30 ------------------------------------
# the file with the same date already exists, so it should not be overwritten
# and log should not be rotated
preptest test.log 30 1 0

DATESTRING=$(/bin/date +%Y%m%d)
echo "one" > test.log-$DATESTRING

$RLR test-config.30 --force
checkoutput <<EOF
test.log 0 zero
test.log-$DATESTRING 0 one
EOF

cleanup 31

# ------------------------------- Test 31 ------------------------------------
# Test mode in create option
preptest test.log 31 1 0

$RLR test-config.31 --force

stat -c %f test.log|grep 8180 >/dev/null
if [ $? != 0 ]; then
	echo "Bad mode of test.log, should be 0600"
	exit 3
fi

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

if [ $ACL_TESTS = 0 ]; then
  echo "Skipping test 32: no ACL support"
else

cleanup 32

# ------------------------------- Test 32 ------------------------------------
# Without mode in 'create' directive, ACLs should be respected.
# Also check that chmod is respected when setting ACLs
preptest test.log 32 1 0

chmod 600 test.log
setfacl -m u:nobody:rwx test.log

$RLR test-config.32 --force
getfacl test.log|grep "user:nobody:rwx" >/dev/null
if [ $? != 0 ]; then
	echo "test.log must have user:nobody:rwx ACL"
	getfacl test.log
	exit 3
fi

getfacl test.log|grep "group::---" >/dev/null
if [ $? != 0 ]; then
	echo "test.log must have group::--- ACL"
	getfacl test.log
	exit 3
fi

getfacl test.log.1|grep "user:nobody:rwx" >/dev/null
if [ $? != 0 ]; then
	echo "test.log.1 must have user:nobody:rwx ACL"
	getfacl test.log.1
	exit 3
fi

getfacl test.log.1|grep "group::---" >/dev/null
if [ $? != 0 ]; then
	echo "test.log.1 must have group::--- ACL"
	getfacl test.log.1
	exit 3
fi

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

fi

if [ $ACL_TESTS = 0 ]; then
  echo "Skipping test 33: no ACL support"
else

cleanup 33

# ------------------------------- Test 33 ------------------------------------
# With mode in 'create' directive, ACLs are overwriten by chmod
preptest test.log 33 1 0


setfacl -m u:nobody:rwx test.log
$RLR test-config.33 --force

getfacl test.log|grep "user:nobody:rwx" >/dev/null
if [ $? = 0 ]; then
	echo "test.log must not contain user:nobody:rwx"
	exit 3
fi

getfacl test.log.1|grep "user:nobody:rwx" >/dev/null
if [ $? != 0 ]; then
	echo "test.log.1 must contain user:nobody:rwx"
	exit 3
fi

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

fi

cleanup 34

# ------------------------------- Test 34 ------------------------------------
# We support changing user/mode without setting mode in create directive now
# We can't change user/group as normal user, so this test uses debug mode and
# checks the logrotate -d output.
preptest test.log 34 1 0

$RLR test-config.34 -d -f 2>&1|grep "uid = 0 gid = 0" > /dev/null

if [ $? != 0 ]; then
	echo "logrotate output must contain 'uid = 0 gid = 0'"
	exit 3
fi

checkoutput <<EOF
test.log 0 zero
EOF

if [ $ACL_TESTS = 0 ]; then
  echo "Skipping test 35: no ACL support"
else

cleanup 35

# ------------------------------- Test 35 ------------------------------------
# Test 'create' directive without mode but with user/group with ACLs. ACLs should
# be respected.
preptest test.log 35 1 0

setfacl -m u:nobody:rwx test.log
$RLR test-config.35 --force

getfacl test.log|grep "user:nobody:rwx" >/dev/null
if [ $? != 0 ]; then
	echo "test.log must contain user:nobody:rwx"
	getfacl test.log
	exit 3
fi

getfacl test.log.1|grep "user:nobody:rwx" >/dev/null
if [ $? != 0 ]; then
	echo "test.log.1 must contain user:nobody:rwx"
	getfacl test.log.1
	exit 3
fi

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

fi

cleanup 36

# ------------------------------- Test 36 ------------------------------------
# size 1x - 'x' is unknown unit, config should be skipped
preptest test.log 36 1 0

$RLR test-config.36 --force 2>error.log

grep "unknown unit" error.log >/dev/null
if [ $? != 0 ]; then
	echo "No error 'unknown unit' printed, but there should be one."
	exit 3
fi

checkoutput <<EOF
test.log 0 zero
EOF

cleanup 37

# ------------------------------- Test 37 ------------------------------------
# skip config with firstaction script
preptest test.log 37 1 0

$RLR test-config.37 --force 2>error.log

grep "skipping" error.log >/dev/null
if [ $? != 0 ]; then
	echo "No error 'skipping' printed, but there should be one."
	exit 3
fi

checkoutput <<EOF
test.log 0
test.log.1 0 zero
scriptout 0 second
EOF

cleanup 38

# ------------------------------- Test 38 ------------------------------------
# preremove script
preptest test.log 38 1
preptest test2.log 38 1
$RLR test-config.38 --force 

# Check both possible orders
grep "test2.log.1test.log.1" scriptout >/dev/null
if [ $? != 0 ]; then
	grep "test.log.1test2.log.1" scriptout >/dev/null
	if [ $? != 0 ]; then
		echo "ERROR: scriptout should contain 'test2.log.1test.log.1' or 'test.log.1test2.log.1'"
		exit 3
	fi
fi

rm -f scriptout

checkoutput <<EOF
test.log 0
test2.log 0
EOF

cleanup 39

# ------------------------------- Test 39 ------------------------------------
# preremove script error - do not remove log file
preptest test.log 39 1
preptest test2.log 39 1
$RLR test-config.39 --force 2>error.log

grep "error running preremove script" error.log >/dev/null
if [ $? != 0 ]; then
	echo "No error 'error running preremove script' printed, but there should be one."
	exit 3
fi

# Check both possible orders
grep "test2.log.1" scriptout >/dev/null
if [ $? != 0 ]; then
	grep "test.log.1" scriptout >/dev/null
	if [ $? != 0 ]; then
		echo "ERROR: scriptout should contain 'test2.log.1' or 'test.log.1'"
		exit 3
	fi
fi

rm -f scriptout

checkoutput <<EOF
test.log 0
test.log.1 0 zero
test2.log 0
test2.log.1 0 zero
EOF

cleanup 40

# ------------------------------- Test 40 ------------------------------------
# test tabooext and taboopat parsing and implementation, config.v, config.x and
# .config.z should not be loaded.
preptest test.log 40 1
mkdir -p testingdir
echo 1 > ./testingdir/config.v
echo 2 > ./testingdir/config.x
echo 3 > ./testingdir/.config.z

$RLR test-config.40 --force

rm -rf testingdir

cleanup 41

# ------------------------------- Test 41 ------------------------------------
# Test that prerotate and postrotate scripts are called only when the log files
# are actually rotated
preptest test.log 41 1
echo x > test2.log

$RLR test-config.41

checkoutput <<EOF
test.log 0
test.log.1 0 zero
test2.log 0 x
scriptout 0 test.log;test.log;
EOF

cleanup 42

# ------------------------------- Test 42 ------------------------------------
# Test that script is called only once when sharedscripts is defined
preptest test.log 42 1
echo number2 > test2.log

$RLR test-config.42

checkoutput <<EOF
test.log 0
test.log.1 0 zero
test2.log 0
test2.log.1 0 number2
scriptout 0 "test*.log ;test*.log ;"
EOF

cleanup 43

# ------------------------------- Test 43 ------------------------------------
# Test that prerotate and postrotate scripts are called twice for two files
# when sharedscripts defined
preptest test.log 43 1
echo number2 > test2.log

$RLR test-config.43

# Check both possible orders
grep "test2.log;test2.log;test.log;test.log;" scriptout >/dev/null
if [ $? != 0 ]; then
	grep "test.log;test.log;test2.log;test2.log;" scriptout >/dev/null
	if [ $? != 0 ]; then
		echo "ERROR: scriptout should contain 'test2.log;test2.log;test.log;test.log;' or 'test.log;test.log;test2.log;test2.log;'"
		exit 3
	fi
fi

rm -f scriptout

checkoutput <<EOF
test.log 0
test.log.1 0 zero
test2.log 0
test2.log.1 0 number2
EOF

cleanup 44

# ------------------------------- Test 44 ------------------------------------
# Test that prerotate and postrotate scripts are called once when nosharedscripts
# is defined and one rotation fails
preptest test.log 44 1

$RLR test-config.44 2>error.log

grep "error: stat of" error.log >/dev/null
if [ $? != 0 ]; then
	echo "No error printed, but there should be one."
	exit 3
fi

rm -f error.log

checkoutput <<EOF
test.log 0
test.log.1 0 zero
scriptout 0 "test.log;test.log;"
EOF

cleanup 45

# ------------------------------- Test 45 ------------------------------------
# Test that prerotate and postrotate scripts are not called when sharedscripts
# is defined and one rotation fails
preptest test.log 45 1

touch scriptout
$RLR test-config.45 2>error.log

grep "error: stat of" error.log >/dev/null
if [ $? != 0 ]; then
	echo "No error printed, but there should be one."
	exit 3
fi

rm -f error.log

checkoutput <<EOF
test.log 0 zero
scriptout 0 
EOF

cleanup 46

# ------------------------------- Test 46 ------------------------------------
# the state file is truncated and obviously corrupt, so no rotation should
# happen
preptest test.log 46 1

cat > state << EOF
logrotate state -- version 1
"$PWD/test.log" 2000-1-1
"$PWD/test2.l
EOF

DATESTRING=$(/bin/date +%Y%m%d)
$RLR test-config.46 2>error.log

grep "error: bad line 3 in state file state" error.log >/dev/null
if [ $? != 0 ]; then
	echo "No error printed, but there should be one."
	exit 3
fi

rm -f error.log

checkoutput <<EOF
test.log 0 zero
EOF


if [ $SELINUX_TESTS = 1 ]; then
	chcon --type=logrotate_tmp_t test.log
else
	echo "Skipping SELinux part of test 46"
fi

cleanup 47

if [ $SELINUX_TESTS = 1 ]; then

# ------------------------------- Test 47 ------------------------------------
# test that newly created state file has the same SELinux context as the
# previous one
preptest test.log 47 1

cat > state << EOF
logrotate state -- version 2
EOF

chcon --type=logrotate_tmp_t state

$RLR test-config.47

ls -Z state|grep logrotate_tmp_t >/dev/null
if [ $? != 0 ]; then
	echo "state file should have selinux context logrotate_tmp_t."
	exit 3
fi


checkoutput <<EOF
test.log 0 zero
EOF

else
	echo "Skipping SELinux test 47"
fi

cleanup 48

if [ $ACL_TESTS = 0 ]; then
  echo "Skipping test 48: no ACL support"
else

# ------------------------------- Test 48 ------------------------------------
# Test that state file keeps the set ACLs
preptest test.log 48 1 0

cat > state << EOF
logrotate state -- version 2
EOF

setfacl -m u:nobody:rwx state

$RLR test-config.48

getfacl state|grep "user:nobody:rwx" >/dev/null
if [ $? != 0 ]; then
	echo "state file must have acls user:nobody:rwx"
	exit 3
fi

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

fi

cleanup 49

# ------------------------------- Test 49 ------------------------------------
# Test that state files without hours/minutes/seconds still works properly
preptest test.log 49 1 0

cat > state << EOF
logrotate state -- version 2
"test.log" 2012-8-19
EOF

$RLR test-config.49

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

cleanup 50

# ------------------------------- Test 50 ------------------------------------
# test that hourly rotation works properly
preptest test.log 50 1 0

DATESTRING=$(/bin/date +%Y%m%d%H)
NOW=$(/bin/date "+%Y-%-m-%-d-%-H" 2>/dev/null)
HOURAGO=$(/bin/date "+%Y-%-m-%-d-%-H" --date "1 hour ago" 2>/dev/null)
GNUDATE=$?

# --force to trigger rotation
$RLR test-config.50 --force
checkoutput <<EOF
test.log 0
test.log-$DATESTRING 0 zero
EOF

# It should not rotate this hour again
echo second > test.log
rm -f test.log-$DATESTRING
$RLR test-config.50
checkoutput <<EOF
test.log 0 second
EOF

if [ -f test.log.1 ]; then
    echo "file $file does exist!"
    exit 2
fi

if [ $GNUDATE = 0 ]; then
# Simulate previous rotation by editing state file. This should overwrite
# our previously rotated log
sed -i "s,$NOW,$HOURAGO,g" state
$RLR test-config.50
checkoutput <<EOF
test.log 0
test.log-$DATESTRING 0 second
EOF
else
echo "Does not have GNU Date, skipping part of this test"
fi

cleanup 51

# ------------------------------- Test 51 ------------------------------------
# regression in 3.8.4, logrotate crashes with sharedscripts when 0 logs rotated
preptest test.log 51 1 0

# It's memory corruption and without something in state file, it won't crash
# reliably. It would be better to run valgrind here and check the errors, but
# I don't want the test-suite to depend on valgrind...
cat > state << EOF
logrotate state -- version 2
"/var/log/httpd/backend_error_log" 2013-6-16
"/var/log/tokyotyrant/*.log" 2011-5-30
"/var/log/mailman/digest" 2011-5-30
"/var/log/piranha/piranha-gui-access" 2011-5-30
"/var/log/boincerr.log" 2011-5-30
"/var/log/btmp" 2013-7-9
"/var/log/httpd/a_log" 2011-11-15
"/var/log/cups/*_log" 2012-7-19
"/var/log/rabbitmq/*.log" 2011-5-30
"/var/log/func/func.log" 2011-11-17
"/var/log/wtmp" 2013-7-9
"/var/log/glusterfs/*glusterd.vol.log" 2011-11-17
"/var/log/imapd.log" 2011-5-30
"/var/log/cobbler/cobbler.log" 2011-11-6
"/var/log/httpd/ssl_access_log" 2013-3-27
"/var/log/mrepo.log" 2011-5-30
EOF

$RLR test-config.51

if [ $? != 0 ]; then
	echo "logrotate ended with non-zero exit code (probably crashed)"
	exit 3
fi

cleanup 52

# ------------------------------- Test 52 ------------------------------------
# sharedscripts are not run if the first log file does not exist
preptest test.log 52 1 0

$RLR test-config.52

checkoutput <<EOF
test.log 0
test.log.1 0 zero
scriptout 0 foo
EOF

cleanup 53

# ------------------------------- Test 53 ------------------------------------
# test if --force works
preptest test.log 53 1 0

$RLR test-config.53 --force

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

cleanup 54

# ------------------------------- Test 54 ------------------------------------
# removing last log file when using %Y-%m-%d
rm -f *test.log*
preptest test.log 54 1 0

DATE=""
for i in $(seq 1 60)
do
    DATE=$(/bin/date "+%Y-%m-%d" --date "$i day ago" 2>/dev/null)   
    echo "x" > test.log-$DATE
done

$RLR test-config.54 --force

if [ -e test.log-$DATE ]; then
    echo "File test.log-$DATE should not exist (it should be deleted)"
    exit 3
fi

rm -f *test.log*

cleanup 55

# ------------------------------- Test 55 ------------------------------------
# removing last log file when using %s and hourly
rm -f *test.log*
preptest test.log 55 1 0

DATE=""
for i in $(seq 1 60)
do
    DATE=$(/bin/date "+%s" --date "$i hour ago" 2>/dev/null)   
    echo "x" > test.log-$DATE.gz
done

$RLR test-config.55 --force

if [ -e test.log-$DATE.gz ]; then
    echo "File test.log-$DATE.gz should not exist (it should be deleted)"
    exit 3
fi

rm -f *test.log*

cleanup 56

# ------------------------------- Test 56 ------------------------------------
# removing last log file when using %d-%m-%Y
rm -f *test.log*
preptest test.log 56 1 0

DATE=""
for i in $(seq 1 60)
do
    DATE=$(/bin/date "+%d-%m-%Y" --date "$i day ago" 2>/dev/null)   
    echo "x" > test.log-$DATE
done

$RLR test-config.56 --force

if [ -e test.log-$DATE ]; then
    echo "File test.log-$DATE should not exist (it should be deleted)"
    exit 3
fi

rm -f *test.log*

cleanup 57

# ------------------------------- Test 57 ------------------------------------
# When compressing program prints something to stderr, we should prepend it
# with the log name.
preptest test.log 57 1
$RLR test-config.57 --force 2>error.log

grep "error: Compressing" error.log >/dev/null
if [ $? != 0 ]; then
	cat error.log
	echo "No error printed, but there should be one."
	exit 3
fi

grep "compression error" error.log >/dev/null
if [ $? != 0 ]; then
	cat error.log
	echo "No error printed, but there should be one."
	exit 3
fi

rm -f error.log

checkoutput <<EOF
test.log 0
test.log.1.gz 1 zero
EOF

cleanup 58

# ------------------------------- Test 58 ------------------------------------
# Test renamecopy
preptest test.log 58 1 0
$RLR test-config.58 --force

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

cleanup 59

# ------------------------------- Test 59 ------------------------------------
# Test renamecopy in debug mode, nothing should happen
preptest test.log 58 1 0
touch test.log.1
touch test.log.2
$RLR test-config.58 --force -d 2>/dev/null

checkoutput <<EOF
test.log 0 zero
EOF

rm -f test.log.1
rm -f test.log.2

cleanup 60

# ------------------------------- Test 60 ------------------------------------
# Test we log debug output using -l option when passed.
preptest test.log 61 1 0

$RLR test-config.61 --force -l ./logrotate.log

DATESTRING=$(/bin/date +%Y-%m-%d-%H)

grep "reading config file test-config.61" logrotate.log >/dev/null
if [ $? != 0 ]; then
	echo "There is no log output in logrotate.log"
	exit 3
fi

rm -f logrotate.log

checkoutput <<EOF
test.log 0
test.log.$DATESTRING 0 zero
EOF

cleanup 61

# ------------------------------- Test 61 ------------------------------------
preptest test.log 61 1 0

$RLR test-config.61 --force

DATESTRING=$(/bin/date +%Y-%m-%d-%H)

checkoutput <<EOF
test.log 0
test.log.$DATESTRING 0 zero
EOF

cleanup 62

# ------------------------------- Test 62 ------------------------------------
# Rotate sparse file
preptest test.log 24 1 0

printf zero > test.log
truncate -s 10M test.log
echo x >> test.log

cp test.log test.example

SIZE_SPARSE_OLD=$(du test.log|awk '{print $1}')
SIZE_OLD=$(du --apparent-size test.log|awk '{print $1}')
$RLR test-config.24 --force
SIZE_NEW=$(du --apparent-size test.log.1|awk '{print $1}')
SIZE_SPARSE_NEW=$(du test.log.1|awk '{print $1}')

if [ $SIZE_OLD != $SIZE_NEW ]; then
	echo "Bad apparent size of sparse logs"
	echo "test.log: $SIZE_OLD"
	echo "test.log.1: $SIZE_NEW"
	exit 3
fi

PAGESIZE="$(getconf PAGESIZE)"
if [ -z "$PAGESIZE" ] || [ "$PAGESIZE" -lt 32768 ] ; then
    LIMIT1=100
    LIMIT2=100
else
    LIMIT1=200
    LIMIT2=20000
fi
if [ $SIZE_SPARSE_OLD -gt $LIMIT1 ] || [ $SIZE_SPARSE_NEW -gt $LIMIT2 ]; then
	echo "Bad size of sparse logs"
	echo "test.log: $SIZE_SPARSE_OLD"
	echo "test.log.1: $SIZE_SPARSE_NEW"
	exit 3
fi

checkoutput <<EOF
test.log 0
test.log.1 0 zerox
EOF

cleanup 63

# ------------------------------- Test 63 ------------------------------------
# Rotate sparse file, no data should be lost when hole is in the end of file
preptest test.log 24 1 0

printf zero > test.log
truncate -s 10M test.log

cp test.log test.example

SIZE_SPARSE_OLD=$(du test.log|awk '{print $1}')
SIZE_OLD=$(du --apparent-size test.log|awk '{print $1}')
$RLR test-config.24 --force
SIZE_NEW=$(du --apparent-size test.log.1|awk '{print $1}')
SIZE_SPARSE_NEW=$(du test.log.1|awk '{print $1}')

if [ $SIZE_OLD != $SIZE_NEW ]; then
    echo "Bad apparent size of sparse logs"
    echo "test.log: $SIZE_OLD"
    echo "test.log.1: $SIZE_NEW"
    exit 3
fi

if [ $SIZE_SPARSE_OLD -gt 100 ] || [ $SIZE_SPARSE_NEW -gt 100 ]; then
    echo "Bad size of sparse logs"
    echo "test.log: $SIZE_SPARSE_OLD"
    echo "test.log.1: $SIZE_SPARSE_NEW"
    exit 3
fi

checkoutput <<EOF
test.log 0
test.log.1 0 zero
EOF

cleanup 64

# ------------------------------- Test 64 ------------------------------------
# filename in mail's subject with compress directive and maillast directive
# should be the name of the removed log
preptest test.log 64 1 0

DATESTRING=$(/bin/date +%Y%m%d)

$RLR test-config.64 --force
checkoutput <<EOF
test.log 0
EOF

checkmail test.log-$DATESTRING.gz zero

cleanup 65

# ------------------------------- Test 65 ------------------------------------
# filename in mail's subject without compress directive and maillast directive
# should be the name of the removed log
preptest test.log 65 1 0

DATESTRING=$(/bin/date +%Y%m%d)

$RLR test-config.65 --force
checkoutput <<EOF
test.log 0
EOF

checkmail test.log-$DATESTRING zero

cleanup 66

# ------------------------------- Test 66 ------------------------------------
# When using %Y in the dateformat, the old logs are not removed
preptest test.log 66 1 0

DATESTRING=$(/bin/date +%Y%m%d)
DAYAGO=$(/bin/date "+%Y-%m-%d" --date "1 day ago" 2>/dev/null)

echo removed > "test.log$DAYAGO"

$RLR test-config.66 --force
checkoutput <<EOF
test.log 0
EOF

if [ -f test.log$DAYAGO ]; then
	echo "file $file does exist!"
	exit 2
fi

cleanup 67

# ------------------------------- Test 67 ------------------------------------
# firstaction and lastaction scripts should be called if no file is rotated
preptest test.log 67 1 0

DATESTRING=$(/bin/date +%Y%m%d)
TODAY=$(/bin/date "+%Y-%m-%d" 2>/dev/null)

echo removed > "test.log$TODAY"

$RLR test-config.67 --force

cat scriptout|grep firstaction >/dev/null
if [ $? != 0 ]; then
	echo "scriptout should contain 'firstaction'"
	exit 3
fi

cat scriptout|grep lastaction >/dev/null
if [ $? != 0 ]; then
	echo "scriptout should contain 'lastaction'"
	exit 3
fi

cleanup 68

# ------------------------------- Test 68 ------------------------------------
# Old state file entries should be removed when not used. Logrotate should
# not freeze on big state file.
preptest test.log 68 1 0

cat > state << EOF
logrotate state -- version 1
"$PWD/test.log" 2000-1-1
EOF

for i in $(seq 1 200000)
do
   echo "\"$PWD/removed.log$i\" 2000-1-1" >> state
done

$RLR test-config.68 --force

cat state|grep test.log >/dev/null
if [ $? != 0 ]; then
	echo "state file should contain 'test.log'"
	exit 3
fi

cat state|grep removed.log >/dev/null
if [ $? = 0 ]; then
	echo "state file should not contain 'removed.log'"
	exit 3
fi

cleanup 69

# ------------------------------- Test 69 ------------------------------------
# Test olddir with wildcard in the pattern
preptest test.log 69 1 0
rm -rf testdir adir bdir
mkdir adir
mkdir bdir
cp test.log adir
cp test.log bdir
$RLR test-config.69 --force -v

checkoutput <<EOF
adir/test.log 0
testdir/test.log.1 0 zero
EOF

rm -rf testdir adir
rm -rf testdir bdir

cleanup 70

# ------------------------------- Test 70 ------------------------------------
# No rotation should occur because file is too young
preptest test.log 70 2

# Put in place a state file that will force a rotation
cat > state <<EOF
logrotate state -- version 2
"$PWD/test.log" 2000-1-1
EOF

$RLR test-config.70

checkoutput <<EOF
test.log 0 zero
test.log.1 0 first
EOF

cleanup 71

# ------------------------------- Test 71 ------------------------------------
# Rotation should occur because file is old
preptest test.log 71 2

# Set log modification time to some date in the past
touch -t 200001010000 test.log

# Put in place a state file that will force a rotation
cat > state <<EOF
logrotate state -- version 2
"$PWD/test.log" 2000-1-1
EOF

$RLR test-config.71

checkoutput <<EOF
test.log 0
test.log.1 0 zero
test.log.2 0 first
EOF

cleanup 72

# ------------------------------- Test 72 ------------------------------------
preptest test.log 72 2

$RLR test-config.72 --force

checkoutput <<EOF
test.log 0
test.log.1 0 zero
test.log.2.gz 1 first
EOF

echo 'unexpected' > test.log.1.gz

$RLR test-config.72 --force
dt="$(date +%Y%m%d%H)"

checkoutput <<EOF
test.log 0
test.log.1 0
test.log.1.gz-$dt.backup 0 unexpected
test.log.2.gz 1 zero
test.log.3.gz 1 first
EOF

cleanup 100

# ------------------------------- Test 100 ------------------------------------
# check rotation with extension appended to the filename
preptest test.log 100 1 0
$RLR test-config.100 --force

checkoutput <<EOF
test.log 0
test.log.1.newext 0 zero
EOF

# check rotation with extension moved after the number
cleanup 101

preptest test.log 101 1 0
$RLR test-config.101 --force

checkoutput <<EOF
test.log 0
test.1.log 0 zero
EOF


cleanup