cryptonas/src/cryptobox/tests/test.core.blockdevice.py

83 lines
3.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2006-11-30 15:50:28 +01:00
#
# Copyright 2006 sense.lab e.V.
#
# This file is part of the CryptoBox.
#
# The CryptoBox is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# The CryptoBox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the CryptoBox; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""Unittests for cryptobox.core.tools
"""
__revision__ = "$Id$"
2006-11-06 17:05:00 +01:00
import cryptobox.core.blockdevice as blockdevice
from cryptobox.tests.base import CommonTestClass
from cryptobox.core.exceptions import CBInternalError
2006-11-06 17:05:00 +01:00
import os
2006-11-30 15:50:28 +01:00
2008-07-17 20:10:48 +02:00
# different commonly available block devices - used for testing major/minor
COMMON_DEV_INFOS = [
('/sys/block/hda/hda1', '/dev/hda1', '/dev/hda', (3,1)),
('/sys/block/sda/sda1', '/dev/sda1', '/dev/sda', (8,1)),
('/sys/block/ubda/ubda', '/dev/ubda1', '/dev/ubda', (98,1)),
('/sys/block/ubdb/ubdb1', '/dev/ubdb1', '/dev/ubdb', (98,17)),
]
class CoreBlockDevice(CommonTestClass):
2007-08-18 02:45:12 +02:00
"""All unittests for cryptoboxtools
"""
2006-11-06 17:05:00 +01:00
2007-08-18 02:45:12 +02:00
def test_find_major_minor_of_device(self):
"""check the find_major_minor_of_device function
"""
func = blockdevice.get_blockdevice
2008-07-17 20:10:48 +02:00
# check if any of the common block devices is available
blockdevice_found = False
for sys_dir, dev_node, parent, major_minor in COMMON_DEV_INFOS:
if os.path.isdir(sys_dir):
self.assertTrue(func(dev_node).devnum == major_minor)
blockdevice_found = True
# we assume, that we found at least one blockdevice
self.assertTrue(blockdevice_found)
2007-08-18 02:45:12 +02:00
self.assertTrue(func("/dev/nothere") is None)
2006-11-06 17:05:00 +01:00
2007-08-18 02:45:12 +02:00
def test_is_part_of_blockdevice(self):
"""check the is_part_of_blockdevice function
"""
get_device = lambda devname: blockdevice.get_blockdevice(devname)
2008-07-17 20:10:48 +02:00
func = lambda parent, child: get_device(parent) and get_device(parent).is_parent_of(get_device(child))
2008-02-18 20:28:19 +01:00
func_raw = lambda parent: get_device(parent).is_parent_of
2007-08-18 02:45:12 +02:00
self.assertTrue(func(self.blockdevice, self.device))
self.assertFalse(func(self.blockdevice, self.blockdevice))
self.assertFalse(func(self.device, self.blockdevice))
self.assertFalse(func(self.device, self.device))
self.assertFalse(func(self.blockdevice, "/dev/hde1"))
2008-07-17 20:10:48 +02:00
# check if any of the common block devices is available
blockdevice_found = False
for sys_dir, dev_node, parent, major_minor in COMMON_DEV_INFOS:
if os.path.isdir(sys_dir):
self.assertFalse(func(dev_node, parent))
self.assertTrue(func(parent, dev_node))
blockdevice_found = True
self.assertFalse(func("ram0", "ram1"))
2008-02-18 20:28:19 +01:00
self.assertFalse(func_raw(self.blockdevice)(None))
self.assertRaises(CBInternalError, func_raw(self.blockdevice), "")
2006-11-06 17:05:00 +01:00