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 "\/$" | cut -d' ' -f1`
11root_fs_type=`blkid -o value $root_fs | tail -1`
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
23# TODO: replace /mnt with a directory we know exists. (Android has no /mnt.)
24
25testing "$root_fs /mnt" \
26  "mount $root_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
27   sleep 1 && umount /mnt && test -e /testDir && rmdir /testDir" "" "" ""
28testing "$tmp_b_fs /mnt" \
29  "mount $tmp_b_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
30   sleep 1 && umount /mnt && ! test -e /mnt/testDir" "" "" ""
31reCreateTmpFs
32
33chmod 444 /mnt
34testing "$root_fs /mnt (read_only dir)" \
35  "mount $root_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
36   sleep 1 && umount /mnt && test -e /testDir && rmdir /testDir" "" "" ""
37testing "$tmp_b_fs /mnt (read_only dir)" \
38  "mount $tmp_b_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
39   sleep 1 && umount /mnt && ! test -e /mnt/testDir" "" "" ""
40reCreateTmpFs
41chmod 755 /mnt
42testing "-w $root_fs /mnt (write_only mode)" \
43  "mount -w $root_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
44   sleep 1 && umount /mnt && test -e /testDir && rmdir /testDir" "" "" ""
45testing "-w $tmp_b_fs /mnt (write_only mode)" \
46  "mount -w $tmp_b_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
47   sleep 1 && umount /mnt && ! test -e /mnt/testDir" "" "" ""
48reCreateTmpFs
49testing "-rw $tmp_b_fs /mnt (read_write mode)" \
50  'mount -rw $tmp_b_fs /mnt >/dev/null && mkdir /mnt/testDir && \
51   sleep 1 && ! test -e /mnt/testDir && umount /mnt' "" "" ""
52reCreateTmpFs
53testing "$tmp_b_fs /mnt -t fs_type" \
54  "mount $tmp_b_fs /mnt -t $tmp_b_fs_type >/dev/null 2>&1 &&
55   mkdir /mnt/testDir && sleep 1 && umount /mnt &&
56   ! test -e /mnt/testDir" "" "" ""
57reCreateTmpFs
58mkdir -p testDir1/testDir2 testDir
59echo "abcdefghijklmnopqrstuvwxyz" > testDir1/testDir2/testFile
60testing "-o bind dir1 dir2" \
61  'mount -o bind testDir1 testDir >/dev/null 2>&1 && \
62   cat testDir/testDir2/testFile && sleep 1 && umount testDir' \
63  "abcdefghijklmnopqrstuvwxyz\n" "" ""
64testing "-o rbind dir1 dir2" \
65  'mount -o rbind testDir1 testDir >/dev/null 2>&1 && \
66   cat testDir/testDir2/testFile && sleep 1 && umount testDir' \
67  "abcdefghijklmnopqrstuvwxyz\n" "" ""
68testing "-o loop $tmp_b_fs /mnt" \
69  "mount -o loop $tmp_b_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDirp &&
70   sleep 1 && umount -d /mnt && ! test -e /mnt/testDirp" "" "" ""
71reCreateTmpFs
72
73mkdir testDir2
74testing "-o move mount_1 mount_2" \
75  "mount $tmp_b_fs testDir1 && mkdir testDir1/testDirr &&
76   mount -o move testDir1 testDir2 && test -r testDir2/testDirr &&
77   sleep 1 && umount testDir2" "" "" ""
78reCreateTmpFs
79testing "-o rw $tmp_b_fs /mnt" \
80  "mount -o rw $tmp_b_fs /mnt >/dev/null 2>&1 && mkdir /mnt/testDir &&
81   sleep 1 && umount /mnt && ! test -e /mnt/testDir" "" "" ""
82reCreateTmpFs
83testing "-o ro $tmp_b_fs /mnt" \
84  "mount -o ro $tmp_b_fs /mnt >/dev/null 2>&1 &&
85   mkdir /mnt/testDir 2>/dev/null || sleep 1 && umount /mnt" "" "" ""
86reCreateTmpFs
87testing "-o ro,remount $tmp_b_fs /mnt" \
88  "mount -o ro $tmp_b_fs /mnt >/dev/null 2>&1 &&
89   mkdir /mnt/testDir 2>/dev/null || sleep 1 && umount /mnt" "" "" ""
90reCreateTmpFs
91
92umount testDir1
93rm -f $tmp_b_fs
94