1#!/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5if [ "$(id -u)" -ne 0 ]; then
6  echo "$SHOWSKIP: losetup (not root)"
7  return 2>/dev/null
8  exit
9fi
10
11if [ "$(losetup -a | wc -l)" -ne 0 ]; then
12  echo "$SHOWSKIP: losetup (devices already in use)"
13  return 2>/dev/null
14  exit
15fi
16
17# Android's loopback devices are only in /dev/block/loop*.
18# Debian has symlinks like /dev/block/7:0 back to ../loop*.
19if [ -b /dev/block/sda ]; then
20  DIR="/dev/block" # Presumably Android.
21else
22  DIR="/dev"
23fi
24
25#testing "name" "command" "result" "infile" "stdin"
26
27truncate -s 1M blah.img &&
28FILE="$(readlink -f blah.img)"
29DEV="$(stat --format %d blah.img)"
30NODE="$(stat --format %i blah.img)"
31
32# TODO: assumes there are no loopback devices!
33testcmd "-f" "-f" "$DIR/loop0\n" "" ""
34testcmd "-f blah.img" "-f blah.img" "" "" ""
35testcmd "-f --show" "-f --show blah.img" "$DIR/loop1\n" "" ""
36testcmd "-a" "-a | sort" \
37  "$DIR/loop0: [$DEV]:$NODE ($FILE)\n$DIR/loop1: [$DEV]:$NODE ($FILE)\n" "" ""
38testcmd "-d $DIR/loop0" "-d $DIR/loop0 && losetup -a" \
39  "$DIR/loop1: [$DEV]:$NODE ($FILE)\n" "" ""
40testcmd "-D" "-D && losetup -a" "" "" ""
41
42rm blah.img
43