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