aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.ssh.apache.test/tst/org/eclipse/jgit/internal/signing/ssh/AllowedSignersParseTest.java
blob: 84d8179a3db2b1486fb017656c760bcf6b5941fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright (C) 2024, Thomas Wolf <twolf@apache.org> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.internal.signing.ssh;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;

import java.io.StreamCorruptedException;
import java.time.Instant;

import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.util.SystemReader;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Tests for the line parsing in {@link AllowedSigners}.
 */
public class AllowedSignersParseTest {

	@Before
	public void setup() {
		// Uses GMT-03:30 as time zone.
		SystemReader.setInstance(new MockSystemReader());
	}

	@After
	public void tearDown() {
		SystemReader.setInstance(null);
	}

	@Test
	public void testValidDate() {
		assertEquals(Instant.parse("2024-09-01T00:00:00.00Z"),
				AllowedSigners.parseDate("20240901Z"));
		assertEquals(Instant.parse("2024-09-01T01:02:00.00Z"),
				AllowedSigners.parseDate("202409010102Z"));
		assertEquals(Instant.parse("2024-09-01T01:02:03.00Z"),
				AllowedSigners.parseDate("20240901010203Z"));
		assertEquals(Instant.parse("2024-09-01T03:30:00.00Z"),
				AllowedSigners.parseDate("20240901"));
		assertEquals(Instant.parse("2024-09-01T04:32:00.00Z"),
				AllowedSigners.parseDate("202409010102"));
		assertEquals(Instant.parse("2024-09-01T04:32:03.00Z"),
				AllowedSigners.parseDate("20240901010203"));
	}

	@Test
	public void testInvalidDate() {
		assertThrows(Exception.class, () -> AllowedSigners.parseDate("1234"));
		assertThrows(Exception.class,
				() -> AllowedSigners.parseDate("09/01/2024"));
		assertThrows(Exception.class,
				() -> AllowedSigners.parseDate("2024-09-01"));
	}

	private void checkValidKey(String expected, String input, int from)
			throws StreamCorruptedException {
		assertEquals(expected, AllowedSigners.parsePublicKey(input, from));
	}
	@Test
	public void testValidPublicKey() throws StreamCorruptedException {
		checkValidKey(
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO",
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO",
				0);
		checkValidKey(
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO",
				"xyzssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO",
				3);
		checkValidKey(
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO",
				"xyz ssh-ed25519   AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO abc",
				3);
		checkValidKey(
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO",
				"xyz\tssh-ed25519 \tAAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO abc",
				3);
	}

	@Test
	public void testInvalidPublicKey() {
		assertThrows(Exception.class,
				() -> AllowedSigners.parsePublicKey(null, 0));
		assertThrows(Exception.class,
				() -> AllowedSigners.parsePublicKey("", 0));
		assertThrows(Exception.class,
				() -> AllowedSigners.parsePublicKey("foo", 0));
		assertThrows(Exception.class,
				() -> AllowedSigners.parsePublicKey("ssh-ed25519 bar", -1));
		assertThrows(Exception.class,
				() -> AllowedSigners.parsePublicKey("ssh-ed25519 bar", 12));
		assertThrows(Exception.class,
				() -> AllowedSigners.parsePublicKey("ssh-ed25519 bar", 13));
		assertThrows(Exception.class,
				() -> AllowedSigners.parsePublicKey("ssh-ed25519 bar", 16));
	}

	@Test
	public void testValidDequote() {
		assertEquals(new AllowedSigners.Dequoted("a\\bc", 4),
				AllowedSigners.dequote("a\\bc", 0));
		assertEquals(new AllowedSigners.Dequoted("a\\bc\"", 5),
				AllowedSigners.dequote("a\\bc\"", 0));
		assertEquals(new AllowedSigners.Dequoted("a\\b\"c", 5),
				AllowedSigners.dequote("a\\b\"c", 0));
		assertEquals(new AllowedSigners.Dequoted("a\\b\"c", 8),
				AllowedSigners.dequote("\"a\\b\\\"c\"", 0));
		assertEquals(new AllowedSigners.Dequoted("a\\b\"c", 11),
				AllowedSigners.dequote("xyz\"a\\b\\\"c\"", 3));
		assertEquals(new AllowedSigners.Dequoted("abc", 6),
				AllowedSigners.dequote("   abc def", 3));
	}

