File: TestOfPR27372.java

package info (click to toggle)
mauve 20120103-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 28,504 kB
  • sloc: java: 250,155; sh: 2,834; xml: 208; makefile: 66
file content (176 lines) | stat: -rw-r--r-- 6,219 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* TestOfPR27372.java
   Copyright (C) 2006 Free Software Foundation, Inc.
This file is part of Mauve.

Mauve is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

Mauve is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with Mauve; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

*/

// Tags: JDK1.2

package gnu.testlet.java.math.BigInteger;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Arrays;

/**
 * Regression test for PR Classpath/27372
 */
public class TestOfPR27372
    implements Testlet
{
  /** A control value. */
  private static final byte[] BYTES = { 0x12, 0x34, 0x56, 0x78 };
  /** Set to <code>true</code> if BigInteger is using the GNU MP native code. */
  private boolean usingNativeImpl = Arrays.equals(BYTES,
                                                  new BigInteger(BYTES).toByteArray());

  public void test(TestHarness harness)
  {
    harness.checkPoint("TestOfPR27372");
    SecureRandom prng1, prng2;
    byte[] ba, bb;
    try
      {
        prng1 = SecureRandom.getInstance("SHA1PRNG");
        prng1.setSeed(98243647L);
        prng2 = SecureRandom.getInstance("SHA1PRNG");
        prng2.setSeed(98243647L);
        // from here on the two PRNGs are in sync.  they should consume as
        // little bytes as possible, and continue to be in sync
        for (int numBytes = 1; numBytes < 10; numBytes++)
          {
            ba = new BigInteger(8 * numBytes, prng1).toByteArray();
            bb = new byte[numBytes];
            prng2.nextBytes(bb);
            harness.check(areEqual(ba, bb),
                          "BigInteger(int, Random) SHOULD consume as little "
                          + "bytes as possible from Random (and SecureRandom): "
                          + numBytes);
          }
      }
    catch (Exception x)
      {
        harness.debug(x);
        harness.fail("TestOfPR27372: " + x);
      }
  }

  /**
   * In both cases --the pure Java implementation, and the native one based on
   * the GNU MP library-- a BigInteger's toByteArray(), can produce an extra
   * 0x00 byte as the most significant byte.  This method ensures that there
   * is no more than just one zero-byte at the high end, and then channels the
   * call to the appropriate are-equal method depending on the type of the
   * underlying implementation of the MPI.
   * 
   * @param a the result of a {@link BigInteger#toByteArray()} of an instance
   *          constructed with {@link BigInteger#BigInteger(int, Random)}.
   * @param b a non-null byte array filled with randomly generated values.
   * @return <code>true</code> if the two byte arrays contain the same values
   *         taking into consideration how our BigInteger constructs its
   *         internal data structures.
   */
  private boolean areEqual(byte[] a, byte[] b)
  {
    int offset = 0;
    switch (a.length - b.length)
    {
      case 0:
        break;
      case 1:
        if (a[0] != 0)
          return false;
        offset = 1;
        break;
      default:
        return false;
    }
    if (usingNativeImpl)
      return areEqualNativeBI(a, offset, b);
    return areEqualJavaBI(a, offset, b);
  }

  /**
   * Special byte array comparison method used to compare the result of
   * <code>byte[] a = new BigInteger(numBits, random).toByteArray()</code> with
   * a byte array filled with randomly generated values.
   * <p>
   * This method takes into consideration how an array of bytes is used to
   * fill the (pure java) <code>BigInteger</code>'s internal data structure. As
   * an example, here is what the two byte arrays may look like from the
   * outside:
   * 
   * <pre>
   *   a = 009ECB38BFD4C6
   *   b = CB9EC6D4BF38
   * </pre>
   * 
   * @param a the result of a {@link BigInteger#toByteArray()} of an instance
   *          constructed with {@link BigInteger#BigInteger(int, Random)}.
   * @param <code>0</code> or <code>1</code> depending on the value of the
   *          leftmost byte (at index #0) of <code>a</code>.
   * @param b a non-null byte array filled with randomly generated values.
   * @return <code>true</code> if the two byte arrays contain the same values
   *         taking into consideration how our BigInteger constructs its
   *         internal data structures.
   */
  private boolean areEqualJavaBI(byte[] a, int offset, byte[] b)
  {
    for (int i = b.length - 1, j, k; i >= 0; )
      {
        j = i - 3;
        if (j < 0)
          j = 0;
        for (k = 0; k < 4; k++)
          {
            if (a[offset + j++] != b[i--])
              return false;
            if (i < 0)
              break;
          }
      }
    return true;
  }

  /**
   * Straight equality check, byte-for-byte, of the two designated arrays. The
   * first starting from either <code>0</code> or <code>1</code>, while the
   * second always starting from <code>0</code>. This is used with GMP-based
   * MPIs.
   * 
   * @param a the result of a {@link BigInteger#toByteArray()} of an instance
   *          constructed with {@link BigInteger#BigInteger(int, Random)}.
   * @param <code>0</code> or <code>1</code> depending on the value of the
   *          leftmost byte (at index #0) of <code>a</code>.
   * @param b a non-null byte array filled with randomly generated values.
   * @return <code>true</code> if the two byte arrays contain the same values
   *         taking into consideration how our BigInteger constructs its
   *         internal data structures.
   */
  private boolean areEqualNativeBI(byte[] a, int offset, byte[] b)
  {
    for (int i = 0; i < b.length; i++)
      if (a[offset + i] != b[i])
        return false;
    return true;
    
  }
}