8314094: java/lang/ProcessHandle/InfoTest.java fails on Windows when run as user with Administrator privileges

Reviewed-by: mbaesken, azeller
This commit is contained in:
Christoph Langer 2023-09-05 08:30:03 +00:00
parent fe4f90021f
commit 69c9ec92d0

View File

@ -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
@ -39,12 +39,13 @@ import java.util.Optional;
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
@ -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();
@ -160,11 +160,7 @@ public class InfoTest {
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()) {
@ -291,11 +287,8 @@ public class InfoTest {
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";
@ -397,7 +390,7 @@ public class InfoTest {
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();
@ -410,6 +403,7 @@ public class InfoTest {
}
}
}
/**
* Check two Durations, the second should be greater than the first or
* within the supplied Epsilon.
@ -443,4 +437,31 @@ public class InfoTest {
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");
}
}
}