Skip to content

Commit

Permalink
8314094: java/lang/ProcessHandle/InfoTest.java fails on Windows when …
Browse files Browse the repository at this point in the history
…run as user with Administrator privileges

Reviewed-by: mbaesken, azeller
  • Loading branch information
RealCLanger committed Sep 5, 2023
1 parent fe4f900 commit 69c9ec9
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions test/jdk/java/lang/ProcessHandle/InfoTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2023, 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 Down Expand Up @@ -39,12 +39,13 @@
import java.util.Random;
import java.util.concurrent.TimeUnit;

import jdk.test.lib.Platform;
import jdk.test.lib.Utils;
import org.testng.Assert;
import org.testng.TestNG;
import org.testng.annotations.Test;

import jdk.test.lib.Platform;
import jdk.test.lib.Utils;

/*
* @test
* @bug 8077350 8081566 8081567 8098852 8136597
Expand Down Expand Up @@ -81,7 +82,6 @@ public class InfoTest {
}

// Main can be used to run the tests from the command line with only testng.jar.
@SuppressWarnings("raw_types")
public static void main(String[] args) {
Class<?>[] testclass = {InfoTest.class};
TestNG testng = new TestNG();
Expand Down Expand Up @@ -160,11 +160,7 @@ public static void test2() {
ProcessHandle.Info info = p1.info();
System.out.printf(" info: %s%n", info);

if (info.user().isPresent()) {
String user = info.user().get();
Assert.assertNotNull(user, "User name");
Assert.assertEquals(user, whoami, "User name");
}
assertUser(info);

Optional<String> command = info.command();
if (command.isPresent()) {
Expand Down Expand Up @@ -291,11 +287,8 @@ public static void test3() {
ProcessHandle.Info info = p.info();
System.out.printf(" info: %s%n", info);

if (info.user().isPresent()) {
String user = info.user().get();
Assert.assertNotNull(user);
Assert.assertEquals(user, whoami);
}
assertUser(info);

if (info.command().isPresent()) {
String command = info.command().get();
String expected = "sleep";
Expand Down Expand Up @@ -397,7 +390,7 @@ public static void test5() {
Instant end = Instant.now().plusMillis(500L);
while (end.isBefore(Instant.now())) {
// burn the cpu time checking the time
long x = r.nextLong();
r.nextLong();
}
if (self.info().totalCpuDuration().isPresent()) {
Duration totalCpu = self.info().totalCpuDuration().get();
Expand All @@ -410,6 +403,7 @@ public static void test5() {
}
}
}

/**
* Check two Durations, the second should be greater than the first or
* within the supplied Epsilon.
Expand Down Expand Up @@ -443,4 +437,31 @@ static Process spawn(String command, String... args) throws IOException {
pb.command(list);
return pb.start();
}

/**
* Asserts the expected process user.
*
* The Expected user is determined by creating a file and reading its owner, see static block above.
*
* On Windows, when run privileged as member of the Administrators group, this does not always
* work because new files can be owned by BUILTIN\Administrators instead, depending on system
* settings. In that case we resort to comparing System property user.name, which does not contain
* the domain name, though.
*
* @param info ProcessHanlde info object
*/
static void assertUser(ProcessHandle.Info info) {
if (!info.user().isPresent()) {
return;
}
String user = info.user().get();
Assert.assertNotNull(user, "User name");
if (Platform.isWindows() && "BUILTIN\\Administrators".equals(whoami)) {
int bsi = user.lastIndexOf("\\");
Assert.assertEquals(bsi == -1 ? user : user.substring(bsi + 1),
System.getProperty("user.name"), "User name");
} else {
Assert.assertEquals(user, whoami, "User name");
}
}
}

7 comments on commit 69c9ec9

@RealCLanger
Copy link
Contributor Author

Choose a reason for hiding this comment

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

/backport jdk21u

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@openjdk
Copy link

@openjdk openjdk bot commented on 69c9ec9 Sep 5, 2023

Choose a reason for hiding this comment

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

@RealCLanger the backport was successfully created on the branch RealCLanger-backport-69c9ec92 in my personal fork of openjdk/jdk21u. To create a pull request with this backport targeting openjdk/jdk21u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 69c9ec92 from the openjdk/jdk repository.

The commit being backported was authored by Christoph Langer on 5 Sep 2023 and was reviewed by Matthias Baesken and Arno Zeller.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u:

$ git fetch https://github.com/openjdk-bots/jdk21u.git RealCLanger-backport-69c9ec92:RealCLanger-backport-69c9ec92
$ git checkout RealCLanger-backport-69c9ec92
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u.git RealCLanger-backport-69c9ec92

@RealCLanger
Copy link
Contributor Author

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 69c9ec9 Sep 13, 2023

Choose a reason for hiding this comment

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

@RealCLanger the backport was successfully created on the branch RealCLanger-backport-69c9ec92 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 69c9ec92 from the openjdk/jdk repository.

The commit being backported was authored by Christoph Langer on 5 Sep 2023 and was reviewed by Matthias Baesken and Arno Zeller.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git RealCLanger-backport-69c9ec92:RealCLanger-backport-69c9ec92
$ git checkout RealCLanger-backport-69c9ec92
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git RealCLanger-backport-69c9ec92

@RealCLanger
Copy link
Contributor Author

Choose a reason for hiding this comment

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

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 69c9ec9 Sep 13, 2023

Choose a reason for hiding this comment

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

@RealCLanger the backport was successfully created on the branch RealCLanger-backport-69c9ec92 in my personal fork of openjdk/jdk11u-dev. To create a pull request with this backport targeting openjdk/jdk11u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 69c9ec92 from the openjdk/jdk repository.

The commit being backported was authored by Christoph Langer on 5 Sep 2023 and was reviewed by Matthias Baesken and Arno Zeller.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk11u-dev:

$ git fetch https://github.com/openjdk-bots/jdk11u-dev.git RealCLanger-backport-69c9ec92:RealCLanger-backport-69c9ec92
$ git checkout RealCLanger-backport-69c9ec92
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk11u-dev.git RealCLanger-backport-69c9ec92

Please sign in to comment.