diff options
author | Sergio Giro <sgiro@google.com> | 2016-10-27 21:58:31 +0100 |
---|---|---|
committer | Sergio Giro <sgiro@google.com> | 2016-10-28 13:19:29 +0100 |
commit | 0310e4db77a3eb2f0ff6a98af37f6ebd262014d3 (patch) | |
tree | 929a8231d23ec66533d4d27a1f3afcae3df45032 /support | |
parent | 6d115cc32bfaa30c29fb57a2bfc2ec8f6ceb6e43 (diff) |
sun.security.x509: porting rev/04cda5b7a3c1
Changes to KeyUsageExtension, NetscapeCerTypeExtension and
ReasonFlags as to check as to improve checks of array bounds.
sun.security.provider.certpath.DistributionPointFetcher has a
similar change concerning array bounds, where the opportunity
to improve probably was spotted by the fact that it uses
ReasonFlags.
Since in the classes in sun.security.x509 the logic for toString
changed slightly, tests are added that passed both before and
after the change, as to check that the outcome is the same.
Bug: 29631070
Test: run cts -m CtsLibcoreTestCases
Change-Id: I2a2c10c59509063e648f238f82ac71b4f513cd71
Diffstat (limited to 'support')
-rw-r--r-- | support/src/test/java/libcore/sun/security/x509/Utils.java | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/support/src/test/java/libcore/sun/security/x509/Utils.java b/support/src/test/java/libcore/sun/security/x509/Utils.java new file mode 100644 index 0000000000..412a06d1a1 --- /dev/null +++ b/support/src/test/java/libcore/sun/security/x509/Utils.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * 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 libcore.sun.security.x509; + +import junit.framework.Assert; + +import java.util.function.Function; + +public class Utils extends Assert { + /** + * Many classes in this package can be created using a bit array, and the toString method + * depends only on the bit array. The logic for toString was changed in rev/04cda5b7a3c1. + * The expected result is the same before and after the change. + * @param parts The different parts of the result + * @param objectCreator Function to create a new instance of the class tested. + * @param prefix prefix in all toString results + * @param suffix suffix in all toString results + */ + static void test_toString_bitArrayBasedClass( + String[] parts, + Function<byte[], Object> objectCreator, + String prefix, + String suffix) { + testWithEachSinglePart(parts, objectCreator, prefix, suffix); + testWithAllParts(parts, objectCreator, prefix, suffix); + testWithNoParts(parts, objectCreator, prefix, suffix); + testWithEveryOtherPart(parts, objectCreator, prefix, suffix); + } + + private static void testWithEachSinglePart( + String[] parts, Function<byte[], Object> objectCreator, String prefix, String suffix) { + int bitCounter = 0, byteCounter = 1; + for (int i = 0; i < parts.length; i++) { + byte[] ba = new byte[byteCounter]; + ba[byteCounter - 1] = (byte) (1 << (7 - bitCounter)); + bitCounter++; + if (bitCounter == 8) { + bitCounter = 0; + byteCounter++; + } + Object o = objectCreator.apply(ba); + assertEquals(prefix + parts[i] + suffix, o.toString()); + } + } + + private static void testWithAllParts( + String[] parts, Function<byte[], Object> objectCreator, String prefix, String suffix) { + int bitsInAByte = 8; + int allOnesLength = (parts.length + bitsInAByte - 1) / bitsInAByte; + byte[] allOnes = new byte[allOnesLength]; + + for (int i = 0; i < allOnes.length; i++) { + allOnes[i] = -1; + } + assertEquals(prefix + String.join("", parts) + + suffix, objectCreator.apply(allOnes).toString()); + } + + private static void testWithNoParts( + String[] parts, Function<byte[], Object> objectCreator, String prefix, String suffix) { + // Test with empty array + assertEquals(prefix + suffix, objectCreator.apply(new byte[0]).toString()); + + // Test with array will all zeros + assertEquals(prefix + suffix, objectCreator.apply(new byte[parts.length]).toString()); + } + + private static void testWithEveryOtherPart( + String[] parts, Function<byte[], Object> objectCreator, String prefix, String suffix) { + int bitsInAByte = 8; + byte[] ba = new byte[(parts.length + bitsInAByte - 1) / bitsInAByte]; + String expectedResult = new String(); + for (int i = 0; i < parts.length; i += 2) { + ba[i / bitsInAByte] = (byte) 170; // Binary 10101010 + expectedResult += parts[i]; + } + assertEquals(prefix + expectedResult + suffix, objectCreator.apply(ba).toString()); + } +} |