Example #1
1
 // 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);
   }
 }
Example #2
0
    @Override
    protected void map(
        NullWritable n1,
        NullWritable n2,
        Mapper<NullWritable, NullWritable, ImmutableBytesWritable, Cell>.Context context)
        throws java.io.IOException, InterruptedException {

      byte keyBytes[] = new byte[keyLength];
      byte valBytes[] = new byte[valLength];

      int taskId = context.getTaskAttemptID().getTaskID().getId();
      assert taskId < Byte.MAX_VALUE : "Unit tests dont support > 127 tasks!";

      Random random = new Random();
      for (int i = 0; i < ROWSPERSPLIT; i++) {

        random.nextBytes(keyBytes);
        // Ensure that unique tasks generate unique keys
        keyBytes[keyLength - 1] = (byte) (taskId & 0xFF);
        random.nextBytes(valBytes);
        ImmutableBytesWritable key = new ImmutableBytesWritable(keyBytes);

        for (byte[] family : TestHFileOutputFormat2.FAMILIES) {
          Cell kv = new KeyValue(keyBytes, family, QUALIFIER, valBytes);
          context.write(key, kv);
        }
      }
    }
  @Override
  public Xid createTransactionIdentifier() {
    Random random = new Random(System.currentTimeMillis());

    final int formatId = random.nextInt();
    final byte[] globalTransactionId = new byte[Xid.MAXGTRIDSIZE];
    random.nextBytes(globalTransactionId);
    final byte[] branchQualifier = new byte[Xid.MAXBQUALSIZE];
    random.nextBytes(branchQualifier);

    return new Xid() {
      @Override
      public int getFormatId() {
        return formatId;
      }

      @Override
      public byte[] getGlobalTransactionId() {
        return globalTransactionId;
      }

      @Override
      public byte[] getBranchQualifier() {
        return branchQualifier;
      }
    };
  }
Example #4
0
  @Test
  public void insert_many_reopen_check() throws InterruptedException {
    e = openEngine();
    int max = 1000;
    int size = 100000;
    Random r = new Random(0);
    List<Long> recids = new ArrayList<Long>();
    for (int j = 0; j < max; j++) {
      byte[] b = new byte[r.nextInt(size)];
      r.nextBytes(b);
      long recid = e.put(b, Serializer.BYTE_ARRAY_NOSIZE);
      recids.add(recid);
    }
    e.commit();

    reopen();

    r = new Random(0);
    for (long recid : recids) {
      byte[] b = new byte[r.nextInt(size)];
      r.nextBytes(b);
      byte[] b2 = e.get(recid, Serializer.BYTE_ARRAY_NOSIZE);
      assertTrue("Data were not commited recid=" + recid, Arrays.equals(b, b2));
    }
  }
  // indexes Integer.MAX_VALUE docs with a fixed binary field
  // TODO: must use random.nextBytes (like Test2BTerms) to avoid BytesRefHash probing issues
  public void test2BOrds() throws Exception {
    BaseDirectoryWrapper dir = newFSDirectory(_TestUtil.getTempDir("2BOrds"));
    if (dir instanceof MockDirectoryWrapper) {
      ((MockDirectoryWrapper) dir).setThrottling(MockDirectoryWrapper.Throttling.NEVER);
    }

    IndexWriter w =
        new IndexWriter(
            dir,
            new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))
                .setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH)
                .setRAMBufferSizeMB(256.0)
                .setMergeScheduler(new ConcurrentMergeScheduler())
                .setMergePolicy(newLogMergePolicy(false, 10))
                .setOpenMode(IndexWriterConfig.OpenMode.CREATE));

    Document doc = new Document();
    byte bytes[] = new byte[4];
    BytesRef data = new BytesRef(bytes);
    SortedDocValuesField dvField = new SortedDocValuesField("dv", data);
    doc.add(dvField);

    long seed = random().nextLong();
    Random random = new Random(seed);

    for (int i = 0; i < Integer.MAX_VALUE; i++) {
      random.nextBytes(bytes);
      w.addDocument(doc);
      if (i % 100000 == 0) {
        System.out.println("indexed: " + i);
        System.out.flush();
      }
    }

    w.forceMerge(1);
    w.close();

    System.out.println("verifying...");
    System.out.flush();

    DirectoryReader r = DirectoryReader.open(dir);
    random.setSeed(seed);
    for (AtomicReaderContext context : r.leaves()) {
      AtomicReader reader = context.reader();
      BytesRef scratch = new BytesRef();
      BinaryDocValues dv = reader.getSortedDocValues("dv");
      for (int i = 0; i < reader.maxDoc(); i++) {
        random.nextBytes(bytes);
        dv.get(i, scratch);
        assertEquals(data, scratch);
      }
    }

    r.close();
    dir.close();
  }
 private Put setupPut(Random rand, byte[] key, byte[] value, final int numFamilies) {
   rand.nextBytes(key);
   Put put = new Put(key);
   for (int cf = 0; cf < numFamilies; ++cf) {
     for (int q = 0; q < numQualifiers; ++q) {
       rand.nextBytes(value);
       put.add(Bytes.toBytes(FAMILY_PREFIX + cf), Bytes.toBytes(QUALIFIER_PREFIX + q), value);
     }
   }
   return put;
 }
    /** null if not requested. */
    DDACheckJob(Random r, File directory, File readFilename, File writeFilename) {
      this.directory = directory;
      this.readFilename = readFilename;
      this.writeFilename = writeFilename;

      byte[] random = new byte[128];

      r.nextBytes(random);
      this.readContent = HexUtil.bytesToHex(random);

      r.nextBytes(random);
      this.writeContent = HexUtil.bytesToHex(random);
    }
