|
17 | 17 |
|
18 | 18 | import java.io.File;
|
19 | 19 | import java.io.FileWriter;
|
| 20 | +import java.lang.reflect.Field; |
20 | 21 | import java.util.ArrayList;
|
21 | 22 | import java.util.Collections;
|
22 | 23 | import java.util.List;
|
23 | 24 |
|
24 |
| -import junit.framework.Test; |
25 |
| -import junit.framework.TestCase; |
26 |
| -import junit.framework.TestSuite; |
27 |
| - |
28 | 25 | import org.owasp.esapi.ESAPI;
|
29 | 26 | import org.owasp.esapi.ExecuteResult;
|
30 | 27 | import org.owasp.esapi.Executor;
|
|
34 | 31 | import org.owasp.esapi.codecs.UnixCodec;
|
35 | 32 | import org.owasp.esapi.codecs.WindowsCodec;
|
36 | 33 |
|
| 34 | +import junit.framework.Test; |
| 35 | +import junit.framework.TestCase; |
| 36 | +import junit.framework.TestSuite; |
| 37 | + |
37 | 38 | /**
|
38 | 39 | * The Class ExecutorTest.
|
39 | 40 | *
|
@@ -92,6 +93,62 @@ public static Test suite() {
|
92 | 93 | TestSuite suite = new TestSuite(ExecutorTest.class);
|
93 | 94 | return suite;
|
94 | 95 | }
|
| 96 | + |
| 97 | + private void resetSingletonField() throws Exception { |
| 98 | + //Wipe the singleton field here so we can force recreation. |
| 99 | + Field singletonField = DefaultExecutor.class.getDeclaredField("singletonInstance"); |
| 100 | + singletonField.setAccessible(true); |
| 101 | + //Object ref is ignored since field is static. |
| 102 | + singletonField.set(new Object(), null); |
| 103 | + } |
| 104 | + |
| 105 | + public void testPlatformResoveWindows() throws Exception { |
| 106 | + String origName = System.getProperty("os.name"); |
| 107 | + |
| 108 | + try { |
| 109 | + //Wipe the singleton field here so we can force recreation. |
| 110 | + resetSingletonField(); |
| 111 | + |
| 112 | + System.setProperty("os.name", "a name that includes the literal 'Windows'"); |
| 113 | + Executor ex = DefaultExecutor.getInstance(); |
| 114 | + |
| 115 | + |
| 116 | + Field codecField = DefaultExecutor.class.getDeclaredField("codec"); |
| 117 | + codecField.setAccessible(true); |
| 118 | + |
| 119 | + Object instCodec = codecField.get(ex); |
| 120 | + |
| 121 | + assertTrue(instCodec instanceof WindowsCodec); |
| 122 | + } finally { |
| 123 | + System.setProperty("os.name", origName); |
| 124 | + resetSingletonField(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public void testPlatformResolveNx() throws Exception{ |
| 129 | + String origName = System.getProperty("os.name"); |
| 130 | + |
| 131 | + try { |
| 132 | + //Wipe the singleton field here so we can force recreation. |
| 133 | + resetSingletonField(); |
| 134 | + |
| 135 | + //Unmatched Platform is anything but the literal string "Windows" - In part or in whole. |
| 136 | + System.setProperty("os.name", "unmatchedPlatform"); |
| 137 | + Executor ex = DefaultExecutor.getInstance(); |
| 138 | + |
| 139 | + |
| 140 | + Field codecField = DefaultExecutor.class.getDeclaredField("codec"); |
| 141 | + codecField.setAccessible(true); |
| 142 | + |
| 143 | + Object instCodec = codecField.get(ex); |
| 144 | + |
| 145 | + assertTrue(instCodec instanceof UnixCodec); |
| 146 | + } finally { |
| 147 | + System.setProperty("os.name", origName); |
| 148 | + resetSingletonField(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
95 | 152 |
|
96 | 153 | /**
|
97 | 154 | * Test of executeOSCommand method, of class org.owasp.esapi.Executor
|
|
0 commit comments