// Path: src/main/java/org/igniterealtime/jbosh/RequestIDSequence.java
// final class RequestIDSequence {
// 
//     /**
//      * Maximum number of bits available for representing request IDs, according
//      * to the XEP-0124 spec.s
//      */
//     private static final int MAX_BITS = 53;
// 
//     /**
//      * Bits devoted to incremented values.
//      */
//     private static final int INCREMENT_BITS = 32;
// 
//     /**
//      * Minimum number of times the initial RID can be incremented before
//      * exceeding the maximum.
//      */
//     private static final long MIN_INCREMENTS = 1L << INCREMENT_BITS;
// 
//     /**
//      * Max initial value. 
//      */
//     private static final long MAX_INITIAL = (1L << MAX_BITS) - MIN_INCREMENTS;
// 
//     /**
//      * Max bits mask.
//      */
//     private static final long MASK = ~(Long.MAX_VALUE << MAX_BITS);
// 
//     /**
//      * Random number generator.
//      */
//     private static final SecureRandom RAND = new SecureRandom();
// 
//     /**
//      * The last reqest ID used, or &lt;= 0 if a new request ID needs to be
//      * generated.
//      */
//     private final AtomicLong nextRequestID;
// 
//     ///////////////////////////////////////////////////////////////////////////
//     // Constructors:
// 
//     /**
//      * Prevent direct construction.
//      */
//     RequestIDSequence() {
//         nextRequestID = new AtomicLong(generateInitialValue());
//     }
// 
//     ///////////////////////////////////////////////////////////////////////////
//     // Public methods:
// 
//     /**
//      * Calculates the next request ID value to use.  This number must be
//      * initialized such that it is unlikely to ever exceed 2 ^ 53, according
//      * to XEP-0124.
//      *
//      * @return next request ID value
//      */
//     public long getNextRID() {
//         return nextRequestID.getAndIncrement();
//     }
// 
//     ///////////////////////////////////////////////////////////////////////////
//     // Private methods:
// 
//     /**
//      * Generates an initial RID value by generating numbers until a number is
//      * found which is smaller than the maximum allowed value and greater
//      * than zero.
//      *
//      * @return random initial value
//      */
//     private long generateInitialValue() {
//         long result;
//         do {
//             result = RAND.nextLong() & MASK;
//         } while (result > MAX_INITIAL);
//         return result;
//     }
// 
// }