Example #8
0
 protected List<HRegionInfo> uniformRegions(int numRegions) {
   List<HRegionInfo> regions = new ArrayList<HRegionInfo>(numRegions);
   byte[] start = new byte[16];
   byte[] end = new byte[16];
   rand.nextBytes(start);
   rand.nextBytes(end);
   for (int i = 0; i < numRegions; i++) {
     Bytes.putInt(start, 0, numRegions << 1);
     Bytes.putInt(end, 0, (numRegions << 1) + 1);
     TableName tableName = TableName.valueOf("table" + i);
     HRegionInfo hri = new HRegionInfo(tableName, start, end, false);
     regions.add(hri);
   }
   return regions;
 }
Example #9
0
  private static long write(Connector conn, ArrayList<byte[]> cfset, String table)
      throws TableNotFoundException, MutationsRejectedException {
    Random rand = new Random();

    byte val[] = new byte[50];

    BatchWriter bw = conn.createBatchWriter(table, new BatchWriterConfig());

    long t1 = System.currentTimeMillis();

    for (int i = 0; i < 1 << 15; i++) {
      byte[] row = FastFormat.toZeroPaddedString(abs(rand.nextLong()), 16, 16, new byte[0]);

      Mutation m = new Mutation(row);
      for (byte[] cf : cfset) {
        byte[] cq = FastFormat.toZeroPaddedString(rand.nextInt(1 << 16), 4, 16, new byte[0]);
        rand.nextBytes(val);
        m.put(cf, cq, val);
      }

      bw.addMutation(m);
    }

    bw.close();

    long t2 = System.currentTimeMillis();

    return t2 - t1;
  }
Example #10
0
  private List<byte[]> generateRows() {
    // generate prefixes
    List<byte[]> prefixes = new ArrayList<byte[]>();
    prefixes.add(new byte[0]);
    for (int i = 1; i < numberOfRowPrefixes; ++i) {
      int prefixLength = averagePrefixLength;
      prefixLength += randomizer.nextInt(2 * prefixLengthVariance + 1) - prefixLengthVariance;
      byte[] newPrefix = new byte[prefixLength];
      randomizer.nextBytes(newPrefix);
      byte[] newPrefixWithCommon = newPrefix;
      prefixes.add(newPrefixWithCommon);
    }

    // generate rest of the row
    List<byte[]> rows = new ArrayList<byte[]>();
    for (int i = 0; i < numberOfRows; ++i) {
      int suffixLength = averageSuffixLength;
      suffixLength += randomizer.nextInt(2 * suffixLengthVariance + 1) - suffixLengthVariance;
      int randomPrefix = randomizer.nextInt(prefixes.size());
      byte[] row = new byte[prefixes.get(randomPrefix).length + suffixLength];
      byte[] rowWithCommonPrefix = Bytes.concat(commonPrefix, row);
      rows.add(rowWithCommonPrefix);
    }

    return rows;
  }