	@Test
	public void testInvalidDequote() {
		assertThrows(Exception.class, () -> AllowedSigners.dequote("\"abc", 0));
		assertThrows(Exception.class,
				() -> AllowedSigners.dequote("\"abc\\\"", 0));
	}

	@Test
	public void testValidLine() throws Exception {
		assertEquals(new AllowedSigners.AllowedEntry(
				new String[] { "*@a.com" },
				true, null, null, null,
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"),
				AllowedSigners.parseLine(
						"*@a.com cert-authority ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertEquals(new AllowedSigners.AllowedEntry(
				new String[] { "*@a.com", "*@b.a.com" },
				true, null, null, null,
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"),
				AllowedSigners.parseLine(
						"*@a.com,*@b.a.com cert-authority ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertEquals(new AllowedSigners.AllowedEntry(
				new String[] { "foo@a.com" },
				false, null, null, null,
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"),
				AllowedSigners.parseLine(
						"foo@a.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertEquals(new AllowedSigners.AllowedEntry(
				new String[] { "foo@a.com" },
				false, new String[] { "foo", "bar" }, null, null,
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"),
				AllowedSigners.parseLine(
						"foo@a.com namespaces=\"foo,bar\" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertEquals(new AllowedSigners.AllowedEntry(
				new String[] { "foo@a.com" },
				false, null, Instant.parse("2024-09-01T03:30:00.00Z"), null,
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"),
				AllowedSigners.parseLine(
						"foo@a.com valid-After=\"20240901\" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertEquals(new AllowedSigners.AllowedEntry(
				new String[] { "*@a.com", "*@b.a.com" },
				true, new String[] { "git" },
				Instant.parse("2024-09-01T03:30:00.00Z"),
				Instant.parse("2024-09-01T12:00:00.00Z"),
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"),
				AllowedSigners.parseLine(
						"*@a.com,*@b.a.com cert-authority namespaces=\"git\" valid-after=\"20240901\" valid-before=\"202409011200Z\" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertEquals(new AllowedSigners.AllowedEntry(
				new String[] { "foo@a.com" },
				false, new String[] { "git" },
				Instant.parse("2024-09-01T03:30:00.00Z"),
				Instant.parse("2024-09-01T12:00:00.00Z"),
				"ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBGxkz2AUld8eitmyIYlVV+Sot4jT3CigyBmvFRff0q4cSsKLx4x2TxGQeKKVueJEawtsUC2GNRV9FxXsTCUGcZU="),
				AllowedSigners.parseLine(
						"foo@a.com namespaces=\"git\" valid-after=\"20240901\" valid-before=\"202409011200Z\" ecdsa-sha2-nistp256   AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBGxkz2AUld8eitmyIYlVV+Sot4jT3CigyBmvFRff0q4cSsKLx4x2TxGQeKKVueJEawtsUC2GNRV9FxXsTCUGcZU="));
	}

	@Test
	public void testInvalidLine() {
		assertThrows(Exception.class, () -> AllowedSigners.parseLine(
				"cert-authority ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertThrows(Exception.class, () -> AllowedSigners.parseLine(
				"namespaces=\"git\" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertThrows(Exception.class, () -> AllowedSigners.parseLine(
				"valid-after=\"20240901\" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertThrows(Exception.class, () -> AllowedSigners.parseLine(
				"valid-before=\"20240901\" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertThrows(Exception.class, () -> AllowedSigners.parseLine(
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertThrows(Exception.class, () -> AllowedSigners.parseLine(
				"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO foo@bar.com"));
		assertThrows(Exception.class, () -> AllowedSigners.parseLine(
				"AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertThrows(Exception.class, () -> AllowedSigners.parseLine(
				"a@a.com namespaces=\"\" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertThrows(Exception.class, () -> AllowedSigners.parseLine(
				"a@a.com namespaces=\",,,\" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
		assertThrows(Exception.class, () -> AllowedSigners.parseLine(
				"a@a.com,,b@a.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGATOZ8PcOKdY978fzIstnZ0+FuefIWKp7wRZynQLdzO"));
	}

	@Test
	public void testSkippedLine() throws Exception {
		assertNull(AllowedSigners.parseLine(null));
		assertNull(AllowedSigners.parseLine(""));
		assertNull(AllowedSigners.parseLine("# Comment"));
	}
}