1
1
package com .github .mike10004 .crxtool .maven ;
2
2
3
+ import com .github .mike10004 .crxtool .maven .PackExtensionMojo .PrivateKeyParameterConflictException ;
3
4
import com .google .common .collect .ImmutableSet ;
4
5
import com .google .common .io .BaseEncoding ;
5
6
import com .google .common .io .ByteStreams ;
17
18
import java .io .ByteArrayInputStream ;
18
19
import java .io .File ;
19
20
import java .io .FileInputStream ;
21
+ import java .io .FileNotFoundException ;
20
22
import java .io .IOException ;
21
23
import java .io .InputStream ;
22
24
import java .net .URISyntaxException ;
@@ -36,30 +38,47 @@ public class PackExtensionMojoTest {
36
38
37
39
@ Test
38
40
public void testExecute_crx_noPrivateKey () throws Exception {
39
- File crxFile = testExecute (false , null );
41
+ File crxFile = testExecute (false , null , false );
40
42
checkMetadata (crxFile );
41
43
checkZipDataInCrxFile (crxFile );
42
44
}
43
45
44
46
@ Test
45
47
public void testExecute_crx_privateKey () throws Exception {
46
48
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 );
48
68
checkMetadata (crxFile );
49
69
checkZipDataInCrxFile (crxFile );
50
70
}
51
71
52
72
@ Test
53
73
public void testExecute_zip_noPrivateKey () throws Exception {
54
- File zipFile = testExecute (true , null );
74
+ File zipFile = testExecute (true , null , false );
55
75
checkZipData (Files .asByteSource (zipFile ).read ());
56
76
}
57
77
58
- @ Test
78
+ @ Test ( expected = PrivateKeyParameterConflictException . class )
59
79
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 );
63
82
}
64
83
65
84
private File buildPemFile (byte [] privateKey ) throws IOException {
@@ -84,10 +103,11 @@ private PackExtensionMojo buildMojo() throws IOException, URISyntaxException {
84
103
return mojo ;
85
104
}
86
105
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 {
88
107
PackExtensionMojo mojo = buildMojo ();
89
108
mojo .setExcludeHeader (excludeHeader );
90
109
mojo .setPrivateKey (privateKey );
110
+ mojo .setGenerateKeyIfAbsent (generateKey );
91
111
mojo .execute ();
92
112
File outputFile = mojo .getOutputFile ();
93
113
assertTrue ("file length" , outputFile .length () > 0 );
@@ -118,4 +138,5 @@ private void checkZipData(byte[] zipData) throws IOException {
118
138
assertEquals ("filepaths" , ImmutableSet .of ("manifest.json" , "background.js" ), ImmutableSet .copyOf (unzippage .fileEntries ()));
119
139
}
120
140
141
+
121
142
}
0 commit comments