View Javadoc
1   /*
2    * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.internal.transport.sshd.proxy;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertNull;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.util.Arrays;
17  import java.util.LinkedHashMap;
18  import java.util.List;
19  import java.util.Map;
20  
21  import org.junit.Test;
22  
23  public class HttpParserTest {
24  
25  	private static final String STATUS_LINE = "HTTP/1.1. 407 Authentication required";
26  
27  	@Test
28  	public void testEmpty() throws Exception {
29  		String[] lines = { STATUS_LINE };
30  		List<AuthenticationChallenge> challenges = HttpParser
31  				.getAuthenticationHeaders(Arrays.asList(lines),
32  						"WWW-Authenticate:");
33  		assertTrue("No challenges expected", challenges.isEmpty());
34  	}
35  
36  	@Test
37  	public void testRFC7235Example() throws Exception {
38  		// The example from RFC 7235, sec. 4.1, slightly modified ("kind"
39  		// argument with whitespace around '=')
40  		String[] lines = { STATUS_LINE,
41  				"WWW-Authenticate: Newauth realm=\"apps\", type=1  , kind = \t2 ",
42  				"   \t  title=\"Login to \\\"apps\\\"\", Basic realm=\"simple\"" };
43  		List<AuthenticationChallenge> challenges = HttpParser
44  				.getAuthenticationHeaders(Arrays.asList(lines),
45  						"WWW-Authenticate:");
46  		assertEquals("Unexpected number of challenges", 2, challenges.size());
47  		assertNull("No token expected", challenges.get(0).getToken());
48  		assertNull("No token expected", challenges.get(1).getToken());
49  		assertEquals("Unexpected mechanism", "Newauth",
50  				challenges.get(0).getMechanism());
51  		assertEquals("Unexpected mechanism", "Basic",
52  				challenges.get(1).getMechanism());
53  		Map<String, String> expectedArguments = new LinkedHashMap<>();
54  		expectedArguments.put("realm", "apps");
55  		expectedArguments.put("type", "1");
56  		expectedArguments.put("kind", "2");
57  		expectedArguments.put("title", "Login to \"apps\"");
58  		assertEquals("Unexpected arguments", expectedArguments,
59  				challenges.get(0).getArguments());
60  		expectedArguments.clear();
61  		expectedArguments.put("realm", "simple");
62  		assertEquals("Unexpected arguments", expectedArguments,
63  				challenges.get(1).getArguments());
64  	}
65  
66  	@Test
67  	public void testMultipleHeaders() {
68  		String[] lines = { STATUS_LINE,
69  				"Server: Apache",
70  				"WWW-Authenticate: Newauth realm=\"apps\", type=1  , kind = \t2 ",
71  				"   \t  title=\"Login to \\\"apps\\\"\", Basic realm=\"simple\"",
72  				"Content-Type: text/plain",
73  				"WWW-Authenticate: Other 0123456789===  , YetAnother, ",
74  				"WWW-Authenticate: Negotiate   ",
75  				"WWW-Authenticate: Negotiate a87421000492aa874209af8bc028" };
76  		List<AuthenticationChallenge> challenges = HttpParser
77  				.getAuthenticationHeaders(Arrays.asList(lines),
78  						"WWW-Authenticate:");
79  		assertEquals("Unexpected number of challenges", 6, challenges.size());
80  		assertEquals("Mismatched challenge", "Other",
81  				challenges.get(2).getMechanism());
82  		assertEquals("Token expected", "0123456789===",
83  				challenges.get(2).getToken());
84  		assertEquals("Mismatched challenge", "YetAnother",
85  				challenges.get(3).getMechanism());
86  		assertNull("No token expected", challenges.get(3).getToken());
87  		assertTrue("No arguments expected",
88  				challenges.get(3).getArguments().isEmpty());
89  		assertEquals("Mismatched challenge", "Negotiate",
90  				challenges.get(4).getMechanism());
91  		assertNull("No token expected", challenges.get(4).getToken());
92  		assertEquals("Mismatched challenge", "Negotiate",
93  				challenges.get(5).getMechanism());
94  		assertEquals("Token expected", "a87421000492aa874209af8bc028",
95  				challenges.get(5).getToken());
96  	}
97  
98  	@Test
99  	public void testStopOnEmptyLine() {
100 		String[] lines = { STATUS_LINE, "Server: Apache",
101 				"WWW-Authenticate: Newauth realm=\"apps\", type=1  , kind = \t2 ",
102 				"   \t  title=\"Login to \\\"apps\\\"\", Basic realm=\"simple\"",
103 				"Content-Type: text/plain",
104 				"WWW-Authenticate: Other 0123456789===", "",
105 				// Not headers anymore; this would be the body
106 				"WWW-Authenticate: Negotiate   ",
107 				"WWW-Authenticate: Negotiate a87421000492aa874209af8bc028" };
108 		List<AuthenticationChallenge> challenges = HttpParser
109 				.getAuthenticationHeaders(Arrays.asList(lines),
110 						"WWW-Authenticate:");
111 		assertEquals("Unexpected number of challenges", 3, challenges.size());
112 	}
113 }