View Javadoc
1   /*
2    * Copyright (C) 2018, Salesforce. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.lib;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertFalse;
14  import static org.junit.Assert.assertNull;
15  import static org.junit.Assert.assertTrue;
16  import static org.junit.Assert.fail;
17  
18  import org.eclipse.jgit.errors.ConfigInvalidException;
19  import org.junit.Test;
20  
21  public class GpgConfigTest {
22  
23  	private static Config parse(String content) throws ConfigInvalidException {
24  		final Config c = new Config(null);
25  		c.fromText(content);
26  		return c;
27  	}
28  
29  	@Test
30  	public void isSignCommits_defaultIsFalse() throws Exception {
31  		Config c = parse("");
32  
33  		assertFalse(new GpgConfig(c).isSignCommits());
34  	}
35  
36  	@Test
37  	public void isSignCommits_false() throws Exception {
38  		Config c = parse("" //
39  				+ "[gpg]\n" //
40  				+ "  format = x509\n" //
41  				+ "[commit]\n" //
42  				+ "  gpgSign = false\n" //
43  		);
44  
45  		assertFalse(new GpgConfig(c).isSignCommits());
46  	}
47  
48  	@Test
49  	public void isSignCommits_true() throws Exception {
50  		Config c = parse("" //
51  				+ "[commit]\n" //
52  				+ "  gpgSign = true\n" //
53  		);
54  
55  		assertTrue(new GpgConfig(c).isSignCommits());
56  	}
57  
58  	@Test
59  	public void testGetKeyFormat_defaultsToOpenpgp() throws Exception {
60  		Config c = parse("");
61  
62  		assertEquals(GpgConfig.GpgFormat.OPENPGP,
63  				new GpgConfig(c).getKeyFormat());
64  	}
65  
66  	@Test(expected = IllegalArgumentException.class)
67  	public void testGetKeyFormat_failsForInvalidValue() throws Exception {
68  		Config c = parse("" //
69  				+ "[gpg]\n" //
70  				+ "  format = invalid\n" //
71  		);
72  
73  		new GpgConfig(c).getKeyFormat();
74  		fail("Call should not have succeeded!");
75  	}
76  
77  	@Test
78  	public void testGetKeyFormat_openpgp() throws Exception {
79  		Config c = parse("" //
80  				+ "[gpg]\n" //
81  				+ "  format = openpgp\n" //
82  		);
83  
84  		assertEquals(GpgConfig.GpgFormat.OPENPGP,
85  				new GpgConfig(c).getKeyFormat());
86  	}
87  
88  	@Test
89  	public void testGetKeyFormat_x509() throws Exception {
90  		Config c = parse("" //
91  				+ "[gpg]\n" //
92  				+ "  format = x509\n" //
93  		);
94  
95  		assertEquals(GpgConfig.GpgFormat.X509, new GpgConfig(c).getKeyFormat());
96  	}
97  
98  	@Test
99  	public void testGetSigningKey() throws Exception {
100 		Config c = parse("" //
101 				+ "[user]\n" //
102 				+ "  signingKey = 0x2345\n" //
103 		);
104 
105 		assertEquals("0x2345", new GpgConfig(c).getSigningKey());
106 	}
107 
108 	@Test
109 	public void testGetSigningKey_defaultToNull() throws Exception {
110 		Config c = parse("");
111 
112 		assertNull(new GpgConfig(c).getSigningKey());
113 	}
114 }