forge
Explore
Log in

Don't you dare go Hollow

Ssh command parser tests merge into tables

Browse files at this commit

36ee1fa1f3040fe60b6a1c7ea0fdc513bccca0c9Ahmet Kaan GümüşParent: 7a15066
  • test
    • ssh command parser tests merge into tables
Show full message
✅ test: ssh command parser tests merge into tables

Test count in forge-ssh src went from 20 to 8.
The 18 parse.rs tests are now 6 table-driven tests. Every accept and
reject case is kept and labelled in its assert message.
Server tests are unchanged.
crates/forge-ssh/src/parse.rsmodified+179 -240
@@ -144,184 +144,151 @@ mod tests {
144 }144 }
145 145
146 #[test]146 #[test]
147- fn accepts_upload_and_receive_pack() {147+ fn accepts_valid_exec_commands() {
148- assert_eq!(148+ // Covers both services, wiki suffix, spaced form, leading slash and
149- ok("git-upload-pack '/tahinli/proj.git'"),149+ // tilde stripping, and unquoted paths.
150- (GitService::UploadPack, "tahinli/proj".into())150+ let cases: &[(&str, GitService, &str)] = &[
151- );151+ (
152- assert_eq!(152+ "git-upload-pack '/tahinli/proj.git'",
153- ok("git-receive-pack '/tahinli/proj.git'"),153+ GitService::UploadPack,
154- (GitService::ReceivePack, "tahinli/proj".into())154+ "tahinli/proj",
155- );155+ ),
156- }156+ (
157-157+ "git-receive-pack '/tahinli/proj.git'",
158- #[test]158+ GitService::ReceivePack,
159- fn wiki_paths_keep_the_wiki_suffix() {159+ "tahinli/proj",
160- assert_eq!(160+ ),
161- ok("git-receive-pack '/tahinli/proj.wiki.git'"),161+ (
162- (GitService::ReceivePack, "tahinli/proj.wiki".to_owned())162+ "git-receive-pack '/tahinli/proj.wiki.git'",
163- );163+ GitService::ReceivePack,
164- assert_eq!(164+ "tahinli/proj.wiki",
165- ok("git-upload-pack 'tahinli/proj.wiki'"),165+ ),
166- (GitService::UploadPack, "tahinli/proj.wiki".to_owned())166+ (
167- );167+ "git-upload-pack 'tahinli/proj.wiki'",
168- }168+ GitService::UploadPack,
169-169+ "tahinli/proj.wiki",
170- #[test]170+ ),
171- fn accepts_spaced_form() {171+ (
172- assert_eq!(172+ "git upload-pack '/tahinli/proj'",
173- ok("git upload-pack '/tahinli/proj'"),173+ GitService::UploadPack,
174- (GitService::UploadPack, "tahinli/proj".into())174+ "tahinli/proj",
175- );175+ ),
176- assert_eq!(176+ (
177- ok("git receive-pack 'tahinli/proj.git'"),177+ "git receive-pack 'tahinli/proj.git'",
178- (GitService::ReceivePack, "tahinli/proj".into())178+ GitService::ReceivePack,
179- );179+ "tahinli/proj",
180- }180+ ),
181-181+ (
182- #[test]182+ "git-upload-pack '~/tahinli/proj'",
183- fn strips_leading_slash_and_tilde() {183+ GitService::UploadPack,
184- assert_eq!(ok("git-upload-pack '~/tahinli/proj'").1, "tahinli/proj");184+ "tahinli/proj",
185- assert_eq!(ok("git-upload-pack '/~/tahinli/proj'").1, "tahinli/proj");185+ ),
186- assert_eq!(ok("git-upload-pack 'tahinli/proj'").1, "tahinli/proj");186+ (
187- // Only one leading slash is stripped.187+ "git-upload-pack '/~/tahinli/proj'",
188- assert_eq!(188+ GitService::UploadPack,
189- err("git-upload-pack '//tahinli/proj'"),189+ "tahinli/proj",
190- ParseError::MalformedPath190+ ),
191- );191+ (
192- }192+ "git-upload-pack 'tahinli/proj'",
193-193+ GitService::UploadPack,
194- #[test]194+ "tahinli/proj",
195- fn unquoted_path_without_spaces_is_accepted() {195+ ),
196- assert_eq!(196+ (
197- ok("git-upload-pack /tahinli/proj.git"),197+ "git-upload-pack /tahinli/proj.git",
198- (GitService::UploadPack, "tahinli/proj".into())198+ GitService::UploadPack,
199- );199+ "tahinli/proj",
200- assert_eq!(200+ ),
201- ok("git-receive-pack tahinli/proj"),201+ (
202- (GitService::ReceivePack, "tahinli/proj".into())202+ "git-receive-pack tahinli/proj",
203- );203+ GitService::ReceivePack,
204- }204+ "tahinli/proj",
205-205+ ),
206- #[test]206+ ];
207- fn unquoted_path_with_quote_or_space_is_rejected() {207+ for (cmd, service, path) in cases {
208- assert_eq!(err("git-upload-pack a/b c"), ParseError::MalformedPath);208+ assert_eq!(ok(cmd), (*service, path.to_string()), "case {cmd}");
209- assert_eq!(err("git-upload-pack a/b'"), ParseError::MalformedPath);209+ }
210- assert_eq!(err("git-upload-pack a\"b/c"), ParseError::MalformedPath);
211- assert_eq!(err("git-upload-pack a\\b/c"), ParseError::MalformedPath);
212- }
213-
214- #[test]
215- fn rejects_option_injection() {
216- assert_eq!(err("git-upload-pack '--help'"), ParseError::MalformedPath);
217- assert_eq!(
218- err("git-upload-pack '--upload-pack=touch /x'"),
219- ParseError::MalformedPath
220- );
221- assert_eq!(err("git-upload-pack '-x/y'"), ParseError::MalformedPath);
222- assert_eq!(err("git-upload-pack 'a/-b'"), ParseError::MalformedPath);
223- }
224-
225- #[test]
226- fn rejects_shell_injection() {
227- assert_eq!(
228- err("git-upload-pack 'a/b'; rm -rf /"),
229- ParseError::MalformedPath
230- );
231- assert_eq!(
232- err("git-upload-pack 'a/b' 'c/d'"),
233- ParseError::MalformedPath
234- );
235- assert_eq!(err("git-upload-pack 'a/b$(id)'"), ParseError::MalformedPath);
236- assert_eq!(err("git-upload-pack 'a/b;c'"), ParseError::MalformedPath);
237- assert_eq!(err("git-upload-pack 'a/b|c'"), ParseError::MalformedPath);
238- assert_eq!(err("git-upload-pack 'a/b&c'"), ParseError::MalformedPath);
239- assert_eq!(err("git-upload-pack 'a/b%20c'"), ParseError::MalformedPath);
240- assert_eq!(err("git-upload-pack 'a/é'"), ParseError::MalformedPath);
241- assert_eq!(err("git-upload-pack 'a/b`id`'"), ParseError::MalformedPath);
242- assert_eq!(
243- err("git-upload-pack 'a/b' && id"),
244- ParseError::MalformedPath
245- );
246- }
247-
248- #[test]
249- fn rejects_quote_backslash_newline_nul_and_control_chars_inside_quotes() {
250- assert_eq!(err("git-upload-pack 'a/b\\c'"), ParseError::MalformedPath);
251- assert_eq!(err("git-upload-pack 'a/b\nc'"), ParseError::MalformedPath);
252- assert_eq!(err("git-upload-pack 'a/b\rc'"), ParseError::MalformedPath);
253- assert_eq!(err("git-upload-pack 'a/b\0c'"), ParseError::MalformedPath);
254- assert_eq!(err("git-upload-pack 'a/b\x07c'"), ParseError::MalformedPath);
255- assert_eq!(err("git-upload-pack 'a/b\x7fc'"), ParseError::MalformedPath);
256- assert_eq!(err("git-upload-pack 'a/b'c'"), ParseError::MalformedPath);
257- }
258-
259- #[test]
260- fn rejects_traversal() {
261- assert_eq!(err("git-upload-pack '../x/y'"), ParseError::MalformedPath);
262- assert_eq!(err("git-upload-pack 'a/..'"), ParseError::MalformedPath);
263- assert_eq!(err("git-upload-pack '/a/../b'"), ParseError::MalformedPath);
264- assert_eq!(
265- err("git-upload-pack '.hidden/repo'"),
266- ParseError::MalformedPath
267- );
268- assert_eq!(
269- err("git-upload-pack 'owner/.git'"),
270- ParseError::MalformedPath
271- );
272- }
273-
274- #[test]
275- fn rejects_wrong_segment_count() {
276- assert_eq!(err("git-upload-pack 'a/b/c'"), ParseError::MalformedPath);
277- assert_eq!(err("git-upload-pack 'a'"), ParseError::MalformedPath);
278- assert_eq!(err("git-upload-pack 'a/'"), ParseError::MalformedPath);
279- assert_eq!(err("git-upload-pack '/a/b/'"), ParseError::MalformedPath);
280- assert_eq!(err("git-upload-pack '//'"), ParseError::MalformedPath);
281 }210 }
282 211
283 #[test]212 #[test]
284- fn rejects_empty_values() {213+ fn rejects_malformed_exec_paths() {
285- assert_eq!(err("git-upload-pack ''"), ParseError::MalformedPath);214+ // Option, shell and traversal injection, bad quoting, control
286- assert_eq!(err("git-upload-pack '/'"), ParseError::MalformedPath);215+ // characters, wrong segment counts, empty values and stray spaces.
287- assert_eq!(err("git-upload-pack '~/'"), ParseError::MalformedPath);216+ let cases: &[&str] = &[
288- assert_eq!(err("git-upload-pack '.git'"), ParseError::MalformedPath);217+ // Only one leading slash is stripped.
289- assert_eq!(err("git-upload-pack "), ParseError::MalformedPath);218+ "git-upload-pack '//tahinli/proj'",
219+ // Unquoted paths with a quote, backslash or space.
220+ "git-upload-pack a/b c",
221+ "git-upload-pack a/b'",
222+ "git-upload-pack a\"b/c",
223+ "git-upload-pack a\\b/c",
224+ // Option injection.
225+ "git-upload-pack '--help'",
226+ "git-upload-pack '--upload-pack=touch /x'",
227+ "git-upload-pack '-x/y'",
228+ "git-upload-pack 'a/-b'",
229+ // Shell injection.
230+ "git-upload-pack 'a/b'; rm -rf /",
231+ "git-upload-pack 'a/b' 'c/d'",
232+ "git-upload-pack 'a/b$(id)'",
233+ "git-upload-pack 'a/b;c'",
234+ "git-upload-pack 'a/b|c'",
235+ "git-upload-pack 'a/b&c'",
236+ "git-upload-pack 'a/b%20c'",
237+ "git-upload-pack 'a/é'",
238+ "git-upload-pack 'a/b`id`'",
239+ "git-upload-pack 'a/b' && id",
240+ // Quote, backslash, newline, NUL and control characters inside quotes.
241+ "git-upload-pack 'a/b\\c'",
242+ "git-upload-pack 'a/b\nc'",
243+ "git-upload-pack 'a/b\rc'",
244+ "git-upload-pack 'a/b\0c'",
245+ "git-upload-pack 'a/b\x07c'",
246+ "git-upload-pack 'a/b\x7fc'",
247+ "git-upload-pack 'a/b'c'",
248+ // Traversal and hidden segments.
249+ "git-upload-pack '../x/y'",
250+ "git-upload-pack 'a/..'",
251+ "git-upload-pack '/a/../b'",
252+ "git-upload-pack '.hidden/repo'",
253+ "git-upload-pack 'owner/.git'",
254+ // Wrong segment count.
255+ "git-upload-pack 'a/b/c'",
256+ "git-upload-pack 'a'",
257+ "git-upload-pack 'a/'",
258+ "git-upload-pack '/a/b/'",
259+ "git-upload-pack '//'",
260+ // Empty values.
261+ "git-upload-pack ''",
262+ "git-upload-pack '/'",
263+ "git-upload-pack '~/'",
264+ "git-upload-pack '.git'",
265+ "git-upload-pack ",
266+ // Extra spaces and trailing garbage.
267+ "git-upload-pack '/a/b'",
268+ "git-upload-pack '/a/b' ",
269+ "git-upload-pack '/a/b",
270+ ];
271+ for cmd in cases {
272+ assert_eq!(err(cmd), ParseError::MalformedPath, "case {cmd:?}");
273+ }
290 }274 }
291 275
292 #[test]276 #[test]
293 fn rejects_unknown_commands() {277 fn rejects_unknown_commands() {
294- assert_eq!(278+ let cases: &[&str] = &[
295- err("git-upload-archive '/a/b'"),279+ "git-upload-archive '/a/b'",
296- ParseError::UnsupportedCommand280+ "git upload-archive '/a/b'",
297- );281+ "ls -la",
298- assert_eq!(282+ "",
299- err("git upload-archive '/a/b'"),283+ "git-upload-packx '/a/b'",
300- ParseError::UnsupportedCommand284+ "git-upload-pack'/a/b'",
301- );285+ "git-upload-pack",
302- assert_eq!(err("ls -la"), ParseError::UnsupportedCommand);286+ "GIT-UPLOAD-PACK '/a/b'",
303- assert_eq!(err(""), ParseError::UnsupportedCommand);287+ " git-upload-pack '/a/b'",
304- assert_eq!(288+ ];
305- err("git-upload-packx '/a/b'"),289+ for cmd in cases {
306- ParseError::UnsupportedCommand290+ assert_eq!(err(cmd), ParseError::UnsupportedCommand, "case {cmd:?}");
307- );291+ }
308- assert_eq!(err("git-upload-pack'/a/b'"), ParseError::UnsupportedCommand);
309- assert_eq!(err("git-upload-pack"), ParseError::UnsupportedCommand);
310- assert_eq!(
311- err("GIT-UPLOAD-PACK '/a/b'"),
312- ParseError::UnsupportedCommand
313- );
314- assert_eq!(
315- err(" git-upload-pack '/a/b'"),
316- ParseError::UnsupportedCommand
317- );
318- }
319-
320- #[test]
321- fn rejects_extra_spaces_and_trailing_garbage() {
322- assert_eq!(err("git-upload-pack '/a/b'"), ParseError::MalformedPath);
323- assert_eq!(err("git-upload-pack '/a/b' "), ParseError::MalformedPath);
324- assert_eq!(err("git-upload-pack '/a/b"), ParseError::MalformedPath);
325 }292 }
326 293
327 fn lfs_ok(cmd: &str) -> (String, LfsOperation) {294 fn lfs_ok(cmd: &str) -> (String, LfsOperation) {
@@ -340,78 +307,50 @@ mod tests {
340 307
341 #[test]308 #[test]
342 fn lfs_accepts_quoted_and_bare_paths() {309 fn lfs_accepts_quoted_and_bare_paths() {
343- assert_eq!(310+ let cases: &[(&str, &str, LfsOperation)] = &[
344- lfs_ok("git-lfs-authenticate '/tahinli/proj.git' download"),311+ (
345- ("tahinli/proj".into(), LfsOperation::Download)312+ "git-lfs-authenticate '/tahinli/proj.git' download",
346- );313+ "tahinli/proj",
347- assert_eq!(314+ LfsOperation::Download,
348- lfs_ok("git-lfs-authenticate 'tahinli/proj' upload"),315+ ),
349- ("tahinli/proj".into(), LfsOperation::Upload)316+ (
350- );317+ "git-lfs-authenticate 'tahinli/proj' upload",
351- assert_eq!(318+ "tahinli/proj",
352- lfs_ok("git-lfs-authenticate /tahinli/proj.git upload"),319+ LfsOperation::Upload,
353- ("tahinli/proj".into(), LfsOperation::Upload)320+ ),
354- );321+ (
355- }322+ "git-lfs-authenticate /tahinli/proj.git upload",
356-323+ "tahinli/proj",
357- #[test]324+ LfsOperation::Upload,
358- fn lfs_rejects_bad_operation() {325+ ),
359- assert_eq!(326+ ];
360- lfs_err("git-lfs-authenticate 'a/b' push"),327+ for (cmd, path, op) in cases {
361- ParseError::MalformedPath328+ assert_eq!(lfs_ok(cmd), (path.to_string(), *op), "case {cmd}");
362- );329+ }
363- assert_eq!(
364- lfs_err("git-lfs-authenticate 'a/b' "),
365- ParseError::MalformedPath
366- );
367- assert_eq!(
368- lfs_err("git-lfs-authenticate 'a/b' download extra"),
369- ParseError::MalformedPath
370- );
371- assert_eq!(
372- lfs_err("git-lfs-authenticate 'a/b'download"),
373- ParseError::MalformedPath
374- );
375- assert_eq!(
376- lfs_err("git-lfs-authenticate 'a/b' Download"),
377- ParseError::MalformedPath
378- );
379 }330 }
380 331
381 #[test]332 #[test]
382- fn lfs_rejects_bad_paths() {333+ fn lfs_rejects_malformed_commands() {
383- assert_eq!(334+ let cases: &[&str] = &[
384- lfs_err("git-lfs-authenticate '--help' download"),335+ // Bad operation.
385- ParseError::MalformedPath336+ "git-lfs-authenticate 'a/b' push",
386- );337+ "git-lfs-authenticate 'a/b' ",
387- assert_eq!(338+ "git-lfs-authenticate 'a/b' download extra",
388- lfs_err("git-lfs-authenticate '../x/y' upload"),339+ "git-lfs-authenticate 'a/b'download",
389- ParseError::MalformedPath340+ "git-lfs-authenticate 'a/b' Download",
390- );341+ // Bad paths.
391- assert_eq!(342+ "git-lfs-authenticate '--help' download",
392- lfs_err("git-lfs-authenticate 'a/b'; id download"),343+ "git-lfs-authenticate '../x/y' upload",
393- ParseError::MalformedPath344+ "git-lfs-authenticate 'a/b'; id download",
394- );345+ "git-lfs-authenticate 'a/b' 'c' download",
395- assert_eq!(346+ "git-lfs-authenticate 'a/b download",
396- lfs_err("git-lfs-authenticate 'a/b' 'c' download"),347+ "git-lfs-authenticate a/b c download",
397- ParseError::MalformedPath348+ "git-lfs-authenticate 'a/b/c' download",
398- );349+ "git-lfs-authenticate ''",
399- assert_eq!(350+ ];
400- lfs_err("git-lfs-authenticate 'a/b download"),351+ for cmd in cases {
401- ParseError::MalformedPath352+ assert_eq!(lfs_err(cmd), ParseError::MalformedPath, "case {cmd:?}");
402- );353+ }
403- assert_eq!(
404- lfs_err("git-lfs-authenticate a/b c download"),
405- ParseError::MalformedPath
406- );
407- assert_eq!(
408- lfs_err("git-lfs-authenticate 'a/b/c' download"),
409- ParseError::MalformedPath
410- );
411- assert_eq!(
412- lfs_err("git-lfs-authenticate ''"),
413- ParseError::MalformedPath
414- );
415 }354 }
416 355
417 #[test]356 #[test]