Skip to content

Commit

Permalink
8321925: sun/security/mscapi/KeytoolChangeAlias.java fails with "Alia…
Browse files Browse the repository at this point in the history
…s <246810> does not exist"

Backport-of: b6233c3de773fb57b23704f1fec05d8b2d9c11c0
  • Loading branch information
SendaoYan authored and TheRealMDoerr committed May 13, 2024
1 parent 153de5a commit bddd6a7
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions test/jdk/sun/security/mscapi/KeytoolChangeAlias.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,6 +25,7 @@
import jdk.test.lib.security.CertUtils;

import java.security.KeyStore;
import java.security.SecureRandom;

/*
* @test
Expand All @@ -35,15 +36,17 @@
*/
public class KeytoolChangeAlias {
public static void main(String[] args) throws Exception {

SecureRandom random = new SecureRandom();
String alias = Integer.toString(random.nextInt(1000, 8192));
String newAlias = alias + "1";
KeyStore ks = KeyStore.getInstance("Windows-MY");
ks.load(null, null);

try {
ks.setCertificateEntry("246810", CertUtils.getCertFromFile("246810.cer"));
ks.setCertificateEntry(alias, CertUtils.getCertFromFile("246810.cer"));

if (ks.containsAlias("13579")) {
ks.deleteEntry("13579");
if (ks.containsAlias(newAlias)) {
ks.deleteEntry(newAlias);
}

int before = ks.size();
Expand All @@ -52,8 +55,8 @@ public static void main(String[] args) throws Exception {

SecurityTools.keytool("-changealias",
"-storetype", "Windows-My",
"-alias", "246810",
"-destalias", "13579").shouldHaveExitValue(0);
"-alias", alias,
"-destalias", newAlias).shouldHaveExitValue(0);

ks.load(null, null);

Expand All @@ -63,13 +66,23 @@ public static void main(String[] args) throws Exception {
+ ". After: " + ks.size());
}

if (!ks.containsAlias("13579")) {
if (!ks.containsAlias(newAlias)) {
throw new Exception("error: cannot find the new alias name"
+ " in the Windows-MY store");
}
} finally {
ks.deleteEntry("13579");
ks.deleteEntry("246810");
try {
ks.deleteEntry(newAlias);
} catch (Exception e) {
System.err.println("Couldn't delete alias " + newAlias);
e.printStackTrace(System.err);
}
try {
ks.deleteEntry(alias);
} catch (Exception e) {
System.err.println("Couldn't delete alias " + alias);
e.printStackTrace(System.err);
}
ks.store(null, null);
}
}
Expand Down

1 comment on commit bddd6a7

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.