Search in sources :

Example 1 with NullOutputStream

use of org.apache.commons.io.output.NullOutputStream in project hudson-2.x by hudson.

the class FilePathTest method testArchiveBug4039.

public void testArchiveBug4039() throws Exception {
    File tmp = Util.createTempDir();
    try {
        FilePath d = new FilePath(french, tmp.getPath());
        d.child("test").touch(0);
        d.zip(new NullOutputStream());
        d.zip(new NullOutputStream(), "**/*");
    } finally {
        Util.deleteRecursive(tmp);
    }
}
Also used : File(java.io.File) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 2 with NullOutputStream

use of org.apache.commons.io.output.NullOutputStream in project hudson-2.x by hudson.

the class UpdateSite method verifySignature.

/**
     * Verifies the signature in the update center data file.
     */
private boolean verifySignature(JSONObject o) throws GeneralSecurityException, IOException {
    JSONObject signature = o.getJSONObject("signature");
    if (signature.isNullObject()) {
        LOGGER.severe("No signature block found");
        return false;
    }
    o.remove("signature");
    List<X509Certificate> certs = new ArrayList<X509Certificate>();
    {
        // load and verify certificates
        CertificateFactory cf = CertificateFactory.getInstance("X509");
        for (Object cert : o.getJSONArray("certificates")) {
            X509Certificate c = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(Base64.decode(cert.toString().toCharArray())));
            c.checkValidity();
            certs.add(c);
        }
        // all default root CAs in JVM are trusted, plus certs bundled in Hudson
        Set<TrustAnchor> anchors = CertificateUtil.getDefaultRootCAs();
        ServletContext context = Hudson.getInstance().servletContext;
        for (String cert : (Set<String>) context.getResourcePaths("/WEB-INF/update-center-rootCAs")) {
            // skip text files that are meant to be documentation
            if (cert.endsWith(".txt"))
                continue;
            anchors.add(new TrustAnchor((X509Certificate) cf.generateCertificate(context.getResourceAsStream(cert)), null));
        }
        CertificateUtil.validatePath(certs);
    }
    // this is for computing a digest to check sanity
    MessageDigest sha1 = MessageDigest.getInstance("SHA1");
    DigestOutputStream dos = new DigestOutputStream(new NullOutputStream(), sha1);
    // this is for computing a signature
    Signature sig = Signature.getInstance("SHA1withRSA");
    sig.initVerify(certs.get(0));
    SignatureOutputStream sos = new SignatureOutputStream(sig);
    JSONCanonicalUtils.write(o, new OutputStreamWriter(new TeeOutputStream(dos, sos), "UTF-8"));
    // did the digest match? this is not a part of the signature validation, but if we have a bug in the c14n
    // (which is more likely than someone tampering with update center), we can tell
    String computedDigest = new String(Base64.encode(sha1.digest()));
    String providedDigest = signature.getString("digest");
    if (!computedDigest.equalsIgnoreCase(providedDigest)) {
        LOGGER.severe("Digest mismatch: " + computedDigest + " vs " + providedDigest);
        return false;
    }
    if (!sig.verify(Base64.decode(signature.getString("signature").toCharArray()))) {
        LOGGER.severe("Signature in the update center doesn't match with the certificate");
        return false;
    }
    return true;
}
Also used : TeeOutputStream(org.apache.commons.io.output.TeeOutputStream) Set(java.util.Set) ArrayList(java.util.ArrayList) TrustAnchor(java.security.cert.TrustAnchor) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) JSONObject(net.sf.json.JSONObject) SignatureOutputStream(org.jvnet.hudson.crypto.SignatureOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestOutputStream(java.security.DigestOutputStream) Signature(java.security.Signature) ServletContext(javax.servlet.ServletContext) JSONObject(net.sf.json.JSONObject) OutputStreamWriter(java.io.OutputStreamWriter) MessageDigest(java.security.MessageDigest) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 3 with NullOutputStream

use of org.apache.commons.io.output.NullOutputStream in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class ContentAssemblerDefault method contentHashFor.

protected Collection<EncodedHash> contentHashFor(InputStream stream) throws IOException {
    final HashAssembler hashAssembler = getContentHashAssembler();
    hashAssembler.initialize();
    IOStreams.copy(stream, new NullOutputStream(), BUFFER_SIZE, hashAssembler);
    return hashAssembler.get();
}
Also used : HashAssembler(com.opentext.ia.sdk.support.io.HashAssembler) NullOutputStream(org.apache.commons.io.output.NullOutputStream)

Example 4 with NullOutputStream

use of org.apache.commons.io.output.NullOutputStream in project jackrabbit-oak by apache.

the class StatsCollectingStreamsTest method downloadCallback.

@Test
public void downloadCallback() throws Exception {
    NullInputStream in = new NullInputStream(1042);
    TestCollector stats = new TestCollector();
    InputStream wrappedStream = StatsCollectingStreams.wrap(stats, "foo", in);
    assertEquals(0, stats.callbackCount);
    //Copy the content to get size
    CountingOutputStream cos = new CountingOutputStream(new NullOutputStream());
    IOUtils.copy(wrappedStream, cos);
    assertEquals(1042, cos.getCount());
    //Stream not closed so no callback
    assertEquals(0, stats.callbackCount);
    wrappedStream.close();
    assertEquals(1042, stats.size);
    assertEquals(1, stats.downloadCompletedCount);
}
Also used : CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) NullInputStream(org.apache.commons.io.input.NullInputStream) InputStream(java.io.InputStream) NullInputStream(org.apache.commons.io.input.NullInputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) Test(org.junit.Test)

Example 5 with NullOutputStream

use of org.apache.commons.io.output.NullOutputStream in project jackrabbit-oak by apache.

the class AbstractBlobStoreTest method downloadCallback.

@Test
public void downloadCallback() throws Exception {
    assumeTrue(supportsStatsCollection());
    TestCollector collector = new TestCollector();
    setupCollector(collector);
    int size = 10 * 1024;
    String id = store.writeBlob(randomStream(42, size));
    store.clearCache();
    collector.reset();
    InputStream is = store.getInputStream(id);
    CountingOutputStream cos = new CountingOutputStream(new NullOutputStream());
    IOUtils.copy(is, cos);
    is.close();
    assertEquals(size, cos.getCount());
    //For chunked storage the actual stored size is greater than the file size
    assertCollectedSize(collector.size, size);
    assertEquals(1, collector.downloadCount);
}
Also used : CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) Test(org.junit.Test)

Aggregations

NullOutputStream (org.apache.commons.io.output.NullOutputStream)30 InputStream (java.io.InputStream)7 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 OutputStream (java.io.OutputStream)5 DigestOutputStream (java.security.DigestOutputStream)5 MessageDigest (java.security.MessageDigest)5 FileInputStream (java.io.FileInputStream)3 PrintStream (java.io.PrintStream)3 DataHandler (javax.activation.DataHandler)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 PipedInputStream (java.io.PipedInputStream)2 ArrayList (java.util.ArrayList)2 Node (javax.jcr.Node)2 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)2 OMFactory (org.apache.axiom.om.OMFactory)2 ExceptionInputStream (org.apache.axiom.testutils.io.ExceptionInputStream)2