11package com .github .mike10004 .crxtool .maven ;
22
3+ import com .github .mike10004 .crxtool .maven .PackExtensionMojo .PrivateKeyParameterConflictException ;
34import com .google .common .collect .ImmutableSet ;
45import com .google .common .io .BaseEncoding ;
56import com .google .common .io .ByteStreams ;
1718import java .io .ByteArrayInputStream ;
1819import java .io .File ;
1920import java .io .FileInputStream ;
21+ import java .io .FileNotFoundException ;
2022import java .io .IOException ;
2123import java .io .InputStream ;
2224import java .net .URISyntaxException ;
@@ -36,30 +38,47 @@ public class PackExtensionMojoTest {
3638
3739 @ Test
3840 public void testExecute_crx_noPrivateKey () throws Exception {
39- File crxFile = testExecute (false , null );
41+ File crxFile = testExecute (false , null , false );
4042 checkMetadata (crxFile );
4143 checkZipDataInCrxFile (crxFile );
4244 }
4345
4446 @ Test
4547 public void testExecute_crx_privateKey () throws Exception {
4648 File pemFile = buildPemFile (KeyPairs .generateRsKeyPair (new SecureRandom ()).getPrivate ().getEncoded ());
47- File crxFile = testExecute (false , pemFile );
49+ File crxFile = testExecute (false , pemFile , false );
50+ checkMetadata (crxFile );
51+ checkZipDataInCrxFile (crxFile );
52+ }
53+
54+ @ Test
55+ public void testExecute_crx_privateKeyFileSpecifiedButAbsent () throws Exception {
56+ try {
57+ File pemFile = new File (temporaryFolder .newFolder (), "mykey.pem" );
58+ testExecute (false , pemFile , false );
59+ } catch (MojoExecutionException e ) {
60+ assertTrue (e .getCause () instanceof FileNotFoundException );
61+ }
62+ }
63+
64+ @ Test
65+ public void testExecute_crx_privateKeyFileSpecifiedButAbsent_ignoreAbsent () throws Exception {
66+ File pemFile = new File (temporaryFolder .newFolder (), "mykey.pem" );
67+ File crxFile = testExecute (false , pemFile , true );
4868 checkMetadata (crxFile );
4969 checkZipDataInCrxFile (crxFile );
5070 }
5171
5272 @ Test
5373 public void testExecute_zip_noPrivateKey () throws Exception {
54- File zipFile = testExecute (true , null );
74+ File zipFile = testExecute (true , null , false );
5575 checkZipData (Files .asByteSource (zipFile ).read ());
5676 }
5777
58- @ Test
78+ @ Test ( expected = PrivateKeyParameterConflictException . class )
5979 public void testExecute_zip_privateKey () throws Exception {
60- File pemFile = buildPemFile (KeyPairs .generateRsKeyPair (new SecureRandom ()).getPrivate ().getEncoded ());
61- File zipFile = testExecute (true , pemFile );
62- checkZipData (Files .asByteSource (zipFile ).read ());
80+ File pemFile = temporaryFolder .newFile ();
81+ testExecute (true , pemFile , false );
6382 }
6483
6584 private File buildPemFile (byte [] privateKey ) throws IOException {
@@ -84,10 +103,11 @@ private PackExtensionMojo buildMojo() throws IOException, URISyntaxException {
84103 return mojo ;
85104 }
86105
87- private File testExecute (boolean excludeHeader , @ Nullable File privateKey ) throws IOException , MojoExecutionException , URISyntaxException {
106+ private File testExecute (boolean excludeHeader , @ Nullable File privateKey , boolean generateKey ) throws IOException , MojoExecutionException , URISyntaxException {
88107 PackExtensionMojo mojo = buildMojo ();
89108 mojo .setExcludeHeader (excludeHeader );
90109 mojo .setPrivateKey (privateKey );
110+ mojo .setGenerateKeyIfAbsent (generateKey );
91111 mojo .execute ();
92112 File outputFile = mojo .getOutputFile ();
93113 assertTrue ("file length" , outputFile .length () > 0 );
@@ -118,4 +138,5 @@ private void checkZipData(byte[] zipData) throws IOException {
118138 assertEquals ("filepaths" , ImmutableSet .of ("manifest.json" , "background.js" ), ImmutableSet .copyOf (unzippage .fileEntries ()));
119139 }
120140
141+
121142}
0 commit comments