]> git.proxmox.com Git - libgit2.git/blob - tests/stream/registration.c
bf3c20502a0092135024aebfe2f6ae20dce8d023
[libgit2.git] / tests / stream / registration.c
1 #include "clar_libgit2.h"
2 #include "git2/sys/stream.h"
3 #include "streams/tls.h"
4 #include "streams/socket.h"
5 #include "stream.h"
6
7 static git_stream test_stream;
8 static int ctor_called;
9
10 void test_stream_registration__cleanup(void)
11 {
12 cl_git_pass(git_stream_register(GIT_STREAM_TLS | GIT_STREAM_STANDARD, NULL));
13 }
14
15 static int test_stream_init(git_stream **out, const char *host, const char *port)
16 {
17 GIT_UNUSED(host);
18 GIT_UNUSED(port);
19
20 ctor_called = 1;
21 *out = &test_stream;
22
23 return 0;
24 }
25
26 static int test_stream_wrap(git_stream **out, git_stream *in, const char *host)
27 {
28 GIT_UNUSED(in);
29 GIT_UNUSED(host);
30
31 ctor_called = 1;
32 *out = &test_stream;
33
34 return 0;
35 }
36
37 void test_stream_registration__insecure(void)
38 {
39 git_stream *stream;
40 git_stream_registration registration = {0};
41
42 registration.version = 1;
43 registration.init = test_stream_init;
44 registration.wrap = test_stream_wrap;
45
46 ctor_called = 0;
47 cl_git_pass(git_stream_register(GIT_STREAM_STANDARD, &registration));
48 cl_git_pass(git_socket_stream_new(&stream, "localhost", "80"));
49 cl_assert_equal_i(1, ctor_called);
50 cl_assert_equal_p(&test_stream, stream);
51
52 ctor_called = 0;
53 stream = NULL;
54 cl_git_pass(git_stream_register(GIT_STREAM_STANDARD, NULL));
55 cl_git_pass(git_socket_stream_new(&stream, "localhost", "80"));
56
57 cl_assert_equal_i(0, ctor_called);
58 cl_assert(&test_stream != stream);
59
60 git_stream_free(stream);
61 }
62
63 void test_stream_registration__tls(void)
64 {
65 git_stream *stream;
66 git_stream_registration registration = {0};
67 int error;
68
69 registration.version = 1;
70 registration.init = test_stream_init;
71 registration.wrap = test_stream_wrap;
72
73 ctor_called = 0;
74 cl_git_pass(git_stream_register(GIT_STREAM_TLS, &registration));
75 cl_git_pass(git_tls_stream_new(&stream, "localhost", "443"));
76 cl_assert_equal_i(1, ctor_called);
77 cl_assert_equal_p(&test_stream, stream);
78
79 ctor_called = 0;
80 stream = NULL;
81 cl_git_pass(git_stream_register(GIT_STREAM_TLS, NULL));
82 error = git_tls_stream_new(&stream, "localhost", "443");
83
84 /* We don't have TLS support enabled, or we're on Windows,
85 * which has no arbitrary TLS stream support.
86 */
87 #if defined(GIT_WIN32) || !defined(GIT_HTTPS)
88 cl_git_fail_with(-1, error);
89 #else
90 cl_git_pass(error);
91 #endif
92
93 cl_assert_equal_i(0, ctor_called);
94 cl_assert(&test_stream != stream);
95
96 git_stream_free(stream);
97 }
98
99 void test_stream_registration__both(void)
100 {
101 git_stream *stream;
102 git_stream_registration registration = {0};
103
104 registration.version = 1;
105 registration.init = test_stream_init;
106 registration.wrap = test_stream_wrap;
107
108 cl_git_pass(git_stream_register(GIT_STREAM_STANDARD | GIT_STREAM_TLS, &registration));
109
110 ctor_called = 0;
111 cl_git_pass(git_tls_stream_new(&stream, "localhost", "443"));
112 cl_assert_equal_i(1, ctor_called);
113 cl_assert_equal_p(&test_stream, stream);
114
115 ctor_called = 0;
116 cl_git_pass(git_socket_stream_new(&stream, "localhost", "80"));
117 cl_assert_equal_i(1, ctor_called);
118 cl_assert_equal_p(&test_stream, stream);
119 }