Example #11
0
  public static String encrypt(String str) {

    BASE64Encoder encoder = new BASE64Encoder();
    byte[] salt = new byte[8];
    random.nextBytes(salt);
    return encoder.encode(salt) + encoder.encode(str.getBytes());
  }
  @Test
  public void testStupidlyLargeSetAndSizeOverride() throws Exception {
    Random r = new Random();
    SerializingTranscoder st = new SerializingTranscoder(Integer.MAX_VALUE);

    st.setCompressionThreshold(Integer.MAX_VALUE);

    byte[] data = new byte[21 * 1024 * 1024];
    r.nextBytes(data);

    try {
      client.set("bigobject", 60, data, st).get();
      fail("Didn't fail setting big object.");
    } catch (ExecutionException e) {
      System.err.println(
          "Successful failure setting big object.  Object size "
              + data.length
              + " bytes doesn't fit.");
      e.printStackTrace();
      OperationException oe = (OperationException) e.getCause();
      assertSame(OperationErrorType.SERVER, oe.getType());
    }

    // But I should still be able to do something.
    client.set("k", 5, "Blah");
    assertEquals("Blah", client.get("k"));
  }
  @Test
  public void testStupidlyLargeSet() throws Exception {
    Random r = new Random();
    SerializingTranscoder st = new SerializingTranscoder();
    st.setCompressionThreshold(Integer.MAX_VALUE);

    byte[] data = new byte[21 * 1024 * 1024];
    r.nextBytes(data);

    try {
      client.set("bigobject", 60, data, st).get();
      fail("Didn't fail setting big object.");
    } catch (IllegalArgumentException e) {
      assertEquals(
          "Cannot cache data larger than "
              + CachedData.MAX_SIZE
              + " bytes "
              + "(you tried to cache a "
              + data.length
              + " byte object)",
          e.getMessage());
    }

    // But I should still be able to do something.
    client.set("k", 5, "Blah");
    assertEquals("Blah", client.get("k"));
  }
 public NetHandlerLoginServer(MinecraftServer p_i45298_1_, NetworkManager p_i45298_2_) {
   this.currentLoginState = NetHandlerLoginServer.LoginState.HELLO;
   this.serverId = "";
   this.server = p_i45298_1_;
   this.networkManager = p_i45298_2_;
   RANDOM.nextBytes(this.field_147330_e);
 }
  public void testRecordGrowFactor() throws Exception {
    paginatedCluster.set(OCluster.ATTRIBUTES.COMPRESSION, ONothingCompression.NAME);
    paginatedCluster.set(OCluster.ATTRIBUTES.RECORD_GROW_FACTOR, 1.5);

    byte[] record = new byte[100];
    Random random = new Random();
    random.nextBytes(record);

    OPhysicalPosition physicalPosition = paginatedCluster
        .createRecord(record, OVersionFactory.instance().createVersion(), (byte) 1);

    OCacheEntry cacheEntry = diskCache.load(1, 1, false);
    OCachePointer pagePointer = cacheEntry.getCachePointer();

    OClusterPage page = new OClusterPage(pagePointer.getDataPointer(), false, ODurablePage.TrackMode.NONE);
    int recordIndex = (int) (physicalPosition.clusterPosition.longValue() & 0xFFFF);

    Assert.assertEquals(page.getRecordSize(recordIndex), ((int) (record.length * 1.5)) + RECORD_SYSTEM_INFORMATION);
    diskCache.release(cacheEntry);

    paginatedCluster.set(OCluster.ATTRIBUTES.RECORD_GROW_FACTOR, 2);
    physicalPosition = paginatedCluster.createRecord(record, OVersionFactory.instance().createVersion(), (byte) 1);

    recordIndex = (int) (physicalPosition.clusterPosition.longValue() & 0xFFFF);
    cacheEntry = diskCache.load(1, 1, false);
    pagePointer = cacheEntry.getCachePointer();

    page = new OClusterPage(pagePointer.getDataPointer(), false, ODurablePage.TrackMode.NONE);

    Assert.assertEquals(page.getRecordSize(recordIndex), record.length * 2 + RECORD_SYSTEM_INFORMATION);
    diskCache.release(cacheEntry);
  }
 public static EdgeLabel createPublic(boolean queryable) {
   System.out.println("TODO: Funktion richtig implementieren.");
   final byte[] data = new byte[17];
   Random random = new Random();
   random.nextBytes(data);
   return publicEdge(queryable, data);
 }
  @Override
  public TagVerifier writeTag(Tag tag) throws IOException {
    Random random = new Random();
    MifareUltralight ultralight = MifareUltralight.get(tag);
    ultralight.connect();

    final byte[] fourPages = new byte[NUM_PAGES * MifareUltralight.PAGE_SIZE];
    byte[] onePage = new byte[MifareUltralight.PAGE_SIZE];
    for (int i = 0; i < NUM_PAGES; i++) {
      random.nextBytes(onePage);
      System.arraycopy(onePage, 0, fourPages, i * onePage.length, onePage.length);
      ultralight.writePage(USER_PAGE_OFFSET + i, onePage);
    }

    final CharSequence expectedContent = NfcUtils.displayByteArray(fourPages);

    return new TagVerifier() {
      @Override
      public Result verifyTag(Tag tag) throws IOException {
        MifareUltralight ultralight = MifareUltralight.get(tag);
        if (ultralight != null) {
          ultralight.connect();
          byte[] actualFourPages = ultralight.readPages(USER_PAGE_OFFSET);
          CharSequence actualContent = NfcUtils.displayByteArray(actualFourPages);
          return new Result(
              expectedContent, actualContent, Arrays.equals(fourPages, actualFourPages));
        } else {
          return new Result(expectedContent, null, false);
        }
      }
    };
  }
