@@ -446,4 +446,106 @@ public void testUpdatePasswordFileWithBom() throws Exception {
446446 assertArrayEquals ("Key must survive update with BOM password file" ,
447447 originalKey , updated .getSignInterface ().getPrivateKey ());
448448 }
449+
450+ @ Test
451+ public void testUpdateNonExistentKeystoreDir () throws Exception {
452+ File dir = new File (tempFolder .getRoot (), "does-not-exist" );
453+
454+ File pwFile = tempFolder .newFile ("pw-nodir.txt" );
455+ Files .write (pwFile .toPath (),
456+ ("oldpass123\n newpass456" ).getBytes (StandardCharsets .UTF_8 ));
457+
458+ StringWriter err = new StringWriter ();
459+ CommandLine cmd = new CommandLine (new Toolkit ());
460+ cmd .setErr (new PrintWriter (err ));
461+ int exitCode = cmd .execute ("keystore" , "update" , "TSomeAddress" ,
462+ "--keystore-dir" , dir .getAbsolutePath (),
463+ "--password-file" , pwFile .getAbsolutePath ());
464+
465+ assertEquals (1 , exitCode );
466+ assertTrue ("Error should mention no keystore found" ,
467+ err .toString ().contains ("No keystore found for address" ));
468+ }
469+
470+ @ Test
471+ public void testUpdateKeystoreDirIsFile () throws Exception {
472+ File notADir = tempFolder .newFile ("not-a-dir" );
473+
474+ File pwFile = tempFolder .newFile ("pw-notdir.txt" );
475+ Files .write (pwFile .toPath (),
476+ ("oldpass123\n newpass456" ).getBytes (StandardCharsets .UTF_8 ));
477+
478+ StringWriter err = new StringWriter ();
479+ CommandLine cmd = new CommandLine (new Toolkit ());
480+ cmd .setErr (new PrintWriter (err ));
481+ int exitCode = cmd .execute ("keystore" , "update" , "TSomeAddress" ,
482+ "--keystore-dir" , notADir .getAbsolutePath (),
483+ "--password-file" , pwFile .getAbsolutePath ());
484+
485+ assertEquals (1 , exitCode );
486+ assertTrue ("Error should mention no keystore found" ,
487+ err .toString ().contains ("No keystore found for address" ));
488+ }
489+
490+ @ Test
491+ public void testUpdateWithOldMacLineEndings () throws Exception {
492+ File dir = tempFolder .newFolder ("keystore-cr" );
493+ String oldPassword = "oldpass123" ;
494+ String newPassword = "newpass456" ;
495+
496+ SignInterface keyPair = SignUtils .getGeneratedRandomSign (
497+ SecureRandom .getInstance ("NativePRNG" ), true );
498+ byte [] originalKey = keyPair .getPrivateKey ();
499+ String fileName = WalletUtils .generateWalletFile (oldPassword , keyPair , dir , true );
500+ Credentials creds = WalletUtils .loadCredentials (oldPassword ,
501+ new File (dir , fileName ), true );
502+
503+ // Password file with old Mac line endings (\r only)
504+ File pwFile = tempFolder .newFile ("cr.txt" );
505+ Files .write (pwFile .toPath (),
506+ (oldPassword + "\r " + newPassword + "\r " ).getBytes (StandardCharsets .UTF_8 ));
507+
508+ CommandLine cmd = new CommandLine (new Toolkit ());
509+ int exitCode = cmd .execute ("keystore" , "update" , creds .getAddress (),
510+ "--keystore-dir" , dir .getAbsolutePath (),
511+ "--password-file" , pwFile .getAbsolutePath ());
512+
513+ assertEquals ("Update with old Mac CR line endings should succeed" , 0 , exitCode );
514+
515+ Credentials updated = WalletUtils .loadCredentials (newPassword ,
516+ new File (dir , fileName ), true );
517+ assertArrayEquals ("Key must survive update with CR passwords" ,
518+ originalKey , updated .getSignInterface ().getPrivateKey ());
519+ }
520+
521+ @ Test
522+ public void testUpdateSkipsInvalidVersionKeystores () throws Exception {
523+ File dir = tempFolder .newFolder ("keystore-badver" );
524+ String password = "test123456" ;
525+
526+ SignInterface keyPair = SignUtils .getGeneratedRandomSign (
527+ SecureRandom .getInstance ("NativePRNG" ), true );
528+ String address = Credentials .create (keyPair ).getAddress ();
529+
530+ // Create a JSON file with correct address but wrong version
531+ String fakeKeystore = "{\" address\" :\" " + address
532+ + "\" ,\" version\" :2,\" crypto\" :{\" cipher\" :\" aes-128-ctr\" }}" ;
533+ Files .write (new File (dir , "fake.json" ).toPath (),
534+ fakeKeystore .getBytes (StandardCharsets .UTF_8 ));
535+
536+ File pwFile = tempFolder .newFile ("pw-badver.txt" );
537+ Files .write (pwFile .toPath (),
538+ (password + "\n newpass789" ).getBytes (StandardCharsets .UTF_8 ));
539+
540+ StringWriter err = new StringWriter ();
541+ CommandLine cmd = new CommandLine (new Toolkit ());
542+ cmd .setErr (new PrintWriter (err ));
543+ int exitCode = cmd .execute ("keystore" , "update" , address ,
544+ "--keystore-dir" , dir .getAbsolutePath (),
545+ "--password-file" , pwFile .getAbsolutePath ());
546+
547+ assertEquals ("Should not find keystore with wrong version" , 1 , exitCode );
548+ assertTrue ("Error should mention no keystore found" ,
549+ err .toString ().contains ("No keystore found" ));
550+ }
449551}
0 commit comments