import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import java.util.Arrays;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
+import org.eclipse.jgit.errors.PackProtocolException;
import org.junit.Test;
public class FirstWantTest {
@Test
- public void testFirstWantWithOptions() {
+ public void testFirstWantWithOptions() throws PackProtocolException {
String line = "want b9d4d1eb2f93058814480eae9e1b67550f46ee38 "
+ "no-progress include-tag ofs-delta agent=JGit/unknown";
}
@Test
- public void testFirstWantWithoutOptions() {
+ public void testFirstWantWithoutOptions() throws PackProtocolException {
String line = "want b9d4d1eb2f93058814480eae9e1b67550f46ee38";
FirstWant r = FirstWant.fromLine(line);
r.getLine());
assertTrue(r.getCapabilities().isEmpty());
}
+
+ private String makeFirstWantLine(String capability) {
+ return String.format("want b9d4d1eb2f93058814480eae9e1b67550f46ee38 %s", capability);
+ }
+
+ @Test
+ public void testFirstWantNoWhitespace() {
+ try {
+ FirstWant.fromLine(
+ "want b9d4d1eb2f93058814480eae9e1b67550f400000capability");
+ fail("Accepting first want line without SP between oid and first capability");
+ } catch (PackProtocolException e) {
+ // pass
+ }
+ }
+
+ @Test
+ public void testFirstWantOnlyWhitespace() throws PackProtocolException {
+ FirstWant r = FirstWant
+ .fromLine("want b9d4d1eb2f93058814480eae9e1b67550f46ee38 ");
+ assertEquals("want b9d4d1eb2f93058814480eae9e1b67550f46ee38",
+ r.getLine());
+ }
+
+ @Test
+ public void testFirstWantValidCapabilityNames()
+ throws PackProtocolException {
+ List<String> validNames = Arrays.asList(
+ "c", "cap", "C", "CAP", "1", "1cap", "cap-64k_test",
+ "-", "-cap",
+ "_", "_cap", "agent=pack.age/Version");
+
+ for (String capability: validNames) {
+ FirstWant r = FirstWant.fromLine(makeFirstWantLine(capability));
+ assertEquals(r.getCapabilities().size(), 1);
+ assertTrue(r.getCapabilities().contains(capability));
+ }
+ }
}
userConfigFileInvalid=User config file {0} invalid {1}
validatingGitModules=Validating .gitmodules files
walkFailure=Walk failure.
+wantNoSpaceWithCapabilities=No space between oid and first capability in first want line
wantNotValid=want {0} not valid
weeksAgo={0} weeks ago
windowSizeMustBeLesserThanLimit=Window size must be < limit
/***/ public String userConfigFileInvalid;
/***/ public String validatingGitModules;
/***/ public String walkFailure;
+ /***/ public String wantNoSpaceWithCapabilities;
/***/ public String wantNotValid;
/***/ public String weeksAgo;
/***/ public String windowSizeMustBeLesserThanLimit;
*/
package org.eclipse.jgit.internal.transport.parser;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
+import org.eclipse.jgit.errors.PackProtocolException;
+import org.eclipse.jgit.internal.JGitText;
+
/**
* In the pack negotiation phase (protocol v0/v1), the client sends a list of
* wants. The first "want" line is special, as it (can) have a list of
* @param line
* line from the client.
* @return an instance of FirstWant
+ * @throws PackProtocolException
+ * if the line doesn't follow the protocol format.
*/
- public static FirstWant fromLine(String line) {
+ public static FirstWant fromLine(String line) throws PackProtocolException {
String wantLine;
Set<String> capabilities;
if (line.length() > 45) {
- final HashSet<String> opts = new HashSet<>();
String opt = line.substring(45);
- if (opt.startsWith(" ")) { //$NON-NLS-1$
- opt = opt.substring(1);
- }
- for (String c : opt.split(" ")) { //$NON-NLS-1$
- opts.add(c);
+ if (!opt.startsWith(" ")) { //$NON-NLS-1$
+ throw new PackProtocolException(JGitText.get().wantNoSpaceWithCapabilities);
}
+ opt = opt.substring(1);
+ HashSet<String> opts = new HashSet<>(
+ Arrays.asList(opt.split(" "))); //$NON-NLS-1$
wantLine = line.substring(0, 45);
capabilities = Collections.unmodifiableSet(opts);
} else {
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.UncheckedIOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
* line from the client.
*/
public FirstLine(String line) {
- firstWant = FirstWant.fromLine(line);
+ try {
+ firstWant = FirstWant.fromLine(line);
+ } catch (PackProtocolException e) {
+ throw new UncheckedIOException(e);
+ }
}
/** @return non-capabilities part of the line. */