Example #18
0
  /**
   * Creates a new SshMsgKexInit object.
   *
   * @param props
   */
  public SshMsgKexInit(SshConnectionProperties props) {
    super(SSH_MSG_KEX_INIT);

    // Create some random data
    cookie = new byte[16];

    // Seed the random number generator
    Random r = ConfigurationLoader.getRND();

    // Get the next random bytes into our cookie
    r.nextBytes(cookie);

    // Get the supported algorithms from the factory objects but adding the
    // preffered algorithm to the top of the list
    supportedKex =
        sortAlgorithmList(SshKeyExchangeFactory.getSupportedKeyExchanges(), props.getPrefKex());
    supportedPK = sortAlgorithmList(SshKeyPairFactory.getSupportedKeys(), props.getPrefPublicKey());
    supportedEncryptCS =
        sortAlgorithmList(SshCipherFactory.getSupportedCiphers(), props.getPrefCSEncryption());
    supportedEncryptSC =
        sortAlgorithmList(SshCipherFactory.getSupportedCiphers(), props.getPrefSCEncryption());
    supportedMacCS = sortAlgorithmList(SshHmacFactory.getSupportedMacs(), props.getPrefCSMac());
    supportedMacSC = sortAlgorithmList(SshHmacFactory.getSupportedMacs(), props.getPrefSCMac());
    supportedCompCS =
        sortAlgorithmList(SshCompressionFactory.getSupportedCompression(), props.getPrefCSComp());
    supportedCompSC =
        sortAlgorithmList(SshCompressionFactory.getSupportedCompression(), props.getPrefSCComp());

    // We currently don't support language preferences
    supportedLangCS = new ArrayList();
    supportedLangSC = new ArrayList();

    // We don't guess (I don't see the point of this in the protocol!)
    firstKexFollows = false;
  }
