11/*
2- * Copyright (c) 2018, 2019 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2018, 2024 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2525import jdk .test .lib .security .CertUtils ;
2626
2727import java .security .KeyStore ;
28+ import java .security .SecureRandom ;
2829
2930/*
3031 * @test
3536 */
3637public class KeytoolChangeAlias {
3738 public static void main (String [] args ) throws Exception {
38-
39+ SecureRandom random = new SecureRandom ();
40+ String alias = Integer .toString (random .nextInt (1000 , 8192 ));
41+ String newAlias = alias + "1" ;
3942 KeyStore ks = KeyStore .getInstance ("Windows-MY" );
4043 ks .load (null , null );
4144
4245 try {
43- ks .setCertificateEntry ("246810" , CertUtils .getCertFromFile ("246810.cer" ));
46+ ks .setCertificateEntry (alias , CertUtils .getCertFromFile ("246810.cer" ));
4447
45- if (ks .containsAlias ("13579" )) {
46- ks .deleteEntry ("13579" );
48+ if (ks .containsAlias (newAlias )) {
49+ ks .deleteEntry (newAlias );
4750 }
4851
4952 int before = ks .size ();
@@ -52,8 +55,8 @@ public static void main(String[] args) throws Exception {
5255
5356 SecurityTools .keytool ("-changealias" ,
5457 "-storetype" , "Windows-My" ,
55- "-alias" , "246810" ,
56- "-destalias" , "13579" ).shouldHaveExitValue (0 );
58+ "-alias" , alias ,
59+ "-destalias" , newAlias ).shouldHaveExitValue (0 );
5760
5861 ks .load (null , null );
5962
@@ -63,13 +66,23 @@ public static void main(String[] args) throws Exception {
6366 + ". After: " + ks .size ());
6467 }
6568
66- if (!ks .containsAlias ("13579" )) {
69+ if (!ks .containsAlias (newAlias )) {
6770 throw new Exception ("error: cannot find the new alias name"
6871 + " in the Windows-MY store" );
6972 }
7073 } finally {
71- ks .deleteEntry ("13579" );
72- ks .deleteEntry ("246810" );
74+ try {
75+ ks .deleteEntry (newAlias );
76+ } catch (Exception e ) {
77+ System .err .println ("Couldn't delete alias " + newAlias );
78+ e .printStackTrace (System .err );
79+ }
80+ try {
81+ ks .deleteEntry (alias );
82+ } catch (Exception e ) {
83+ System .err .println ("Couldn't delete alias " + alias );
84+ e .printStackTrace (System .err );
85+ }
7386 ks .store (null , null );
7487 }
7588 }
0 commit comments