Skip to content

Commit 9da3a43

Browse files
committed
Add tests for DigestAlgorithms and EncryptionAlgorithms in sign module
DEVSIX-5840
1 parent 24d931f commit 9da3a43

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.signatures;
44+
45+
import com.itextpdf.test.ExtendedITextTest;
46+
import com.itextpdf.test.annotations.type.UnitTest;
47+
48+
import java.io.ByteArrayInputStream;
49+
import java.io.IOException;
50+
import java.io.InputStream;
51+
import java.security.GeneralSecurityException;
52+
import org.junit.Assert;
53+
import org.junit.Test;
54+
import org.junit.experimental.categories.Category;
55+
56+
@Category(UnitTest.class)
57+
public class DigestAlgorithmsManualTest extends ExtendedITextTest {
58+
59+
@Test
60+
public void digestSHA1SunPKCS11Test() throws GeneralSecurityException, IOException {
61+
InputStream data = new ByteArrayInputStream(new byte[] {13, 16, 20, 0, 10});
62+
byte[] hash = DigestAlgorithms.digest(data, DigestAlgorithms.SHA1, "SunPKCS11");
63+
byte[] expected =
64+
new byte[] {15, 20, 1, 9, -106, 49, -37, -65, -45, -63, 53, -70, 76, -71, 102, -68, 78, -51, -100, 50};
65+
Assert.assertArrayEquals(expected, hash);
66+
}
67+
68+
@Test
69+
public void digestSHA256SUNTest() throws GeneralSecurityException, IOException {
70+
InputStream data = new ByteArrayInputStream(new byte[] {13, 16, 20, 0, 10});
71+
byte[] hash = DigestAlgorithms.digest(data, DigestAlgorithms.SHA256, "SUN");
72+
byte[] expected =
73+
new byte[] {19, -84, -84, -45, -36, 121, -15, -18, -89, 97, -17, 51, 81,
74+
119, -42, -59, -31, 121, -87, -82, -45, 119, 61, 92, 110, -99, 105, 4, 97, 12, 127, -62};
75+
Assert.assertArrayEquals(expected, hash);
76+
}
77+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.signatures;
24+
25+
import com.itextpdf.test.ExtendedITextTest;
26+
import com.itextpdf.test.annotations.type.UnitTest;
27+
28+
import org.junit.Assert;
29+
import org.junit.Test;
30+
import org.junit.experimental.categories.Category;
31+
32+
@Category(UnitTest.class)
33+
public class EncryptionAlgorithmsManualTest extends ExtendedITextTest {
34+
35+
@Test
36+
public void getAlgorithmNullTest() {
37+
Assert.assertNull(EncryptionAlgorithms.getAlgorithm(null));
38+
}
39+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.signatures;
24+
25+
import com.itextpdf.test.ExtendedITextTest;
26+
import com.itextpdf.test.annotations.type.UnitTest;
27+
28+
import org.junit.Assert;
29+
import org.junit.Test;
30+
import org.junit.experimental.categories.Category;
31+
32+
@Category(UnitTest.class)
33+
public class EncryptionAlgorithmsTest extends ExtendedITextTest {
34+
35+
@Test
36+
public void getAlgorithmRSATest() {
37+
Assert.assertEquals("RSA", EncryptionAlgorithms.getAlgorithm("1.2.840.113549.1.1.1"));
38+
}
39+
40+
@Test
41+
public void getAlgorithmECDSATest() {
42+
Assert.assertEquals("ECDSA", EncryptionAlgorithms.getAlgorithm("1.2.840.10045.2.1"));
43+
}
44+
45+
@Test
46+
public void getAlgorithmEmptyTest() {
47+
Assert.assertEquals("", EncryptionAlgorithms.getAlgorithm(""));
48+
}
49+
50+
@Test
51+
public void getAlgorithmEmptySpaceTest() {
52+
Assert.assertEquals(" ", EncryptionAlgorithms.getAlgorithm(" "));
53+
}
54+
55+
@Test
56+
public void getAlgorithmUndefinedTest() {
57+
Assert.assertEquals("undefined", EncryptionAlgorithms.getAlgorithm("undefined"));
58+
}
59+
}

0 commit comments

Comments
 (0)