repo_index_tests.c (2339B)
1 #include "writer/html/repo_index.h" 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 #include "git/repo.h" 8 #include "test_utils.h" 9 #include "utest.h" 10 11 UTEST(html_repo_index, basic) { 12 char* buf = NULL; 13 size_t size = 0; 14 FILE* out = open_memstream(&buf, &size); 15 16 HtmlRepoIndex* index = html_repoindex_create(out); 17 ASSERT_NE(NULL, index); 18 html_repoindex_set_me_url(index, "https://me.example.com"); 19 20 GitRepo r1 = { 21 .short_name = "repo1", 22 .description = "Desc 1", 23 .owner = "Owner 1", 24 .last_commit_time = 1702031400, 25 }; 26 27 html_repoindex_begin(index); 28 html_repoindex_add_repo(index, &r1); 29 html_repoindex_end(index); 30 html_repoindex_free(index); 31 fclose(out); 32 33 ASSERT_NE(NULL, buf); 34 35 /* Verify Header/Metadata */ 36 EXPECT_STR_SEQUENCE(buf, "Repositories", "rel=\"me\"", 37 "https://me.example.com"); 38 39 /* Verify Repo Row */ 40 EXPECT_STR_SEQUENCE(buf, "repo1/log.html", "repo1", "Desc 1", "Owner 1", 41 "2023-12-08 10:30"); 42 43 free(buf); 44 } 45 46 UTEST(html_repo_index, multiple) { 47 char* buf = NULL; 48 size_t size = 0; 49 FILE* out = open_memstream(&buf, &size); 50 51 HtmlRepoIndex* index = html_repoindex_create(out); 52 53 GitRepo r1 = {.short_name = "a", .description = "d1", .owner = "o1"}; 54 GitRepo r2 = {.short_name = "b", .description = "d2", .owner = "o2"}; 55 56 html_repoindex_begin(index); 57 html_repoindex_add_repo(index, &r1); 58 html_repoindex_add_repo(index, &r2); 59 html_repoindex_end(index); 60 html_repoindex_free(index); 61 fclose(out); 62 63 ASSERT_NE(NULL, buf); 64 EXPECT_STR_SEQUENCE(buf, "a/log.html", "d1", "o1", "b/log.html", "d2", "o2"); 65 66 free(buf); 67 } 68 69 UTEST(html_repo_index, escaping) { 70 char* buf = NULL; 71 size_t size = 0; 72 FILE* out = open_memstream(&buf, &size); 73 74 HtmlRepoIndex* index = html_repoindex_create(out); 75 ASSERT_NE(NULL, index); 76 html_repoindex_set_me_url( 77 index, "https://me.example.com/?a=1&b=2\" onclick=\"alert(1)"); 78 79 html_repoindex_begin(index); 80 html_repoindex_end(index); 81 html_repoindex_free(index); 82 fclose(out); 83 84 ASSERT_NE(NULL, buf); 85 86 // Verify that characters like & and " are escaped, and not written literally. 87 EXPECT_TRUE(strstr(buf, 88 "href=\"https://me.example.com/?a=1&b=2" " 89 "onclick="alert(1)\"") != NULL); 90 91 free(buf); 92 }