1+ #include <stdio.h>
2+ #include <stdlib.h>
3+ #include <string.h>
4+ #include "../src/include/lavandula_test.h"
5+ #include "../src/include/cors.h"
6+
7+ void testCorsPolicyInit () {
8+ CorsConfig config = corsPolicy ();
9+
10+ expectNotNull (config .allowOrigin );
11+ expect (config .allowOriginCount , toBe (0 ));
12+ expect (config .allowMethods [0 ], toBe (0 ));
13+ expect (config .methodCount , toBe (0 ));
14+ expectNotNull (config .allowHeaders );
15+ expect (config .headerCount , toBe (0 ));
16+
17+ freeCorsPolicy (config );
18+ }
19+
20+ void testAllowOrigin () {
21+ CorsConfig config = corsPolicy ();
22+ allowOrigin (& config , "https://example.com" );
23+ expect (config .allowOriginCount , toBe (1 ));
24+ expect (strcmp (config .allowOrigin [0 ], "https://example.com" ), toBe (0 ));
25+
26+ allowOrigin (& config , "https://test.com" );
27+ expect (config .allowOriginCount , toBe (2 ));
28+ expect (strcmp (config .allowOrigin [1 ], "https://test.com" ), toBe (0 ));
29+
30+ freeCorsPolicy (config );
31+ }
32+
33+ void testAllowOriginLimit () {
34+ CorsConfig config = corsPolicy ();
35+
36+ // Add 50 origins (the limit)
37+ for (int i = 0 ; i < 50 ; i ++ ) {
38+ allowOrigin (& config , "https://example.com" );
39+ }
40+ expect (config .allowOriginCount , toBe (50 ));
41+
42+ // Try to add one more
43+ allowOrigin (& config , "https://overflow.com" );
44+ expect (config .allowOriginCount , toBe (50 ));
45+
46+ freeCorsPolicy (config );
47+ }
48+
49+ void testAllowMethod () {
50+ CorsConfig config = corsPolicy ();
51+
52+ allowMethod (& config , HTTP_GET );
53+ expect (config .methodCount , toBe (1 ));
54+ expect (config .allowMethods [0 ], toBe (HTTP_GET ));
55+
56+ allowMethod (& config , HTTP_POST );
57+ expect (config .methodCount , toBe (2 ));
58+ expect (config .allowMethods [1 ], toBe (HTTP_POST ));
59+
60+ freeCorsPolicy (config );
61+ }
62+
63+ void testAllowMethodLimit () {
64+ CorsConfig config = corsPolicy ();
65+
66+ // Add all 6 methods (GET, POST, PUT, DELETE, PATCH, OPTIONS)
67+ for (int i = 0 ; i < 6 ; i ++ ) {
68+ allowMethod (& config , i );
69+ }
70+ expect (config .methodCount , toBe (6 ));
71+
72+ // Try to add one more
73+ allowMethod (& config , (HttpMethod )0xffff );
74+ expect (config .methodCount , toBe (6 ));
75+
76+ freeCorsPolicy (config );
77+ }
78+
79+ void testAllowAnyOrigin () {
80+ CorsConfig config = corsPolicy ();
81+
82+ allowAnyOrigin (& config );
83+ expect (config .allowOriginCount , toBe (1 ));
84+ expect (strcmp (config .allowOrigin [0 ], "*" ), toBe (0 ));
85+
86+ freeCorsPolicy (config );
87+ }
88+
89+ void testAllowAnyMethod () {
90+ CorsConfig config = corsPolicy ();
91+
92+ allowAnyMethod (& config );
93+ expect (config .methodCount , toBe (6 ));
94+
95+ for (int i = 0 ; i < 6 ; i ++ ) {
96+ expect (config .allowMethods [i ], toBe ((HttpMethod )i ));
97+ }
98+
99+ freeCorsPolicy (config );
100+ }
101+
102+ void testAllowHeader () {
103+ CorsConfig config = corsPolicy ();
104+
105+ allowHeader (& config , "Content-Type" );
106+ expect (config .headerCount , toBe (1 ));
107+ expect (strcmp (config .allowHeaders [0 ], "Content-Type" ), toBe (0 ));
108+
109+ allowHeader (& config , "Authorization" );
110+ expect (config .headerCount , toBe (2 ));
111+ expect (strcmp (config .allowHeaders [1 ], "Authorization" ), toBe (0 ));
112+
113+ freeCorsPolicy (config );
114+ }
115+
116+ void testAllowHeaderLimit () {
117+ CorsConfig config = corsPolicy ();
118+
119+ // Add 50 headers (the limit)
120+ for (int i = 0 ; i < 50 ; i ++ ) {
121+ allowHeader (& config , "X-Custom-Header" );
122+ }
123+ expect (config .headerCount , toBe (50 ));
124+
125+ // Try to add one more
126+ allowHeader (& config , "X-Overflow" );
127+ expect (config .headerCount , toBe (50 ));
128+
129+ freeCorsPolicy (config );
130+ }
131+
132+ void testAllowAnyHeader () {
133+ CorsConfig config = corsPolicy ();
134+
135+ allowAnyHeader (& config );
136+ expect (config .headerCount , toBe (1 ));
137+ expect (strcmp (config .allowHeaders [0 ], "*" ), toBe (0 ));
138+
139+ freeCorsPolicy (config );
140+ }
141+
142+ void testCorsAllowAll () {
143+ CorsConfig config = corsAllowAll ();
144+
145+ // Check that origin is set to "*"
146+ expect (config .allowOriginCount , toBe (1 ));
147+ expect (strcmp (config .allowOrigin [0 ], "*" ), toBe (0 ));
148+
149+ // Check that all methods are allowed
150+ expect (config .methodCount , toBe (6 ));
151+ for (int i = 0 ; i < 6 ; i ++ ) {
152+ expect (config .allowMethods [i ], toBe ((HttpMethod )i ));
153+ }
154+ freeCorsPolicy (config );
155+ }
156+
157+ void testAllowOriginWithDynamicStrings () {
158+ CorsConfig config = corsPolicy ();
159+
160+ char * dynamic_origin = strdup ("https://dynamic.com" );
161+ allowOrigin (& config , dynamic_origin );
162+ free (dynamic_origin );
163+ expect (config .allowOriginCount , toBe (1 ));
164+ expect (strcmp (config .allowOrigin [0 ], "https://dynamic.com" ), toBe (0 ));
165+
166+ allowOrigin (& config , "https://literal.com" );
167+ expect (config .allowOriginCount , toBe (2 ));
168+ expect (strcmp (config .allowOrigin [1 ], "https://literal.com" ), toBe (0 ));
169+
170+ freeCorsPolicy (config );
171+ }
172+
173+ void testAllowHeaderWithDynamicStrings () {
174+ CorsConfig config = corsPolicy ();
175+
176+ char * dynamic_header = strdup ("X-Custom-Header" );
177+ allowHeader (& config , dynamic_header );
178+ free (dynamic_header );
179+ expect (config .headerCount , toBe (1 ));
180+ expect (strcmp (config .allowHeaders [0 ], "X-Custom-Header" ), toBe (0 ));
181+
182+ allowHeader (& config , "Authorization" );
183+ expect (config .headerCount , toBe (2 ));
184+ expect (strcmp (config .allowHeaders [1 ], "Authorization" ), toBe (0 ));
185+
186+ freeCorsPolicy (config );
187+ }
188+
189+ void runCorsTests () {
190+ runTest (testCorsPolicyInit );
191+ runTest (testAllowOrigin );
192+ runTest (testAllowOriginLimit );
193+ runTest (testAllowMethod );
194+ runTest (testAllowMethodLimit );
195+ runTest (testAllowAnyOrigin );
196+ runTest (testAllowAnyMethod );
197+ runTest (testAllowHeader );
198+ runTest (testAllowHeaderLimit );
199+ runTest (testAllowAnyHeader );
200+ runTest (testCorsAllowAll );
201+ runTest (testAllowOriginWithDynamicStrings );
202+ runTest (testAllowHeaderWithDynamicStrings );
203+ }
0 commit comments