|
29 | 29 | import com.sun.jna.platform.linux.Udev.UdevDevice;
|
30 | 30 | import com.sun.jna.platform.linux.Udev.UdevEnumerate;
|
31 | 31 | import com.sun.jna.platform.linux.Udev.UdevListEntry;
|
| 32 | +import java.util.regex.Matcher; |
| 33 | +import java.util.regex.Pattern; |
32 | 34 |
|
33 | 35 | import junit.framework.TestCase;
|
34 | 36 |
|
|
37 | 39 | */
|
38 | 40 | public class UdevTest extends TestCase {
|
39 | 41 |
|
| 42 | + private static final Pattern SCSI_DISK_PATTERN = Pattern.compile(".*/sd[a-z](\\d+)$"); |
| 43 | + |
40 | 44 | @Test
|
41 | 45 | public void testEnumerateDevices() {
|
42 | 46 | // Start with the context object
|
@@ -74,29 +78,40 @@ public void testEnumerateDevices() {
|
74 | 78 | // No additional reference is acquired from getParent
|
75 | 79 | if (parent != null && parent2 != null) {
|
76 | 80 | // Devnode should match same parent without restricting to block and disk
|
77 |
| - assertEquals("Partition parent should match with and without filter", |
| 81 | + assertEquals(String.format( |
| 82 | + "Partition parent should match with and without filter (%s)", |
| 83 | + devnode |
| 84 | + ), |
78 | 85 | parent.getDevnode(), parent2.getDevnode());
|
79 | 86 | // Save the size and major
|
80 | 87 | parentSize = parent.getSysattrValue("size");
|
81 | 88 | parentMajor = parent.getPropertyValue("MAJOR");
|
82 | 89 | }
|
83 | 90 | }
|
84 | 91 | String size = device.getSysattrValue("size");
|
85 |
| - assertTrue("Size must be nonnegative", 0 <= Long.parseLong(size)); |
| 92 | + assertTrue(String.format("Size must be nonnegative (%s)", devnode), 0 <= Long.parseLong(size)); |
86 | 93 | if (parentSize != null) {
|
87 |
| - assertTrue("Partition can't be bigger than its disk", |
| 94 | + assertTrue(String.format("Partition can't be bigger than its disk (%s)", devnode), |
88 | 95 | Long.parseLong(size) <= Long.parseLong(parentSize));
|
89 | 96 | }
|
90 | 97 | String major = device.getPropertyValue("MAJOR");
|
91 |
| - assertTrue("Major value must be nonnegative", 0 <= Long.parseLong(major)); |
| 98 | + assertTrue(String.format("Major value must be nonnegative (%s)", devnode), 0 <= Long.parseLong(major)); |
92 | 99 | if (parentMajor != null) {
|
93 |
| - assertEquals("Partition and its parent disk should have same major number", major, |
94 |
| - parentMajor); |
| 100 | + // For scsi disks only the first 15 Partitions have the |
| 101 | + // same major as their disk |
| 102 | + Matcher scsiMatcher = SCSI_DISK_PATTERN.matcher(devnode); |
| 103 | + boolean scsiDiskDynamicMinor = scsiMatcher.matches() && Integer.parseInt(scsiMatcher.group(1)) > 15; |
| 104 | + if(! scsiDiskDynamicMinor) { |
| 105 | + assertEquals( |
| 106 | + String.format("Partition and its parent disk should have same major number (%s)", devnode), |
| 107 | + major, parentMajor |
| 108 | + ); |
| 109 | + } |
95 | 110 | }
|
96 |
| - assertEquals("DevType mismatch", devType, device.getDevtype()); |
97 |
| - assertEquals("Subsystem mismatch", "block", device.getSubsystem()); |
98 |
| - assertEquals("Syspath mismatch", syspath, device.getSyspath()); |
99 |
| - assertTrue("Syspath should end with name", syspath.endsWith(device.getSysname())); |
| 111 | + assertEquals(String.format("DevType mismatch (%s)", devnode), devType, device.getDevtype()); |
| 112 | + assertEquals(String.format("Subsystem mismatch (%s)", devnode), "block", device.getSubsystem()); |
| 113 | + assertEquals(String.format("Syspath mismatch (%s)", devnode), syspath, device.getSyspath()); |
| 114 | + assertTrue(String.format("Syspath should end with name (%s)", devnode), syspath.endsWith(device.getSysname())); |
100 | 115 | }
|
101 | 116 | } finally {
|
102 | 117 | // Release the reference and iterate to the next device
|
|
0 commit comments