Example #19
0
  /**
   * Write random values to the writer assuming a table created using {@link #FAMILIES} as column
   * family descriptors
   */
  private void writeRandomKeyValues(
      RecordWriter<ImmutableBytesWritable, Cell> writer,
      TaskAttemptContext context,
      Set<byte[]> families,
      int numRows)
      throws IOException, InterruptedException {
    byte keyBytes[] = new byte[Bytes.SIZEOF_INT];
    int valLength = 10;
    byte valBytes[] = new byte[valLength];

    int taskId = context.getTaskAttemptID().getTaskID().getId();
    assert taskId < Byte.MAX_VALUE : "Unit tests dont support > 127 tasks!";
    final byte[] qualifier = Bytes.toBytes("data");
    Random random = new Random();
    for (int i = 0; i < numRows; i++) {

      Bytes.putInt(keyBytes, 0, i);
      random.nextBytes(valBytes);
      ImmutableBytesWritable key = new ImmutableBytesWritable(keyBytes);

      for (byte[] family : families) {
        Cell kv = new KeyValue(keyBytes, family, qualifier, valBytes);
        writer.write(key, kv);
      }
    }
  }
Example #20
0
  private File createTestFile(int length, int offset) throws IOException {
    File ret = createTestFile();

    byte[] data = new byte[length];
    rand.nextBytes(data);

    long crc32 = FileUtils.calcCRC(data);

    File baseDir = ret.getParentFile();
    String fileName = ret.getName();

    WriteRequest request = new WriteRequest();
    request.setBaseDir(baseDir);
    request.setFileName(fileName);
    request.setData(data);
    request.setOffset(offset);
    request.setLength(length);

    WriteResponse response = FileUtils.writeBlock(request);
    if (!response.isSuccess() || crc32 != response.getCrc32()) {
      throw new IOException(
          String.format("Failed creating temporary file: ", ret.getAbsolutePath()));
    }

    return ret;
  }
 public static void main(String[] args) throws Exception {
   ProgressiveFileBasedCSMap b = new ProgressiveFileBasedCSMap();
   b.init(1000000, "/opt/sdfs/hash");
   long start = System.currentTimeMillis();
   Random rnd = new Random();
   byte[] hash = null;
   long val = -33;
   byte[] hash1 = null;
   long val1 = -33;
   Tiger16HashEngine eng = new Tiger16HashEngine();
   for (int i = 0; i < 60000; i++) {
     byte[] z = new byte[64];
     rnd.nextBytes(z);
     hash = eng.getHash(z);
     val = rnd.nextLong();
     if (i == 1) {
       val1 = val;
       hash1 = hash;
     }
     if (val < 0) val = val * -1;
     ChunkData cm = new ChunkData(hash, val);
     boolean k = b.put(cm);
     if (k == false) System.out.println("Unable to add this " + k);
   }
   long end = System.currentTimeMillis();
   System.out.println("Took " + (end - start) / 1000 + " s " + val1);
   System.out.println(
       "Took " + (System.currentTimeMillis() - end) / 1000 + " ms at pos " + b.get(hash1));
   b.claimRecords(SDFSEvent.gcInfoEvent("testing 123"));
   b.close();
 }
Example #22
0
  @Test
  public void cas_uses_serializer() {
    Random r = new Random();
    byte[] data = new byte[1024];
    r.nextBytes(data);

    e = openEngine();
    long recid = e.put(data, Serializer.BYTE_ARRAY);

    byte[] data2 = new byte[100];
    r.nextBytes(data2);
    assertTrue(e.compareAndSwap(recid, data.clone(), data2.clone(), Serializer.BYTE_ARRAY));

    assertTrue(Serializer.BYTE_ARRAY.equals(data2, e.get(recid, Serializer.BYTE_ARRAY)));
    e.close();
  }
  /** Tests the method {@link ReferenceGsmAlphabet#bytesToString(byte[])}. */
  public void testBytesToString() {
    /** RNG, initialised to a fixed number. */
    Random randy = new Random(0);

    for (int i = 0; i < 512; ++i) {
      byte[] testBytes = new byte[randy.nextInt(420)];
      randy.nextBytes(testBytes);

      // make sure all bytes are 7-bit values
      for (int j = 0; j < testBytes.length; j++) {
        testBytes[j] = (byte) (testBytes[j] & 0x7F);
        if (testBytes[j] == 0x1B) {
          if (j == testBytes.length - 1) {
            // no room for extended char, so just chuck in a random @
            testBytes[j] = 0;
          } else {
            testBytes[j + 1] =
                HexUtils.decode(
                    ReferenceGsmAlphabet.extBytes[
                        randy.nextInt(ReferenceGsmAlphabet.extBytes.length)])[1];
          }
        }
      }

      testBytesToString(testBytes);
    }
  }
