diff --git a/pom.xml b/pom.xml
index ffad1a70..fdc3308f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
com.inthebacklog
javampd
jar
- 7.3.0
+ 7.3.1
JavaMPD
A Java API for controlling the Music Player Daemon (MPD)
https://github.com/finnyb/javampd/
diff --git a/src/main/java/org/bff/javampd/command/MPDCommandExecutor.java b/src/main/java/org/bff/javampd/command/MPDCommandExecutor.java
index 13bee213..ba6ec364 100644
--- a/src/main/java/org/bff/javampd/command/MPDCommandExecutor.java
+++ b/src/main/java/org/bff/javampd/command/MPDCommandExecutor.java
@@ -36,7 +36,7 @@ public synchronized List sendCommand(String command) {
@Override
public synchronized List sendCommand(String command, String... params) {
- return sendCommand(new MPDCommand(command, params));
+ return sendCommand(new MPDCommand(command, process(params)));
}
@Override
@@ -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;
+ }
}
diff --git a/src/test/java/org/bff/javampd/command/MPDCommandExecutorTest.java b/src/test/java/org/bff/javampd/command/MPDCommandExecutorTest.java
index 5ac54a6a..9b282e4c 100644
--- a/src/test/java/org/bff/javampd/command/MPDCommandExecutorTest.java
+++ b/src/test/java/org/bff/javampd/command/MPDCommandExecutorTest.java
@@ -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;
@@ -21,6 +24,10 @@
class MPDCommandExecutorTest {
@Mock private MPDSocket mpdSocket;
@Mock private MPD mpd;
+ @Captor private ArgumentCaptor paramCaptor;
+ @Captor private ArgumentCaptor commandStringCaptor;
+ @Captor private ArgumentCaptor commandCaptor;
+
@InjectMocks private MPDCommandExecutor commandExecutor;
@Test
@@ -47,7 +54,11 @@ void testSendCommandString() {
List 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
@@ -61,7 +72,11 @@ void testSendCommand() {
List 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
@@ -77,9 +92,90 @@ void testSendCommandWithStringParams() {
List 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 testResponse = new ArrayList<>();
+ testResponse.add("testResponse");
+ when(mpdSocket.sendCommand(new MPDCommand(commandString, "param\\\"")))
+ .thenReturn(testResponse);
+
+ List 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 testResponse = new ArrayList<>();
+ testResponse.add("testResponse");
+ when(mpdSocket.sendCommand(new MPDCommand(commandString, "pa\\\"ram\\\"")))
+ .thenReturn(testResponse);
+
+ List 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 testResponse = new ArrayList<>();
+ testResponse.add("testResponse");
+ when(mpdSocket.sendCommand(new MPDCommand(commandString, "pa\\\"ram\\\"1", "pa\\\"ram\\\"2")))
+ .thenReturn(testResponse);
+
+ List 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";
@@ -93,7 +189,12 @@ void testSendCommandWithIntegerParams() {
List 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