more crypto and security test fixes for windows from jfdenise

This commit is contained in:
akhil 2014-01-21 12:44:35 -08:00
parent ccf506d582
commit 9a793a987c
6 changed files with 57 additions and 12 deletions

View File

@ -23,8 +23,13 @@
* questions.
*/
import com.oracle.avatar.js.Loader;
import java.io.File;
import com.oracle.avatar.js.Server;
import com.oracle.avatar.js.log.Logging;
import java.util.HashMap;
import java.util.Map;
import javax.script.ScriptEngine;
import org.testng.annotations.Test;
/**
@ -33,6 +38,9 @@ import org.testng.annotations.Test;
*/
public class CryptoTest {
private static int p = 59152;
private static final String SCRIPT_PORT = "SCRIPT_PORT";
@Test
public void testCrypto() throws Exception {
File dir = new File("src/test/js/crypto");
@ -41,7 +49,10 @@ public class CryptoTest {
final String[] args = { f.getAbsolutePath() };
System.out.println("Running " + f.getAbsolutePath());
try {
new Server().run(args);
Map<String, Object> bindings = new HashMap<>();
bindings.put(SCRIPT_PORT, getPort());
ScriptEngine engine = newEngine(bindings);
newServer(engine);
System.out.println(f + " test passed");
} catch(Exception ex) {
System.out.println(f + " test failure");
@ -53,4 +64,22 @@ public class CryptoTest {
throw new Exception("Crypto test failed");
}
}
private static int getPort() {
return ++p;
}
private static ScriptEngine newEngine(Map<String, Object> b) throws Exception {
ScriptEngine engine = Server.newEngine();
for (String k : b.keySet()) {
engine.put(k, b.get(k));
}
return engine;
}
private static Server newServer(ScriptEngine engine) throws Exception {
Server server = new Server(engine, new Loader.Core(), new Logging(false),
System.getProperty("user.dir"));
return server;
}
}

View File

@ -58,7 +58,7 @@ public class PermissionTest {
private static final String OS = System.getProperty("os.name");
private static final String ADDRESS = "127.0.0.1";
private static int p = 49152;
private static int p = 61234;
private static final String SCRIPT_PORT = "SCRIPT_PORT";
private static final String SCRIPT_ON_EXIT = "SCRIPT_ON_EXIT";
private static final String SCRIPT_OK = "SCRIPT_OK";
@ -360,7 +360,7 @@ public class PermissionTest {
Map<String, Object> bindings = new HashMap<String, Object>();
Permissions permissions = new Permissions();
bindings.put(SCRIPT_PORT, getPort());
doFail(bindings, f, permissions);
}
@ -382,11 +382,11 @@ public class PermissionTest {
throw new RuntimeException("Should have failed");
} catch (Throwable ex) {
if (!(getCause(ex) instanceof AccessControlException)) {
System.out.println("UNEXPECTED EXCEPTION " + ex);
System.err.println("UNEXPECTED EXCEPTION " + ex);
ex.printStackTrace();
throw ex;
} else {
System.out.println("Catch expected exception " + ex);
System.err.println("Catch expected exception " + ex);
}
}
}
@ -402,7 +402,7 @@ public class PermissionTest {
try {
r.call();
} catch (Exception ex) {
System.out.println("UNEXPECTED EXCEPTION " + ex);
System.err.println("UNEXPECTED EXCEPTION " + ex);
ex.printStackTrace();
throw ex;
}
@ -413,6 +413,8 @@ public class PermissionTest {
}
private static void doFail(Map<String, Object> bindings, File f, Permissions permissions) throws Exception {
// Capture caller
System.err.println("Called by " + new Exception().getStackTrace()[1].getMethodName());
URL location = f.toURI().toURL();
final String[] args = {f.getAbsolutePath()};
final ScriptEngine engine = newEngine(bindings);
@ -431,6 +433,8 @@ public class PermissionTest {
}
private static void doSuccess(Map<String, Object> bindings, File f, Permissions permissions) throws Exception {
// Capture caller
System.err.println("Called by " + new Exception().getStackTrace()[1].getMethodName());
URL location = f.toURI().toURL();
final String[] args = { f.getAbsolutePath() };
final ScriptEngine engine = newEngine(bindings);
@ -449,7 +453,7 @@ public class PermissionTest {
}
});
System.out.println(f + " Auth test passed");
System.err.println(f + " Auth test passed");
}
private static void init(Permissions permissions, URL location) throws Exception {

View File

@ -22,7 +22,7 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
var PORT = 9999;
var PORT = SCRIPT_PORT;
var assert = require('assert');
var tls = require('tls');
var fs = require('fs');

View File

@ -22,7 +22,7 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
var PORT = 9999;
var PORT = SCRIPT_PORT;
var assert = require('assert');
var tls = require('tls');
var fs = require('fs');

View File

@ -24,6 +24,6 @@
*/
// tcp can't be loaded, does a connect at load time but we don't have connect permission.
global.SCRIPT_PORT = 9999;
global.SCRIPT_PORT = SCRIPT_PORT;
global.SCRIPT_ON_EXIT = false;
require("./tcp");

View File

@ -25,6 +25,7 @@
var net = require('net');
var assert = require('assert');
var connected = false;
var closed = false;
var server = net.Server(function(connection) {
console.error('SERVER got connection');
connected = true;
@ -32,9 +33,11 @@ var server = net.Server(function(connection) {
server.close();
});
server.listen(SCRIPT_PORT, function() {
server.on('close', function() {closed = true;});
console.log("TCP PORT TO LISTEN TO " + global.SCRIPT_PORT);
server.listen(global.SCRIPT_PORT, function() {
var c = net.createConnection({
port: SCRIPT_PORT
port: global.SCRIPT_PORT
});
c.end();
});
@ -43,5 +46,14 @@ if(SCRIPT_ON_EXIT) {
process.on('exit', function() {
assert.ok(connected);
SCRIPT_OK = connected;
if (!closed) {
server.close();
}
});
} else {
process.on('exit', function() {
if (!closed) {
server.close();
}
});
}