View Javadoc
1   /*
2    * Copyright (C) 2012, Robin Rosenberg 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  
11  package org.eclipse.jgit.util.io;
12  
13  import static java.nio.charset.StandardCharsets.UTF_8;
14  
15  import java.io.ByteArrayInputStream;
16  import java.io.ByteArrayOutputStream;
17  import java.io.IOException;
18  import java.io.InputStream;
19  
20  import org.junit.Assert;
21  import org.junit.Test;
22  
23  public class AutoCRLFInputStreamTest {
24  
25  	@Test
26  	public void test() throws IOException {
27  		assertNoCrLf("", "");
28  		assertNoCrLf("\r", "\r");
29  		assertNoCrLf("\r\n", "\n");
30  		assertNoCrLf("\r\n", "\r\n");
31  		assertNoCrLf("\r\r", "\r\r");
32  		assertNoCrLf("\r\n\r", "\n\r");
33  		assertNoCrLf("\r\n\r\r", "\r\n\r\r");
34  		assertNoCrLf("\r\n\r\n", "\r\n\r\n");
35  		assertNoCrLf("\r\n\r\n\r", "\n\r\n\r");
36  		assertNoCrLf("\0\n", "\0\n");
37  	}
38  
39  	@Test
40  	public void testBoundary() throws IOException {
41  		for (int i = AutoCRLFInputStream.BUFFER_SIZE - 10; i < AutoCRLFInputStream.BUFFER_SIZE + 10; i++) {
42  			String s1 = Strings.repeat("a", i);
43  			assertNoCrLf(s1, s1);
44  			String s2 = Strings.repeat("\0", i);
45  			assertNoCrLf(s2, s2);
46  		}
47  	}
48  
49  	private void assertNoCrLf(String string, String string2) throws IOException {
50  		assertNoCrLfHelper(string, string2);
51  		// \u00e5 = LATIN SMALL LETTER A WITH RING ABOVE
52  		// the byte value is negative
53  		assertNoCrLfHelper("\u00e5" + string, "\u00e5" + string2);
54  		assertNoCrLfHelper("\u00e5" + string + "\u00e5", "\u00e5" + string2
55  				+ "\u00e5");
56  		assertNoCrLfHelper(string + "\u00e5", string2 + "\u00e5");
57  	}
58  
59  	private void assertNoCrLfHelper(String expect, String input)
60  			throws IOException {
61  		byte[] inbytes = input.getBytes(UTF_8);
62  		byte[] expectBytes = expect.getBytes(UTF_8);
63  		for (int i = 0; i < 5; ++i) {
64  			byte[] buf = new byte[i];
65  			try (ByteArrayInputStream bis = new ByteArrayInputStream(inbytes);
66  					InputStream in = new AutoCRLFInputStream(bis, true);
67  					ByteArrayOutputStream out = new ByteArrayOutputStream()) {
68  				if (i > 0) {
69  					int n;
70  					while ((n = in.read(buf)) >= 0) {
71  						out.write(buf, 0, n);
72  					}
73  				} else {
74  					int c;
75  					while ((c = in.read()) != -1)
76  						out.write(c);
77  				}
78  				out.flush();
79  				byte[] actualBytes = out.toByteArray();
80  				Assert.assertEquals("bufsize=" + i, encode(expectBytes),
81  						encode(actualBytes));
82  			}
83  		}
84  	}
85  
86  	String encode(byte[] in) {
87  		StringBuilder str = new StringBuilder();
88  		for (byte b : in) {
89  			if (b < 32)
90  				str.append(0xFF & b);
91  			else {
92  				str.append("'");
93  				str.append((char) b);
94  				str.append("'");
95  			}
96  			str.append(' ');
97  		}
98  		return str.toString();
99  	}
100 
101 }