Class AlgorithmResolutionTest


  • public class AlgorithmResolutionTest
    extends java.lang.Object
    This test class can be used to validate that the algorithm lookup of a low-level cryptographic implementation allocates nothing.

    Every LLAPI that resolves an algorithm name returns an algorithm id. That id is a constant of the low-level implementation, and Managed Code never closes it. An implementation that allocates a native resource on each lookup therefore drains the native memory of an application that calls getInstance() repeatedly.

    Managed Code cannot read the native memory, so each test drives the public API in a loop instead. It captures a reference result on the first resolution, forces a garbage collection at regular intervals so that everything the implementation may legitimately reclaim is reclaimed, and checks that the reference result still holds. An implementation that leaks runs out of native memory during the loop.

    The default loop is short, because this test class shares the testsuite budget with every other test. Raise the com.microej.crypto.test.algorithmresolution.iterations property to hunt a slow leak.

    • Constructor Detail

      • AlgorithmResolutionTest

        public AlgorithmResolutionTest()
    • Method Detail

      • testMessageDigestResolution

        public void testMessageDigestResolution()
                                         throws java.lang.Exception
        Checks the message digest lookup. It resolves SHA-256 and SHA-512 in turn, so a lookup that returns the id of the other algorithm also fails.
        Throws:
        java.lang.Exception - if a resolution or a digest fails
      • testMacResolution

        public void testMacResolution()
                               throws java.lang.Exception
        Checks the MAC lookup. It resolves HmacSHA1 and HmacSHA256 in turn, so a lookup that returns the id of the other algorithm also fails.
        Throws:
        java.lang.Exception - if a resolution or a MAC computation fails
      • testCipherResolution

        public void testCipherResolution()
                                  throws java.lang.Exception
        Checks the cipher lookup. It resolves AES/CBC/NoPadding and AES/ECB/NoPadding in turn, so a lookup that returns the id of the other transformation also fails.
        Throws:
        java.lang.Exception - if a resolution or an encryption fails
      • testSecretKeyFactoryResolution

        public void testSecretKeyFactoryResolution()
                                            throws java.lang.Exception
        Checks the secret key factory lookup. It resolves PBKDF2WithHmacSHA1 and PBKDF2WithHmacSHA256 in turn, so a lookup that returns the id of the other algorithm also fails.
        Throws:
        java.lang.Exception - if a resolution or a key derivation fails
      • testKeyFactoryResolution

        public void testKeyFactoryResolution()
                                      throws java.lang.Exception
        Checks the key factory lookup. It decodes the same EC public key at each resolution, so the encoding it returns must never change.
        Throws:
        java.lang.Exception - if a resolution or a key decoding fails
      • testKeyGeneratorResolution

        public void testKeyGeneratorResolution()
                                        throws java.lang.Exception
        Checks the key generator lookup. It generates an AES key at each resolution, and the key material is random, so only the algorithm and the length of the key can be compared.
        Throws:
        java.lang.Exception - if a resolution or a key generation fails
      • testSignatureResolution

        public void testSignatureResolution()
                                     throws java.lang.Exception
        Checks the signature lookup. Like the key pair generator, the loop only resolves the algorithm: a verification needs a certificate and a pre-computed signature, and SignatureTest already checks that each algorithm id maps to the right algorithm.
        Throws:
        java.lang.Exception - if a resolution fails
      • testKeyPairGeneratorResolution

        public void testKeyPairGeneratorResolution()
                                            throws java.lang.Exception
        Checks the key pair generator lookup, which is the one lookup this class cannot follow with an operation. A key generation costs far too much to run in a loop, so the loop only resolves the algorithm. That is enough for the contract under test: an implementation that allocates on each lookup runs out of native memory here.
        Throws:
        java.lang.Exception - if a resolution fails