-
Notifications
You must be signed in to change notification settings - Fork 6k
8358171: Additional code coverage for PEM API #25588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
98f6532
more test coverage for PEM
fguallini 032afc0
multi thread and defaut algo tests
fguallini 920cf4c
add final break and test comment
fguallini 6f18e8b
lines comments
fguallini 9d4dc2b
add logging line
fguallini a74a76c
Merge branch 'master' into 8358171
fguallini c16eabe
add invalid PEM content test
fguallini 50707eb
Merge branch 'master' into 8358171
fguallini ea306f5
comment
fguallini ff00187
styling fixes
fguallini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (c) 2025, 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 | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
/* | ||
* @test | ||
* @bug 8298420 | ||
* @library /test/lib | ||
* @summary Testing PEM API is thread safe | ||
* @enablePreview | ||
* @modules java.base/sun.security.util | ||
*/ | ||
|
||
import java.security.*; | ||
import java.util.*; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import jdk.test.lib.security.SecurityUtils; | ||
|
||
public class PEMMultiThreadTest { | ||
static final int THREAD_COUNT = 5; | ||
static final int KEYS_COUNT = 50; | ||
|
||
public static void main(String[] args) throws Exception { | ||
PEMEncoder encoder = PEMEncoder.of(); | ||
try (ExecutorService ex = Executors.newFixedThreadPool(THREAD_COUNT)) { | ||
Map<Integer, PublicKey> keys = new HashMap<>(); | ||
Map<Integer, String> encoded = Collections.synchronizedMap(new HashMap<>()); | ||
Map<Integer, String> decoded = Collections.synchronizedMap(new HashMap<>()); | ||
final CountDownLatch encodingComplete = new CountDownLatch(KEYS_COUNT); | ||
final CountDownLatch decodingComplete = new CountDownLatch(KEYS_COUNT); | ||
|
||
// Generate keys and encode them in parallel | ||
for (int i = 0; i < KEYS_COUNT; i++) { | ||
final int finalI = i; | ||
KeyPair kp = getKeyPair(); | ||
keys.put(finalI, kp.getPublic()); | ||
|
||
ex.submit(() -> { | ||
encoded.put(finalI, encoder.encodeToString(kp.getPublic())); | ||
encodingComplete.countDown(); | ||
}); | ||
} | ||
encodingComplete.await(); | ||
|
||
// Decode keys in parallel | ||
PEMDecoder decoder = PEMDecoder.of(); | ||
for (Map.Entry<Integer, String> entry : encoded.entrySet()) { | ||
ex.submit(() -> { | ||
decoded.put(entry.getKey(), decoder.decode(entry.getValue(), PublicKey.class) | ||
.toString()); | ||
decodingComplete.countDown(); | ||
}); | ||
} | ||
decodingComplete.await(); | ||
|
||
// verify all keys were properly encoded and decoded comparing with the original key map | ||
for (Map.Entry<Integer, PublicKey> kp : keys.entrySet()) { | ||
if (!decoded.get(kp.getKey()).equals(kp.getValue().toString())) { | ||
throw new RuntimeException("a key was not properly encoded and decoded: " + decoded); | ||
} | ||
} | ||
} | ||
|
||
System.out.println("PASS: testThreadSafety"); | ||
} | ||
|
||
private static KeyPair getKeyPair() throws NoSuchAlgorithmException { | ||
String alg = "EC"; | ||
KeyPairGenerator kpg = KeyPairGenerator.getInstance(alg); | ||
kpg.initialize(SecurityUtils.getTestKeySize(alg)); | ||
return kpg.generateKeyPair(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.