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

Backport-of: 69c9ec92d04a399946b2157690a1dc3fec517329
  • Loading branch information
RealCLanger committed Sep 13, 2023
1 parent 9b983f3 commit f6426fa
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");
}
}
}

1 comment on commit f6426fa

@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.