View Javadoc
1   /*
2    * Copyright (C) 2015 Thomas Wolf <thomas.wolf@paranor.ch>
3    *
4    * This program and the accompanying materials are made available
5    * under the terms of the Eclipse Distribution License v1.0 which
6    * accompanies this distribution, is reproduced below, and is
7    * available at http://www.eclipse.org/org/documents/edl-v10.php
8    *
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or
12   * without modification, are permitted provided that the following
13   * conditions are met:
14   *
15   * - Redistributions of source code must retain the above copyright
16   *   notice, this list of conditions and the following disclaimer.
17   *
18   * - Redistributions in binary form must reproduce the above
19   *   copyright notice, this list of conditions and the following
20   *   disclaimer in the documentation and/or other materials provided
21   *   with the distribution.
22   *
23   * - Neither the name of the Eclipse Foundation, Inc. nor the
24   *   names of its contributors may be used to endorse or promote
25   *   products derived from this software without specific prior
26   *   written permission.
27   *
28   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
30   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
33   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
40   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41   */
42  package org.eclipse.jgit.indexdiff;
43  
44  import static org.junit.Assert.assertArrayEquals;
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertNotNull;
47  import static org.junit.Assert.assertTrue;
48  import static org.junit.Assert.fail;
49  import static org.junit.Assume.assumeTrue;
50  
51  import java.io.BufferedOutputStream;
52  import java.io.BufferedReader;
53  import java.io.File;
54  import java.io.FileOutputStream;
55  import java.io.IOException;
56  import java.io.InputStream;
57  import java.io.InputStreamReader;
58  import java.io.OutputStream;
59  import java.io.OutputStreamWriter;
60  import java.io.Writer;
61  import java.lang.reflect.InvocationTargetException;
62  import java.lang.reflect.Method;
63  import java.nio.charset.StandardCharsets;
64  import java.nio.file.Files;
65  import java.nio.file.Path;
66  import java.nio.file.Paths;
67  import java.util.Collections;
68  
69  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
70  import org.eclipse.jgit.lib.Constants;
71  import org.eclipse.jgit.lib.IndexDiff;
72  import org.eclipse.jgit.lib.Repository;
73  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
74  import org.eclipse.jgit.treewalk.FileTreeIterator;
75  import org.eclipse.jgit.treewalk.WorkingTreeIterator;
76  import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
77  import org.eclipse.jgit.util.FS;
78  import org.eclipse.jgit.util.SystemReader;
79  import org.junit.Before;
80  import org.junit.Test;
81  
82  /**
83   * MacOS-only test for dealing with symlinks in IndexDiff. Recreates using cgit
84   * a test repository prepared with git 2.2.1 on MacOS 10.7.5 containing some
85   * symlinks. Examines a symlink pointing to a file named "äéü.txt" (should be
86   * encoded as UTF-8 NFC), changes it through Java, examines it again to verify
87   * it's been changed to UTF-8 NFD, and finally calculates an IndexDiff.
88   */
89  public class IndexDiffWithSymlinkTest extends LocalDiskRepositoryTestCase {
90  
91  	private static final String FILEREPO = "filerepo";
92  
93  	private static final String TESTFOLDER = "testfolder";
94  
95  	private static final String TESTTARGET = "äéü.txt";
96  
97  	private static final String TESTLINK = "aeu.txt";
98  
99  	private static final byte[] NFC = // "äéü.txt" in NFC
100 	{ -61, -92, -61, -87, -61, -68, 46, 116, 120, 116 };
101 
102 	private static final byte[] NFD = // "äéü.txt" in NFD
103 	{ 97, -52, -120, 101, -52, -127, 117, -52, -120, 46, 116, 120, 116 };
104 
105 	private File testRepoDir;
106 
107 	@Override
108 	@Before
109 	public void setUp() throws Exception {
110 		assumeTrue(SystemReader.getInstance().isMacOS()
111 				&& FS.DETECTED.supportsSymlinks());
112 		super.setUp();
113 		File testDir = createTempDirectory(this.getClass().getSimpleName());
114 		InputStream in = this.getClass().getClassLoader().getResourceAsStream(
115 				this.getClass().getPackage().getName().replace('.', '/') + '/'
116 						+ FILEREPO + ".txt");
117 		assertNotNull("Test repo file not found", in);
118 		try {
119 			testRepoDir = restoreGitRepo(in, testDir, FILEREPO);
120 		} finally {
121 			in.close();
122 		}
123 	}
124 
125 	private File restoreGitRepo(InputStream in, File testDir, String name)
126 			throws Exception {
127 		File exportedTestRepo = new File(testDir, name + ".txt");
128 		copy(in, exportedTestRepo);
129 		// Use CGit to restore
130 		File restoreScript = new File(testDir, name + ".sh");
131 		try (OutputStream out = new BufferedOutputStream(
132 				new FileOutputStream(restoreScript));
133 				Writer writer = new OutputStreamWriter(out,
134 						StandardCharsets.UTF_8)) {
135 			writer.write("echo `which git` 1>&2\n");
136 			writer.write("echo `git --version` 1>&2\n");
137 			writer.write("git init " + name + " && \\\n");
138 			writer.write("cd ./" + name + " && \\\n");
139 			writer.write("git fast-import < ../" + name + ".txt && \\\n");
140 			writer.write("git checkout -f\n");
141 		}
142 		String[] cmd = { "/bin/sh", "./" + name + ".sh" };
143 		int exitCode;
144 		String stdErr;
145 		Process process = Runtime.getRuntime().exec(cmd, null, testDir);
146 		try (InputStream stdOutStream = process.getInputStream();
147 				InputStream stdErrStream = process.getErrorStream();
148 				OutputStream stdInStream = process.getOutputStream()) {
149 			readStream(stdOutStream);
150 			stdErr = readStream(stdErrStream);
151 			process.waitFor();
152 			exitCode = process.exitValue();
153 		}
154 		if (exitCode != 0) {
155 			fail("cgit repo restore returned " + exitCode + '\n' + stdErr);
156 		}
157 		return new File(new File(testDir, name), Constants.DOT_GIT);
158 	}
159 
160 	private void copy(InputStream from, File to) throws IOException {
161 		try (OutputStream out = new FileOutputStream(to)) {
162 			byte[] buffer = new byte[4096];
163 			int n;
164 			while ((n = from.read(buffer)) > 0) {
165 				out.write(buffer, 0, n);
166 			}
167 		}
168 	}
169 
170 	private String readStream(InputStream stream) throws IOException {
171 		try (BufferedReader in = new BufferedReader(
172 				new InputStreamReader(stream))) {
173 			StringBuilder out = new StringBuilder();
174 			String line;
175 			while ((line = in.readLine()) != null) {
176 				out.append(line).append('\n');
177 			}
178 			return out.toString();
179 		}
180 	}
181 
182 	@Test
183 	public void testSymlinkWithEncodingDifference() throws Exception {
184 		try (Repository testRepo = FileRepositoryBuilder.create(testRepoDir)) {
185 			File workingTree = testRepo.getWorkTree();
186 			File symLink = new File(new File(workingTree, TESTFOLDER),
187 					TESTLINK);
188 			// Read the symlink as it was created by cgit
189 			Path linkTarget = Files.readSymbolicLink(symLink.toPath());
190 			assertEquals("Unexpected link target", TESTTARGET,
191 					linkTarget.toString());
192 			byte[] raw = rawPath(linkTarget);
193 			if (raw != null) {
194 				assertArrayEquals("Expected an NFC link target", NFC, raw);
195 			}
196 			// Now re-create that symlink through Java
197 			assertTrue("Could not delete symlink", symLink.delete());
198 			Files.createSymbolicLink(symLink.toPath(), Paths.get(TESTTARGET));
199 			// Read it again
200 			linkTarget = Files.readSymbolicLink(symLink.toPath());
201 			assertEquals("Unexpected link target", TESTTARGET,
202 					linkTarget.toString());
203 			raw = rawPath(linkTarget);
204 			if (raw != null) {
205 				assertArrayEquals("Expected an NFD link target", NFD, raw);
206 			}
207 			// Do the indexdiff
208 			WorkingTreeIterator iterator = new FileTreeIterator(testRepo);
209 			IndexDiff diff = new IndexDiff(testRepo, Constants.HEAD, iterator);
210 			diff.setFilter(PathFilterGroup.createFromStrings(
211 					Collections.singleton(TESTFOLDER + '/' + TESTLINK)));
212 			diff.diff();
213 			// We're testing that this does NOT throw "EOFException: Short read
214 			// of block." The diff will not report any modified files -- the
215 			// link modification is not visible to JGit, which always works with
216 			// the Java internal NFC encoding. CGit does report the link as an
217 			// unstaged modification here, though.
218 		}
219 	}
220 
221 	private byte[] rawPath(Path p) {
222 		try {
223 			Method method = p.getClass().getDeclaredMethod("asByteArray");
224 			if (method != null) {
225 				method.setAccessible(true);
226 				return (byte[]) method.invoke(p);
227 			}
228 		} catch (NoSuchMethodException | IllegalAccessException
229 				| IllegalArgumentException | InvocationTargetException e) {
230 			// Ignore and fall through.
231 		}
232 		return null;
233 	}
234 }