// Makes sure QueryKeys have no problem going in and out of GGEP blocks public void testQueryKeysAndGGEP() throws Exception { MACCalculatorRepositoryManager macManager = new MACCalculatorRepositoryManager(); Random rand = new Random(); for (int i = 4; i < 17; i++) { byte[] qk = new byte[i]; Arrays.sort(qk); // make sure the bytes have offensive characters.... while ((Arrays.binarySearch(qk, (byte) 0x1c) < 0) || (Arrays.binarySearch(qk, (byte) 0x00) < 0)) { rand.nextBytes(qk); Arrays.sort(qk); } AddressSecurityToken addressSecurityToken = new AddressSecurityToken(qk, macManager); ByteArrayOutputStream baos = new ByteArrayOutputStream(); addressSecurityToken.write(baos); GGEP in = new GGEP(true); in.put(GGEPKeys.GGEP_HEADER_QUERY_KEY_SUPPORT, baos.toByteArray()); baos = new ByteArrayOutputStream(); in.write(baos); GGEP out = new GGEP(baos.toByteArray(), 0, null); AddressSecurityToken queryKey2 = new AddressSecurityToken( out.getBytes(GGEPKeys.GGEP_HEADER_QUERY_KEY_SUPPORT), macManager); assertEquals("qks not equal, i = " + i, addressSecurityToken, queryKey2); } }
public void testGGEPPing() throws Exception { // first make a GGEP block.... GGEP ggepBlock = new GGEP(true); ggepBlock.put(GGEPKeys.GGEP_HEADER_QUERY_KEY_SUPPORT); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ggepBlock.write(baos); byte[] ggepBytes = baos.toByteArray(); // Headers plus payload(6 bytes) byte[] buffer = new byte[23 + ggepBytes.length + 1]; byte[] guid = GUID.makeGuid(); // get a GUID System.arraycopy(guid, 0, buffer, 0, guid.length); // copy GUID int currByte = guid.length; buffer[currByte] = Message.F_PING; currByte++; buffer[currByte] = 0x0001; // TTL currByte++; buffer[currByte] = 0x0000; // Hops currByte++; buffer[currByte] = (byte) (ggepBytes.length + 1); // 1st byte = 6 currByte++; buffer[currByte] = 0x0000; // 2nd byte = 0 currByte++; buffer[currByte] = 0x0000; // 3rd byte = 0 currByte++; buffer[currByte] = 0x0000; // 4th byte = 0 - remember it's little endian currByte++; // stick in GGEP for (int i = 0; i < ggepBytes.length; i++) buffer[currByte++] = ggepBytes[i]; buffer[currByte++] = 0; // trailing 0 assertGreaterThanOrEquals(buffer.length, currByte); // OK, ggep ping ready ByteArrayInputStream stream = new ByteArrayInputStream(buffer); Message m; m = messageFactory.read(stream, Network.TCP); PingRequest pr; pr = (PingRequest) m; assertTrue(!pr.isQueryKeyRequest()); pr.hop(); assertTrue(pr.isQueryKeyRequest()); // Came this far means its all OK }
public HUGEExtension(byte[] extsBytes) { int currIndex = 0; // while we don't encounter a null.... while ((currIndex < extsBytes.length) && (extsBytes[currIndex] != (byte) 0x00)) { // HANDLE GGEP STUFF if (extsBytes[currIndex] == GGEP.GGEP_PREFIX_MAGIC_NUMBER) { int start = currIndex; int[] endIndex = new int[1]; endIndex[0] = currIndex + 1; try { GGEP ggep = new GGEP(extsBytes, currIndex, endIndex); if (_ggep == null) { _ggep = new GGEP(); } _ggep.merge(ggep); if (_ggepBlocks == null) { _ggepBlocks = new ArrayList<GGEPBlock>(2); } _ggepBlocks.add(new GGEPBlock(ggep, start, endIndex[0])); } catch (BadGGEPBlockException ignored) { } currIndex = endIndex[0]; } else { // HANDLE HUGE STUFF int delimIndex = currIndex; while ((delimIndex < extsBytes.length) && (extsBytes[delimIndex] != (byte) 0x1c)) delimIndex++; if (delimIndex <= extsBytes.length) { try { // another GEM extension String curExtStr = new String(extsBytes, currIndex, delimIndex - currIndex, "UTF-8"); if (URN.isUrn(curExtStr)) { // it's an URN to match, of form "urn:namespace:etc" URN urn = URN.createSHA1Urn(curExtStr); if (_urns == null) _urns = new UrnSet(); _urns.add(urn); } else if (URN.Type.isSupportedUrnType(curExtStr)) { if (_urnTypes == null) _urnTypes = EnumSet.noneOf(URN.Type.class); _urnTypes.add(URN.Type.createUrnType(curExtStr)); } else { // miscellaneous, but in the case of queries, xml if (_miscBlocks == null) _miscBlocks = new HashSet<String>(1); _miscBlocks.add(curExtStr); } } catch (IOException bad) { } } // else we've overflown and not encounted a 0x1c - discard currIndex = delimIndex + 1; } } }
@Override public void decode(final GGEP rawGGEP) throws BadGGEPBlockException { if (!rawGGEP.hasValueFor(KEY_WRAPPED_BYTES)) throw new BadGGEPBlockException("Missing wrappedBytes"); super.decode(rawGGEP); }