Example #24
0
  private static UUID rndUUIDv4() {
    // ~ return UUID.randomUUID() :
    final Random random = threadRandom.get();

    final byte[] uuid = new byte[16];
    random.nextBytes(uuid);
    uuid[6] &= 0x0f; /* clear version        */
    uuid[6] |= 0x40; /* set to version 4     */
    uuid[8] &= 0x3f; /* clear variant        */
    uuid[8] |= 0x80; /* set to IETF variant  */

    long msb = 0;
    msb = (msb << 8) | (uuid[0] & 0xff);
    msb = (msb << 8) | (uuid[1] & 0xff);
    msb = (msb << 8) | (uuid[2] & 0xff);
    msb = (msb << 8) | (uuid[3] & 0xff);
    msb = (msb << 8) | (uuid[4] & 0xff);
    msb = (msb << 8) | (uuid[5] & 0xff);
    msb = (msb << 8) | (uuid[6] & 0xff);
    msb = (msb << 8) | (uuid[7] & 0xff);

    long lsb = 0;
    lsb = (lsb << 8) | (uuid[8] & 0xff);
    lsb = (lsb << 8) | (uuid[9] & 0xff);
    lsb = (lsb << 8) | (uuid[10] & 0xff);
    lsb = (lsb << 8) | (uuid[11] & 0xff);
    lsb = (lsb << 8) | (uuid[12] & 0xff);
    lsb = (lsb << 8) | (uuid[13] & 0xff);
    lsb = (lsb << 8) | (uuid[14] & 0xff);
    lsb = (lsb << 8) | (uuid[15] & 0xff);

    return new UUID(msb, lsb);
  }
  /**
   * Test of a large write on a socket to understand what happens when the write is greater than the
   * combined size of the client send buffer and the server receive buffer and the server side of
   * the socket is either not accepted or already shutdown.
   *
   * @throws IOException
   * @throws InterruptedException
   */
  public void testDirectSockets_largeWrite_NotAccepted() throws IOException, InterruptedException {

    final Random r = new Random();

    // Get a socket addresss for an unused port.
    final InetSocketAddress serverAddr = new InetSocketAddress(getPort(0));

    // First our ServerSocket
    final ServerSocket ss = new ServerSocket();
    try {

      // Size of the server socket receive buffer.
      final int receiveBufferSize = ss.getReceiveBufferSize();

      // Allocate buffer twice as large as the receive buffer.
      final byte[] largeBuffer = new byte[receiveBufferSize * 10];

      if (log.isInfoEnabled()) {
        log.info(
            "receiveBufferSize=" + receiveBufferSize + ", largeBufferSize=" + largeBuffer.length);
      }

      // fill buffer with random data.
      r.nextBytes(largeBuffer);

      // bind the ServerSocket to the specified port.
      ss.bind(serverAddr);

      // Now the first Client SocketChannel
      final SocketChannel cs = SocketChannel.open();
      try {
        /*
         * Note: true if connection made. false if connection in
         * progress.
         */
        final boolean immediate = cs.connect(serverAddr);
        if (!immediate) {
          // Did not connect immediately, so finish connect now.
          if (!cs.finishConnect()) {
            fail("Did not connect.");
          }
        }

        /*
         * Attempt to write data. The server socket is not yet accepted.
         * This should hit a timeout.
         */
        assertTimeout(10L, TimeUnit.SECONDS, new WriteBufferTask(cs, ByteBuffer.wrap(largeBuffer)));

        accept(ss);

      } finally {
        cs.close();
      }

    } finally {

      ss.close();
    }
  }
