PageRenderTime 72ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 1ms

/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/EncryptedPrivateKeyInfoTest.java

https://github.com/mkedwards/platform__libcore
Java | 1232 lines | 674 code | 105 blank | 453 comment | 24 complexity | 2db02a78fe1d3e6c328ef1b1442ff48c MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @author Vladimir N. Molotkov
  19. * @version $Revision$
  20. */
  21. package org.apache.harmony.crypto.tests.javax.crypto;
  22. import dalvik.annotation.TestTargetClass;
  23. import dalvik.annotation.TestLevel;
  24. import dalvik.annotation.TestTargetNew;
  25. import java.io.IOException;
  26. import java.security.AlgorithmParameters;
  27. import java.security.AlgorithmParametersSpi;
  28. import java.security.InvalidAlgorithmParameterException;
  29. import java.security.InvalidKeyException;
  30. import java.security.Key;
  31. import java.security.KeyPair;
  32. import java.security.KeyPairGenerator;
  33. import java.security.NoSuchAlgorithmException;
  34. import java.security.NoSuchProviderException;
  35. import java.security.Provider;
  36. import java.security.Security;
  37. import java.security.spec.InvalidKeySpecException;
  38. import java.security.spec.InvalidParameterSpecException;
  39. import java.security.spec.PKCS8EncodedKeySpec;
  40. import java.util.Arrays;
  41. import javax.crypto.BadPaddingException;
  42. import javax.crypto.Cipher;
  43. import javax.crypto.EncryptedPrivateKeyInfo;
  44. import javax.crypto.IllegalBlockSizeException;
  45. import javax.crypto.KeyGenerator;
  46. import javax.crypto.NoSuchPaddingException;
  47. import javax.crypto.SecretKeyFactory;
  48. import javax.crypto.spec.PBEKeySpec;
  49. import javax.crypto.spec.PBEParameterSpec;
  50. import org.apache.harmony.crypto.tests.support.EncryptedPrivateKeyInfoData;
  51. import junit.framework.TestCase;
  52. @TestTargetClass(EncryptedPrivateKeyInfo.class)
  53. /**
  54. * Test for EncryptedPrivateKeyInfo class.
  55. *
  56. * All binary data for this test were generated using BEA JRockit j2sdk1.4.2_04
  57. * (http://www.bea.com) with security providers list extended by Bouncy Castle's
  58. * one (http://www.bouncycastle.org)
  59. */
  60. public class EncryptedPrivateKeyInfoTest extends TestCase {
  61. private static final Provider[] provider = Security.getProviders();
  62. /**
  63. * Algorithm names/transformations used in roundtrip tests of
  64. * getKeySpec(...) methods
  65. */
  66. private static final String[][] algName = {
  67. // AES
  68. { "AES", null},
  69. // {"AES", "AES/ECB/PKCS5Padding"},
  70. // {"AES", "AES/CBC/PKCS5Padding"},
  71. // {"AES", "AES/OFB/PKCS5Padding"},
  72. // {"AES", "AES/CFB/PKCS5Padding"},
  73. // {"2.16.840.1.101.3.4.1.1", null},
  74. // {"2.16.840.1.101.3.4.1.2", null},
  75. // {"2.16.840.1.101.3.4.1.3", null},
  76. // {"2.16.840.1.101.3.4.1.4", null},
  77. // {"2.16.840.1.101.3.4.1.5", null},
  78. // {"2.16.840.1.101.3.4.1.21", null},
  79. // {"2.16.840.1.101.3.4.1.22", null},
  80. // {"2.16.840.1.101.3.4.1.23", null},
  81. // {"2.16.840.1.101.3.4.1.24", null},
  82. // {"2.16.840.1.101.3.4.1.25", null},
  83. // {"2.16.840.1.101.3.4.1.41", null},
  84. // {"2.16.840.1.101.3.4.1.42", null},
  85. // {"2.16.840.1.101.3.4.1.43", null},
  86. // {"2.16.840.1.101.3.4.1.44", null},
  87. // {"2.16.840.1.101.3.4.1.45", null},
  88. // Blowfish
  89. // NO OIDs for Blowfish defined (?)
  90. { "Blowfish", null },
  91. // {"Blowfish","Blowfish/CBC/PKCS5Padding"},
  92. // {"Blowfish","Blowfish/CFB/PKCS5Padding"},
  93. // {"Blowfish","Blowfish/OFB/PKCS5Padding"},
  94. // {"Blowfish","Blowfish/PCBC/PKCS5Padding"},
  95. // DES: OIW OIDs only
  96. // {iso(1) identified-organization(3) oiw(14) secsig(3)
  97. // algorithms(2) desECB(6)}
  98. // 1.3.14.3.2.6
  99. // 1.3.14.3.2.7
  100. // 1.3.14.3.2.8
  101. // 1.3.14.3.2.9
  102. { "DES", null },
  103. // {"DES", "DES/CBC/PKCS5Padding"},
  104. // {"DES","DES/CFB/PKCS5Padding"},
  105. // {"DES","DES/OFB/PKCS5Padding"},
  106. // {"DES","DES/PCBC/PKCS5Padding"},
  107. // DESede (=TripleDes)
  108. //{iso(1) identified-organization(3) oiw(14) secsig(3)
  109. // algorithms(2) desEDE(17)}
  110. // 1.3.14.3.2.17
  111. // {"DESede",null},
  112. // {"DESede","DESede/CBC/PKCS5Padding"},
  113. // {"DESede","DESede/CFB/PKCS5Padding"},
  114. // {"DESede","DESede/OFB/PKCS5Padding"},
  115. // {"DESede","DESede/PCBC/PKCS5Padding"},
  116. { "TripleDES", null },
  117. // {"TripleDES","TripleDES/CBC/PKCS5Padding"},
  118. // {"TripleDES","TripleDES/CFB/PKCS5Padding"},
  119. // {"TripleDES","TripleDES/OFB/PKCS5Padding"},
  120. // {"TripleDES","TripleDES/PCBC/PKCS5Padding"},
  121. // PBEWith<digest>And<encryption>
  122. { "PBEWithMD5AndTripleDES", null },
  123. // {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-5(5)
  124. // pbeWithMD5AndDES-CBC(3)}
  125. { "PBEWithMD5AndDES", "PBEWithMD5AndDES/CBC/PKCS5Padding", "PBEWithMD5AndDES"},
  126. { "PBEWithMD5AndDES", null, "PBEWithMD5AndDES"}, { "PBEWithHmacSHA1AndDESede", null },
  127. // more oids:
  128. // {iso(1) member-body(2) us(840) nortelnetworks(113533) entrust(7)
  129. // algorithms(66) pbeWithMD5AndCAST5-CBC(12)}
  130. //
  131. // also named pbeWithSHAAnd128BitRC4, pbeWithSHA1And128BitRC4:
  132. // {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-12(12)
  133. // pkcs-12-PbeIds(1) pkcs-12-OfflineTransportMode(1)}
  134. //
  135. // {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-12(12)
  136. // pkcs-12-PbeIds(1)} +
  137. // pbeWithSHAAnd40BitRC4(2) pbeWithSHAAnd3-KeyTripleDES-CBC(3)
  138. // pbeWithSHAAnd2-KeyTripleDES-CBC(4) pbeWithSHAAnd128BitRC2-CBC(5)
  139. // pbeWithSHAAnd40BitRC2-CBC(6)
  140. // DiffieHellman
  141. { "DiffieHellman", null }, // 1.2.840.10046.2.1
  142. // {"DH",null}, // 1.2.840.10046.2.1
  143. // {"1.2.840.113549.1.3.1", null},
  144. { "DSA", null }, // 1.2.840.10040.4.1
  145. { "RC2", null },
  146. { "RC4", null },
  147. { "RC5", null },
  148. // {"1.2.840.113549.1.12.1.1",null},
  149. // {"1.2.840.113549.1.12.1.2",null},
  150. //{ "1.2.840.113549.1.12.1.3", null, "PBEWithSHA1AndDESede"},
  151. //{ "PBEWithSHA1AndDESede", null, "PBEWithSHA1AndDESede"},
  152. // {"1.2.840.113549.1.12.1.4",null},
  153. // {"1.2.840.113549.1.12.1.5",null},
  154. // {"1.2.840.113549.1.12.1.6",null},
  155. // {"ELGAMAL/PKCS1", "ELGAMAL/ECB/PKCS1PADDING"},
  156. // {"ELGAMAL/PKCS1","ELGAMAL/NONE/PKCS1PADDING"},
  157. // {"PBEWITHSHAAND3-KEYTRIPLEDES-CBC", null},
  158. // {"PBEWITHSHA1ANDDESEDE", null},
  159. // {"PBEWithSHAAnd3KeyTripleDES",null},
  160. // {"PBEWITHSHAAND3-KEYTRIPLEDES-CBC",null},
  161. //
  162. // {"RC5-32",null},
  163. //
  164. // {"RSA/1", "RSA/1/PKCS1PADDING"},
  165. // {"RSA/2", "RSA/2/PKCS1PADDING"},
  166. // {"RSA/ISO9796-1", "RSA/ECB/ISO9796-1PADDING"},
  167. // {"RSA", "RSA/ECB/NOPADDING"},
  168. // {"RSA/OAEP", "RSA/ECB/OAEPPADDING"},
  169. // {"RSA/PKCS1", "RSA/ECB/PKCS1PADDING"},
  170. // {"RSA/ISO9796-1", "RSA/NONE/ISO9796-1PADDING"},
  171. // {"RSA", "RSA/NONE/NOPADDING"},
  172. // {"RSA/OAEP", "RSA/NONE/OAEPPADDING"},
  173. // {"RSA/PKCS1", "RSA/NONE/PKCS1PADDING"},
  174. // {"RSA",null}, // 1.2.840.113549.1.1.1
  175. // {"1.2.840.113549.1.1.1", null},
  176. };
  177. @Override protected void setUp() throws Exception {
  178. super.setUp();
  179. }
  180. @TestTargetNew(
  181. level = TestLevel.COMPLETE,
  182. notes = "",
  183. method = "getAlgName",
  184. args = {}
  185. )
  186. public void test_getAlgName () {
  187. boolean performed = false;
  188. for (int i = 0; i < algName.length; i++) {
  189. try {
  190. // generate test data
  191. TestDataGenerator g = new TestDataGenerator(algName[i][0],
  192. algName[i][1], privateKeyInfoDamaged, null);
  193. // create test object
  194. EncryptedPrivateKeyInfo epki;
  195. if (g.ap() == null) {
  196. epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
  197. } else {
  198. epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
  199. }
  200. // call methods under test
  201. if (algName[i].length == 3) {
  202. assertEquals(algName[i][2], epki.getAlgName());
  203. }
  204. performed = true;
  205. } catch (TestDataGenerator.AllowedFailure allowedFailure) {
  206. } catch (NoSuchAlgorithmException allowedFailure) {
  207. }
  208. }
  209. assertTrue("Test not performed", performed);
  210. }
  211. /**
  212. * Test #1 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
  213. * <br>
  214. * Assertion: creates <code>EncryptedPrivateKeyInfo</code> instance <br>
  215. * Test preconditions: valid parameters passed <br>
  216. * Expected: must pass without any exceptions
  217. *
  218. * @throws IOException
  219. * @throws NoSuchAlgorithmException
  220. */
  221. @TestTargetNew(
  222. level = TestLevel.PARTIAL_COMPLETE,
  223. notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(byte[]) constructor.",
  224. method = "EncryptedPrivateKeyInfo",
  225. args = {byte[].class}
  226. )
  227. public final void testEncryptedPrivateKeyInfobyteArray1() throws Exception {
  228. new EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfoData
  229. .getValidEncryptedPrivateKeyInfoEncoding("DH"));
  230. }
  231. /**
  232. * Test #2 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
  233. * <br>
  234. * Assertion: <code>NullPointerException</code> if encoding is
  235. * <code>null</code><br>
  236. * Test preconditions: <code>null</code> passed as a parameter <br>
  237. * Expected: <code>NullPointerException</code>
  238. *
  239. * @throws IOException
  240. */
  241. @TestTargetNew(
  242. level = TestLevel.PARTIAL_COMPLETE,
  243. notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(byte[]) constructor.",
  244. method = "EncryptedPrivateKeyInfo",
  245. args = {byte[].class}
  246. )
  247. public final void testEncryptedPrivateKeyInfobyteArray2()
  248. throws IOException {
  249. try {
  250. new EncryptedPrivateKeyInfo(null);
  251. fail(getName() + ": NullPointerException has not been thrown");
  252. } catch (NullPointerException ok) {
  253. }
  254. }
  255. /**
  256. * Test #3 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
  257. * <br>
  258. * Assertion: <code>IOException</code> if encoding is wrong <br>
  259. * Test preconditions: wrong encoding passed as a parameter <br>
  260. * Expected: <code>IOException</code>
  261. */
  262. @TestTargetNew(
  263. level = TestLevel.PARTIAL_COMPLETE,
  264. notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(byte[]) constructor.",
  265. method = "EncryptedPrivateKeyInfo",
  266. args = {byte[].class}
  267. )
  268. public final void testEncryptedPrivateKeyInfobyteArray3() {
  269. try {
  270. new EncryptedPrivateKeyInfo(new byte[0]);
  271. fail(getName() + ": IOException has not been thrown");
  272. } catch (IOException ok) {
  273. }
  274. }
  275. /**
  276. * Test #4 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
  277. * <br>
  278. * Assertion: <code>IOException</code> if encoding is wrong <br>
  279. * Test preconditions: wrong encoding passed as a parameter <br>
  280. * Expected: <code>IOException</code>
  281. */
  282. @TestTargetNew(
  283. level = TestLevel.PARTIAL_COMPLETE,
  284. notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(byte[]) constructor.",
  285. method = "EncryptedPrivateKeyInfo",
  286. args = {byte[].class}
  287. )
  288. public final void testEncryptedPrivateKeyInfobyteArray4() {
  289. try {
  290. new EncryptedPrivateKeyInfo(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9,
  291. 10 });
  292. fail(getName() + ": IOException has not been thrown");
  293. } catch (IOException ok) {
  294. }
  295. }
  296. /**
  297. * Test #5 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
  298. * <br>
  299. * Assertion: <code>IOException</code> if encoding is wrong <br>
  300. * Test preconditions: wrong encoding passed as a parameter <br>
  301. * Expected: <code>IOException</code>
  302. */
  303. @TestTargetNew(
  304. level = TestLevel.PARTIAL_COMPLETE,
  305. notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(byte[]) constructor.",
  306. method = "EncryptedPrivateKeyInfo",
  307. args = {byte[].class}
  308. )
  309. public final void testEncryptedPrivateKeyInfobyteArray5() throws Exception {
  310. byte[] enc = null;
  311. try {
  312. // 1: get valid encoding
  313. enc = EncryptedPrivateKeyInfoData
  314. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  315. // ... and corrupt it (set wrong alg OID length)
  316. enc[9] = (byte) 6;
  317. new EncryptedPrivateKeyInfo(enc);
  318. fail(getName() + "(1): IOException has not been thrown");
  319. } catch (IOException ok) {
  320. }
  321. try {
  322. // 2: get valid encoding
  323. enc = EncryptedPrivateKeyInfoData
  324. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  325. // ... and corrupt it (set wrong encrypted data tag)
  326. enc[307] = (byte) 6;
  327. new EncryptedPrivateKeyInfo(enc);
  328. fail(getName() + "(2): IOException has not been thrown");
  329. } catch (IOException ok) {
  330. }
  331. try {
  332. // 3: get valid encoding
  333. enc = EncryptedPrivateKeyInfoData
  334. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  335. // ... and corrupt it (set wrong encrypted data length)
  336. enc[310] = (byte) 1;
  337. new EncryptedPrivateKeyInfo(enc);
  338. fail(getName() + "(3): IOException has not been thrown");
  339. } catch (IOException ok) {
  340. }
  341. try {
  342. // 4: get valid encoding
  343. enc = EncryptedPrivateKeyInfoData
  344. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  345. // ... and corrupt it (set wrong tag for alg params sequence)
  346. enc[17] = (byte) 0x29;
  347. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(enc);
  348. if (epki.getAlgParameters() == null) {
  349. // This kind of encoding corruption can
  350. // be only determined while AlgorithmParameters
  351. // initialization BUT No AlgorithmParameters instance
  352. // available for algName0[i][0].
  353. // So just skip this sub test
  354. } else {
  355. fail(getName() + "(4): IOException has not been thrown");
  356. }
  357. } catch (IOException ok) {
  358. }
  359. try {
  360. // 5: get valid encoding
  361. enc = EncryptedPrivateKeyInfoData
  362. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  363. // ... and corrupt it (set wrong length for alg params sequence)
  364. enc[20] = (byte) 0x1d;
  365. new EncryptedPrivateKeyInfo(enc);
  366. fail(getName() + "(5): IOException has not been thrown");
  367. } catch (IOException ok) {
  368. }
  369. try {
  370. // 6: get valid encoding
  371. enc = EncryptedPrivateKeyInfoData
  372. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  373. // ... and corrupt it (set wrong length for alg params sequence)
  374. enc[20] = (byte) 0x1f;
  375. new EncryptedPrivateKeyInfo(enc);
  376. fail(getName() + "(6): IOException has not been thrown");
  377. } catch (IOException ok) {
  378. }
  379. }
  380. /**
  381. * Test #6 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
  382. * <br>
  383. * Assertion: byte array is copied to prevent subsequent modification <br>
  384. * Test preconditions: valid array passed then modified <br>
  385. * Expected: getEncoded(), invoked after above modification, must return
  386. * array as it was before the modification
  387. *
  388. * @throws IOException
  389. */
  390. @TestTargetNew(
  391. level = TestLevel.PARTIAL_COMPLETE,
  392. notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(byte[]) constructor.",
  393. method = "EncryptedPrivateKeyInfo",
  394. args = {byte[].class}
  395. )
  396. public final void testEncryptedPrivateKeyInfobyteArray6() throws Exception {
  397. byte[] encoded = EncryptedPrivateKeyInfoData
  398. .getValidEncryptedPrivateKeyInfoEncoding("DSA");
  399. byte[] encodedCopy = encoded.clone();
  400. // pass valid array
  401. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(encodedCopy);
  402. // modify array passed
  403. encodedCopy[9] = (byte) 6;
  404. // check that internal state has not been affected
  405. assertTrue(Arrays.equals(encoded, epki.getEncoded()));
  406. }
  407. /**
  408. * Test #1 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
  409. * constructor <br>
  410. * Assertion: creates <code>EncryptedPrivateKeyInfo</code> instance <br>
  411. * Test preconditions: valid parameters passed <br>
  412. * Expected: must pass without any exceptions
  413. */
  414. @TestTargetNew(
  415. level = TestLevel.PARTIAL_COMPLETE,
  416. notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(String, byte[]) constructor.",
  417. method = "EncryptedPrivateKeyInfo",
  418. args = {java.lang.String.class, byte[].class}
  419. )
  420. public final void testEncryptedPrivateKeyInfoStringbyteArray1() {
  421. boolean performed = false;
  422. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  423. try {
  424. new EncryptedPrivateKeyInfo(
  425. EncryptedPrivateKeyInfoData.algName0[i][0],
  426. EncryptedPrivateKeyInfoData.encryptedData);
  427. performed = true;
  428. } catch (NoSuchAlgorithmException allowed) {
  429. }
  430. }
  431. assertTrue("Test not performed", performed);
  432. }
  433. /**
  434. * Test #2 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
  435. * constructor <br>
  436. * Assertion: <code>NoSuchAlgorithmException</code>- if the specified
  437. * algorithm is not supported <br>
  438. * Test preconditions: pass nonexistent algorithm name <br>
  439. * Expected: <code>NoSuchAlgorithmException</code>
  440. */
  441. @TestTargetNew(
  442. level = TestLevel.PARTIAL_COMPLETE,
  443. notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(String, byte[]) constructor.",
  444. method = "EncryptedPrivateKeyInfo",
  445. args = {java.lang.String.class, byte[].class}
  446. )
  447. public final void testEncryptedPrivateKeyInfoStringbyteArray2() {
  448. try {
  449. new EncryptedPrivateKeyInfo("bla-bla",
  450. EncryptedPrivateKeyInfoData.encryptedData);
  451. fail(getName() + ": NoSuchAlgorithmException has not been thrown");
  452. } catch (NoSuchAlgorithmException ok) {
  453. }
  454. try {
  455. new EncryptedPrivateKeyInfo("",
  456. EncryptedPrivateKeyInfoData.encryptedData);
  457. fail(getName() + ": NoSuchAlgorithmException has not been thrown");
  458. } catch (NoSuchAlgorithmException ok) {
  459. }
  460. }
  461. /**
  462. * Test #3 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
  463. * constructor <br>
  464. * Assertion: <code>NullPointerException</code>- if the specified
  465. * algorithm or encrypted data is <code>null</code><br>
  466. * Test preconditions: pass <code>null</code> as algorithm name then as
  467. * encrypted data <br>
  468. * Expected: <code>NullPointerException</code> in both cases
  469. *
  470. * @throws NoSuchAlgorithmException
  471. */
  472. @TestTargetNew(
  473. level = TestLevel.PARTIAL_COMPLETE,
  474. notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(String, byte[]) constructor.",
  475. method = "EncryptedPrivateKeyInfo",
  476. args = {java.lang.String.class, byte[].class}
  477. )
  478. public final void testEncryptedPrivateKeyInfoStringbyteArray3()
  479. throws NoSuchAlgorithmException {
  480. // pass null as name
  481. try {
  482. new EncryptedPrivateKeyInfo((String) null,
  483. EncryptedPrivateKeyInfoData.encryptedData);
  484. fail(getName() + ": NullPointerException has not been thrown");
  485. } catch (NullPointerException ok) {
  486. }
  487. // pass null as encrypted data
  488. try {
  489. new EncryptedPrivateKeyInfo("DSA", null);
  490. fail(getName() + ": NullPointerException has not been thrown");
  491. } catch (NullPointerException ok) {
  492. }
  493. }
  494. /**
  495. * Test #4 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
  496. * constructor <br>
  497. * Assertion: <code>IllegalArgumentException</code>- if encrypted data is
  498. * empty, i.e. 0-length <br>
  499. * Test preconditions: pass empty encrypted data <br>
  500. * Expected: <code>IllegalArgumentException</code>
  501. */
  502. @TestTargetNew(
  503. level = TestLevel.PARTIAL_COMPLETE,
  504. notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(String, byte[]) constructor.",
  505. method = "EncryptedPrivateKeyInfo",
  506. args = {java.lang.String.class, byte[].class}
  507. )
  508. public final void testEncryptedPrivateKeyInfoStringbyteArray4()
  509. throws Exception {
  510. try {
  511. new EncryptedPrivateKeyInfo("DSA", new byte[] {});
  512. fail(getName() + ": IllegalArgumentException has not been thrown");
  513. } catch (IllegalArgumentException ok) {
  514. }
  515. }
  516. /**
  517. * Test #5 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
  518. * constructor <br>
  519. * Assertion: byte array is copied to prevent subsequent modification <br>
  520. * Test preconditions: valid array passed then modified <br>
  521. * Expected: getEncryptedData(), invoked after above modification, must
  522. * return array as it was before the modification
  523. *
  524. * @throws IOException
  525. */
  526. @TestTargetNew(
  527. level = TestLevel.PARTIAL_COMPLETE,
  528. notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(String, byte[]) constructor.",
  529. method = "EncryptedPrivateKeyInfo",
  530. args = {java.lang.String.class, byte[].class}
  531. )
  532. public final void testEncryptedPrivateKeyInfoStringbyteArray5()
  533. throws Exception {
  534. byte[] encryptedDataCopy = EncryptedPrivateKeyInfoData.encryptedData
  535. .clone();
  536. // pass valid array
  537. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo("DSA",
  538. encryptedDataCopy);
  539. // modify array passed
  540. encryptedDataCopy[0] = (byte) 6;
  541. // check that internal state has not been affected
  542. assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.encryptedData,
  543. epki.getEncryptedData()));
  544. }
  545. /**
  546. * @tests javax/crypto/EncryptedPrivateKeyInfo(String, byte[])
  547. * Checks exception order
  548. */
  549. @TestTargetNew(
  550. level = TestLevel.PARTIAL_COMPLETE,
  551. notes = "This is a complete subset of tests for EncryptedPrivateKeyInfo(String, byte[]) constructor.",
  552. method = "EncryptedPrivateKeyInfo",
  553. args = {java.lang.String.class, byte[].class}
  554. )
  555. public final void testEncryptedPrivateKeyInfoStringbyteArray6() {
  556. //Regression for HARMONY-768
  557. try {
  558. new EncryptedPrivateKeyInfo("0", new byte[] {});
  559. fail("NoSuchAlgorithmException expected");
  560. } catch (NoSuchAlgorithmException e) {
  561. //expected
  562. }
  563. }
  564. class Mock_AlgorithmParameters extends AlgorithmParameters {
  565. protected Mock_AlgorithmParameters(AlgorithmParametersSpi paramSpi, Provider provider, String algorithm) {
  566. super(paramSpi, provider, algorithm);
  567. }
  568. }
  569. /**
  570. * Test #1 for
  571. * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
  572. * </code>
  573. * constructor <br>
  574. * Assertion: creates <code>EncryptedPrivateKeyInfo</code> instance <br>
  575. * Test preconditions: valid parameters passed <br>
  576. * Expected: must pass without any exceptions
  577. *
  578. * @throws IOException
  579. * @throws NoSuchAlgorithmException
  580. */
  581. @TestTargetNew(
  582. level = TestLevel.PARTIAL_COMPLETE,
  583. notes = "Functionality checked. NoSuchAlgorithmException should be tested for complete tests subset.",
  584. method = "EncryptedPrivateKeyInfo",
  585. args = {java.security.AlgorithmParameters.class, byte[].class}
  586. )
  587. public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray1()
  588. throws IOException, NoSuchAlgorithmException {
  589. AlgorithmParameters ap = null;
  590. boolean performed = false;
  591. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  592. try {
  593. ap = AlgorithmParameters
  594. .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
  595. // use pregenerated AlgorithmParameters encodings
  596. ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(
  597. EncryptedPrivateKeyInfoData.algName0[i][0]));
  598. new EncryptedPrivateKeyInfo(ap,
  599. EncryptedPrivateKeyInfoData.encryptedData);
  600. performed = true;
  601. } catch (NoSuchAlgorithmException allowedFailure) {
  602. }
  603. }
  604. assertTrue("Test not performed", performed);
  605. ap = new Mock_AlgorithmParameters(null, null, "Wrong alg name");
  606. try {
  607. new EncryptedPrivateKeyInfo(ap,
  608. EncryptedPrivateKeyInfoData.encryptedData);
  609. fail("NoSuchAlgorithmException expected");
  610. } catch (NoSuchAlgorithmException e) {
  611. //expected
  612. }
  613. }
  614. /**
  615. * Test #2 for
  616. * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
  617. * </code>
  618. * constructor <br>
  619. * Assertion: <code>NullPointerException</code>- if the specified
  620. * algorithm parameters or encrypted data is <code>null</code><br>
  621. * Test preconditions: pass <code>null</code> as algorithm parameters then
  622. * as encrypted data <br>
  623. * Expected: <code>NullPointerException</code> in both cases
  624. *
  625. * @throws NoSuchAlgorithmException
  626. * @throws IOException
  627. */
  628. @TestTargetNew(
  629. level = TestLevel.PARTIAL_COMPLETE,
  630. notes = "NullPointerException checked.",
  631. method = "EncryptedPrivateKeyInfo",
  632. args = {java.security.AlgorithmParameters.class, byte[].class}
  633. )
  634. public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray2()
  635. throws NoSuchAlgorithmException, IOException {
  636. // 1: pass null as AlgorithmParameters
  637. try {
  638. new EncryptedPrivateKeyInfo((AlgorithmParameters) null,
  639. EncryptedPrivateKeyInfoData.encryptedData);
  640. fail(getName() + ": NullPointerException has not been thrown");
  641. } catch (NullPointerException ok) {
  642. }
  643. // 2: pass null as encrypted data
  644. try {
  645. AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
  646. // use pregenerated AlgorithmParameters encodings
  647. ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
  648. new EncryptedPrivateKeyInfo(ap, null);
  649. fail(getName() + ": NullPointerException has not been thrown");
  650. } catch (NullPointerException ok) {
  651. }
  652. }
  653. /**
  654. * Test #3 for
  655. * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
  656. * </code>
  657. * constructor <br>
  658. * Assertion: <code>IllegalArgumentException</code>- if encrypted data is
  659. * empty, i.e. 0-length <br>
  660. * Test preconditions: pass empty encrypted data <br>
  661. * Expected: <code>IllegalArgumentException</code>
  662. *
  663. * @throws NoSuchAlgorithmException
  664. * @throws IOException
  665. */
  666. @TestTargetNew(
  667. level = TestLevel.PARTIAL_COMPLETE,
  668. notes = "IllegalArgumentException checked.",
  669. method = "EncryptedPrivateKeyInfo",
  670. args = {java.security.AlgorithmParameters.class, byte[].class}
  671. )
  672. public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray3()
  673. throws Exception {
  674. try {
  675. AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
  676. // use pregenerated AlgorithmParameters encodings
  677. ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
  678. new EncryptedPrivateKeyInfo(ap, new byte[] {});
  679. fail(getName() + ": IllegalArgumentException has not been thrown");
  680. } catch (IllegalArgumentException ok) {
  681. }
  682. }
  683. /**
  684. * Test #4 for
  685. * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
  686. * </code>
  687. * constructor <br>
  688. * Assertion: byte array is copied to prevent subsequent modification <br>
  689. * Test preconditions: valid array passed then modified <br>
  690. * Expected: getEncryptedData(), invoked after above modification, must
  691. * return array as it was before the modification
  692. *
  693. * @throws IOException
  694. */
  695. @TestTargetNew(
  696. level = TestLevel.PARTIAL_COMPLETE,
  697. notes = "Functionality checked.",
  698. method = "EncryptedPrivateKeyInfo",
  699. args = {java.security.AlgorithmParameters.class, byte[].class}
  700. )
  701. public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray4()
  702. throws Exception {
  703. AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
  704. // use pregenerated AlgorithmParameters encodings
  705. ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
  706. byte[] encryptedDataCopy = EncryptedPrivateKeyInfoData.encryptedData.clone();
  707. // pass valid array
  708. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
  709. encryptedDataCopy);
  710. // modify array passed
  711. encryptedDataCopy[0] = (byte) 6;
  712. // check that internal state has not been affected
  713. assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.encryptedData,
  714. epki.getEncryptedData()));
  715. }
  716. /**
  717. * Test #1 for <code>getAlgParameters()</code> method <br>
  718. * Assertion: returns the algorithm parameters <br>
  719. * Test preconditions: test object created using ctor which takes encoded
  720. * form as the only parameter; encoded form passed contains algorithm
  721. * parameters encoding <br>
  722. * Expected: corresponding algorithm parameters must be returned
  723. *
  724. * @throws IOException
  725. */
  726. @TestTargetNew(
  727. level = TestLevel.PARTIAL_COMPLETE,
  728. notes = "This is a complete subset of tests for getAlgParameters method.",
  729. method = "getAlgParameters",
  730. args = {}
  731. )
  732. public final void testGetAlgParameters01() throws IOException {
  733. boolean performed = false;
  734. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  735. try {
  736. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  737. EncryptedPrivateKeyInfoData
  738. .getValidEncryptedPrivateKeyInfoEncoding(
  739. EncryptedPrivateKeyInfoData.algName0[i][0]));
  740. AlgorithmParameters apar = epki.getAlgParameters();
  741. if (apar == null) {
  742. continue;
  743. }
  744. // check that method under test returns
  745. // parameters with the same encoded form
  746. assertTrue(Arrays
  747. .equals(
  748. EncryptedPrivateKeyInfoData
  749. .getParametersEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]),
  750. apar.getEncoded()));
  751. performed = true;
  752. } catch (NoSuchAlgorithmException allowedFailure) {
  753. }
  754. }
  755. assertTrue("Test not performed", performed);
  756. }
  757. @TestTargetNew(
  758. level = TestLevel.PARTIAL_COMPLETE,
  759. notes = "This is a complete subset of tests for getAlgParameters method.",
  760. method = "getAlgParameters",
  761. args = {}
  762. )
  763. public final void testGetAlgParameters01_01() throws Exception {
  764. byte[] validEncodingWithUnknownAlgOID = EncryptedPrivateKeyInfoData
  765. .getValidEncryptedPrivateKeyInfoEncoding("DH");
  766. // correct oid value
  767. validEncodingWithUnknownAlgOID[18] = 0;
  768. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  769. validEncodingWithUnknownAlgOID);
  770. assertNull(epki.getAlgParameters());
  771. }
  772. /**
  773. * Test #2 for <code>getAlgParameters()</code> method <br>
  774. * Assertion: returns the algorithm parameters <br>
  775. * Test preconditions: test object created using ctor which takes encoded
  776. * form as the only parameter; encoded form passed does not contain
  777. * algorithm parameters encoding <br>
  778. * Expected: <code>null</code> must be returned
  779. *
  780. * @throws IOException
  781. */
  782. @TestTargetNew(
  783. level = TestLevel.PARTIAL_COMPLETE,
  784. notes = "This is a complete subset of tests for getAlgParameters method.",
  785. method = "getAlgParameters",
  786. args = {}
  787. )
  788. public final void testGetAlgParameters02() throws IOException {
  789. boolean performed = false;
  790. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  791. try {
  792. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  793. EncryptedPrivateKeyInfoData
  794. .getValidEncryptedPrivateKeyInfoEncoding(
  795. EncryptedPrivateKeyInfoData.algName0[i][0],
  796. false));
  797. // check that method under test returns null
  798. assertNull(epki.getAlgParameters());
  799. performed = true;
  800. } catch (NoSuchAlgorithmException allowedFailure) {
  801. }
  802. }
  803. assertTrue("Test not performed", performed);
  804. }
  805. /**
  806. * Test #3 for <code>getAlgParameters()</code> method <br>
  807. * Assertion: returns the algorithm parameters <br>
  808. * Test #6 for <code>EncryptedPrivateKeyInfo(String, byte[])</code>
  809. * constructor <br>
  810. * Assertion: ...This constructor will use null as the value of the
  811. * algorithm parameters. <br>
  812. * Test preconditions: test object created using ctor which takes algorithm
  813. * name and encrypted data as a parameters <br>
  814. * Expected: <code>null</code> must be returned
  815. *
  816. * @throws IOException
  817. */
  818. @TestTargetNew(
  819. level = TestLevel.PARTIAL_COMPLETE,
  820. notes = "This is a complete subset of tests for getAlgParameters method.",
  821. method = "getAlgParameters",
  822. args = {}
  823. )
  824. public final void testGetAlgParameters03() throws IOException {
  825. boolean performed = false;
  826. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  827. try {
  828. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  829. EncryptedPrivateKeyInfoData.algName0[i][0],
  830. EncryptedPrivateKeyInfoData.encryptedData);
  831. // check that method under test returns null
  832. // for object constructed in such a way
  833. assertNull(epki.getAlgParameters());
  834. performed = true;
  835. } catch (NoSuchAlgorithmException allowedFailure) {
  836. }
  837. }
  838. assertTrue("Test not performed", performed);
  839. }
  840. /**
  841. * Test #4 for <code>getAlgParameters()</code> method <br>
  842. * Assertion: returns the algorithm parameters <br>
  843. * Test preconditions: test object created using ctor which takes
  844. * AlgorithmParameters and encrypted data as a parameters; <br>
  845. * Expected: the same algorithm parameters as ones passed to the ctor must be
  846. * returned
  847. *
  848. * @throws IOException
  849. */
  850. @TestTargetNew(
  851. level = TestLevel.PARTIAL_COMPLETE,
  852. notes = "This is a complete subset of tests for getAlgParameters method.",
  853. method = "getAlgParameters",
  854. args = {}
  855. )
  856. public final void testGetAlgParameters04() throws IOException {
  857. boolean performed = false;
  858. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  859. try {
  860. AlgorithmParameters ap = AlgorithmParameters
  861. .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
  862. // use pregenerated AlgorithmParameters encodings
  863. ap
  864. .init(EncryptedPrivateKeyInfoData
  865. .getParametersEncoding(
  866. EncryptedPrivateKeyInfoData.algName0[i][0]));
  867. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
  868. EncryptedPrivateKeyInfoData.encryptedData);
  869. // check that method under test returns
  870. // the same parameters instance
  871. assertSame(ap, epki.getAlgParameters());
  872. performed = true;
  873. } catch (NoSuchAlgorithmException allowedFailure) {
  874. }
  875. }
  876. assertTrue("Test not performed", performed);
  877. }
  878. /**
  879. * Test #1 for <code>getEncryptedData()</code> method <br>
  880. * Assertion: returns the encrypted data <br>
  881. * Test preconditions: test object created using ctor which takes encoded
  882. * form as the only parameter; encoded form passed contains encrypted data
  883. * <br>
  884. * Expected: the equivalent encrypted data must be returned
  885. *
  886. * @throws IOException
  887. */
  888. @TestTargetNew(
  889. level = TestLevel.PARTIAL_COMPLETE,
  890. notes = "This is a complete subset of tests for getEncryptedData method.",
  891. method = "getEncryptedData",
  892. args = {}
  893. )
  894. public final void testGetEncryptedData01() throws IOException {
  895. boolean performed = false;
  896. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  897. try {
  898. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  899. EncryptedPrivateKeyInfoData
  900. .getValidEncryptedPrivateKeyInfoEncoding(
  901. EncryptedPrivateKeyInfoData.algName0[i][0]));
  902. // check that method under test returns
  903. // valid encrypted data
  904. assertTrue(Arrays.equals(
  905. EncryptedPrivateKeyInfoData.encryptedData, epki
  906. .getEncryptedData()));
  907. performed = true;
  908. } catch (NoSuchAlgorithmException allowedFailure) {
  909. }
  910. }
  911. assertTrue("Test not performed", performed);
  912. }
  913. /**
  914. * Test #2 for <code>getEncryptedData()</code> method <br>
  915. * Assertion: returns the encrypted data <br>
  916. * Test preconditions: test object created using ctor which takes algorithm
  917. * name and encrypted data as a parameters <br>
  918. * Expected: the equivalent encrypted data must be returned
  919. */
  920. @TestTargetNew(
  921. level = TestLevel.PARTIAL_COMPLETE,
  922. notes = "This is a complete subset of tests for getEncryptedData method.",
  923. method = "getEncryptedData",
  924. args = {}
  925. )
  926. public final void testGetEncryptedData02() {
  927. boolean performed = false;
  928. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  929. try {
  930. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  931. EncryptedPrivateKeyInfoData.algName0[i][0],
  932. EncryptedPrivateKeyInfoData.encryptedData);
  933. // check that method under test returns
  934. // valid encrypted data
  935. assertTrue(Arrays.equals(
  936. EncryptedPrivateKeyInfoData.encryptedData, epki
  937. .getEncryptedData()));
  938. performed = true;
  939. } catch (NoSuchAlgorithmException allowedFailure) {
  940. }
  941. }
  942. assertTrue("Test not performed", performed);
  943. }
  944. /**
  945. * Test #3 for <code>getEncryptedData()</code> method <br>
  946. * Assertion: returns the encrypted data <br>
  947. * Test preconditions: test object created using ctor which takes algorithm
  948. * parameters and encrypted data as a parameters <br>
  949. * Expected: the equivalent encrypted data must be returned
  950. *
  951. * @throws IOException
  952. */
  953. @TestTargetNew(
  954. level = TestLevel.PARTIAL_COMPLETE,
  955. notes = "This is a complete subset of tests for getEncryptedData method.",
  956. method = "getEncryptedData",
  957. args = {}
  958. )
  959. public final void testGetEncryptedData03() throws IOException {
  960. boolean performed = false;
  961. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  962. try {
  963. AlgorithmParameters ap = AlgorithmParameters
  964. .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
  965. // use pregenerated AlgorithmParameters encodings
  966. ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(
  967. EncryptedPrivateKeyInfoData.algName0[i][0]));
  968. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
  969. EncryptedPrivateKeyInfoData.encryptedData);
  970. // check that method under test returns
  971. // valid encrypted data
  972. assertTrue(Arrays.equals(
  973. EncryptedPrivateKeyInfoData.encryptedData, epki
  974. .getEncryptedData()));
  975. performed = true;
  976. } catch (NoSuchAlgorithmException allowedFailure) {
  977. }
  978. }
  979. assertTrue("Test not performed", performed);
  980. }
  981. /**
  982. * Test #4 for <code>getEncryptedData()</code> method <br>
  983. * Assertion: returns a new array each time this method is called <br>
  984. * Test preconditions: test object created using ctor which takes algorithm
  985. * name and encrypted data as a parameters <br>
  986. * Expected: refs to encrypted data byte array passed to the ctor and
  987. * returned by the method under test must be different
  988. */
  989. @TestTargetNew(
  990. level = TestLevel.PARTIAL_COMPLETE,
  991. notes = "This is a complete subset of tests for getEncryptedData method.",
  992. method = "getEncryptedData",
  993. args = {}
  994. )
  995. public final void testGetEncryptedData04() {
  996. boolean performed = false;
  997. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  998. try {
  999. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  1000. EncryptedPrivateKeyInfoData.algName0[i][0],
  1001. EncryptedPrivateKeyInfoData.encryptedData);
  1002. // check that method under test returns
  1003. // new array each time
  1004. byte[] ecd1 = epki.getEncryptedData();
  1005. byte[] ecd2 = epki.getEncryptedData();
  1006. assertNotSame(EncryptedPrivateKeyInfoData.encryptedData, ecd1);
  1007. assertNotSame(EncryptedPrivateKeyInfoData.encryptedData, ecd2);
  1008. assertNotSame(ecd1, ecd2);
  1009. performed = true;
  1010. } catch (NoSuchAlgorithmException allowedFailure) {
  1011. }
  1012. }
  1013. assertTrue("Test not performed", performed);
  1014. }
  1015. /**
  1016. * Test #1 for <code>getEncoded()</code> method <br>
  1017. * Assertion: returns the ASN.1 encoding of this object <br>
  1018. * Test preconditions: test object created using ctor which takes encoded
  1019. * form as the only parameter <br>
  1020. * Expected: equivalent encoded form must be returned
  1021. *
  1022. * @throws IOException
  1023. */
  1024. @TestTargetNew(
  1025. level = TestLevel.PARTIAL_COMPLETE,
  1026. notes = "Can not check IOException",
  1027. method = "getEncoded",
  1028. args = {}
  1029. )
  1030. public final void testGetEncoded01() throws IOException {
  1031. boolean performed = false;
  1032. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  1033. try {
  1034. byte[] enc = EncryptedPrivateKeyInfoData
  1035. .getValidEncryptedPrivateKeyInfoEncoding(
  1036. EncryptedPrivateKeyInfoData.algName0[i][0]);
  1037. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(enc);
  1038. // check that method under test returns
  1039. // valid encoded form
  1040. assertTrue(Arrays.equals(enc, epki.getEncoded()));
  1041. performed = true;
  1042. } catch (NoSuchAlgorithmException allowedFailure) {
  1043. }
  1044. }
  1045. assertTrue("Test not performed", performed);
  1046. }
  1047. /**
  1048. * Test #2 for <code>getEncoded()</code> method <br>
  1049. * Assertion: returns the ASN.1 encoding of this object <br>
  1050. * Test preconditions: test object created using ctor which takes algorithm
  1051. * name and encrypted data as a parameters <br>
  1052. * Expected: equivalent encoded form (without alg params) must be returned
  1053. *
  1054. * @throws IOException
  1055. */
  1056. @TestTargetNew(
  1057. level = TestLevel.PARTIAL_COMPLETE,
  1058. notes = "Can not check IOException",
  1059. method = "getEncoded",
  1060. args = {}
  1061. )
  1062. public final void testGetEncoded02() throws IOException {
  1063. boolean performed = false;
  1064. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  1065. try {
  1066. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(
  1067. EncryptedPrivateKeyInfoData.algName0[i][0],
  1068. EncryptedPrivateKeyInfoData.encryptedData);
  1069. // check that method under test returns
  1070. // valid encoded form
  1071. byte[] refEnc = EncryptedPrivateKeyInfoData
  1072. .getValidEncryptedPrivateKeyInfoEncoding(
  1073. EncryptedPrivateKeyInfoData.algName0[i][0],
  1074. false);
  1075. // System.out.println(Array.toString(refEnc, " "));
  1076. byte[] actEnc = epki.getEncoded();
  1077. // System.out.println(Array.toString(actEnc, " "));
  1078. assertTrue(Arrays.equals(refEnc, actEnc));
  1079. performed = true;
  1080. } catch (NoSuchAlgorithmException allowedFailure) {
  1081. }
  1082. }
  1083. assertTrue("Test not performed", performed);
  1084. }
  1085. /**
  1086. * Test #3 for <code>getEncoded()</code> method <br>
  1087. * Assertion: returns the ASN.1 encoding of this object <br>
  1088. * Test preconditions: test object created using ctor which takes algorithm
  1089. * name and encrypted data as a parameters <br>
  1090. * Expected: equivalent encoded form (without alg params) must be returned
  1091. *
  1092. * @throws IOException
  1093. */
  1094. @TestTargetNew(
  1095. level = TestLevel.PARTIAL_COMPLETE,
  1096. notes = "Can not check IOException",
  1097. method = "getEncoded",
  1098. args = {}
  1099. )
  1100. public final void testGetEncoded03() throws IOException {
  1101. boolean performed = false;
  1102. for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
  1103. try {
  1104. AlgorithmParameters ap = AlgorithmParameters
  1105. .getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
  1106. // use pregenerated AlgorithmParameters encodings
  1107. ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(
  1108. EncryptedPrivateKeyInfoData.algName0[i][0]));
  1109. EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap,
  1110. EncryptedPrivateKeyInfoData.encryptedData);
  1111. // check that method under test returns
  1112. // valid encoded form
  1113. assertTrue(Arrays.equals(
  1114. EncryptedPrivateKeyInfoData
  1115. .getValidEncryptedPrivateKeyInfoEncoding(
  1116. EncryptedPrivateKeyInfoData.algName0[i][0]),
  1117. epki.getEncoded()));
  1118. performed = true;
  1119. } catch (NoSuchAlgorithmException allowedFailure) {
  1120. }
  1121. }
  1122. assertTrue("Test not performed", performed);
  1123. }
  1124. /**
  1125. * Test #4 for <code>getEncoded()</code> method <br>
  1126. * Assertion: returns a new array each time this method is called <br>
  1127. * Test preconditi