1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.internal.digest.factory

File AbstractDigestFactoryTest.java

 

Code metrics

6
36
9
1
133
95
13
0.36
4
9
1.44

Classes

Class Line # Actions
AbstractDigestFactoryTest 37 36 0% 13 2
0.960784396.1%
 

Contributing tests

This file is covered by 36 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.crypto.internal.digest.factory;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.IOException;
24    import java.io.InputStream;
25    import java.io.OutputStream;
26   
27    import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
28    import org.junit.BeforeClass;
29    import org.junit.Test;
30    import org.xwiki.crypto.Digest;
31    import org.xwiki.crypto.DigestFactory;
32   
33    import static org.hamcrest.CoreMatchers.equalTo;
34    import static org.hamcrest.CoreMatchers.not;
35    import static org.junit.Assert.assertThat;
36   
 
37    public abstract class AbstractDigestFactoryTest extends AbstractDigestFactoryTestConstants
38    {
39    private static final byte[] BYTES = TEXT.getBytes();
40   
41    protected DigestFactory factory;
42   
43    protected static Digest digest;
44   
45    protected String digestAlgo;
46    protected AlgorithmIdentifier digestAlgId;
47    protected int digestSize;
48    protected byte[] digestResult;
49   
 
50  6 toggle @BeforeClass
51    public static void cleanUpCaches() {
52  6 digest = null;
53    }
54   
 
55  60 toggle Digest getDigestInstance()
56    {
57  60 if (digest == null) {
58  6 digest = factory.getInstance();
59    }
60  60 return digest;
61    }
62   
 
63  6 toggle @Test
64    public void testDigestFactoryProperties() throws Exception
65    {
66  6 assertThat(factory.getDigestAlgorithmName(), equalTo(digestAlgo));
67  6 assertThat(factory.getDigestSize(), equalTo(digestSize));
68    }
69   
 
70  6 toggle @Test
71    public void testDigestProperties() throws Exception
72    {
73  6 assertThat(getDigestInstance().getAlgorithmName(), equalTo(digestAlgo));
74  6 assertThat(getDigestInstance().getDigestSize(), equalTo(digestSize));
75  6 assertThat(getDigestInstance().getEncoded(), equalTo(digestAlgId.getEncoded()));
76    }
77   
 
78  6 toggle @Test
79    public void testDigestOneShot() throws Exception
80    {
81  6 assertThat(getDigestInstance().digest(BYTES), equalTo(digestResult));
82  6 assertThat(getDigestInstance().digest(BYTES, 10, 20), not(equalTo(digestResult)));
83    }
84   
 
85  6 toggle @Test
86    public void testDigestMultiplePart() throws Exception
87    {
88  6 Digest dig = getDigestInstance();
89  6 byte[] b = BYTES;
90   
91  6 dig.update(b);
92  6 assertThat(dig.digest(), equalTo(digestResult));
93   
94  6 dig.update(b, 0, 13);
95  6 dig.update(b, 13, 23);
96  6 dig.update(b, 36, b.length - 36);
97  6 assertThat(dig.digest(), equalTo(digestResult));
98    }
99   
 
100  6 toggle @Test
101    public void testDigestOutputStream() throws Exception
102    {
103  6 OutputStream os = getDigestInstance().getOutputStream();
104  6 os.write(BYTES);
105  6 assertThat(getDigestInstance().digest(), equalTo(digestResult));
106    }
107   
 
108  6 toggle private int readAll(InputStream is, byte[] out) throws IOException
109    {
110  6 int readLen, len = 0;
111  6 int blen = 17;
112  ? while( blen > 0 && (readLen = is.read(out, len, blen)) > 0 ) {
113  96 len += readLen;
114  96 if (len + blen > out.length) {
115  6 blen = out.length - len;
116    }
117    }
118  6 is.close();
119  6 return len;
120    }
121   
 
122  6 toggle @Test
123    public void testDigestInputStream() throws Exception
124    {
125  6 ByteArrayInputStream bais = new ByteArrayInputStream(BYTES);
126  6 InputStream is = getDigestInstance().getInputStream(bais);
127  6 byte[] out = new byte[BYTES.length];
128   
129  6 assertThat(readAll(is, out), equalTo(BYTES.length));
130  6 assertThat(out, equalTo(BYTES));
131  6 assertThat(getDigestInstance().digest(), equalTo(digestResult));
132    }
133    }