1#!/bin/bash
2
3# Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>
4# Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
5
6[ -f testing.sh ] && . testing.sh
7
8#testing "name" "command" "result" "infile" "stdin"
9
10root_fs=`df | grep "\/$" | awk '{print $1}'`
11root_fs_type=`file -sL $root_fs | awk '{print $5}'`
12
13tmp_b_fs="tmp_b_fs"
14tmp_b_fs_type="ext3"
15
16reCreateTmpFs() {
17  rm -rf $tmp_b_fs
18  mknod $tmp_b_fs b 1 0
19  mkfs.ext3 $tmp_b_fs >/dev/null 2>&1
20}
21reCreateTmpFs
22
23testing "mount $root_fs /mnt" \
24  "mount $root_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
25   sleep 1 && umount /mnt && test -e /testDir && rmdir /testDir" "" "" ""
26testing "mount $tmp_b_fs /mnt" \
27  "mount $tmp_b_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
28   sleep 1 && umount /mnt && ! test -e /mnt/testDir" "" "" ""
29reCreateTmpFs
30
31chmod 444 /mnt
32testing "mount $root_fs /mnt (read_only dir)" \
33  "mount $root_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
34   sleep 1 && umount /mnt && test -e /testDir && rmdir /testDir" "" "" ""
35testing "mount $tmp_b_fs /mnt (read_only dir)" \
36  "mount $tmp_b_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
37   sleep 1 && umount /mnt && ! test -e /mnt/testDir" "" "" ""
38reCreateTmpFs
39chmod 755 /mnt
40testing "mount -w $root_fs /mnt (write_only mode)" \
41  "mount -w $root_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
42   sleep 1 && umount /mnt && test -e /testDir && rmdir /testDir" "" "" ""
43testing "mount -w $tmp_b_fs /mnt (write_only mode)" \
44  "mount -w $tmp_b_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
45   sleep 1 && umount /mnt && ! test -e /mnt/testDir" "" "" ""
46reCreateTmpFs
47testing "mount -rw $tmp_b_fs /mnt (read_write mode)" \
48  'mount -rw $tmp_b_fs /mnt >/dev/null && mkdir /mnt/testDir && \
49   sleep 1 && ! test -e /mnt/testDir && umount /mnt' "" "" ""
50reCreateTmpFs
51testing "mount $tmp_b_fs /mnt -t fs_type" \
52  "mount $tmp_b_fs /mnt -t $tmp_b_fs_type >/dev/null 2>&1 &&
53   mkdir /mnt/testDir && sleep 1 && umount /mnt &&
54   ! test -e /mnt/testDir" "" "" ""
55reCreateTmpFs
56mkdir -p testDir1/testDir2 testDir
57echo "abcdefghijklmnopqrstuvwxyz" > testDir1/testDir2/testFile
58testing "mount -o bind dir1 dir2" \
59  'mount -o bind testDir1 testDir >/dev/null 2>&1 && \
60   cat testDir/testDir2/testFile && sleep 1 && umount testDir' \
61  "abcdefghijklmnopqrstuvwxyz\n" "" ""
62testing "mount -o rbind dir1 dir2" \
63  'mount -o rbind testDir1 testDir >/dev/null 2>&1 && \
64   cat testDir/testDir2/testFile && sleep 1 && umount testDir' \
65  "abcdefghijklmnopqrstuvwxyz\n" "" ""
66testing "mount -o loop $tmp_b_fs /mnt" \
67  "mount -o loop $tmp_b_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDirp &&
68   sleep 1 && umount -d /mnt && ! test -e /mnt/testDirp" "" "" ""
69reCreateTmpFs
70
71mkdir testDir2
72testing "mount -o move mount_1 mount_2" \
73  "mount $tmp_b_fs testDir1 && mkdir testDir1/testDirr &&
74   mount -o move testDir1 testDir2 && test -r testDir2/testDirr &&
75   sleep 1 && umount testDir2" "" "" ""
76reCreateTmpFs
77testing "mount -o rw $tmp_b_fs /mnt" \
78  "mount -o rw $tmp_b_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
79   sleep 1 && umount /mnt && ! test -e /mnt/testDir" "" "" ""
80reCreateTmpFs
81testing "mount -o ro $tmp_b_fs /mnt" \
82  "mount -o ro $tmp_b_fs /mnt >/dev/null 2>&1 &&
83   mkdir /mnt/testDir 2>/dev/null || sleep 1 && umount /mnt" "" "" ""
84reCreateTmpFs
85testing "mount -o ro,remount $tmp_b_fs /mnt" \
86  "mount -o ro $tmp_b_fs /mnt >/dev/null 2>&1 &&
87   mkdir /mnt/testDir 2>/dev/null || sleep 1 && umount /mnt" "" "" ""
88reCreateTmpFs
89
90umount testDir1
91rm -f $tmp_b_fs
92