-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16992 from ymanton/fix-checkpoint-username
Fix getting username from env in checkpoint mode
- Loading branch information
Showing
3 changed files
with
95 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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
69 changes: 69 additions & 0 deletions
69
test/functional/cmdLineTests/criu/src/org/openj9/criu/UsernameTest.java
This file contains 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,69 @@ | ||
/******************************************************************************* | ||
* Copyright IBM Corp. and others 2023 | ||
* | ||
* This program and the accompanying materials are made available under | ||
* the terms of the Eclipse Public License 2.0 which accompanies this | ||
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* or the Apache License, Version 2.0 which accompanies this distribution and | ||
* is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* This Source Code may also be made available under the following | ||
* Secondary Licenses when the conditions for such availability set | ||
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU | ||
* General Public License, version 2 with the GNU Classpath | ||
* Exception [1] and GNU General Public License, version 2 with the | ||
* OpenJDK Assembly Exception [2]. | ||
* | ||
* [1] https://www.gnu.org/software/classpath/license.html | ||
* [2] https://openjdk.org/legal/assembly-exception.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception | ||
*******************************************************************************/ | ||
package org.openj9.criu; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
public class UsernameTest { | ||
|
||
private static String getUsernameFromProperty() { | ||
return System.getProperty("user.name"); | ||
} | ||
|
||
private static String getExpectedUsername() throws IOException { | ||
// Getting the username from the USER env var is easier, but | ||
// since env vars also have special treatment in checkpoint mode | ||
// we don't want to rely on them to validate username handling. | ||
// Instead we call the `id` system command. | ||
return new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("id --user --name").getInputStream())).readLine().trim(); | ||
} | ||
|
||
public static void main(String args[]) throws IOException { | ||
Path path = Paths.get("cpData"); | ||
String expectedUsernamePreCheckpoint = getExpectedUsername(); | ||
// Checkpointing allowed, should skip getpwuid and fall back to env. | ||
String usernamePreCheckpoint = getUsernameFromProperty(); | ||
CRIUTestUtils.checkPointJVM(path); | ||
String expectedUsernamePostCheckpoint = getExpectedUsername(); | ||
// Checkpointing not allowed, should call getpwuid and only fall back on error. | ||
String usernamePostCheckpoint = getUsernameFromProperty(); | ||
boolean failed = false; | ||
if (!usernamePreCheckpoint.equals(usernamePostCheckpoint)) { | ||
System.out.println("FAILURE: user.name property value has changed across checkpoint (" + usernamePreCheckpoint + " -> " + usernamePostCheckpoint + ")"); | ||
failed = true; | ||
} | ||
if (!expectedUsernamePreCheckpoint.equals(expectedUsernamePostCheckpoint)) { | ||
System.out.println("FAILURE: expected user name has changed across checkpoint (" + expectedUsernamePreCheckpoint + " -> " + expectedUsernamePostCheckpoint + ")"); | ||
failed = true; | ||
} | ||
if (!usernamePreCheckpoint.equals(expectedUsernamePreCheckpoint)) { | ||
System.out.println("FAILURE: user.name property value (" + usernamePreCheckpoint + ") does not match expected user name (" + expectedUsernamePreCheckpoint + ")"); | ||
failed = true; | ||
} | ||
if (!failed) | ||
System.out.println("SUCCESS"); | ||
} | ||
} |