[OCaml] Mobile-friendly clone of cgit.
1
(** Input validation for repository names, hashes, branch names and tag names.
2
*)
3
4
let test_valid_repo_names () =
5
Alcotest.(check bool)
6
"project.git" true
7
(Ogit.Resolvers.is_valid_repo_name "project.git");
8
Alcotest.(check bool)
9
"project" true
10
(Ogit.Resolvers.is_valid_repo_name "project");
11
Alcotest.(check bool)
12
"nested/repo" true
13
(Ogit.Resolvers.is_valid_repo_name "nested/repo")
14
15
let test_invalid_repo_names () =
16
List.iter
17
(fun name ->
18
Alcotest.(check bool)
19
(Printf.sprintf "reject %S" name)
20
false
21
(Ogit.Resolvers.is_valid_repo_name name))
22
[
23
"";
24
".hidden";
25
".";
26
"..";
27
"../outside";
28
".hidden/repo";
29
"nested/.hidden";
30
"nested\\repo";
31
"bad\x00repo";
32
]
33
34
let test_valid_hash_hex () =
35
Alcotest.(check bool)
36
"40 lowercase hex" true
37
(Ogit.Resolvers.is_valid_hash_hex (String.make 40 'a'));
38
Alcotest.(check bool)
39
"40 uppercase hex" true
40
(Ogit.Resolvers.is_valid_hash_hex (String.make 40 'A'))
41
42
let test_invalid_hash_hex () =
43
Alcotest.(check bool)
44
"39 chars" false
45
(Ogit.Resolvers.is_valid_hash_hex (String.make 39 'a'));
46
Alcotest.(check bool)
47
"41 chars" false
48
(Ogit.Resolvers.is_valid_hash_hex (String.make 41 'a'));
49
Alcotest.(check bool)
50
"non-hex char" false
51
(Ogit.Resolvers.is_valid_hash_hex (String.make 39 'a' ^ "x"))
52
53
let test_short_hash () =
54
Alcotest.(check string) "short input" "abc" (Ogit.Resolvers.short_hash "abc");
55
Alcotest.(check string)
56
"long input" "01234567"
57
(Ogit.Resolvers.short_hash "0123456789")
58
59
let test_branch_name () =
60
Alcotest.(check (option string))
61
"main" (Some "main")
62
(Ogit.Resolvers.Reference.branch_name "refs/heads/main");
63
Alcotest.(check (option string))
64
"feature/topic" (Some "feature/topic")
65
(Ogit.Resolvers.Reference.branch_name "refs/heads/feature/topic");
66
Alcotest.(check (option string))
67
"HEAD" None
68
(Ogit.Resolvers.Reference.branch_name "HEAD")
69
70
let test_tag_name () =
71
Alcotest.(check (option string))
72
"v1.0.0" (Some "v1.0.0")
73
(Ogit.Resolvers.Reference.tag_name "refs/tags/v1.0.0");
74
Alcotest.(check (option string))
75
"not a tag" None
76
(Ogit.Resolvers.Reference.tag_name "refs/heads/v1.0.0")
77
78
let suite =
79
( "validation",
80
[
81
Alcotest.test_case "valid repo names" `Quick test_valid_repo_names;
82
Alcotest.test_case "invalid repo names" `Quick test_invalid_repo_names;
83
Alcotest.test_case "valid hash hex" `Quick test_valid_hash_hex;
84
Alcotest.test_case "invalid hash hex" `Quick test_invalid_hash_hex;
85
Alcotest.test_case "short hash" `Quick test_short_hash;
86
Alcotest.test_case "branch name" `Quick test_branch_name;
87
Alcotest.test_case "tag name" `Quick test_tag_name;
88
] )
89