@@ -76,6 +76,91 @@ web_client_test_payload_request(const char *method, size_t payload_length, size_
7676 return request ;
7777}
7878
79+ int web_client_registry_cookie_unittest (void )
80+ {
81+ fprintf (stderr , "\n%s() running...\n" , __FUNCTION__ );
82+
83+ #define REGISTRY_COOKIE_TEST_OURS "b5dd255b-a36c-44ad-a598-115c1e06b964"
84+ #define REGISTRY_COOKIE_TEST_OTHER "38992aab-c97a-490c-b9f7-660b0d78965b"
85+
86+ static const struct {
87+ const char * request ;
88+ const char * expected ;
89+ } cases [] = {
90+ {
91+ "GET /api/v1/registry?action=hello HTTP/1.1\r\n"
92+ "Cookie: netdata_registry_id=" REGISTRY_COOKIE_TEST_OURS "\r\n\r\n" ,
93+ REGISTRY_COOKIE_TEST_OURS ,
94+ },
95+ {
96+ "GET /api/v1/registry?action=access&x=netdata_registry_id=" REGISTRY_COOKIE_TEST_OTHER " HTTP/1.1\r\n"
97+ "Cookie: netdata_registry_id=" REGISTRY_COOKIE_TEST_OURS "\r\n\r\n" ,
98+ REGISTRY_COOKIE_TEST_OURS ,
99+ },
100+ {
101+ "GET /api/v1/registry?action=access&x=netdata_registry_id=" REGISTRY_COOKIE_TEST_OTHER " HTTP/1.1\r\n\r\n" ,
102+ "" ,
103+ },
104+ {
105+ "GET /api/v1/registry?action=access HTTP/1.1\r\n"
106+ "Referer: http://example.com/?netdata_registry_id=" REGISTRY_COOKIE_TEST_OTHER "\r\n\r\n" ,
107+ "" ,
108+ },
109+ {
110+ "GET /api/v1/registry?action=access HTTP/1.1\r\n"
111+ "Cookie: a=1; netdata_registry_id=" REGISTRY_COOKIE_TEST_OURS "; b=2\r\n\r\n" ,
112+ REGISTRY_COOKIE_TEST_OURS ,
113+ },
114+ {
115+ "GET /api/v1/registry?action=access HTTP/1.1\r\n"
116+ "Cookie: other_netdata_registry_id=" REGISTRY_COOKIE_TEST_OTHER "\r\n\r\n" ,
117+ "" ,
118+ },
119+ {
120+ "GET /api/v1/registry?action=access HTTP/1.1\r\n"
121+ "Cookie: netdata_registry_id=" REGISTRY_COOKIE_TEST_OURS "; netdata_registry_id=" REGISTRY_COOKIE_TEST_OTHER "\r\n\r\n" ,
122+ REGISTRY_COOKIE_TEST_OURS ,
123+ },
124+ {
125+ "GET /api/v1/registry?action=access HTTP/1.1\r\n"
126+ "Cookie: a=1\r\n"
127+ "Cookie: netdata_registry_id=" REGISTRY_COOKIE_TEST_OURS "\r\n\r\n" ,
128+ REGISTRY_COOKIE_TEST_OURS ,
129+ },
130+ };
131+
132+ int errors = 0 ;
133+ size_t memory_accounting = 0 ;
134+ struct web_client * w = web_client_create (& memory_accounting );
135+
136+ for (size_t i = 0 ; i < sizeof (cases ) / sizeof (cases [0 ]); i ++ ) {
137+ web_client_reuse_from_cache (w );
138+ buffer_flush (w -> response .data );
139+ buffer_strcat (w -> response .data , cases [i ].request );
140+
141+ WEB_CLIENT_TEST (http_request_validate (w ) == HTTP_VALIDATION_OK , "case %zu: request did not validate" , i );
142+ WEB_CLIENT_TEST (
143+ strcmp (w -> auth .registry_person_guid , cases [i ].expected ) == 0 ,
144+ "case %zu: registry cookie is '%s', expected '%s'" ,
145+ i ,
146+ w -> auth .registry_person_guid ,
147+ cases [i ].expected );
148+ }
149+
150+ web_client_free (w );
151+ WEB_CLIENT_TEST (memory_accounting == 0 , "web client leaked %zu accounted bytes" , memory_accounting );
152+
153+ #undef REGISTRY_COOKIE_TEST_OURS
154+ #undef REGISTRY_COOKIE_TEST_OTHER
155+
156+ if (errors )
157+ fprintf (stderr , "%s() failed with %d error(s)\n" , __FUNCTION__ , errors );
158+ else
159+ fprintf (stderr , "%s() passed\n" , __FUNCTION__ );
160+
161+ return errors ;
162+ }
163+
79164int web_client_request_size_unittest (void )
80165{
81166 fprintf (stderr , "\n%s() running...\n" , __FUNCTION__ );
0 commit comments