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