Example #26
0
  /**
   * Generate a UUID using <a href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122</a> UUID
   * generation of a type 4 or randomly generated UUID.
   *
   * @return the 32 character long UUID string.
   * @throws KeyManagerException
   */
  protected String generateUUID() throws KeyManagerException {
    byte vfour[] = new byte[KEY_LENGTH];

    if (isRandomMode() == SECURE) {
      if (secureRandom == null) {
        try {
          secureRandom = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException e) {
          setRandomMode(!SECURE);
          log.warn("Unable to use SecureRandom", e);
        }
      }

      if (isRandomMode() == SECURE) {
        secureRandom.nextBytes(vfour);
      }
    }

    if (isRandomMode() != SECURE) {
      if (random == null) {
        random = new Random();
      }

      random.nextBytes(vfour);
    }

    vfour[6] &= 0x0F;
    vfour[6] |= (4 << 4);
    vfour[8] &= 0x3F;
    vfour[8] |= 0x80;

    return Hex.encode(vfour);
  }
Example #27
0
    /*
     * drive as much data down our output as possible
     */
    private void writeToOutput() {
      if (outBuffer.position() == 0 && outCount == 0) {
        /*
         * We've finished already
         */
        /*
        System.out.println("Already done.");
        */
        return;
      }
      while (true) {
        byte[] foo = new byte[1];
        while (outBuffer.hasRemaining() && outCount > 0) {
          outGenerator.nextBytes(foo);
          outBuffer.put(foo[0]);
          outCount--;
        }

        outBuffer.flip();
        out.broker(outBuffer, outCount == 0);
        outBuffer.compact();

        /*
         * if our outBuffer still has data
         * or if we're no longer filling the outBuffer with
         * data, return, as whatever we're writing to has
         * had its' fill
         */
        if (!outBuffer.hasRemaining() || outCount == 0) {
          return;
        }
      }
    }
Example #28
0
  /**
   * generateRandomHexString
   *
   * @param length a int.
   * @return a {@link java.lang.String} object.
   */
  protected static String generateRandomHexString(int length) {
    if (length < 0) {
      throw new IllegalArgumentException(
          "length argument is " + length + " and cannot be below zero");
    }

    Random random = new Random();
    /*
    SecureRandom sometimes gets tied up in knots in testing (the test process goes off into lala land and never returns from .nextBytes)
    Slow debugging (with pauses) seems to work most of the time, but manual Thread.sleeps doesn't
    Using Random instead of SecureRandom (which should be fine in this context) works much better.  Go figure

    SecureRandom random = null;
    try {
        random = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
        fail("Could not initialize SecureRandom: " + e);
    }*/

    byte bytes[] = new byte[length];
    random.nextBytes(bytes);

    StringBuffer sb = new StringBuffer();
    for (byte b : bytes) {
      sb.append(String.format("%02x", b));
    }
    return sb.toString();
  }
Example #29
0
  byte[] randomLargeData(int bs) {
    Random r = new Random();
    byte[] ret = new byte[bs];

    r.nextBytes(ret);
    return ret;
  }
  public void testCompressionSnappy() throws Exception {
    paginatedCluster.set(OCluster.ATTRIBUTES.COMPRESSION, OSnappyCompression.NAME);
    paginatedCluster.set(OCluster.ATTRIBUTES.RECORD_GROW_FACTOR, 1);

    byte[] record = new byte[100];
    Random random = new Random();
    random.nextBytes(record);

    OPhysicalPosition physicalPosition = paginatedCluster
        .createRecord(record, OVersionFactory.instance().createVersion(), (byte) 1);

    record = OSnappyCompression.INSTANCE.compress(record);

    OCacheEntry cacheEntry = diskCache.load(1, 1, false);
    OCachePointer pagePointer = cacheEntry.getCachePointer();
    int recordIndex = (int) (physicalPosition.clusterPosition.longValue() & 0xFFFF);

    OClusterPage page = new OClusterPage(pagePointer.getDataPointer(), false, ODurablePage.TrackMode.NONE);

    byte[] storedEntity = page.getRecordBinaryValue(recordIndex, 0, page.getRecordSize(recordIndex));
    byte[] storedRecord = new byte[record.length];
    System.arraycopy(storedEntity, OIntegerSerializer.INT_SIZE + OByteSerializer.BYTE_SIZE, storedRecord, 0, storedRecord.length);

    Assert.assertEquals(storedRecord, record);
    diskCache.release(cacheEntry);
  }