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