|
| 1 | +package org.bouncycastle.util.utiltest; |
| 2 | + |
| 3 | +import java.util.Random; |
| 4 | + |
| 5 | +import org.bouncycastle.util.Arrays; |
| 6 | +import org.bouncycastle.util.Pack; |
| 7 | + |
| 8 | +import junit.framework.TestCase; |
| 9 | + |
| 10 | +public class PackTest |
| 11 | + extends TestCase |
| 12 | +{ |
| 13 | + private static final byte FILL = (byte)0xA5; |
| 14 | + |
| 15 | + private static final long[] EXTREMES = { 0L, -1L, Long.MIN_VALUE, Long.MAX_VALUE, 0x8080808080808080L, |
| 16 | + 0x0102030405060708L }; |
| 17 | + |
| 18 | + /* |
| 19 | + * Enough calls for C2 to compile the methods under test, so that the merged stores it generates for them are |
| 20 | + * checked as well as the interpreter. |
| 21 | + */ |
| 22 | + private static final int ITERATIONS = 200000; |
| 23 | + |
| 24 | + public void testLongToBigEndian() |
| 25 | + { |
| 26 | + Random random = new Random(1); |
| 27 | + byte[] bs = new byte[24]; |
| 28 | + for (int i = 0; i < ITERATIONS; ++i) |
| 29 | + { |
| 30 | + long n = nextValue(random, i); |
| 31 | + int off = i % 9; |
| 32 | + |
| 33 | + Arrays.fill(bs, FILL); |
| 34 | + Pack.longToBigEndian(n, bs, off); |
| 35 | + checkBytes(n, true, bs, off); |
| 36 | + if (Pack.bigEndianToLong(bs, off) != n) |
| 37 | + { |
| 38 | + fail("round trip of " + Long.toHexString(n)); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + assertTrue(Arrays.areEqual(new byte[]{ 1, 2, 3, 4, 5, 6, 7, 8 }, Pack.longToBigEndian(0x0102030405060708L))); |
| 43 | + } |
| 44 | + |
| 45 | + public void testLongToLittleEndian() |
| 46 | + { |
| 47 | + Random random = new Random(2); |
| 48 | + byte[] bs = new byte[24]; |
| 49 | + for (int i = 0; i < ITERATIONS; ++i) |
| 50 | + { |
| 51 | + long n = nextValue(random, i); |
| 52 | + int off = i % 9; |
| 53 | + |
| 54 | + Arrays.fill(bs, FILL); |
| 55 | + Pack.longToLittleEndian(n, bs, off); |
| 56 | + checkBytes(n, false, bs, off); |
| 57 | + if (Pack.littleEndianToLong(bs, off) != n) |
| 58 | + { |
| 59 | + fail("round trip of " + Long.toHexString(n)); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + assertTrue(Arrays.areEqual(new byte[]{ 8, 7, 6, 5, 4, 3, 2, 1 }, Pack.longToLittleEndian(0x0102030405060708L))); |
| 64 | + } |
| 65 | + |
| 66 | + public void testLongArrays() |
| 67 | + { |
| 68 | + Random random = new Random(3); |
| 69 | + for (int i = 0; i < 2000; ++i) |
| 70 | + { |
| 71 | + long[] ns = new long[1 + random.nextInt(25)]; |
| 72 | + for (int j = 0; j < ns.length; ++j) |
| 73 | + { |
| 74 | + ns[j] = nextValue(random, i + j); |
| 75 | + } |
| 76 | + int nsOff = random.nextInt(ns.length); |
| 77 | + int nsLen = random.nextInt(ns.length - nsOff + 1); |
| 78 | + int bsOff = random.nextInt(9); |
| 79 | + int bsLen = bsOff + 8 * nsLen + random.nextInt(9); |
| 80 | + |
| 81 | + byte[] be = new byte[bsLen]; |
| 82 | + Arrays.fill(be, FILL); |
| 83 | + Pack.longToBigEndian(ns, nsOff, nsLen, be, bsOff); |
| 84 | + byte[] le = new byte[bsLen]; |
| 85 | + Arrays.fill(le, FILL); |
| 86 | + Pack.longToLittleEndian(ns, nsOff, nsLen, le, bsOff); |
| 87 | + for (int j = 0; j < nsLen; ++j) |
| 88 | + { |
| 89 | + assertEquals(ns[nsOff + j], Pack.bigEndianToLong(be, bsOff + 8 * j)); |
| 90 | + assertEquals(ns[nsOff + j], Pack.littleEndianToLong(le, bsOff + 8 * j)); |
| 91 | + } |
| 92 | + checkUntouched(be, 0, bsOff); |
| 93 | + checkUntouched(be, bsOff + 8 * nsLen, bsLen); |
| 94 | + checkUntouched(le, 0, bsOff); |
| 95 | + checkUntouched(le, bsOff + 8 * nsLen, bsLen); |
| 96 | + |
| 97 | + long[] part = Arrays.copyOfRange(ns, nsOff, nsOff + nsLen); |
| 98 | + assertTrue(Arrays.areEqual(Arrays.copyOfRange(be, bsOff, bsOff + 8 * nsLen), Pack.longToBigEndian(part))); |
| 99 | + assertTrue(Arrays.areEqual(Arrays.copyOfRange(le, bsOff, bsOff + 8 * nsLen), Pack.longToLittleEndian(part))); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public void testOutOfBounds() |
| 104 | + { |
| 105 | + for (int len = 0; len < 8; ++len) |
| 106 | + { |
| 107 | + try |
| 108 | + { |
| 109 | + Pack.longToBigEndian(-1L, new byte[len], 0); |
| 110 | + fail("no exception for " + len + " bytes"); |
| 111 | + } |
| 112 | + catch (ArrayIndexOutOfBoundsException e) |
| 113 | + { |
| 114 | + // expected |
| 115 | + } |
| 116 | + try |
| 117 | + { |
| 118 | + Pack.longToLittleEndian(-1L, new byte[len], 0); |
| 119 | + fail("no exception for " + len + " bytes"); |
| 120 | + } |
| 121 | + catch (ArrayIndexOutOfBoundsException e) |
| 122 | + { |
| 123 | + // expected |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Check the 8 bytes at off against the definition of the encoding, one byte at a time, and that the bytes around |
| 130 | + * them are untouched. |
| 131 | + */ |
| 132 | + private static void checkBytes(long n, boolean bigEndian, byte[] bs, int off) |
| 133 | + { |
| 134 | + for (int i = 0; i < 8; ++i) |
| 135 | + { |
| 136 | + int shift = bigEndian ? 56 - 8 * i : 8 * i; |
| 137 | + if (bs[off + i] != (byte)(n >>> shift)) |
| 138 | + { |
| 139 | + fail("byte " + i + " of " + Long.toHexString(n) + (bigEndian ? " big" : " little") + " endian"); |
| 140 | + } |
| 141 | + } |
| 142 | + checkUntouched(bs, 0, off); |
| 143 | + checkUntouched(bs, off + 8, bs.length); |
| 144 | + } |
| 145 | + |
| 146 | + private static void checkUntouched(byte[] bs, int from, int to) |
| 147 | + { |
| 148 | + for (int i = from; i < to; ++i) |
| 149 | + { |
| 150 | + if (bs[i] != FILL) |
| 151 | + { |
| 152 | + fail("byte " + i + " overwritten"); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Random values, values with a single byte set or clear at each position in turn, and extremes. |
| 159 | + */ |
| 160 | + private static long nextValue(Random random, int i) |
| 161 | + { |
| 162 | + long oneByte = (long)(1 + random.nextInt(255)) << (8 * ((i >> 2) & 7)); |
| 163 | + switch (i & 3) |
| 164 | + { |
| 165 | + case 0: |
| 166 | + return random.nextLong(); |
| 167 | + case 1: |
| 168 | + return oneByte; |
| 169 | + case 2: |
| 170 | + return ~oneByte; |
| 171 | + default: |
| 172 | + return EXTREMES[(i >> 2) % EXTREMES.length]; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments