Skip to content

Commit

Permalink
[issue-#208] - escape double quotes before sending command to MPD (#210)
Browse files Browse the repository at this point in the history
* [issue-#208] - escape double quotes before sending command to MPD

* [issue-#208] - remove comment
  • Loading branch information
finnyb authored Apr 21, 2024
1 parent dfb116a commit 5d769d0
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<groupId>com.inthebacklog</groupId>
<artifactId>javampd</artifactId>
<packaging>jar</packaging>
<version>7.3.0</version>
<version>7.3.1</version>
<name>JavaMPD</name>
<description>A Java API for controlling the Music Player Daemon (MPD)</description>
<url>https://github.com/finnyb/javampd/</url>
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/bff/javampd/command/MPDCommandExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public synchronized List<String> sendCommand(String command) {

@Override
public synchronized List<String> sendCommand(String command, String... params) {
return sendCommand(new MPDCommand(command, params));
return sendCommand(new MPDCommand(command, process(params)));
}

@Override
Expand Down Expand Up @@ -127,4 +127,13 @@ private void checkSocket() {
mpdSocket = createSocket();
}
}

private String[] process(String[] params) {

for (int i = 0; i < params.length; i++) {
params[i] = params[i].replace("\"", "\\\"");
}

return params;
}
}
107 changes: 104 additions & 3 deletions src/test/java/org/bff/javampd/command/MPDCommandExecutorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
import java.util.ArrayList;
import java.util.List;
import org.bff.javampd.server.*;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -21,6 +24,10 @@
class MPDCommandExecutorTest {
@Mock private MPDSocket mpdSocket;
@Mock private MPD mpd;
@Captor private ArgumentCaptor<String> paramCaptor;
@Captor private ArgumentCaptor<String> commandStringCaptor;
@Captor private ArgumentCaptor<MPDCommand> commandCaptor;

@InjectMocks private MPDCommandExecutor commandExecutor;

@Test
Expand All @@ -47,7 +54,11 @@ void testSendCommandString() {

List<String> response = commandExecutor.sendCommand(commandString);

assertEquals(response.get(0), testResponse.get(0));
verify(mpdSocket).sendCommand(commandCaptor.capture());

assertAll(
() -> assertEquals(response.get(0), testResponse.get(0)),
() -> assertEquals("command", commandCaptor.getAllValues().get(0).getCommand()));
}

@Test
Expand All @@ -61,7 +72,11 @@ void testSendCommand() {

List<String> response = commandExecutor.sendCommand(command);

assertEquals(response.get(0), testResponse.get(0));
verify(mpdSocket).sendCommand(commandCaptor.capture());

assertAll(
() -> assertEquals(response.get(0), testResponse.get(0)),
() -> assertEquals("command", commandCaptor.getAllValues().get(0).getCommand()));
}

@Test
Expand All @@ -77,9 +92,90 @@ void testSendCommandWithStringParams() {

List<String> response = commandExecutor.sendCommand(commandString, paramString);

verify(mpdSocket).sendCommand(commandCaptor.capture());

assertAll(
() -> assertEquals(response.get(0), testResponse.get(0)),
() -> assertEquals("command", commandCaptor.getAllValues().get(0).getCommand()),
() -> assertEquals("param", commandCaptor.getAllValues().get(0).getParams().get(0)));

assertEquals(response.get(0), testResponse.get(0));
}

@Test
@DisplayName("Escape double quote search parameter")
void testSendCommandWithQuote() {
String commandString = "command";
String paramString = "param\"";
commandExecutor = new TestMPDCommandExecutor();
commandExecutor.setMpd(mpd);

List<String> testResponse = new ArrayList<>();
testResponse.add("testResponse");
when(mpdSocket.sendCommand(new MPDCommand(commandString, "param\\\"")))
.thenReturn(testResponse);

List<String> response = commandExecutor.sendCommand(commandString, paramString);

verify(mpdSocket).sendCommand(commandCaptor.capture());

assertAll(
() -> assertEquals(response.get(0), testResponse.get(0)),
() -> assertEquals("command", commandCaptor.getAllValues().get(0).getCommand()),
() -> assertEquals("param\\\"", commandCaptor.getAllValues().get(0).getParams().get(0)));
}

@Test
@DisplayName("Escape multiple double quotes search parameter")
void testSendCommandWithMultipleQuotes() {
String commandString = "command";
String paramString = "pa\"ram\"";
commandExecutor = new TestMPDCommandExecutor();
commandExecutor.setMpd(mpd);

List<String> testResponse = new ArrayList<>();
testResponse.add("testResponse");
when(mpdSocket.sendCommand(new MPDCommand(commandString, "pa\\\"ram\\\"")))
.thenReturn(testResponse);

List<String> response = commandExecutor.sendCommand(commandString, paramString);

verify(mpdSocket).sendCommand(commandCaptor.capture());

assertAll(
() -> assertEquals(response.get(0), testResponse.get(0)),
() -> assertEquals("command", commandCaptor.getAllValues().get(0).getCommand()),
() ->
assertEquals("pa\\\"ram\\\"", commandCaptor.getAllValues().get(0).getParams().get(0)));
}

@Test
@DisplayName("Escape multiple double quotes for multiple search parameters")
void testSendCommandsWithMultipleQuotes() {
String commandString = "command";
String paramString1 = "pa\"ram\"1";
String paramString2 = "pa\"ram\"2";
commandExecutor = new TestMPDCommandExecutor();
commandExecutor.setMpd(mpd);

List<String> testResponse = new ArrayList<>();
testResponse.add("testResponse");
when(mpdSocket.sendCommand(new MPDCommand(commandString, "pa\\\"ram\\\"1", "pa\\\"ram\\\"2")))
.thenReturn(testResponse);

List<String> response = commandExecutor.sendCommand(commandString, paramString1, paramString2);

verify(mpdSocket).sendCommand(commandCaptor.capture());

assertAll(
() -> assertEquals(response.get(0), testResponse.get(0)),
() -> assertEquals("command", commandCaptor.getAllValues().get(0).getCommand()),
() ->
assertEquals("pa\\\"ram\\\"1", commandCaptor.getAllValues().get(0).getParams().get(0)),
() ->
assertEquals("pa\\\"ram\\\"2", commandCaptor.getAllValues().get(0).getParams().get(1)));
}

@Test
void testSendCommandWithIntegerParams() {
String commandString = "command";
Expand All @@ -93,7 +189,12 @@ void testSendCommandWithIntegerParams() {

List<String> response = commandExecutor.sendCommand(commandString, paramInteger);

assertEquals(response.get(0), testResponse.get(0));
verify(mpdSocket).sendCommand(commandCaptor.capture());

assertAll(
() -> assertEquals(response.get(0), testResponse.get(0)),
() -> assertEquals("command", commandCaptor.getAllValues().get(0).getCommand()),
() -> assertEquals("1", commandCaptor.getAllValues().get(0).getParams().get(0)));
}

@Test
Expand Down

0 comments on commit 5d769d0

Please sign in to comment.