LCOV - code coverage report
Current view: top level - gpg - BouncyCastleUtil.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 9 9 100.0 %
Date: 2022-11-19 15:00:39 Functions: 2 2 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2015 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.gpg;
      16             : 
      17             : import java.lang.reflect.Constructor;
      18             : import java.lang.reflect.InvocationTargetException;
      19             : import java.security.Security;
      20             : import org.bouncycastle.jce.provider.BouncyCastleProvider;
      21             : import org.bouncycastle.openpgp.PGPPublicKey;
      22             : 
      23             : /** Utility methods for Bouncy Castle. */
      24             : public class BouncyCastleUtil {
      25             :   /**
      26             :    * Check for Bouncy Castle PGP support.
      27             :    *
      28             :    * <p>As a side effect, adds {@link BouncyCastleProvider} as a security provider.
      29             :    *
      30             :    * @return whether Bouncy Castle PGP support is enabled.
      31             :    */
      32             :   public static boolean havePGP() {
      33             :     try {
      34         143 :       Class.forName(PGPPublicKey.class.getName());
      35         143 :       addBouncyCastleProvider();
      36         143 :       return true;
      37           9 :     } catch (NoClassDefFoundError
      38             :         | ClassNotFoundException
      39             :         | SecurityException
      40             :         | NoSuchMethodException
      41             :         | InstantiationException
      42             :         | IllegalAccessException
      43             :         | InvocationTargetException
      44             :         | ClassCastException noBouncyCastle) {
      45           9 :       return false;
      46             :     }
      47             :   }
      48             : 
      49             :   private static void addBouncyCastleProvider()
      50             :       throws ClassNotFoundException, SecurityException, NoSuchMethodException,
      51             :           InstantiationException, IllegalAccessException, InvocationTargetException {
      52         143 :     Class<?> clazz = Class.forName(BouncyCastleProvider.class.getName());
      53         143 :     Constructor<?> constructor = clazz.getConstructor();
      54         143 :     Security.addProvider((java.security.Provider) constructor.newInstance());
      55         143 :   }
      56             : 
      57             :   private BouncyCastleUtil() {}
      58             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750