View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
3    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2007, Shawn O. Pearce <spearce@spearce.org>
5    * and other copyright owners as documented in the project's IP log.
6    *
7    * This program and the accompanying materials are made available
8    * under the terms of the Eclipse Distribution License v1.0 which
9    * accompanies this distribution, is reproduced below, and is
10   * available at http://www.eclipse.org/org/documents/edl-v10.php
11   *
12   * All rights reserved.
13   *
14   * Redistribution and use in source and binary forms, with or
15   * without modification, are permitted provided that the following
16   * conditions are met:
17   *
18   * - Redistributions of source code must retain the above copyright
19   *   notice, this list of conditions and the following disclaimer.
20   *
21   * - Redistributions in binary form must reproduce the above
22   *   copyright notice, this list of conditions and the following
23   *   disclaimer in the documentation and/or other materials provided
24   *   with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the
27   *   names of its contributors may be used to endorse or promote
28   *   products derived from this software without specific prior
29   *   written permission.
30   *
31   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44   */
45  
46  package org.eclipse.jgit.junit;
47  
48  import static org.junit.Assert.assertFalse;
49  import static org.junit.Assert.fail;
50  
51  import java.io.File;
52  import java.io.IOException;
53  import java.util.ArrayList;
54  import java.util.Collections;
55  import java.util.HashMap;
56  import java.util.HashSet;
57  import java.util.List;
58  import java.util.Map;
59  import java.util.Set;
60  import java.util.TreeSet;
61  
62  import org.eclipse.jgit.dircache.DirCache;
63  import org.eclipse.jgit.dircache.DirCacheEntry;
64  import org.eclipse.jgit.internal.storage.file.FileRepository;
65  import org.eclipse.jgit.lib.ConfigConstants;
66  import org.eclipse.jgit.lib.Constants;
67  import org.eclipse.jgit.lib.ObjectId;
68  import org.eclipse.jgit.lib.PersonIdent;
69  import org.eclipse.jgit.lib.Repository;
70  import org.eclipse.jgit.lib.RepositoryCache;
71  import org.eclipse.jgit.storage.file.FileBasedConfig;
72  import org.eclipse.jgit.storage.file.WindowCacheConfig;
73  import org.eclipse.jgit.util.FS;
74  import org.eclipse.jgit.util.FileUtils;
75  import org.eclipse.jgit.util.SystemReader;
76  import org.junit.After;
77  import org.junit.Before;
78  
79  /**
80   * JUnit TestCase with specialized support for temporary local repository.
81   * <p>
82   * A temporary directory is created for each test, allowing each test to use a
83   * fresh environment. The temporary directory is cleaned up after the test ends.
84   * <p>
85   * Callers should not use {@link RepositoryCache} from within these tests as it
86   * may wedge file descriptors open past the end of the test.
87   * <p>
88   * A system property {@code jgit.junit.usemmap} defines whether memory mapping
89   * is used. Memory mapping has an effect on the file system, in that memory
90   * mapped files in Java cannot be deleted as long as the mapped arrays have not
91   * been reclaimed by the garbage collector. The programmer cannot control this
92   * with precision, so temporary files may hang around longer than desired during
93   * a test, or tests may fail altogether if there is insufficient file
94   * descriptors or address space for the test process.
95   */
96  public abstract class LocalDiskRepositoryTestCase {
97  	private static final boolean useMMAP = "true".equals(System
98  			.getProperty("jgit.junit.usemmap"));
99  
100 	/** A fake (but stable) identity for author fields in the test. */
101 	protected PersonIdent author;
102 
103 	/** A fake (but stable) identity for committer fields in the test. */
104 	protected PersonIdent committer;
105 
106 	/**
107 	 * A {@link SystemReader} used to coordinate time, envars, etc.
108 	 * @since 4.2
109 	 */
110 	protected MockSystemReader mockSystemReader;
111 
112 	private final Set<Repository> toClose = new HashSet<>();
113 	private File tmp;
114 
115 	@Before
116 	public void setUp() throws Exception {
117 		tmp = File.createTempFile("jgit_test_", "_tmp");
118 		CleanupThread.deleteOnShutdown(tmp);
119 		if (!tmp.delete() || !tmp.mkdir())
120 			throw new IOException("Cannot create " + tmp);
121 
122 		mockSystemReader = new MockSystemReader();
123 		mockSystemReader.userGitConfig = new FileBasedConfig(new File(tmp,
124 				"usergitconfig"), FS.DETECTED);
125 		// We have to set autoDetach to false for tests, because tests expect to be able
126 		// to clean up by recursively removing the repository, and background GC might be
127 		// in the middle of writing or deleting files, which would disrupt this.
128 		mockSystemReader.userGitConfig.setBoolean(ConfigConstants.CONFIG_GC_SECTION,
129 				null, ConfigConstants.CONFIG_KEY_AUTODETACH, false);
130 		mockSystemReader.userGitConfig.save();
131 		ceilTestDirectories(getCeilings());
132 		SystemReader.setInstance(mockSystemReader);
133 
134 		author = new PersonIdent("J. Author", "jauthor@example.com");
135 		committer = new PersonIdent("J. Committer", "jcommitter@example.com");
136 
137 		final WindowCacheConfig c = new WindowCacheConfig();
138 		c.setPackedGitLimit(128 * WindowCacheConfig.KB);
139 		c.setPackedGitWindowSize(8 * WindowCacheConfig.KB);
140 		c.setPackedGitMMAP(useMMAP);
141 		c.setDeltaBaseCacheLimit(8 * WindowCacheConfig.KB);
142 		c.install();
143 	}
144 
145 	protected File getTemporaryDirectory() {
146 		return tmp.getAbsoluteFile();
147 	}
148 
149 	protected List<File> getCeilings() {
150 		return Collections.singletonList(getTemporaryDirectory());
151 	}
152 
153 	private void ceilTestDirectories(List<File> ceilings) {
154 		mockSystemReader.setProperty(Constants.GIT_CEILING_DIRECTORIES_KEY, makePath(ceilings));
155 	}
156 
157 	private static String makePath(List<?> objects) {
158 		final StringBuilder stringBuilder = new StringBuilder();
159 		for (Object object : objects) {
160 			if (stringBuilder.length() > 0)
161 				stringBuilder.append(File.pathSeparatorChar);
162 			stringBuilder.append(object.toString());
163 		}
164 		return stringBuilder.toString();
165 	}
166 
167 	@After
168 	public void tearDown() throws Exception {
169 		RepositoryCache.clear();
170 		for (Repository r : toClose)
171 			r.close();
172 		toClose.clear();
173 
174 		// Since memory mapping is controlled by the GC we need to
175 		// tell it this is a good time to clean up and unlock
176 		// memory mapped files.
177 		//
178 		if (useMMAP)
179 			System.gc();
180 		if (tmp != null)
181 			recursiveDelete(tmp, false, true);
182 		if (tmp != null && !tmp.exists())
183 			CleanupThread.removed(tmp);
184 
185 		SystemReader.setInstance(null);
186 	}
187 
188 	/** Increment the {@link #author} and {@link #committer} times. */
189 	protected void tick() {
190 		mockSystemReader.tick(5 * 60);
191 		final long now = mockSystemReader.getCurrentTime();
192 		final int tz = mockSystemReader.getTimezone(now);
193 
194 		author = new PersonIdent(author, now, tz);
195 		committer = new PersonIdent(committer, now, tz);
196 	}
197 
198 	/**
199 	 * Recursively delete a directory, failing the test if the delete fails.
200 	 *
201 	 * @param dir
202 	 *            the recursively directory to delete, if present.
203 	 */
204 	protected void recursiveDelete(final File dir) {
205 		recursiveDelete(dir, false, true);
206 	}
207 
208 	private static boolean recursiveDelete(final File dir,
209 			boolean silent, boolean failOnError) {
210 		assert !(silent && failOnError);
211 		if (!dir.exists())
212 			return silent;
213 		final File[] ls = dir.listFiles();
214 		if (ls != null)
215 			for (int k = 0; k < ls.length; k++) {
216 				final File e = ls[k];
217 				if (e.isDirectory())
218 					silent = recursiveDelete(e, silent, failOnError);
219 				else if (!e.delete()) {
220 					if (!silent)
221 						reportDeleteFailure(failOnError, e);
222 					silent = !failOnError;
223 				}
224 			}
225 		if (!dir.delete()) {
226 			if (!silent)
227 				reportDeleteFailure(failOnError, dir);
228 			silent = !failOnError;
229 		}
230 		return silent;
231 	}
232 
233 	private static void reportDeleteFailure(boolean failOnError, File e) {
234 		String severity = failOnError ? "ERROR" : "WARNING";
235 		String msg = severity + ": Failed to delete " + e;
236 		if (failOnError)
237 			fail(msg);
238 		else
239 			System.err.println(msg);
240 	}
241 
242 	public static final int MOD_TIME = 1;
243 
244 	public static final int SMUDGE = 2;
245 
246 	public static final int LENGTH = 4;
247 
248 	public static final int CONTENT_ID = 8;
249 
250 	public static final int CONTENT = 16;
251 
252 	public static final int ASSUME_UNCHANGED = 32;
253 
254 	/**
255 	 * Represent the state of the index in one String. This representation is
256 	 * useful when writing tests which do assertions on the state of the index.
257 	 * By default information about path, mode, stage (if different from 0) is
258 	 * included. A bitmask controls which additional info about
259 	 * modificationTimes, smudge state and length is included.
260 	 * <p>
261 	 * The format of the returned string is described with this BNF:
262 	 *
263 	 * <pre>
264 	 * result = ( "[" path mode stage? time? smudge? length? sha1? content? "]" )* .
265 	 * mode = ", mode:" number .
266 	 * stage = ", stage:" number .
267 	 * time = ", time:t" timestamp-index .
268 	 * smudge = "" | ", smudged" .
269 	 * length = ", length:" number .
270 	 * sha1 = ", sha1:" hex-sha1 .
271 	 * content = ", content:" blob-data .
272 	 * </pre>
273 	 *
274 	 * 'stage' is only presented when the stage is different from 0. All
275 	 * reported time stamps are mapped to strings like "t0", "t1", ... "tn". The
276 	 * smallest reported time-stamp will be called "t0". This allows to write
277 	 * assertions against the string although the concrete value of the time
278 	 * stamps is unknown.
279 	 *
280 	 * @param repo
281 	 *            the repository the index state should be determined for
282 	 *
283 	 * @param includedOptions
284 	 *            a bitmask constructed out of the constants {@link #MOD_TIME},
285 	 *            {@link #SMUDGE}, {@link #LENGTH}, {@link #CONTENT_ID} and
286 	 *            {@link #CONTENT} controlling which info is present in the
287 	 *            resulting string.
288 	 * @return a string encoding the index state
289 	 * @throws IllegalStateException
290 	 * @throws IOException
291 	 */
292 	public static String indexState(Repository repo, int includedOptions)
293 			throws IllegalStateException, IOException {
294 		DirCache dc = repo.readDirCache();
295 		StringBuilder sb = new StringBuilder();
296 		TreeSet<Long> timeStamps = new TreeSet<>();
297 
298 		// iterate once over the dircache just to collect all time stamps
299 		if (0 != (includedOptions & MOD_TIME)) {
300 			for (int i=0; i<dc.getEntryCount(); ++i)
301 				timeStamps.add(Long.valueOf(dc.getEntry(i).getLastModified()));
302 		}
303 
304 		// iterate again, now produce the result string
305 		for (int i=0; i<dc.getEntryCount(); ++i) {
306 			DirCacheEntry entry = dc.getEntry(i);
307 			sb.append("["+entry.getPathString()+", mode:" + entry.getFileMode());
308 			int stage = entry.getStage();
309 			if (stage != 0)
310 				sb.append(", stage:" + stage);
311 			if (0 != (includedOptions & MOD_TIME)) {
312 				sb.append(", time:t"+
313 						timeStamps.headSet(Long.valueOf(entry.getLastModified())).size());
314 			}
315 			if (0 != (includedOptions & SMUDGE))
316 				if (entry.isSmudged())
317 					sb.append(", smudged");
318 			if (0 != (includedOptions & LENGTH))
319 				sb.append(", length:"
320 						+ Integer.toString(entry.getLength()));
321 			if (0 != (includedOptions & CONTENT_ID))
322 				sb.append(", sha1:" + ObjectId.toString(entry.getObjectId()));
323 			if (0 != (includedOptions & CONTENT)) {
324 				sb.append(", content:"
325 						+ new String(repo.open(entry.getObjectId(),
326 						Constants.OBJ_BLOB).getCachedBytes(), "UTF-8"));
327 			}
328 			if (0 != (includedOptions & ASSUME_UNCHANGED))
329 				sb.append(", assume-unchanged:"
330 						+ Boolean.toString(entry.isAssumeValid()));
331 			sb.append("]");
332 		}
333 		return sb.toString();
334 	}
335 
336 
337 	/**
338 	 * Creates a new empty bare repository.
339 	 *
340 	 * @return the newly created repository, opened for access
341 	 * @throws IOException
342 	 *             the repository could not be created in the temporary area
343 	 */
344 	protected FileRepository createBareRepository() throws IOException {
345 		return createRepository(true /* bare */);
346 	}
347 
348 	/**
349 	 * Creates a new empty repository within a new empty working directory.
350 	 *
351 	 * @return the newly created repository, opened for access
352 	 * @throws IOException
353 	 *             the repository could not be created in the temporary area
354 	 */
355 	protected FileRepository createWorkRepository() throws IOException {
356 		return createRepository(false /* not bare */);
357 	}
358 
359 	/**
360 	 * Creates a new empty repository.
361 	 *
362 	 * @param bare
363 	 *            true to create a bare repository; false to make a repository
364 	 *            within its working directory
365 	 * @return the newly created repository, opened for access
366 	 * @throws IOException
367 	 *             the repository could not be created in the temporary area
368 	 */
369 	private FileRepository createRepository(boolean bare)
370 			throws IOException {
371 		return createRepository(bare, true /* auto close */);
372 	}
373 
374 	/**
375 	 * Creates a new empty repository.
376 	 *
377 	 * @param bare
378 	 *            true to create a bare repository; false to make a repository
379 	 *            within its working directory
380 	 * @param autoClose
381 	 *            auto close the repository in #tearDown
382 	 * @return the newly created repository, opened for access
383 	 * @throws IOException
384 	 *             the repository could not be created in the temporary area
385 	 */
386 	public FileRepository createRepository(boolean bare, boolean autoClose)
387 			throws IOException {
388 		File gitdir = createUniqueTestGitDir(bare);
389 		FileRepository db = new FileRepository(gitdir);
390 		assertFalse(gitdir.exists());
391 		db.create(bare);
392 		if (autoClose) {
393 			addRepoToClose(db);
394 		}
395 		return db;
396 	}
397 
398 	/**
399 	 * Adds a repository to the list of repositories which is closed at the end
400 	 * of the tests
401 	 *
402 	 * @param r
403 	 *            the repository to be closed
404 	 */
405 	public void addRepoToClose(Repository r) {
406 		toClose.add(r);
407 	}
408 
409 	/**
410 	 * Creates a unique directory for a test
411 	 *
412 	 * @param name
413 	 *            a subdirectory
414 	 * @return a unique directory for a test
415 	 * @throws IOException
416 	 */
417 	protected File createTempDirectory(String name) throws IOException {
418 		File directory = new File(createTempFile(), name);
419 		FileUtils.mkdirs(directory);
420 		return directory.getCanonicalFile();
421 	}
422 
423 	/**
424 	 * Creates a new unique directory for a test repository
425 	 *
426 	 * @param bare
427 	 *            true for a bare repository; false for a repository with a
428 	 *            working directory
429 	 * @return a unique directory for a test repository
430 	 * @throws IOException
431 	 */
432 	protected File createUniqueTestGitDir(boolean bare) throws IOException {
433 		String gitdirName = createTempFile().getPath();
434 		if (!bare)
435 			gitdirName += "/";
436 		return new File(gitdirName + Constants.DOT_GIT);
437 	}
438 
439 	/**
440 	 * Allocates a new unique file path that does not exist.
441 	 * <p>
442 	 * Unlike the standard {@code File.createTempFile} the returned path does
443 	 * not exist, but may be created by another thread in a race with the
444 	 * caller. Good luck.
445 	 * <p>
446 	 * This method is inherently unsafe due to a race condition between creating
447 	 * the name and the first use that reserves it.
448 	 *
449 	 * @return a unique path that does not exist.
450 	 * @throws IOException
451 	 */
452 	protected File createTempFile() throws IOException {
453 		File p = File.createTempFile("tmp_", "", tmp);
454 		if (!p.delete()) {
455 			throw new IOException("Cannot obtain unique path " + tmp);
456 		}
457 		return p;
458 	}
459 
460 	/**
461 	 * Run a hook script in the repository, returning the exit status.
462 	 *
463 	 * @param db
464 	 *            repository the script should see in GIT_DIR environment
465 	 * @param hook
466 	 *            path of the hook script to execute, must be executable file
467 	 *            type on this platform
468 	 * @param args
469 	 *            arguments to pass to the hook script
470 	 * @return exit status code of the invoked hook
471 	 * @throws IOException
472 	 *             the hook could not be executed
473 	 * @throws InterruptedException
474 	 *             the caller was interrupted before the hook completed
475 	 */
476 	protected int runHook(final Repository db, final File hook,
477 			final String... args) throws IOException, InterruptedException {
478 		final String[] argv = new String[1 + args.length];
479 		argv[0] = hook.getAbsolutePath();
480 		System.arraycopy(args, 0, argv, 1, args.length);
481 
482 		final Map<String, String> env = cloneEnv();
483 		env.put("GIT_DIR", db.getDirectory().getAbsolutePath());
484 		putPersonIdent(env, "AUTHOR", author);
485 		putPersonIdent(env, "COMMITTER", committer);
486 
487 		final File cwd = db.getWorkTree();
488 		final Process p = Runtime.getRuntime().exec(argv, toEnvArray(env), cwd);
489 		p.getOutputStream().close();
490 		p.getErrorStream().close();
491 		p.getInputStream().close();
492 		return p.waitFor();
493 	}
494 
495 	private static void putPersonIdent(final Map<String, String> env,
496 			final String type, final PersonIdent who) {
497 		final String ident = who.toExternalString();
498 		final String date = ident.substring(ident.indexOf("> ") + 2);
499 		env.put("GIT_" + type + "_NAME", who.getName());
500 		env.put("GIT_" + type + "_EMAIL", who.getEmailAddress());
501 		env.put("GIT_" + type + "_DATE", date);
502 	}
503 
504 	/**
505 	 * Create a string to a UTF-8 temporary file and return the path.
506 	 *
507 	 * @param body
508 	 *            complete content to write to the file. If the file should end
509 	 *            with a trailing LF, the string should end with an LF.
510 	 * @return path of the temporary file created within the trash area.
511 	 * @throws IOException
512 	 *             the file could not be written.
513 	 */
514 	protected File write(final String body) throws IOException {
515 		final File f = File.createTempFile("temp", "txt", tmp);
516 		try {
517 			write(f, body);
518 			return f;
519 		} catch (Error e) {
520 			f.delete();
521 			throw e;
522 		} catch (RuntimeException e) {
523 			f.delete();
524 			throw e;
525 		} catch (IOException e) {
526 			f.delete();
527 			throw e;
528 		}
529 	}
530 
531 	/**
532 	 * Write a string as a UTF-8 file.
533 	 *
534 	 * @param f
535 	 *            file to write the string to. Caller is responsible for making
536 	 *            sure it is in the trash directory or will otherwise be cleaned
537 	 *            up at the end of the test. If the parent directory does not
538 	 *            exist, the missing parent directories are automatically
539 	 *            created.
540 	 * @param body
541 	 *            content to write to the file.
542 	 * @throws IOException
543 	 *             the file could not be written.
544 	 */
545 	protected void write(final File f, final String body) throws IOException {
546 		JGitTestUtil.write(f, body);
547 	}
548 
549 	protected String read(final File f) throws IOException {
550 		return JGitTestUtil.read(f);
551 	}
552 
553 	private static String[] toEnvArray(final Map<String, String> env) {
554 		final String[] envp = new String[env.size()];
555 		int i = 0;
556 		for (Map.Entry<String, String> e : env.entrySet())
557 			envp[i++] = e.getKey() + "=" + e.getValue();
558 		return envp;
559 	}
560 
561 	private static HashMap<String, String> cloneEnv() {
562 		return new HashMap<>(System.getenv());
563 	}
564 
565 	private static final class CleanupThread extends Thread {
566 		private static final CleanupThread me;
567 		static {
568 			me = new CleanupThread();
569 			Runtime.getRuntime().addShutdownHook(me);
570 		}
571 
572 		static void deleteOnShutdown(File tmp) {
573 			synchronized (me) {
574 				me.toDelete.add(tmp);
575 			}
576 		}
577 
578 		static void removed(File tmp) {
579 			synchronized (me) {
580 				me.toDelete.remove(tmp);
581 			}
582 		}
583 
584 		private final List<File> toDelete = new ArrayList<>();
585 
586 		@Override
587 		public void run() {
588 			// On windows accidentally open files or memory
589 			// mapped regions may prevent files from being deleted.
590 			// Suggesting a GC increases the likelihood that our
591 			// test repositories actually get removed after the
592 			// tests, even in the case of failure.
593 			System.gc();
594 			synchronized (this) {
595 				boolean silent = false;
596 				boolean failOnError = false;
597 				for (File tmp : toDelete)
598 					recursiveDelete(tmp, silent, failOnError);
599 			}
600 		}
601 	}
602 }