diff options
Diffstat (limited to 'java/test/Ice/hash/Client.java')
-rw-r--r-- | java/test/Ice/hash/Client.java | 429 |
1 files changed, 429 insertions, 0 deletions
diff --git a/java/test/Ice/hash/Client.java b/java/test/Ice/hash/Client.java new file mode 100644 index 00000000000..07bc968b7e7 --- /dev/null +++ b/java/test/Ice/hash/Client.java @@ -0,0 +1,429 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +package test.Ice.hash; + +public class Client +{ + private static void + test(boolean b) + { + if(!b) + { + throw new RuntimeException(); + } + } + + public static void main(String[] args) + { + int status = 0; + try + { + java.util.Map<Integer, Ice.ObjectPrx> seenProxy = new java.util.HashMap<Integer, Ice.ObjectPrx>(); + java.util.Map<Integer, Ice.Endpoint> seenEndpoint = new java.util.HashMap<Integer, Ice.Endpoint>(); + int proxyCollisions = 0; + int endpointCollisions = 0; + int i = 0; + int maxCollisions = 10; + int maxIterations = 10000; + + Ice.InitializationData initData = new Ice.InitializationData(); + initData.properties = Ice.Util.createProperties(args); + initData.properties.setProperty("Ice.Plugin.IceSSL", "IceSSL.PluginFactory"); + Ice.Communicator communicator = Ice.Util.initialize(args, initData); + + System.out.print("testing proxy & endpoint hash algorithm collisions... "); + { + java.util.Random rand = new java.util.Random(); + for(i = 0; proxyCollisions < maxCollisions && + endpointCollisions < maxCollisions && + i < maxIterations; ++i) + { + java.io.StringWriter sw = new java.io.StringWriter(); + sw.write(Integer.toString(i)); + sw.write(":tcp -p "); + sw.write(Integer.toString(rand.nextInt(65536))); + sw.write(" -t 10"); + sw.write(Integer.toString(rand.nextInt(1000000))); + sw.write(":udp -p "); + sw.write(Integer.toString(rand.nextInt(65536))); + sw.write(" -h "); + sw.write(Integer.toString(rand.nextInt(100))); + + Ice.ObjectPrx obj = communicator.stringToProxy(sw.toString()); + java.util.List<Ice.Endpoint> endpoints = new java.util.ArrayList<Ice.Endpoint>(java.util.Arrays.asList(obj.ice_getEndpoints())); + if(seenProxy.containsKey(obj.hashCode())) + { + if(obj.equals(seenProxy.get(obj.hashCode()))) + { + continue; // Same object + } + ++proxyCollisions; + } + else + { + seenProxy.put(obj.hashCode(), obj); + } + + java.util.Iterator<Ice.Endpoint> j = endpoints.iterator(); + while(j.hasNext()) + { + Ice.Endpoint endpoint = j.next(); + if(seenEndpoint.containsKey(endpoint.hashCode())) + { + if(endpoint.equals(seenEndpoint.get(endpoint.hashCode()))) + { + continue; // Same endpoint + } + ++endpointCollisions; + System.out.println(endpoint.toString() + " == " + seenEndpoint.get(endpoint.hashCode()).toString() + " == " + endpoint.hashCode()); + } + else + { + seenEndpoint.put(endpoint.hashCode(), endpoint); + } + // + // Check the same endpoint produce always the same hash + // + test(endpoint.hashCode() == endpoint.hashCode()); + } + // + // Check the same proxy produce always the same hash + // + test(obj.hashCode() == obj.hashCode()); + } + test(proxyCollisions < maxCollisions); + test(endpointCollisions < maxCollisions); + + proxyCollisions = 0; + seenProxy = new java.util.HashMap<Integer, Ice.ObjectPrx>(); + for(i = 0; proxyCollisions < maxCollisions && + endpointCollisions < maxCollisions && + i < maxIterations; ++i) + { + java.io.StringWriter sw = new java.io.StringWriter(); + sw.write(Integer.toString(i)); + sw.write(":tcp -p "); + sw.write(Integer.toString(rand.nextInt(65536))); + sw.write(" -t 10"); + sw.write(Integer.toString(rand.nextInt(1000000))); + sw.write(":udp -p "); + sw.write(Integer.toString(rand.nextInt(65536))); + sw.write(" -h "); + sw.write(Integer.toString(rand.nextInt(100))); + + Ice.ProxyIdentityKey obj = new Ice.ProxyIdentityKey(communicator.stringToProxy(sw.toString())); + if(seenProxy.containsKey(obj.hashCode())) + { + ++proxyCollisions; + } + else + { + seenProxy.put(obj.hashCode(), obj.getProxy()); + } + // + // Check the same proxy produce always the same hash + // + test(obj.hashCode() == obj.hashCode()); + } + test(proxyCollisions < maxCollisions); + + proxyCollisions = 0; + seenProxy = new java.util.HashMap<Integer, Ice.ObjectPrx>(); + for(i = 0; proxyCollisions < maxCollisions && + endpointCollisions < maxCollisions && + i < maxIterations; ++i) + { + java.io.StringWriter sw = new java.io.StringWriter(); + sw.write(Integer.toString(i)); + sw.write(":tcp -p "); + sw.write(Integer.toString(rand.nextInt(65536))); + sw.write(" -t 10"); + sw.write(Integer.toString(rand.nextInt(1000000))); + sw.write(":udp -p "); + sw.write(Integer.toString(rand.nextInt(65536))); + sw.write(" -h "); + sw.write(Integer.toString(rand.nextInt(100))); + + Ice.ProxyIdentityFacetKey obj = new Ice.ProxyIdentityFacetKey(communicator.stringToProxy(sw.toString())); + if(seenProxy.containsKey(obj.hashCode())) + { + ++proxyCollisions; + } + else + { + seenProxy.put(obj.hashCode(), obj.getProxy()); + } + // + // Check the same proxy produce always the same hash + // + test(obj.hashCode() == obj.hashCode()); + } + test(proxyCollisions < maxCollisions); + + Ice.ObjectPrx prx1 = communicator.stringToProxy("Glacier2/router:tcp -p 10010"); + Ice.ObjectPrx prx2 = communicator.stringToProxy("Glacier2/router:ssl -p 10011"); + Ice.ObjectPrx prx3 = communicator.stringToProxy("Glacier2/router:udp -p 10012"); + Ice.ObjectPrx prx4 = communicator.stringToProxy("Glacier2/router:tcp -h zeroc.com -p 10010"); + Ice.ObjectPrx prx5 = communicator.stringToProxy("Glacier2/router:ssl -h zeroc.com -p 10011"); + Ice.ObjectPrx prx6 = communicator.stringToProxy("Glacier2/router:udp -h zeroc.com -p 10012"); + Ice.ObjectPrx prx7 = communicator.stringToProxy("Glacier2/router:tcp -p 10010 -t 10000"); + Ice.ObjectPrx prx8 = communicator.stringToProxy("Glacier2/router:ssl -p 10011 -t 10000"); + Ice.ObjectPrx prx9 = communicator.stringToProxy("Glacier2/router:tcp -h zeroc.com -p 10010 -t 10000"); + Ice.ObjectPrx prx10 = communicator.stringToProxy("Glacier2/router:ssl -h zeroc.com -p 10011 -t 10000"); + + java.util.Map<String, Integer> proxyMap = new java.util.HashMap<String, Integer>(); + proxyMap.put("prx1", prx1.hashCode()); + proxyMap.put("prx2", prx2.hashCode()); + proxyMap.put("prx3", prx3.hashCode()); + proxyMap.put("prx4", prx4.hashCode()); + proxyMap.put("prx5", prx5.hashCode()); + proxyMap.put("prx6", prx6.hashCode()); + proxyMap.put("prx7", prx7.hashCode()); + proxyMap.put("prx8", prx8.hashCode()); + proxyMap.put("prx9", prx9.hashCode()); + proxyMap.put("prx10", prx10.hashCode()); + + test(communicator.stringToProxy("Glacier2/router:tcp -p 10010").hashCode() == proxyMap.get("prx1")); + test(communicator.stringToProxy("Glacier2/router:ssl -p 10011").hashCode() == proxyMap.get("prx2")); + test(communicator.stringToProxy("Glacier2/router:udp -p 10012").hashCode() == proxyMap.get("prx3")); + test(communicator.stringToProxy("Glacier2/router:tcp -h zeroc.com -p 10010").hashCode() == proxyMap.get("prx4")); + test(communicator.stringToProxy("Glacier2/router:ssl -h zeroc.com -p 10011").hashCode() == proxyMap.get("prx5")); + test(communicator.stringToProxy("Glacier2/router:udp -h zeroc.com -p 10012").hashCode() == proxyMap.get("prx6")); + test(communicator.stringToProxy("Glacier2/router:tcp -p 10010 -t 10000").hashCode() == proxyMap.get("prx7")); + test(communicator.stringToProxy("Glacier2/router:ssl -p 10011 -t 10000").hashCode() == proxyMap.get("prx8")); + test(communicator.stringToProxy("Glacier2/router:tcp -h zeroc.com -p 10010 -t 10000").hashCode() == proxyMap.get("prx9")); + test(communicator.stringToProxy("Glacier2/router:ssl -h zeroc.com -p 10011 -t 10000").hashCode() == proxyMap.get("prx10")); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx1).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx1).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx2).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx2).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx3).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx3).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx4).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx4).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx5).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx5).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx6).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx6).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx7).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx7).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx8).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx8).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx9).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx9).hashCode()); + + test(new Ice.ProxyIdentityKey(prx1).hashCode() == new Ice.ProxyIdentityKey(prx10).hashCode()); + test(new Ice.ProxyIdentityFacetKey(prx1).hashCode() == new Ice.ProxyIdentityFacetKey(prx10).hashCode()); + } + + System.out.println("ok"); + + System.out.print("testing struct hash algorithm collisions... "); + { + java.util.Map<Integer,Test.PointF> seenPointF = new java.util.HashMap<Integer, Test.PointF>(); + java.util.Random rand = new java.util.Random(); + int structCollisions = 0; + for(i = 0; i < maxIterations && structCollisions < maxCollisions; ++i) + { + Test.PointF pf = new Test.PointF(rand.nextFloat(), rand.nextFloat(), rand.nextFloat()); + if(seenPointF.containsKey(pf.hashCode())) + { + if(pf.equals(seenPointF.get(pf.hashCode()))) + { + continue; // same object + } + structCollisions++; + } + else + { + seenPointF.put(pf.hashCode(), pf); + } + // + // Check the same struct produce always the same hash + // + test(pf.hashCode() == pf.hashCode()); + } + test(structCollisions < maxCollisions); + + java.util.Map<Integer,Test.PointD> seenPointD = new java.util.HashMap<Integer, Test.PointD>(); + rand = new java.util.Random(); + structCollisions = 0; + for(i = 0; i < maxIterations && structCollisions < maxCollisions; ++i) + { + Test.PointD pd = new Test.PointD(rand.nextDouble(), rand.nextDouble(), rand.nextDouble()); + if(seenPointD.containsKey(pd.hashCode())) + { + if(pd.equals(seenPointF.get(pd.hashCode()))) + { + continue; // same object + } + structCollisions++; + } + else + { + seenPointD.put(pd.hashCode(), pd); + } + // + // Check the same struct produce always the same hash + // + test(pd.hashCode() == pd.hashCode()); + } + test(structCollisions < maxCollisions); + + java.util.Map<Integer,Test.Polyline> seenPolyline = new java.util.HashMap<Integer, Test.Polyline>(); + structCollisions = 0; + for(i = 0; i < maxIterations && structCollisions < maxCollisions; ++i) + { + Test.Polyline polyline = new Test.Polyline(); + java.util.List<Test.Point> vertices = new java.util.ArrayList<Test.Point>(); + for(int j = 0; j < 100; ++j) + { + vertices.add(new Test.Point(rand.nextInt(100), rand.nextInt(100))); + } + polyline.vertices = new Test.Point[vertices.size()]; + vertices.toArray(polyline.vertices); + + if(seenPolyline.containsKey(polyline.hashCode())) + { + if(polyline.equals(seenPolyline.get(polyline.hashCode()))) + { + continue; // same object + } + structCollisions++; + } + else + { + seenPolyline.put(polyline.hashCode(), polyline); + } + // + // Check the same struct produce always the same hash + // + test(polyline.hashCode() == polyline.hashCode()); + } + test(structCollisions < maxCollisions); + + java.util.Map<Integer,Test.ColorPalette> seenColorPalette = new java.util.HashMap<Integer, Test.ColorPalette>(); + structCollisions = 0; + for(i = 0; i < maxIterations && structCollisions < maxCollisions; ++i) + { + Test.ColorPalette colorPalette = new Test.ColorPalette(); + colorPalette.colors = new java.util.HashMap<Integer, Test.Color>(); + for(int j = 0; j < 100; ++j) + { + colorPalette.colors.put(j, new Test.Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(255))); + } + + if(seenColorPalette.containsKey(colorPalette.hashCode())) + { + if(colorPalette.equals(seenColorPalette.get(colorPalette.hashCode()))) + { + continue; // same object + } + structCollisions++; + } + else + { + seenColorPalette.put(colorPalette.hashCode(), colorPalette); + } + // + // Check the same struct produce always the same hash + // + test(colorPalette.hashCode() == colorPalette.hashCode()); + } + test(structCollisions < maxCollisions); + + java.util.Map<Integer,Test.Color> seenColor = new java.util.HashMap<Integer, Test.Color>(); + rand = new java.util.Random(); + structCollisions = 0; + for(i = 0; i < maxIterations && structCollisions < maxCollisions; ++i) + { + Test.Color c = new Test.Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)); + if(seenColor.containsKey(c.hashCode())) + { + if(c.equals(seenColor.get(c.hashCode()))) + { + continue; // same object + } + structCollisions++; + } + else + { + seenColor.put(c.hashCode(), c); + } + // + // Check the same struct produce always the same hash + // + test(c.hashCode() == c.hashCode()); + } + test(structCollisions < maxCollisions); + + structCollisions = 0; + java.util.Map<Integer,Test.Draw> seenDraw = new java.util.HashMap<Integer, Test.Draw>(); + structCollisions = 0; + for(i = 0; i < maxIterations && structCollisions < maxCollisions; ++i) + { + Test.Draw draw = new Test.Draw( + new Test.Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)), + new Test.Pen(rand.nextInt(10), + new Test.Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(255))), + false); + + if(seenDraw.containsKey(draw.hashCode())) + { + if(draw.equals(seenDraw.get(draw.hashCode()))) + { + continue; // same object + } + structCollisions++; + } + else + { + seenDraw.put(draw.hashCode(), draw); + } + // + // Check the same struct produce always the same hash + // + test(draw.hashCode() == draw.hashCode()); + } + test(structCollisions < maxCollisions); + } + System.out.println("ok"); + + if(communicator != null) + { + try + { + communicator.destroy(); + } + catch(Ice.LocalException ex) + { + System.out.println(ex.toString()); + status = 1; + } + } + } + catch(Exception ex) + { + System.out.println(ex.toString()); + status = 1; + } + System.gc(); + System.exit(status); + } +} |