@@ -174,7 +174,7 @@ public void testImportKeyFileWithWhitespace() throws Exception {
174174 }
175175
176176 @ Test
177- public void testImportDuplicateAddress () throws Exception {
177+ public void testImportDuplicateAddressBlocked () throws Exception {
178178 File dir = tempFolder .newFolder ("keystore-dup" );
179179 SignInterface keyPair = SignUtils .getGeneratedRandomSign (
180180 SecureRandom .getInstance ("NativePRNG" ), true );
@@ -185,22 +185,70 @@ public void testImportDuplicateAddress() throws Exception {
185185 File pwFile = tempFolder .newFile ("pw-dup.txt" );
186186 Files .write (pwFile .toPath (), "test123456" .getBytes (StandardCharsets .UTF_8 ));
187187
188- // Import same key twice
188+ // First import succeeds
189189 CommandLine cmd1 = new CommandLine (new Toolkit ());
190190 assertEquals (0 , cmd1 .execute ("keystore" , "import" ,
191191 "--keystore-dir" , dir .getAbsolutePath (),
192192 "--key-file" , keyFile .getAbsolutePath (),
193193 "--password-file" , pwFile .getAbsolutePath ()));
194194
195+ // Second import of same key is blocked
195196 CommandLine cmd2 = new CommandLine (new Toolkit ());
196- assertEquals (0 , cmd2 .execute ("keystore" , "import" ,
197+ assertEquals ("Duplicate import should be blocked" , 1 ,
198+ cmd2 .execute ("keystore" , "import" ,
199+ "--keystore-dir" , dir .getAbsolutePath (),
200+ "--key-file" , keyFile .getAbsolutePath (),
201+ "--password-file" , pwFile .getAbsolutePath ()));
202+
203+ File [] files = dir .listFiles ((d , name ) -> name .endsWith (".json" ));
204+ assertNotNull (files );
205+ assertEquals ("Should still have only 1 keystore" , 1 , files .length );
206+ }
207+
208+ @ Test
209+ public void testImportDuplicateAddressWithForce () throws Exception {
210+ File dir = tempFolder .newFolder ("keystore-force" );
211+ SignInterface keyPair = SignUtils .getGeneratedRandomSign (
212+ SecureRandom .getInstance ("NativePRNG" ), true );
213+ String privateKeyHex = ByteArray .toHexString (keyPair .getPrivateKey ());
214+
215+ File keyFile = tempFolder .newFile ("force.key" );
216+ Files .write (keyFile .toPath (), privateKeyHex .getBytes (StandardCharsets .UTF_8 ));
217+ File pwFile = tempFolder .newFile ("pw-force.txt" );
218+ Files .write (pwFile .toPath (), "test123456" .getBytes (StandardCharsets .UTF_8 ));
219+
220+ // First import
221+ CommandLine cmd1 = new CommandLine (new Toolkit ());
222+ assertEquals (0 , cmd1 .execute ("keystore" , "import" ,
197223 "--keystore-dir" , dir .getAbsolutePath (),
198224 "--key-file" , keyFile .getAbsolutePath (),
199225 "--password-file" , pwFile .getAbsolutePath ()));
200226
201- // Should create two separate files (timestamped names differ)
227+ // Second import with --force succeeds
228+ CommandLine cmd2 = new CommandLine (new Toolkit ());
229+ assertEquals (0 , cmd2 .execute ("keystore" , "import" ,
230+ "--keystore-dir" , dir .getAbsolutePath (),
231+ "--key-file" , keyFile .getAbsolutePath (),
232+ "--password-file" , pwFile .getAbsolutePath (),
233+ "--force" ));
234+
202235 File [] files = dir .listFiles ((d , name ) -> name .endsWith (".json" ));
203236 assertNotNull (files );
204- assertEquals ("Duplicate import should create 2 separate files" , 2 , files .length );
237+ assertEquals ("Force import should create 2 files" , 2 , files .length );
238+ }
239+
240+ @ Test
241+ public void testImportKeyFileNotFound () throws Exception {
242+ File dir = tempFolder .newFolder ("keystore-nokey" );
243+ File pwFile = tempFolder .newFile ("pw-nokey.txt" );
244+ Files .write (pwFile .toPath (), "test123456" .getBytes (StandardCharsets .UTF_8 ));
245+
246+ CommandLine cmd = new CommandLine (new Toolkit ());
247+ int exitCode = cmd .execute ("keystore" , "import" ,
248+ "--keystore-dir" , dir .getAbsolutePath (),
249+ "--key-file" , "/tmp/nonexistent-key-file.txt" ,
250+ "--password-file" , pwFile .getAbsolutePath ());
251+
252+ assertEquals ("Should fail when key file not found" , 1 , exitCode );
205253 }
206254}
0 commit comments