X Tutup
Skip to content

Commit 52f4c4e

Browse files
committed
Add more tests, fix.
1 parent c91acc1 commit 52f4c4e

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

src/main/java/com/github/dockerjava/api/model/Device.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import javax.annotation.Nonnull;
1515
import java.util.HashMap;
1616
import java.util.Map;
17+
import java.util.StringTokenizer;
1718

1819
@JsonInclude(Include.NON_NULL)
1920
public class Device {
@@ -59,10 +60,16 @@ public static Device parse(@Nonnull String deviceStr) {
5960
String src = "";
6061
String dst = "";
6162
String permissions = "rwm";
62-
final String[] arr = deviceStr.split(":");
63-
switch (arr.length) {
63+
final String[] arr = deviceStr.trim().split(":");
64+
// java String.split() returns wrong length, use tokenizer instead
65+
switch (new StringTokenizer(deviceStr, ":").countTokens()) {
6466
case 3: {
65-
permissions = arr[2];
67+
// Mismatches docker code logic. While there is no validations after parsing, checking heregit
68+
if (validDeviceMode(arr[2])) {
69+
permissions = arr[2];
70+
} else {
71+
throw new IllegalArgumentException("Invalid device specification: " + deviceStr);
72+
}
6673
}
6774
case 2: {
6875
if (validDeviceMode(arr[1])) {
@@ -76,7 +83,7 @@ public static Device parse(@Nonnull String deviceStr) {
7683
break;
7784
}
7885
default: {
79-
throw new IllegalArgumentException("invalid device specification: " + deviceStr);
86+
throw new IllegalArgumentException("Invalid device specification: " + deviceStr);
8087
}
8188
}
8289

src/test/java/com/github/dockerjava/api/model/DeviceTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,61 @@
22

33
import org.testng.annotations.Test;
44

5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.LinkedHashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
import static junit.framework.Assert.fail;
514
import static org.hamcrest.MatcherAssert.assertThat;
15+
import static org.hamcrest.Matchers.contains;
16+
import static org.hamcrest.Matchers.containsString;
617
import static org.hamcrest.Matchers.equalTo;
18+
import static org.hamcrest.Matchers.is;
719

820
/**
921
* @author Kanstantsin Shautsou
1022
*/
1123
public class DeviceTest {
24+
25+
public static List<String> validPaths = Arrays.asList(
26+
"/home",
27+
"/home:/home",
28+
"/home:/something/else",
29+
"/with space",
30+
"/home:/with space",
31+
"relative:/absolute-path",
32+
"hostPath:/containerPath:r",
33+
"/hostPath:/containerPath:rw",
34+
"/hostPath:/containerPath:mrw"
35+
);
36+
37+
public static HashMap<String, String> badPaths = new LinkedHashMap<String, String>() {{
38+
put("", "bad format for path: ");
39+
// TODO implement ValidatePath
40+
// put("./", "./ is not an absolute path");
41+
// put("../", "../ is not an absolute path");
42+
// put("/:../", "../ is not an absolute path");
43+
// put("/:path", "path is not an absolute path");
44+
// put(":", "bad format for path: :");
45+
// put("/tmp:", " is not an absolute path");
46+
// put(":test", "bad format for path: :test");
47+
// put(":/test", "bad format for path: :/test");
48+
// put("tmp:", " is not an absolute path");
49+
// put(":test:", "bad format for path: :test:");
50+
// put("::", "bad format for path: ::");
51+
// put(":::", "bad format for path: :::");
52+
// put("/tmp:::", "bad format for path: /tmp:::");
53+
// put(":/tmp::", "bad format for path: :/tmp::");
54+
// put("path:ro", "ro is not an absolute path");
55+
// put("path:rr", "rr is not an absolute path");
56+
put("a:/b:ro", "bad mode specified: ro");
57+
put("a:/b:rr", "bad mode specified: rr");
58+
}};
59+
1260
@Test
1361
public void testParse() throws Exception {
1462
assertThat(Device.parse("/dev/sda:/dev/xvdc:r"),
@@ -24,4 +72,24 @@ public void testParse() throws Exception {
2472
equalTo(new Device("rw", "/something", "/dev/snd")));
2573

2674
}
75+
76+
@Test
77+
public void testParseBadPaths() {
78+
for (Map.Entry<String, String> entry : badPaths.entrySet()) {
79+
final String deviceStr = entry.getKey();
80+
try {
81+
Device.parse(deviceStr);
82+
fail("Should fail because: " + entry.getValue() + " '" + deviceStr + "'");
83+
} catch (IllegalArgumentException ex) {
84+
assertThat(ex.getMessage(), containsString("Invalid device specification:"));
85+
}
86+
}
87+
}
88+
89+
@Test
90+
public void testParseValidPaths() {
91+
for (String path : validPaths) {
92+
Device.parse(path);
93+
}
94+
}
2795
}

0 commit comments

Comments
 (0)
X Tutup