import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import org.igniterealtime.jbosh.RequestIDSequence;
import org.junit.Test;
import static org.junit.Assert.*;
/*
 * Copyright 2009 Mike Cumings
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.igniterealtime.jbosh;




public class RequestIDSequenceTest {

    /**
     * The minimum number of RIDs that must be generated before a repeat
     * is observed.  This is an arbitrary, large value and is not explicitly
     * called out by XEP-0124.
     */
    private static final int ITERATIONS = 50000;

    /**
     * Maximum allowed value that we should never exceed.
     */
    private static final long MAX_ALLOWED = 1L << 53;

    /**
     * Number of times which an RID should reasonable be expected to be
     * incremented by before possibly reaching the max value.
     */
    private static final long MAX_INCREMENTS = 1L << 32;

    private static final Logger LOG =
            Logger.getLogger(RequestIDSequenceTest.class.getName());

    /*
     * The session identifier (SID) and initial request identifier (RID) are
     * security-critical and therefore MUST be both unpredictable and
     * nonrepeating.
     */
    @Test(timeout=20000)
    public void checkForIDRepeats() throws Exception {
        long repeats = 0;
        // Run a few thousand iterations and check for repeats
        Set<Long> observed = new HashSet<Long>(ITERATIONS * 2);
        for (int i=0; i<ITERATIONS; i++) {
            RequestIDSequence seq = new RequestIDSequence();
            Long id = Long.valueOf(seq.getNextRID());
            if (!observed.add(id)) {
                LOG.info("Found a repeat on iteration #" + i + ": " + id);
                repeats++;
            }
        }
        LOG.info("Repeated initial RID " + repeats + " time(s) in "
                + ITERATIONS + " iterations");
        if (repeats > 0) {
            fail("Initial RID repeated " + repeats + " time(s) in "
// Path: src/main/java/org/igniterealtime/jbosh/RequestIDSequence.java
// final class RequestIDSequence {
// 
//     /**
//      * Maximum number of bits available for representing request IDs, according
//      * to the XEP-0124 spec.s
//      */
//     private static final int MAX_BITS = 53;
// 
//     /**
//      * Bits devoted to incremented values.
//      */
//     private static final int INCREMENT_BITS = 32;
// 
//     /**
//      * Minimum number of times the initial RID can be incremented before
//      * exceeding the maximum.
//      */
//     private static final long MIN_INCREMENTS = 1L << INCREMENT_BITS;
// 
//     /**
//      * Max initial value. 
//      */
//     private static final long MAX_INITIAL = (1L << MAX_BITS) - MIN_INCREMENTS;
// 
//     /**
//      * Max bits mask.
//      */
//     private static final long MASK = ~(Long.MAX_VALUE << MAX_BITS);
// 
//     /**
//      * Random number generator.
//      */
//     private static final SecureRandom RAND = new SecureRandom();
// 
//     /**
//      * The last reqest ID used, or &lt;= 0 if a new request ID needs to be
//      * generated.
//      */
//     private final AtomicLong nextRequestID;
// 
//     ///////////////////////////////////////////////////////////////////////////
//     // Constructors:
// 
//     /**
//      * Prevent direct construction.
//      */
//     RequestIDSequence() {
//         nextRequestID = new AtomicLong(generateInitialValue());
//     }
// 
//     ///////////////////////////////////////////////////////////////////////////
//     // Public methods:
// 
//     /**
//      * Calculates the next request ID value to use.  This number must be
//      * initialized such that it is unlikely to ever exceed 2 ^ 53, according
//      * to XEP-0124.
//      *
//      * @return next request ID value
//      */
//     public long getNextRID() {
//         return nextRequestID.getAndIncrement();
//     }
// 
//     ///////////////////////////////////////////////////////////////////////////
//     // Private methods:
// 
//     /**
//      * Generates an initial RID value by generating numbers until a number is
//      * found which is smaller than the maximum allowed value and greater
//      * than zero.
//      *
//      * @return random initial value
//      */
//     private long generateInitialValue() {
//         long result;
//         do {
//             result = RAND.nextLong() & MASK;
//         } while (result > MAX_INITIAL);
//         return result;
//     }
// 
// }


// Path: src/test/java/org/igniterealtime/jbosh/RequestIDSequenceTest.java
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import org.igniterealtime.jbosh.RequestIDSequence;
import org.junit.Test;
import static org.junit.Assert.*;

/*
 * Copyright 2009 Mike Cumings
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.igniterealtime.jbosh;




public class RequestIDSequenceTest {

    /**
     * The minimum number of RIDs that must be generated before a repeat
     * is observed.  This is an arbitrary, large value and is not explicitly
     * called out by XEP-0124.
     */
    private static final int ITERATIONS = 50000;

    /**
     * Maximum allowed value that we should never exceed.
     */
    private static final long MAX_ALLOWED = 1L << 53;

    /**
     * Number of times which an RID should reasonable be expected to be
     * incremented by before possibly reaching the max value.
     */
    private static final long MAX_INCREMENTS = 1L << 32;

    private static final Logger LOG =
            Logger.getLogger(RequestIDSequenceTest.class.getName());

    /*
     * The session identifier (SID) and initial request identifier (RID) are
     * security-critical and therefore MUST be both unpredictable and
     * nonrepeating.
     */
    @Test(timeout=20000)
    public void checkForIDRepeats() throws Exception {
        long repeats = 0;
        // Run a few thousand iterations and check for repeats
        Set<Long> observed = new HashSet<Long>(ITERATIONS * 2);
        for (int i=0; i<ITERATIONS; i++) {
            RequestIDSequence seq = new RequestIDSequence();
            Long id = Long.valueOf(seq.getNextRID());
            if (!observed.add(id)) {
                LOG.info("Found a repeat on iteration #" + i + ": " + id);
                repeats++;
            }
        }
        LOG.info("Repeated initial RID " + repeats + " time(s) in "
                + ITERATIONS + " iterations");
        if (repeats > 0) {
            fail("Initial RID repeated " + repeats + " time(s) in "
                    + ITERATIONS + " iterations");