DistanceDemo implements Application<BigInteger, ProtocolBuilderNumeric> { public static <ResourcePoolT extends ResourcePool> void main(String[] args) throws IOException { CmdLineUtil<ResourcePoolT, ProtocolBuilderNumeric> cmdUtil = new CmdLineUtil<>(); int x = 0; int y = 0; cmdUtil.addOption(Option.builder("x").desc("The integer x coordinate of this party. " + "Note only party 1 and 2 should supply this input.").hasArg().build()); cmdUtil.addOption(Option.builder("y").desc("The integer y coordinate of this party. " + "Note only party 1 and 2 should supply this input").hasArg().build()); CommandLine cmd = cmdUtil.parse(args); NetworkConfiguration networkConfiguration = cmdUtil.getNetworkConfiguration(); if (networkConfiguration.getMyId() == 1 || networkConfiguration.getMyId() == 2) { if (!cmd.hasOption("x") || !cmd.hasOption("y")) { cmdUtil.displayHelp(); throw new IllegalArgumentException("Party 1 and 2 must submit input"); } else { x = Integer.parseInt(cmd.getOptionValue("x")); y = Integer.parseInt(cmd.getOptionValue("y")); } } else { if (cmd.hasOption("x") || cmd.hasOption("y")) { throw new IllegalArgumentException("Only party 1 and 2 should submit input"); } } DistanceDemo distDemo = new DistanceDemo(networkConfiguration.getMyId(), x, y); SecureComputationEngine<ResourcePoolT, ProtocolBuilderNumeric> sce = cmdUtil.getSce(); ResourcePoolT resourcePool = cmdUtil.getResourcePool(); BigInteger bigInteger = sce.runApplication(distDemo, resourcePool, cmdUtil.getNetwork()); double dist = Math.sqrt(bigInteger.doubleValue()); log.info("Distance between party 1 and 2 is: " + dist); cmdUtil.closeNetwork(); sce.shutdownSCE(); } DistanceDemo(int id, int x, int y); @Override DRes<BigInteger> buildComputation(ProtocolBuilderNumeric producer); static void main(String[] args);  }
@Test public void testDistanceFromCmdLine() throws Exception { Runnable p1 = () -> { try { DistanceDemo.main( new String[]{"-i", "1", "-p", "1:localhost:8081", "-p", "2:localhost:8082", "-s", "dummyArithmetic", "-x", "10", "-y", "10"}); } catch (IOException e) { throw new RuntimeException("Communication error"); } }; Runnable p2 = () -> { try { DistanceDemo.main( new String[]{"-i", "2", "-p", "1:localhost:8081", "-p", "2:localhost:8082", "-s", "dummyArithmetic", "-x", "20", "-y", "15"}); } catch (IOException e) { throw new RuntimeException("Communication error"); } }; Thread t1 = new Thread(p1); Thread t2 = new Thread(p2); t1.start(); t2.start(); t1.join(); t2.join(); }
@Test public void testDistanceFromCmdLine3Party() throws Exception { Runnable p1 = () -> { try { DistanceDemo.main( new String[]{"-i", "1", "-p", "1:localhost:8081", "-p", "2:localhost:8082", "-p", "3:localhost:8083", "-s", "dummyArithmetic", "-x", "10", "-y", "10"}); } catch (IOException e) { throw new RuntimeException("Communication error"); } }; Runnable p2 = () -> { try { DistanceDemo.main( new String[]{"-i", "2", "-p", "1:localhost:8081", "-p", "2:localhost:8082", "-p", "3:localhost:8083", "-s", "dummyArithmetic", "-x", "20", "-y", "15"}); } catch (IOException e) { throw new RuntimeException("Communication error"); } }; Runnable p3 = () -> { try { DistanceDemo.main( new String[]{"-i", "3", "-p", "1:localhost:8081", "-p", "2:localhost:8082", "-p", "3:localhost:8083", "-s", "dummyArithmetic"}); } catch (IOException e) { throw new RuntimeException("Communication error"); } }; Thread t1 = new Thread(p1); Thread t2 = new Thread(p2); Thread t3 = new Thread(p3); t1.start(); t2.start(); t3.start(); t1.join(); t2.join(); t3.join(); }
@Test(expected = IllegalArgumentException.class) public void testDistanceCmdLine3PartyWithInput() throws Exception { DistanceDemo.main( new String[]{"-i", "3", "-p", "1:localhost:8081", "-p", "2:localhost:8082", "-p", "3:localhost:8083", "-s", "dummyArithmetic", "-x", "20", "-y", "15"}); fail(); }
@Test(expected = IllegalArgumentException.class) public void testDistanceCmdLine3PartyWithInputX() throws Exception { DistanceDemo.main( new String[]{"-i", "3", "-p", "1:localhost:8081", "-p", "2:localhost:8082", "-p", "3:localhost:8083", "-s", "dummyArithmetic", "-x", "20"}); fail(); }
@Test(expected = IllegalArgumentException.class) public void testDistanceCmdLine3PartyWithInputY() throws Exception { DistanceDemo.main( new String[]{"-i", "3", "-p", "1:localhost:8081", "-p", "2:localhost:8082", "-p", "3:localhost:8083", "-s", "dummyArithmetic", "-y", "15"}); fail(); }
@Test(expected = IllegalArgumentException.class) public void testDistanceCmdLine2PartyWithNoInputX() throws Exception { DistanceDemo.main( new String[]{"-i", "1", "-p", "1:localhost:8081", "-p", "2:localhost:8082", "-p", "3:localhost:8083", "-s", "dummyArithmetic", "-y", "15"}); fail(); }
@Test(expected = IllegalArgumentException.class) public void testDistanceCmdLine2PartyWithNoInputY() throws Exception { DistanceDemo.main( new String[]{"-i", "1", "-p", "1:localhost:8081", "-p", "2:localhost:8082", "-p", "3:localhost:8083", "-s", "dummyArithmetic", "-x", "20"}); fail(); }
@Test(expected = IllegalArgumentException.class) public void testDistanceCmdLine2PartyWithNoInput() throws Exception { DistanceDemo.main( new String[]{"-i", "1", "-p", "1:localhost:8081", "-p", "2:localhost:8082", "-p", "3:localhost:8083", "-s", "dummyArithmetic"}); fail(); }