31
31
* @run main/othervm SymLinkTest
32
32
*/
33
33
34
- import java .io .*;
35
- import java .net .URI ;
36
- import java .nio .file .*;
34
+ import java .io .FileInputStream ;
35
+ import java .io .IOException ;
36
+ import java .nio .file .Files ;
37
+ import java .nio .file .Path ;
37
38
import java .util .Formatter ;
38
39
39
40
import jdk .test .lib .SecurityTools ;
40
41
41
42
public class SymLinkTest {
43
+ private final static int BYTES_PER_ROW = 8 ;
42
44
private final static String ZIPFILENAME = "8250968-test.zip" ;
43
45
private static final String WARNING_MSG = "POSIX file permission and/or symlink " +
44
46
"attributes detected. These attributes are ignored when signing and are not " +
45
47
"protected by the signature." ;
46
48
47
49
public static void main (String [] args ) throws Exception {
48
- Files .deleteIfExists (Paths .get (ZIPFILENAME ));
49
- try (FileOutputStream fos = new FileOutputStream (ZIPFILENAME )) {
50
- fos .write (ZIPBYTES );
50
+ // call main with an argument to print the prepared zipfile as byte array declaration
51
+ if (args .length > 0 ) {
52
+ System .out .println ("Bytes of " + ZIPFILENAME + ":" );
53
+ System .out .println (createByteArray (Files .readAllBytes (Path .of (ZIPFILENAME )), "ZIPBYTES" ));
54
+ System .exit (0 );
51
55
}
52
56
53
- // check permissions before signing
57
+ Files .write (Path .of (ZIPFILENAME ), ZIPBYTES );
58
+
59
+ // check attributes before signing
54
60
verifyExtraAttrs (ZIPFILENAME );
55
61
62
+ // generate key for signing
56
63
SecurityTools .keytool (
57
64
"-genkey" ,
58
65
"-keyalg" , "RSA" ,
@@ -64,6 +71,7 @@ public static void main(String[] args) throws Exception {
64
71
"-validity" , "365" )
65
72
.shouldHaveExitValue (0 );
66
73
74
+ // sign zip file - expect warning
67
75
SecurityTools .jarsigner (
68
76
"-keystore" , "examplekeystore" ,
69
77
"-verbose" , ZIPFILENAME ,
@@ -73,10 +81,12 @@ public static void main(String[] args) throws Exception {
73
81
.shouldHaveExitValue (0 )
74
82
.shouldContain (WARNING_MSG );
75
83
76
- // zip file now signed. Recheck attributes
84
+ // recheck attributes after signing
77
85
verifyExtraAttrs (ZIPFILENAME );
78
86
79
- SecurityTools .jarsigner ("-keystore" , "examplekeystore" ,
87
+ // verify zip file - expect warning
88
+ SecurityTools .jarsigner (
89
+ "-keystore" , "examplekeystore" ,
80
90
"-storepass" , "password" ,
81
91
"-keypass" , "password" ,
82
92
"-verbose" ,
@@ -114,48 +124,57 @@ private static void verifyExtraAttrs(String zipFileName) throws IOException {
114
124
* @param name Name to be used in the byte array declaration
115
125
* @return The formatted byte array declaration
116
126
*/
117
- public static String createByteArray (byte [] bytes , String name ) {
118
- StringBuilder sb = new StringBuilder (bytes .length * 5 );
119
- Formatter fmt = new Formatter (sb );
120
- fmt .format (" public static byte[] %s = {" , name );
121
- final int linelen = 8 ;
122
- for (int i = 0 ; i < bytes .length ; i ++) {
123
- if (i % linelen == 0 ) {
124
- fmt .format ("%n " );
127
+ private static String createByteArray (byte [] bytes , String name ) {
128
+ StringBuilder sb = new StringBuilder ();
129
+ try (Formatter fmt = new Formatter (sb )) {
130
+ fmt .format (" public final static byte[] %s = {" , name );
131
+ for (int i = 0 ; i < bytes .length ; i ++) {
132
+ int mod = i % BYTES_PER_ROW ;
133
+ if (mod == 0 ) {
134
+ fmt .format ("%n " );
135
+ } else {
136
+ fmt .format (" " );
137
+ }
138
+ fmt .format ("(byte)0x%02x" , bytes [i ]);
139
+ if (i != bytes .length - 1 ) {
140
+ fmt .format ("," );
141
+ }
125
142
}
126
- fmt .format (" (byte) 0x%x," , bytes [ i ] & 0xff );
143
+ fmt .format ("%n };%n" );
127
144
}
128
- fmt .format ("%n };%n" );
129
145
return sb .toString ();
130
146
}
131
147
132
148
/*
133
- * Created using the createByteArray utility method.
134
- * The zipfile itself was created via this example:
149
+ * The zipfile itself was created like this:
150
+ * $ ln -s ../z z
135
151
* $ ls -l z
136
152
* lrwxrwxrwx 1 test test 4 Aug 27 18:33 z -> ../z
137
- * $ zip -ry test.zip z
153
+ * $ zip -ry 8250968-test.zip z
154
+ *
155
+ * The byte array representation was generated using the createByteArray utility method:
156
+ * $ java SymLinkTest generate
138
157
*/
139
158
public final static byte [] ZIPBYTES = {
140
- (byte ) 0x50 , (byte ) 0x4b , (byte ) 0x3 , (byte ) 0x4 , (byte ) 0xa , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 ,
141
- (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x2e , (byte ) 0x94 , (byte ) 0x1b , (byte ) 0x51 , (byte ) 0xb4 , (byte ) 0xcc ,
142
- (byte ) 0xb6 , (byte ) 0xf1 , (byte ) 0x4 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x4 , (byte ) 0x0 ,
143
- (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x1 , (byte ) 0x0 , (byte ) 0x1c , (byte ) 0x0 , (byte ) 0x7a , (byte ) 0x55 ,
144
- (byte ) 0x54 , (byte ) 0x9 , (byte ) 0x0 , (byte ) 0x3 , (byte ) 0x77 , (byte ) 0xfc , (byte ) 0x47 , (byte ) 0x5f ,
145
- (byte ) 0x78 , (byte ) 0xfc , (byte ) 0x47 , (byte ) 0x5f , (byte ) 0x75 , (byte ) 0x78 , (byte ) 0xb , (byte ) 0x0 ,
146
- (byte ) 0x1 , (byte ) 0x4 , (byte ) 0xec , (byte ) 0x3 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x4 , (byte ) 0xec ,
147
- (byte ) 0x3 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x2e , (byte ) 0x2e , (byte ) 0x2f , (byte ) 0x7a , (byte ) 0x50 ,
148
- (byte ) 0x4b , (byte ) 0x1 , (byte ) 0x2 , (byte ) 0x1e , (byte ) 0x3 , (byte ) 0xa , (byte ) 0x0 , (byte ) 0x0 ,
149
- (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x2e , (byte ) 0x94 , (byte ) 0x1b , (byte ) 0x51 , (byte ) 0xb4 ,
150
- (byte ) 0xcc , (byte ) 0xb6 , (byte ) 0xf1 , (byte ) 0x4 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x4 ,
151
- (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x1 , (byte ) 0x0 , (byte ) 0x18 , (byte ) 0x0 , (byte ) 0x0 ,
152
- (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0xff ,
153
- (byte ) 0xa1 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x7a , (byte ) 0x55 , (byte ) 0x54 ,
154
- (byte ) 0x5 , (byte ) 0x0 , (byte ) 0x3 , (byte ) 0x77 , (byte ) 0xfc , (byte ) 0x47 , (byte ) 0x5f , (byte ) 0x75 ,
155
- (byte ) 0x78 , (byte ) 0xb , (byte ) 0x0 , (byte ) 0x1 , (byte ) 0x4 , (byte ) 0xec , (byte ) 0x3 , (byte ) 0x0 ,
156
- (byte ) 0x0 , (byte ) 0x4 , (byte ) 0xec , (byte ) 0x3 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x50 , (byte ) 0x4b ,
157
- (byte ) 0x5 , (byte ) 0x6 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x1 , (byte ) 0x0 ,
158
- (byte ) 0x1 , (byte ) 0x0 , (byte ) 0x47 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x3f , (byte ) 0x0 ,
159
- (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 , (byte ) 0x0 ,
159
+ (byte )0x50 , (byte )0x4b , (byte )0x03 , (byte )0x04 , (byte )0x0a , (byte )0x00 , (byte )0x00 , (byte )0x00 ,
160
+ (byte )0x00 , (byte )0x00 , (byte )0x2e , (byte )0x94 , (byte )0x1b , (byte )0x51 , (byte )0xb4 , (byte )0xcc ,
161
+ (byte )0xb6 , (byte )0xf1 , (byte )0x04 , (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x04 , (byte )0x00 ,
162
+ (byte )0x00 , (byte )0x00 , (byte )0x01 , (byte )0x00 , (byte )0x1c , (byte )0x00 , (byte )0x7a , (byte )0x55 ,
163
+ (byte )0x54 , (byte )0x09 , (byte )0x00 , (byte )0x03 , (byte )0x77 , (byte )0xfc , (byte )0x47 , (byte )0x5f ,
164
+ (byte )0x78 , (byte )0xfc , (byte )0x47 , (byte )0x5f , (byte )0x75 , (byte )0x78 , (byte )0x0b , (byte )0x00 ,
165
+ (byte )0x01 , (byte )0x04 , (byte )0xec , (byte )0x03 , (byte )0x00 , (byte )0x00 , (byte )0x04 , (byte )0xec ,
166
+ (byte )0x03 , (byte )0x00 , (byte )0x00 , (byte )0x2e , (byte )0x2e , (byte )0x2f , (byte )0x7a , (byte )0x50 ,
167
+ (byte )0x4b , (byte )0x01 , (byte )0x02 , (byte )0x1e , (byte )0x03 , (byte )0x0a , (byte )0x00 , (byte )0x00 ,
168
+ (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x2e , (byte )0x94 , (byte )0x1b , (byte )0x51 , (byte )0xb4 ,
169
+ (byte )0xcc , (byte )0xb6 , (byte )0xf1 , (byte )0x04 , (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x04 ,
170
+ (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x01 , (byte )0x00 , (byte )0x18 , (byte )0x00 , (byte )0x00 ,
171
+ (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0xff ,
172
+ (byte )0xa1 , (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x7a , (byte )0x55 , (byte )0x54 ,
173
+ (byte )0x05 , (byte )0x00 , (byte )0x03 , (byte )0x77 , (byte )0xfc , (byte )0x47 , (byte )0x5f , (byte )0x75 ,
174
+ (byte )0x78 , (byte )0x0b , (byte )0x00 , (byte )0x01 , (byte )0x04 , (byte )0xec , (byte )0x03 , (byte )0x00 ,
175
+ (byte )0x00 , (byte )0x04 , (byte )0xec , (byte )0x03 , (byte )0x00 , (byte )0x00 , (byte )0x50 , (byte )0x4b ,
176
+ (byte )0x05 , (byte )0x06 , (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x01 , (byte )0x00 ,
177
+ (byte )0x01 , (byte )0x00 , (byte )0x47 , (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x3f , (byte )0x00 ,
178
+ (byte )0x00 , (byte )0x00 , (byte )0x00 , (byte )0x00
160
179
};
161
180
}
0 commit comments