gt:         if (!DEFSupported) {
context: /*
   *  Licensed to the Apache Software Foundation (ASF) under one or more
   *  contributor license agreements.  See the NOTICE file distributed with
   *  this work for additional information regarding copyright ownership.
   *  The ASF licenses this file to You 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.
   */
  
  /**
   * @author Vera Y. Petrashkova
   */
  
  package org.apache.harmony.crypto.tests.javax.crypto;
  
  import java.math.BigInteger;
  import java.security.InvalidAlgorithmParameterException;
  import java.security.InvalidKeyException;
  import java.security.NoSuchAlgorithmException;
  import java.security.NoSuchProviderException;
  import java.security.PrivateKey;
  import java.security.Provider;
  import java.security.PublicKey;
  import java.security.SecureRandom;
  import java.security.spec.AlgorithmParameterSpec;
  import java.security.spec.DSAParameterSpec;
  
  import javax.crypto.KeyAgreement;
  import javax.crypto.KeyAgreementSpi;
  import javax.crypto.interfaces.DHPrivateKey;
  import javax.crypto.spec.DHParameterSpec;
  
  import org.apache.harmony.crypto.tests.support.MyKeyAgreementSpi;
  import org.apache.harmony.security.tests.support.SpiEngUtils;
  import org.apache.harmony.security.tests.support.TestKeyPair;
  
  import junit.framework.TestCase;
  
  
  /**
   * Tests for KeyAgreement constructor and methods
   */
  
  public class KeyAgreementTest extends TestCase {
  
      public static final String srvKeyAgreement = "KeyAgreement";
  
      private static String defaultAlgorithm = "DH";
  
      private static String defaultProviderName = null;
  
      private static Provider defaultProvider = null;
  
      private static boolean DEFSupported = false;
  
      private static final String NotSupportMsg = "There is no suitable provider for KeyAgreement";
  
      private static final String[] invalidValues = SpiEngUtils.invalidValues;
  
      private static String[] validValues = { "DH", "dH",
              "Dh", "dh" };
  
      private static PrivateKey privKey = null;
  
      private static PublicKey publKey = null;
  
      private static boolean initKeys = false;
  
      static {
          defaultProvider = SpiEngUtils.isSupport(defaultAlgorithm,
                  srvKeyAgreement);
          DEFSupported = (defaultProvider != null);
          defaultProviderName = (DEFSupported ? defaultProvider.getName() : null);
      }
  
      private void createKeys() throws Exception {
          if (!initKeys) {
              TestKeyPair tkp = new TestKeyPair(defaultAlgorithm);
              privKey = tkp.getPrivate();
              publKey = tkp.getPublic();
              initKeys = true;
          }
  
      }
  
      private KeyAgreement[] createKAs() throws Exception {
          if (!DEFSupported) {
              fail(NotSupportMsg);
          }
  
          KeyAgreement[] ka = new KeyAgreement[3];
          ka[0] = KeyAgreement.getInstance(defaultAlgorithm);
          ka[1] = KeyAgreement.getInstance(defaultAlgorithm, defaultProvider);
          ka[2] = KeyAgreement.getInstance(defaultAlgorithm,
                  defaultProviderName);
          return ka;
      }
  
      public static String getDefAlg() {
          return defaultAlgorithm;
      }
  
      /**
       * Test for <code> getInstance(String algorithm) </code> method Assertions:
       * throws NullPointerException when algorithm is null throws
       * NoSuchAlgorithmException when algorithm isnot available
       */
      public void testGetInstanceString01() throws NoSuchAlgorithmException {
          try {
              KeyAgreement.getInstance(null);
              fail("NullPointerException or NoSuchAlgorithmException should be thrown if algorithm is null");
          } catch (NullPointerException e) {
          } catch (NoSuchAlgorithmException e) {
          }
          for (int i = 0; i < invalidValues.length; i++) {
              try {
                  KeyAgreement.getInstance(invalidValues[i]);
                  fail("NoSuchAlgorithmException must be thrown");
              } catch (NoSuchAlgorithmException e) {
              }
          }
      }
  
      /**
       * Test for <code> getInstance(String algorithm) </code> method Assertions:
       * returns KeyAgreement object
       */
      public void testGetInstanceString02() throws NoSuchAlgorithmException {
          if (!DEFSupported) {
              fail(NotSupportMsg);
              return;
          }
          KeyAgreement keyA;
          for (int i = 0; i < validValues.length; i++) {
              keyA = KeyAgreement.getInstance(validValues[i]);
              assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
                      validValues[i]);
          }
      }
  
      /**
       * Test for <code> getInstance(String algorithm, String provider)</code>
       * method Assertions: throws NullPointerException when algorithm is null
       * throws NoSuchAlgorithmException when algorithm is not available
       */
      public void testGetInstanceStringString01()
              throws NoSuchAlgorithmException, IllegalArgumentException,
              NoSuchProviderException {
          if (!DEFSupported) {
              fail(NotSupportMsg);
              return;
          }
          try {
              KeyAgreement.getInstance(null, defaultProviderName);
              fail("NullPointerException or NoSuchAlgorithmException should be thrown if algorithm is null");
          } catch (NullPointerException e) {
          } catch (NoSuchAlgorithmException e) {
          }
          for (int i = 0; i < invalidValues.length; i++) {
              try {
                  KeyAgreement.getInstance(invalidValues[i], defaultProviderName);
                  fail("NoSuchAlgorithmException must be thrown");
              } catch (NoSuchAlgorithmException e) {
              }
          }
      }
  
      /**
       * Test for <code> getInstance(String algorithm, String provider)</code>
       * method Assertions: throws IllegalArgumentException when provider is null
       * or empty throws NoSuchProviderException when provider has not be
       * configured
       */
      public void testGetInstanceStringString02()
              throws IllegalArgumentException, NoSuchAlgorithmException,
              NoSuchProviderException {
          if (!DEFSupported) {
              fail(NotSupportMsg);
              return;
          }
          String provider = null;
          for (int i = 0; i < validValues.length; i++) {
              try {
                  KeyAgreement.getInstance(validValues[i], provider);
                  fail("IllegalArgumentException must be thrown when provider is null");
              } catch (IllegalArgumentException e) {
              }
              try {
                  KeyAgreement.getInstance(validValues[i], "");
                  fail("IllegalArgumentException must be thrown when provider is empty");
              } catch (IllegalArgumentException e) {
              }
              for (int j = 1; j < invalidValues.length; j++) {
                  try {
                      KeyAgreement.getInstance(validValues[i], invalidValues[j]);
                      fail("NoSuchProviderException must be thrown (algorithm: "
                              .concat(validValues[i]).concat(" provider: ")
                              .concat(invalidValues[j]).concat(")"));
                  } catch (NoSuchProviderException e) {
                  }
              }
          }
      }
  
      /**
       * Test for <code> getInstance(String algorithm, String provider)</code>
       * method Assertions: returns KeyAgreement object
       */
      public void testGetInstanceStringString03()
              throws IllegalArgumentException, NoSuchAlgorithmException,
              NoSuchProviderException {
          if (!DEFSupported) {
              fail(NotSupportMsg);
              return;
          }
          KeyAgreement keyA;
          for (int i = 0; i < validValues.length; i++) {
              keyA = KeyAgreement
                      .getInstance(validValues[i], defaultProviderName);
              assertEquals("Incorrect algorithm", keyA.getAlgorithm(),
                      validValues[i]);
              assertEquals("Incorrect provider", keyA.getProvider().getName(),
                      defaultProviderName);
          }
      }
  
      /**
       * Test for <code> getInstance(String algorithm, Provider provider)</code>
       * method Assertions: throws NullPointerException when algorithm is null
       * throws NoSuchAlgorithmException when algorithm isnot available
       */
      public void testGetInstanceStringProvider01()
              throws NoSuchAlgorithmException, IllegalArgumentException {
  