Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,6 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -213,13 +207,16 @@

<!--
pmd: fail the build on any violation of pmd.xml and on any CPD duplication, so the
PMD and CPD reports stay clean. Version pinned to the one the parent manages for the
site report, so the gate and the report always run the same PMD. minimumTokens matches
the reporting configuration so the CPD gate and the CPD report agree.
PMD and CPD reports stay clean. Version pinned to the same one as the site report
below, so the gate and the report always run the same PMD — 3.28.0 runs PMD 7.17.0,
whose type resolution understands the class files of recent JDKs (7.7.0 failed on the
JDK 25 core library with "Unsupported class file major version 69" and then reported
false positives). minimumTokens matches the reporting configuration so the CPD gate
and the CPD report agree.
-->
<plugin>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.26.0</version>
<version>3.28.0</version>
<executions>
<execution>
<phase>verify</phase>
Expand Down Expand Up @@ -262,13 +259,15 @@

<!--
spotbugs: fail the build on any SpotBugs finding, so the SpotBugs report stays clean.
Version pinned to the one the parent manages for the site report, so the gate and the
report always run the same SpotBugs.
Version pinned to the same one as the site report below, so the gate and the report
always run the same SpotBugs — 4.10.3.0 runs SpotBugs 4.10.3, which reads the class
files of recent JDKs (4.9.3 crashed on the JDK 25 core library with "Unsupported
class file major version 69").
-->
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.9.3.0</version>
<version>4.10.3.0</version>
<executions>
<execution>
<phase>verify</phase>
Expand Down Expand Up @@ -355,9 +354,18 @@
<version>3.5.6</version>
</plugin>

<!-- pmd -->
<!-- spotbugs: same version as the build gate above, so the report shows what the gate
checked (and, like the gate, understands the class files of recent JDKs) -->
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.10.3.0</version>
</plugin>

<!-- pmd: same version as the build gate above, so the report shows what the gate checked -->
<plugin>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.28.0</version>
<configuration>
<linkXref>true</linkXref>
<sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
Expand Down
88 changes: 65 additions & 23 deletions src/test/java/org/metricshub/winrm/cli/WinRmCliTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
package org.metricshub.winrm.cli;

/*-
* ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
* WinRM Java Client
* ჻჻჻჻჻჻
* Copyright 2023 - 2026 MetricsHub
* ჻჻჻჻჻჻
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
*/

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.metricshub.winrm.light.FakeWsmanResponses.enqueueCommandExchange;
import static org.metricshub.winrm.light.FakeWsmanResponses.enqueueEnumeration;
import static org.metricshub.winrm.light.FakeWsmanResponses.enqueueShellCreation;
import static org.metricshub.winrm.light.FakeWsmanResponses.enqueueShellDeletion;
import static org.metricshub.winrm.light.FakeWsmanResponses.instance;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
Expand All @@ -22,10 +40,11 @@
import java.util.Map;
import java.util.concurrent.TimeoutException;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.metricshub.winrm.WinRMHttpProtocolEnum;
import org.metricshub.winrm.WindowsRemoteCommandResult;
import org.metricshub.winrm.WindowsRemoteProcessUtils;
import org.metricshub.winrm.light.FakeWsmanServer;
import org.metricshub.winrm.light.LightWinRMService;
import org.metricshub.winrm.service.WinRMEndpoint;

class WinRmCliTest {

Expand Down Expand Up @@ -238,21 +257,44 @@ void honorsAnAmbientInsecureTlsProperty() throws Exception {

@Test
void decodesCommandOutputUsingTheRemoteWindowsCodePage() throws Exception {
final LightWinRMService service = mock(LightWinRMService.class);
final Charset windowsCharset = Charset.forName("windows-1251");
final WindowsRemoteCommandResult expected = new WindowsRemoteCommandResult("Результат", "", 0.0f, 0);
final long timeout = 1_234L;
when(service.executeCommand("whoami", null, windowsCharset, timeout)).thenReturn(expected);

try (MockedStatic<WindowsRemoteProcessUtils> processUtils = mockStatic(WindowsRemoteProcessUtils.class)) {
processUtils
.when(() -> WindowsRemoteProcessUtils.getWindowsEncodingCharset(service, timeout))
.thenReturn(windowsCharset);
final WinRmCli.LightRemoteOperations remote = new WinRmCli.LightRemoteOperations(service);

assertSame(expected, remote.executeCommand("whoami", timeout));
processUtils.verify(() -> WindowsRemoteProcessUtils.getWindowsEncodingCharset(service, timeout));
verify(service).executeCommand(eq("whoami"), isNull(), eq(windowsCharset), eq(timeout));
final long timeout = 30_000L;

// End to end against the in-process WSMan server: the remote reports Windows code page
// 1251 and the command output arrives in that encoding — the CLI must query the code page
// and decode the stream bytes with it, or the Cyrillic output turns into mojibake.
try (FakeWsmanServer server = new FakeWsmanServer("FAKE", "user", "secret")) {
enqueueEnumeration(server, instance("Win32_OperatingSystem", "CodeSet", "1251"));
enqueueShellCreation(server);
enqueueCommandExchange(server, "Результат".getBytes(windowsCharset), new byte[0], 0);
enqueueShellDeletion(server);

final WinRMEndpoint endpoint = new WinRMEndpoint(
WinRMHttpProtocolEnum.HTTP,
"127.0.0.1",
server.port(),
"FAKE\\user",
"secret".toCharArray(),
null
);
final WindowsRemoteCommandResult result;
try (
WinRmCli.LightRemoteOperations remote = new WinRmCli.LightRemoteOperations(
LightWinRMService.createInstance(endpoint, timeout, null, null)
)) {
result = remote.executeCommand("whoami", timeout);
}

assertEquals("Результат", result.getStdout());
assertEquals(0, result.getStatusCode());

// The decoding charset really came from the remote code-page query
assertTrue(
server
.decryptedRequests()
.stream()
.anyMatch(request -> request.contains("SELECT CodeSet FROM Win32_OperatingSystem"))
);
}
}

Expand Down
Loading
Loading