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