/** Tests {@link ContentInfo#getInstance(InputStream)} method. */
  public void testInit() throws Asn1FormatException, IOException {
    // Make sure illegal arguments are handled correctly
    try {
      ContentInfo.getInstance(null);
      fail("null accepted as content info bytes");
    } catch (IllegalArgumentException e) {
      Log.debug("[DBG] (OK) " + e.getMessage());
    }

    try {
      InputStream in = new ByteArrayInputStream(CONTENT_INFO);
      in.skip(1);
      ContentInfo.getInstance(in);
      fail("rubbish accepted as content info bytes");
    } catch (Asn1FormatException e) {
      Log.debug("[DBG] (OK) " + e.getMessage());
    }

    // Build content info from pre-defined bytes
    InputStream in = new ByteArrayInputStream(CONTENT_INFO);
    ContentInfo.getInstance(in);

    // Build content info using valid components
    DERTaggedObject content = getDerTagged(true, 0, SIGNED_DATA);
    in = getDerStream(CONTENT_TYPE, content);
    ContentInfo.getInstance(in);
  }
  /** Tests property getters. */
  public void testGetParams() throws Asn1FormatException, IOException {
    InputStream in = new ByteArrayInputStream(CONTENT_INFO);
    ContentInfo contentInfo = ContentInfo.getInstance(in);

    assertEquals(ContentInfo.CONTENT_TYPE, contentInfo.getContentType());

    ASN1Object signedData =
        new ASN1InputStream(contentInfo.getContent().getDerEncoded()).readObject();
    assertTrue(
        Arrays.equals(
            signedData.getEncoded(ASN1Encoding.DER), contentInfo.getContent().getDerEncoded()));
  }
  /** Tests {@link ContentInfo#isExtended()} and {@link ContentInfo#extend(CertToken)} methods. */
  public void testExtend() throws Asn1FormatException, IOException {
    InputStream in = new ByteArrayInputStream(CONTENT_INFO);
    ContentInfo contentInfo = ContentInfo.getInstance(in);

    // Make sure created content info is NOT extended
    assertFalse(contentInfo.isExtended());

    // Make sure illegal arguments are handled correctly
    try {
      contentInfo.extend(null);
      fail("null accepted as cert token");
    } catch (IllegalArgumentException e) {
      Log.debug("[DBG] (OK) " + e.getMessage());
    }

    // Create extended content info
    in = new ByteArrayInputStream(CERT_TOKEN);
    CertToken certToken = CertToken.getInstance(in);
    ContentInfo extendedContentInfo = contentInfo.extend(certToken);

    // Make sure created content info is extended;
    // also make sure initial content info is NOT extended
    assertFalse(contentInfo.isExtended());
    assertTrue(extendedContentInfo.isExtended());
  }
  /** Tests {@link ContentInfo#getInstance(InputStream)} method with various content values. */
  public void testInitContent() throws IOException {
    // Make sure empty content value is NOT accepted
    try {
      InputStream in = getDerStream(CONTENT_TYPE, null);
      ContentInfo.getInstance(in);
      fail("null accepted as content");
    } catch (Asn1FormatException e) {
      Log.debug("[DBG] (OK) " + e.getMessage());
    }

    // Make sure invalid content value is NOT accepted
    try {
      // Using `contentInfo` instead of `signedData`
      DERTaggedObject content = getDerTagged(true, 0, CONTENT_INFO);
      InputStream in = getDerStream(CONTENT_TYPE, content);
      ContentInfo.getInstance(in);
      fail("rubbish accepted as content");
    } catch (Asn1FormatException e) {
      Log.debug("[DBG] (OK) " + e.getMessage());
    }

    // Make sure implicitly tagged content is NOT accepted
    try {
      DERTaggedObject content = getDerTagged(false, 0, SIGNED_DATA);
      InputStream in = getDerStream(CONTENT_TYPE, content);
      ContentInfo.getInstance(in);
      // TODO: re-enable check when we upgrade the BC libraries
      // http://www.bouncycastle.org/jira/browse/BJA-259
      // fail("implicitly tagged content accepted");
    } catch (Asn1FormatException e) {
      Log.debug("[DBG] (OK) " + e.getMessage());
    }

    // Make sure content with invalid tag is NOT accepted
    try {
      DERTaggedObject content = getDerTagged(true, 1, SIGNED_DATA);
      InputStream in = getDerStream(CONTENT_TYPE, content);
      ContentInfo.getInstance(in);
      // TODO: re-enable check when we upgrade the BC libraries
      // http://www.bouncycastle.org/jira/browse/BJA-259
      // fail("content tagged with [1] accepted");
    } catch (Asn1FormatException e) {
      Log.debug("[DBG] (OK) " + e.getMessage());
    }
  }
  /** Tests {@link ContentInfo#getInstance(InputStream)} method with various content types. */
  public void testInitContentType() throws IOException {
    DERTaggedObject content = getDerTagged(true, 0, SIGNED_DATA);

    // Make sure empty content type is NOT accepted
    try {
      InputStream in = getDerStream(null, content);
      ContentInfo.getInstance(in);
      fail("null accepted as content type");
    } catch (Asn1FormatException e) {
      Log.debug("[DBG] (OK) " + e.getMessage());
    }

    // Make sure invalid content type is NOT accepted
    try {
      InputStream in = getDerStream("1.2.840.113549.1.7.3", content);
      ContentInfo.getInstance(in);
      fail("1.2.840.113549.1.7.3 accepted as content type");
    } catch (Asn1FormatException e) {
      Log.debug("[DBG] (OK) " + e.getMessage());
    }
  }
 /** Tests {@link ContentInfo#getDerEncoded()} method. */
 public void testGetDerEncoded() throws Asn1FormatException, IOException {
   InputStream in = new ByteArrayInputStream(CONTENT_INFO);
   ContentInfo contentInfo = ContentInfo.getInstance(in);
   assertTrue(Arrays.equals(CONTENT_INFO, contentInfo.getDerEncoded()));
 }