Skip to content

Commit 7f57ac1

Browse files
authored
Merge pull request ESAPI#520 from jeremiahjstacey/issue_143
OS Name DefaultExecutorTests ESAPI#143
2 parents 0ec2b35 + 4bd6690 commit 7f57ac1

File tree

1 file changed

+61
-4
lines changed

1 file changed

+61
-4
lines changed

src/test/java/org/owasp/esapi/reference/ExecutorTest.java

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@
1717

1818
import java.io.File;
1919
import java.io.FileWriter;
20+
import java.lang.reflect.Field;
2021
import java.util.ArrayList;
2122
import java.util.Collections;
2223
import java.util.List;
2324

24-
import junit.framework.Test;
25-
import junit.framework.TestCase;
26-
import junit.framework.TestSuite;
27-
2825
import org.owasp.esapi.ESAPI;
2926
import org.owasp.esapi.ExecuteResult;
3027
import org.owasp.esapi.Executor;
@@ -34,6 +31,10 @@
3431
import org.owasp.esapi.codecs.UnixCodec;
3532
import org.owasp.esapi.codecs.WindowsCodec;
3633

34+
import junit.framework.Test;
35+
import junit.framework.TestCase;
36+
import junit.framework.TestSuite;
37+
3738
/**
3839
* The Class ExecutorTest.
3940
*
@@ -92,6 +93,62 @@ public static Test suite() {
9293
TestSuite suite = new TestSuite(ExecutorTest.class);
9394
return suite;
9495
}
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+
95152

96153
/**
97154
* Test of executeOSCommand method, of class org.owasp.esapi.Executor

0 commit comments

Comments
 (0)