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+174 -235
@@ -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+ (
157+ "git-receive-pack '/tahinli/proj.git'",
158+ GitService::ReceivePack,
159+ "tahinli/proj",
160+ ),
161+ (
162+ "git-receive-pack '/tahinli/proj.wiki.git'",
163+ GitService::ReceivePack,
164+ "tahinli/proj.wiki",
165+ ),
166+ (
167+ "git-upload-pack 'tahinli/proj.wiki'",
168+ GitService::UploadPack,
169+ "tahinli/proj.wiki",
170+ ),
171+ (
172+ "git upload-pack '/tahinli/proj'",
173+ GitService::UploadPack,
174+ "tahinli/proj",
175+ ),
176+ (
177+ "git receive-pack 'tahinli/proj.git'",
178+ GitService::ReceivePack,
179+ "tahinli/proj",
180+ ),
181+ (
182+ "git-upload-pack '~/tahinli/proj'",
183+ GitService::UploadPack,
184+ "tahinli/proj",
185+ ),
186+ (
187+ "git-upload-pack '/~/tahinli/proj'",
188+ GitService::UploadPack,
189+ "tahinli/proj",
190+ ),
191+ (
192+ "git-upload-pack 'tahinli/proj'",
193+ GitService::UploadPack,
194+ "tahinli/proj",
195+ ),
196+ (
197+ "git-upload-pack /tahinli/proj.git",
198+ GitService::UploadPack,
199+ "tahinli/proj",
200+ ),
201+ (
202+ "git-receive-pack tahinli/proj",
203+ GitService::ReceivePack,
204+ "tahinli/proj",
205+ ),
206+ ];
207+ for (cmd, service, path) in cases {
208+ assert_eq!(ok(cmd), (*service, path.to_string()), "case {cmd}");
156 }209 }
157-
158- #[test]
159- fn wiki_paths_keep_the_wiki_suffix() {
160- assert_eq!(
161- ok("git-receive-pack '/tahinli/proj.wiki.git'"),
162- (GitService::ReceivePack, "tahinli/proj.wiki".to_owned())
163- );
164- assert_eq!(
165- ok("git-upload-pack 'tahinli/proj.wiki'"),
166- (GitService::UploadPack, "tahinli/proj.wiki".to_owned())
167- );
168 }210 }
169 211
170 #[test]212 #[test]
171- fn accepts_spaced_form() {213+ fn rejects_malformed_exec_paths() {
172- assert_eq!(214+ // Option, shell and traversal injection, bad quoting, control
173- ok("git upload-pack '/tahinli/proj'"),215+ // characters, wrong segment counts, empty values and stray spaces.
174- (GitService::UploadPack, "tahinli/proj".into())216+ let cases: &[&str] = &[
175- );
176- assert_eq!(
177- ok("git receive-pack 'tahinli/proj.git'"),
178- (GitService::ReceivePack, "tahinli/proj".into())
179- );
180- }
181-
182- #[test]
183- fn strips_leading_slash_and_tilde() {
184- assert_eq!(ok("git-upload-pack '~/tahinli/proj'").1, "tahinli/proj");
185- assert_eq!(ok("git-upload-pack '/~/tahinli/proj'").1, "tahinli/proj");
186- assert_eq!(ok("git-upload-pack 'tahinli/proj'").1, "tahinli/proj");
187 // Only one leading slash is stripped.217 // Only one leading slash is stripped.
188- assert_eq!(218+ "git-upload-pack '//tahinli/proj'",
189- err("git-upload-pack '//tahinli/proj'"),219+ // Unquoted paths with a quote, backslash or space.
190- ParseError::MalformedPath220+ "git-upload-pack a/b c",
191- );221+ "git-upload-pack a/b'",
192- }222+ "git-upload-pack a\"b/c",
193-223+ "git-upload-pack a\\b/c",
194- #[test]224+ // Option injection.
195- fn unquoted_path_without_spaces_is_accepted() {225+ "git-upload-pack '--help'",
196- assert_eq!(226+ "git-upload-pack '--upload-pack=touch /x'",
197- ok("git-upload-pack /tahinli/proj.git"),227+ "git-upload-pack '-x/y'",
198- (GitService::UploadPack, "tahinli/proj".into())228+ "git-upload-pack 'a/-b'",
199- );229+ // Shell injection.
200- assert_eq!(230+ "git-upload-pack 'a/b'; rm -rf /",
201- ok("git-receive-pack tahinli/proj"),231+ "git-upload-pack 'a/b' 'c/d'",
202- (GitService::ReceivePack, "tahinli/proj".into())232+ "git-upload-pack 'a/b$(id)'",
203- );233+ "git-upload-pack 'a/b;c'",
204- }234+ "git-upload-pack 'a/b|c'",
205-235+ "git-upload-pack 'a/b&c'",
206- #[test]236+ "git-upload-pack 'a/b%20c'",
207- fn unquoted_path_with_quote_or_space_is_rejected() {237+ "git-upload-pack 'a/é'",
208- assert_eq!(err("git-upload-pack a/b c"), ParseError::MalformedPath);238+ "git-upload-pack 'a/b`id`'",
209- assert_eq!(err("git-upload-pack a/b'"), ParseError::MalformedPath);239+ "git-upload-pack 'a/b' && id",
210- assert_eq!(err("git-upload-pack a\"b/c"), ParseError::MalformedPath);240+ // Quote, backslash, newline, NUL and control characters inside quotes.
211- assert_eq!(err("git-upload-pack a\\b/c"), ParseError::MalformedPath);241+ "git-upload-pack 'a/b\\c'",
212- }242+ "git-upload-pack 'a/b\nc'",
213-243+ "git-upload-pack 'a/b\rc'",
214- #[test]244+ "git-upload-pack 'a/b\0c'",
215- fn rejects_option_injection() {245+ "git-upload-pack 'a/b\x07c'",
216- assert_eq!(err("git-upload-pack '--help'"), ParseError::MalformedPath);246+ "git-upload-pack 'a/b\x7fc'",
217- assert_eq!(247+ "git-upload-pack 'a/b'c'",
218- err("git-upload-pack '--upload-pack=touch /x'"),248+ // Traversal and hidden segments.
219- ParseError::MalformedPath249+ "git-upload-pack '../x/y'",
220- );250+ "git-upload-pack 'a/..'",
221- assert_eq!(err("git-upload-pack '-x/y'"), ParseError::MalformedPath);251+ "git-upload-pack '/a/../b'",
222- assert_eq!(err("git-upload-pack 'a/-b'"), ParseError::MalformedPath);252+ "git-upload-pack '.hidden/repo'",
223- }253+ "git-upload-pack 'owner/.git'",
224-254+ // Wrong segment count.
225- #[test]255+ "git-upload-pack 'a/b/c'",
226- fn rejects_shell_injection() {256+ "git-upload-pack 'a'",
227- assert_eq!(257+ "git-upload-pack 'a/'",
228- err("git-upload-pack 'a/b'; rm -rf /"),258+ "git-upload-pack '/a/b/'",
229- ParseError::MalformedPath259+ "git-upload-pack '//'",
230- );260+ // Empty values.
231- assert_eq!(261+ "git-upload-pack ''",
232- err("git-upload-pack 'a/b' 'c/d'"),262+ "git-upload-pack '/'",
233- ParseError::MalformedPath263+ "git-upload-pack '~/'",
234- );264+ "git-upload-pack '.git'",
235- assert_eq!(err("git-upload-pack 'a/b$(id)'"), ParseError::MalformedPath);265+ "git-upload-pack ",
236- assert_eq!(err("git-upload-pack 'a/b;c'"), ParseError::MalformedPath);266+ // Extra spaces and trailing garbage.
237- assert_eq!(err("git-upload-pack 'a/b|c'"), ParseError::MalformedPath);267+ "git-upload-pack '/a/b'",
238- assert_eq!(err("git-upload-pack 'a/b&c'"), ParseError::MalformedPath);268+ "git-upload-pack '/a/b' ",
239- assert_eq!(err("git-upload-pack 'a/b%20c'"), ParseError::MalformedPath);269+ "git-upload-pack '/a/b",
240- assert_eq!(err("git-upload-pack 'a/é'"), ParseError::MalformedPath);270+ ];
241- assert_eq!(err("git-upload-pack 'a/b`id`'"), ParseError::MalformedPath);271+ for cmd in cases {
242- assert_eq!(272+ assert_eq!(err(cmd), ParseError::MalformedPath, "case {cmd:?}");
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 }
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- }
282-
283- #[test]
284- fn rejects_empty_values() {
285- assert_eq!(err("git-upload-pack ''"), ParseError::MalformedPath);
286- assert_eq!(err("git-upload-pack '/'"), ParseError::MalformedPath);
287- assert_eq!(err("git-upload-pack '~/'"), ParseError::MalformedPath);
288- assert_eq!(err("git-upload-pack '.git'"), ParseError::MalformedPath);
289- assert_eq!(err("git-upload-pack "), ParseError::MalformedPath);
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- );
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 }291 }
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+ (
322+ "git-lfs-authenticate /tahinli/proj.git upload",
323+ "tahinli/proj",
324+ LfsOperation::Upload,
325+ ),
326+ ];
327+ for (cmd, path, op) in cases {
328+ assert_eq!(lfs_ok(cmd), (path.to_string(), *op), "case {cmd}");
355 }329 }
356-
357- #[test]
358- fn lfs_rejects_bad_operation() {
359- assert_eq!(
360- lfs_err("git-lfs-authenticate 'a/b' push"),
361- ParseError::MalformedPath
362- );
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]