Esempio n. 1
0
void t10_repeated_header(){
	INIT_LOCAL();
	
	onion_request *req;
	int ok;
	
	onion_set_max_post_size(server, 1024);
	req=onion_request_new(custom_io);
	FAIL_IF_EQUAL(req,NULL);
	FAIL_IF_NOT_EQUAL(req->connection.fd, -1);
	
	{
		const char *query="GET / HTTP/1.0\n"
											"Content-Type: application/x-www-form-urlencoded\n"
											"Host: 127.0.0.1\n\r"
											"Content-Length: 24\n"
											"Accept-Language: en\n"
											"Content-Type: application/x-www-form-urlencoded-bis\n\n";
		
		ok=onion_request_write(req,query,strlen(query));
	}
	FAIL_IF_EQUAL(ok,OCS_INTERNAL_ERROR);
	FAIL_IF_NOT_EQUAL_STR(onion_request_get_header(req,"Host"),"127.0.0.1");
	FAIL_IF_NOT_EQUAL_STR(onion_request_get_header(req,"Content-Length"),"24");
	FAIL_IF_NOT_EQUAL_STR(onion_request_get_header(req,"Content-Type"),"application/x-www-form-urlencoded");

	
	onion_request_free(req);
	
	
	END_LOCAL();
}
Esempio n. 2
0
void t06_codecs_c_unicode(){
	INIT_LOCAL();
	
	const char *text="\302Hola!";
	char *res=onion_c_quote_new(text);
	
	FAIL_IF_NOT_STRSTR(res,"\\302");
	FAIL_IF_NOT_STRSTR(res,"\\302Hola!");
	
	free(res);
	
	text="€";
	res=onion_c_quote_new(text);
	
	FAIL_IF_NOT_STRSTR(text,"€");
	FAIL_IF_NOT_EQUAL_STR(res,"\"\\342\\202\\254\"");
	
	free(res);

	text="\377";
	res=onion_c_quote_new(text);
	
	FAIL_IF_NOT_EQUAL_STR(res,"\"\\377\"");
	
	free(res);

	END_LOCAL();
}
Esempio n. 3
0
void t01_codecs_base64_decode(){
	INIT_LOCAL();
	{
	/// Text from wikipedia. Leviathan by Hobbes.
	char *orig ="dGVzdDphYWE=";
	char *realtext="test:aaa";
	
	char *res=onion_base64_decode(orig, NULL);
	FAIL_IF_NOT_EQUAL_STR(res,realtext);
	free(res);
	}
	{
	/// Text from wikipedia. Leviathan by Hobbes.
	char *orig ="TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\n"
							"IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n"
							"dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n"
							"dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n"
							"ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\n";
	char *realtext="Man is distinguished, not only by his reason, but by this singular passion from other animals,"
								 " which is a lust of the mind, that by a perseverance of delight in the continued and"
								 " indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.";
	
	int l;
	char *res=onion_base64_decode(orig, &l);
	//fprintf(stderr,"l %d len %ld\n",l,strlen(realtext));
	FAIL_IF_NOT_EQUAL(l,strlen(realtext));
	FAIL_IF_NOT_EQUAL_STR(res,realtext);
	free(res);
	}
	END_LOCAL();
}
Esempio n. 4
0
void t04_create_and_free_a_dup(){
	INIT_LOCAL();
	onion_dict *dict;
	const char *value;
	
	dict=onion_dict_new();
	FAIL_IF_EQUAL(dict,NULL);

	// Get before anything in
	value=onion_dict_get(dict, "Request");
	FAIL_IF_NOT_EQUAL(value,NULL);

	// basic add
	onion_dict_add(dict, "Request", "GET /", OD_DUP_ALL);
	value=onion_dict_get(dict, "Request");
	FAIL_IF_NOT_EQUAL_STR(value,"GET /");

	onion_dict_add(dict, "Request", "GET /", OD_DUP_ALL);
	value=onion_dict_get(dict, "Request");
	FAIL_IF_NOT_EQUAL_STR(value,"GET /");
	
	// basic remove
	onion_dict_remove(dict, "Request");
	value=onion_dict_get(dict, "Request");
	FAIL_IF_NOT_EQUAL_STR(value,"GET /");
	
	// basic remove
	onion_dict_remove(dict, "Request");
	value=onion_dict_get(dict, "Request");
	FAIL_IF_NOT_EQUAL(value,NULL);

	onion_dict_free(dict);
	
	END_LOCAL();
}
Esempio n. 5
0
void t03_create_and_free_a_lot_random(unsigned int n){
	INIT_LOCAL();
	onion_dict *dict;
	const char *value;
	unsigned int i;
	
	dict=onion_dict_new();
	FAIL_IF_EQUAL(dict,NULL);

	// Linear add
	for (i=0;i<n;i++){
		char key[16], val[16];
		sprintf(key,"key %d",R1(i));
		sprintf(val,"val %d",R2(i));
		
		onion_dict_add(dict, key, val, OD_DUP_ALL);
	}

	// Linear get
	for (i=0;i<n;i++){
		char key[16], val[16];
		sprintf(key,"key %d",R1(i));
		sprintf(val,"val %d",R2(i));
		
		value=onion_dict_get(dict, key);
		FAIL_IF_NOT_EQUAL_STR(val,value);
	}

	// remove all
	for (i=n;i>0;i--){
		char key[16];
		int removed;
		sprintf(key,"key %d",R1(i-1));
		//fprintf(stderr,"%s %d\n",key,i-1);
		removed=onion_dict_remove(dict, key);
		FAIL_IF_NOT_EQUAL(removed,1);
	}

	// check removed all
	for (i=0;i<n;i++){
		char key[16], val[16];
		sprintf(key,"key %d",R1(i));
		sprintf(val,"val %d",R1(i));
		
		value=onion_dict_get(dict, key);
		//fprintf(stderr,"%s %s\n",key,value);
		FAIL_IF_NOT_EQUAL(NULL,value);
		FAIL_IF_NOT_EQUAL_STR(NULL,value);
	}

	onion_dict_free(dict);
	
	END_LOCAL();
}
Esempio n. 6
0
void t01_url() {
  INIT_LOCAL();

  onion_url *url = onion_url_new();
  onion_url_add_handler(url, "^handler1.$",
                        onion_handler_new((onion_handler_handler) handler1,
                                          NULL, NULL));
  onion_url_add(url, "handler2/", handler2);
  onion_url_add_with_data(url, "^handler(3|4)/", handler3, NULL, NULL);

  onion_set_root_handler(server, onion_url_to_handler(url));

  onion_request *req = onion_request_new(onion_get_listen_point(server, 0));

#define R "GET /handler1/ HTTP/1.1\n\n"
  onion_request_write(req, R, sizeof(R));
  onion_request_process(req);
  FAIL_IF_NOT_EQUAL_INT(handler_called, 1);
  FAIL_IF_NOT_EQUAL_STR(urltxt, "");
  free(urltxt);
  urltxt = NULL;

  onion_request_clean(req);
  onion_request_write(req, "GET /handler2/ HTTP/1.1\n\n", sizeof(R));
  onion_request_process(req);
  FAIL_IF_NOT_EQUAL_INT(handler_called, 2);
  ONION_DEBUG("%s", urltxt);
  FAIL_IF_NOT_EQUAL_STR(urltxt, "");
  free(urltxt);
  urltxt = NULL;

  onion_request_clean(req);
  onion_request_write(req, "GET /handler3/hello HTTP/1.1\n\n", sizeof(R) + 5);
  onion_request_process(req);
  FAIL_IF_NOT_EQUAL_INT(handler_called, 3);
  FAIL_IF_NOT_EQUAL_STR(urltxt, "hello");
  free(urltxt);
  urltxt = NULL;

  handler_called = 0;
  onion_request_clean(req);
  onion_request_write(req, "GET /handler2/hello HTTP/1.1\n\n", sizeof(R) + 5);
  onion_request_process(req);
  FAIL_IF_NOT_EQUAL_INT(handler_called, 0);
  FAIL_IF_EQUAL_STR(urltxt, "");
  free(urltxt);
  urltxt = NULL;

  onion_request_free(req);
  onion_url_free(url);
  onion_set_root_handler(server, NULL);

  END_LOCAL();
}
Esempio n. 7
0
void t01_test_mime(){
	INIT_LOCAL();
	
	FAIL_IF_NOT_EQUAL_STR("text/plain", onion_mime_get("txt"));
	FAIL_IF_NOT_EQUAL_STR("text/plain", onion_mime_get("fdsfds"));
	FAIL_IF_NOT_EQUAL_STR("text/html", onion_mime_get("html"));
	FAIL_IF_NOT_EQUAL_STR("image/png", onion_mime_get("file.png"));
	FAIL_IF_NOT_EQUAL_STR("application/javascript", onion_mime_get("js"));

	
	onion_mime_set(NULL);
	END_LOCAL();
}
Esempio n. 8
0
void t18_json_escape_codes(){
	INIT_LOCAL();

	onion_dict *d=onion_dict_from_json("{ \"hello\": \"Hello\\nworld\", \"second\":\"second\" }");
	FAIL_IF_NOT_STRSTR(onion_dict_get(d, "hello"), "Hello\nworld");
	FAIL_IF_NOT_STRSTR(onion_dict_get(d, "second"), "second");
	onion_dict_free(d);

	d=onion_dict_from_json("{ \"hello\": \"\\uD83D\\uDE02\" }");
	FAIL_IF_NOT_STRSTR(onion_dict_get(d, "hello"), "😂");
	onion_dict_free(d);

	d=onion_dict_from_json("{ \"hello\": \"\\uD83D\\uDE03\" }"); // Another code point
	FAIL_IF_STRSTR(onion_dict_get(d, "hello"), "😂");
	onion_dict_free(d);

	d=onion_dict_from_json("{ \"hello\": \"\\u007b\" }"); // simple unicode
	FAIL_IF_NOT_STRSTR(onion_dict_get(d, "hello"), "{");
	onion_dict_free(d);

	d=onion_dict_from_json("{ \"hello\": \"\\\"Quote\" }"); // Escape quote
	FAIL_IF_NOT_STRSTR(onion_dict_get(d, "hello"), "\"Quote");
	onion_dict_free(d);

	d=onion_dict_from_json("{ \"hello\": \"\"Quote\" }"); // Must fail
	FAIL_IF_NOT_EQUAL(d, NULL);

	d=onion_dict_new();
	onion_dict_add(d, "hello", "Hello\nWorld\\", 0);
	onion_dict_add(d, "second", "123", 0);
	onion_block *b=onion_dict_to_json(d);
	FAIL_IF_NOT_EQUAL_STR(onion_block_data(b),"{\"hello\":\"Hello\\nWorld\\\\\", \"second\":\"123\"}");
	onion_block_free(b);
	onion_dict_free(d);

	d=onion_dict_new();
	onion_dict_add(d, "hello", "😂\t\n😂", 0);
	b=onion_dict_to_json(d);
	FAIL_IF_NOT_EQUAL_STR(onion_block_data(b),"{\"hello\":\"😂\\t\\n😂\"}");
	onion_block_free(b);
	onion_dict_free(d);

	d=onion_dict_new();
	onion_dict_add(d, "hello", "\02\03\x7f", 0);
	b=onion_dict_to_json(d);
	FAIL_IF_NOT_EQUAL_STR(onion_block_data(b),"{\"hello\":\"\\u0002\\u0003\\u007F\"}");
	onion_block_free(b);
	onion_dict_free(d);

	END_LOCAL();
}
Esempio n. 9
0
void t04_server_overflow(){
	INIT_LOCAL();
	onion *server=onion_new(0);
  onion_listen_point *lp=onion_buffer_listen_point_new();
  onion_add_listen_point(server,NULL,NULL,lp);
	onion_set_root_handler(server, onion_handler_static("Succedded", 200));
	
	onion_block *long_req=onion_block_new();
	
	onion_block_add_str(long_req,"GET / HTTP/1.1\n");
	int i;
	for(i=0;i<1000;i++){
		onion_block_add_str(long_req,"Header-1: This is header1 Header-2: This is header 2 ");
	}
	onion_request *req=onion_request_new(lp);
	onion_request_write(req, onion_block_data(long_req),onion_block_size(long_req)-1); // send it all, but the final 0.
	const char *buffer=onion_buffer_listen_point_get_buffer_data(req);
	FAIL_IF_NOT_EQUAL_STR(buffer,"");
	onion_request_write(req, "\n\n",2); // finish this request. no \n\n before to check possible bugs.

	buffer=onion_buffer_listen_point_get_buffer_data(req);
	FAIL_IF_EQUAL_STR(buffer,"");
	FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 200 OK\r\n");
	FAIL_IF_NOT_STRSTR(buffer, "\r\nContent-Length: 9\r\n");
	FAIL_IF_NOT_STRSTR(buffer, "libonion");
	FAIL_IF_NOT_STRSTR(buffer, "\r\n\r\nSuccedded");

	onion_block_free(long_req);
	onion_request_free(req);
	onion_free(server);

	END_LOCAL();
}
Esempio n. 10
0
void t08_server_with_error_404(){
	INIT_LOCAL();
	onion *server=onion_new(0);
  onion_listen_point *lp=onion_buffer_listen_point_new();
  onion_add_listen_point(server,NULL,NULL,lp);
	onion_url *urls=onion_url_new();
	onion_set_root_handler(server, onion_url_to_handler(urls));
	
	onion_request *req=onion_request_new(lp);
#define S "GET / HTTP/1.1"
	onion_request_write(req, S,sizeof(S)-1); // send it all, but the final 0.
#undef S
	const char *buffer=onion_buffer_listen_point_get_buffer_data(req);
	FAIL_IF_NOT_EQUAL_STR(buffer,"");
	onion_request_write(req, "\n",1); // finish this request. no \n\n before to check possible bugs.
	onion_request_write(req, "\n",1); // finish this request. no \n\n before to check possible bugs. The last \n was not processed, as was overflowing.
	
	buffer=onion_buffer_listen_point_get_buffer_data(req);
	FAIL_IF_EQUAL_STR(buffer,"");
	FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 404 NOT FOUND\r\n");
	FAIL_IF_NOT_STRSTR(buffer, "libonion");

	onion_request_free(req);
	onion_free(server);

	END_LOCAL();
}
Esempio n. 11
0
void t02_server_full(){
	INIT_LOCAL();
	onion *server=onion_new(0);
  onion_listen_point *lp=onion_buffer_listen_point_new();
  onion_add_listen_point(server,NULL,NULL,lp);
	onion_set_root_handler(server, onion_handler_static("Succedded", 200));
	
	onion_request *req=onion_request_new(lp);
#define S "GET / HTTP/1.1\r\nHeader-1: This is header1\r\nHeader-2: This is header 2\r\n"
	onion_request_write(req, S,sizeof(S)-1); // send it all, but the final 0.
#undef S
	const char *buffer=onion_buffer_listen_point_get_buffer_data(req);
	FAIL_IF_NOT_EQUAL_STR(buffer,"");
	onion_request_write(req, "\n",1); // finish this request. no \n\n before to check possible bugs.

	buffer=onion_buffer_listen_point_get_buffer_data(req);
	
	FAIL_IF_EQUAL_STR(buffer,"");
	FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 200 OK\r\n");
	FAIL_IF_NOT_STRSTR(buffer, "\r\nContent-Length: 9\r\n");
	FAIL_IF_NOT_STRSTR(buffer, "libonion");
	FAIL_IF_NOT_STRSTR(buffer, "\r\n\r\nSuccedded");

	onion_request_free(req);
	onion_free(server);

	END_LOCAL();
}
Esempio n. 12
0
void t03_websocket_server_receive_small_packet() {
  INIT_LOCAL();
  int length = 0;
  char *buffer = NULL, buffer2[115];
  memset(&ws_status, 0, sizeof(ws_status));
  onion *o = websocket_server_new();
  onion_request *req = websocket_start_handshake(o);
  req->connection.listen_point->read =
      (lpreader_sig_t *) websocket_data_buffer_read;

  onion_response *res = onion_response_new(req);
  onion_websocket *ws = onion_websocket_new(req, res);

  length = websocket_forge_small_packet((char **)&buffer);
  websocket_data_buffer_write(req, buffer, length);

  onion_websocket_read(ws, (char *)&buffer2, 120);

  buffer2[114] = '\0';
  FAIL_IF_NOT_EQUAL_STR(buffer2,
                        "Some UTF-8-encoded chars which will be cut at the 117th char so I write some gap-filling text with no meaning unti");

  onion_websocket_free(ws);
  onion_request_free(req);
  onion_free(o);
  END_LOCAL();
}
Esempio n. 13
0
void t17_merge(){
	INIT_LOCAL();

	onion_dict *a=onion_dict_from_json("{\"hello\":\"world\"}");
	onion_dict *b=onion_dict_from_json("{\"bye\":\"_world_\", \"sub\": { \"hello\": \"world!\" } }");

	onion_dict_merge(a,b);

	FAIL_IF_NOT_EQUAL_STR(onion_dict_get(a,"bye"), "_world_");
	FAIL_IF_NOT_EQUAL_STR(onion_dict_rget(a,"sub","hello",NULL), "world!");

	onion_dict_free(b);
	FAIL_IF_NOT_EQUAL_STR(onion_dict_rget(a,"sub","hello",NULL), "world!");
	onion_dict_free(a);

	END_LOCAL();
}
Esempio n. 14
0
void cmpdict(onion_dict *d, const char *key, const char *value, int flags){
	if (flags&OD_DICT){
		onion_dict_preorder((onion_dict*)value, cmpdict, onion_dict_get_dict(d, key));
		onion_dict_preorder(onion_dict_get_dict(d, key), cmpdict, (onion_dict*)value);
	}
	else
		FAIL_IF_NOT_EQUAL_STR(value, onion_dict_get(d, key));
}
Esempio n. 15
0
void t07_codecs_html(){
	INIT_LOCAL();
	
	char *encoded=onion_html_quote("<\"Hello\">");
	FAIL_IF_NOT_EQUAL_STR( encoded, "&lt;&quot;Hello&quot;&gt;");
	free(encoded);
	
	END_LOCAL();
}
Esempio n. 16
0
onion_connection_status process_request(void *_, onion_request *req, onion_response *res){
  pthread_mutex_lock(&processed_mutex);
  processed++;
  pthread_mutex_unlock(&processed_mutex);
  onion_response_write0(res, "Done");

	FAIL_IF_NOT_EQUAL_STR(onion_request_get_client_description(req),"127.0.0.1");

  return OCS_PROCESSED;
}
Esempio n. 17
0
void t02_cookies(){
	INIT_LOCAL();
	
	onion_response *res=onion_response_new(NULL);
	onion_dict *h=onion_response_get_headers(res);
	
	onion_response_add_cookie(res, "key1", "value1", -1, NULL, NULL, 0);
	FAIL_IF_NOT_EQUAL_STR(onion_dict_get(h, "Set-Cookie"), "key1=value1");
	
	onion_dict_remove(h, "Set-Cookie");
	onion_response_add_cookie(res, "key2", "value2", -1, "/", "*.example.org", OC_HTTP_ONLY|OC_SECURE);
	FAIL_IF_NOT_EQUAL_STR(onion_dict_get(h, "Set-Cookie"), "key2=value2; path=/; domain=*.example.org; HttpOnly; Secure");

	onion_dict_remove(h, "Set-Cookie");
	onion_response_add_cookie(res, "key3", "value3", 0, "/", "*.example.org", OC_HTTP_ONLY|OC_SECURE);
	FAIL_IF_NOT_EQUAL_STR(onion_dict_get(h, "Set-Cookie"), "key3=value3; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=*.example.org; HttpOnly; Secure");
	
	onion_dict_remove(h, "Set-Cookie");
	onion_response_add_cookie(res, "key4", "value4", 60, "/", "*.example.org", OC_HTTP_ONLY|OC_SECURE);
	FAIL_IF_EQUAL_STR(onion_dict_get(h, "Set-Cookie"), "key4=value4; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=*.example.org; HttpOnly; Secure");
	FAIL_IF_EQUAL_STR(onion_dict_get(h, "Set-Cookie"), "key4=value4; domain=*.example.org; HttpOnly; path=/; Secure");
	
	int i;
	int valid_expires=0;
	char tmpdate[100];
	const char *setcookie=onion_dict_get(h, "Set-Cookie");
	for(i=59;i<62;i++){
		struct tm *tmp;
		time_t t=time(NULL) + i;
		tmp = localtime(&t);
		strftime(tmpdate, sizeof(tmpdate), "key4=value4; expires=%a, %d %b %Y %H:%M:%S %Z; path=/; domain=*.example.org; HttpOnly; Secure", tmp);
		ONION_DEBUG("\ntest  %s =? \nonion %s", tmpdate, setcookie);
		if (strcmp(tmpdate, setcookie)==0)
			valid_expires=1;
	}
	FAIL_IF_NOT(valid_expires);
	
	onion_response_free(res);
	
	END_LOCAL();
}
Esempio n. 18
0
void t07_multiline_headers(){
	INIT_LOCAL();
	
	onion_request *req;
	int ok;
	
	onion_set_max_post_size(server, 1024);
	req=onion_request_new(custom_io);
	FAIL_IF_EQUAL(req,NULL);
	FAIL_IF_NOT_EQUAL(req->connection.fd, -1);
	
	{
		const char *query="GET / HTTP/1.0\n"
											"Host: 127.0.0.1\n\rContent-Length: 24\n"
											"Other-Header: My header is very long and with several\n lines\n"
											"Extra-Other-Header: My header is very long and with several\n \n lines\n"
											"My-Other-Header: My header is very long and with several\n\tlines\n\n";
		
		ok=onion_request_write(req,query,strlen(query));
	}
	FAIL_IF_EQUAL(ok,OCS_INTERNAL_ERROR);
	FAIL_IF_NOT_EQUAL_STR(onion_request_get_header(req,"other-header"),"My header is very long and with several lines");
	FAIL_IF_NOT_EQUAL_STR(onion_request_get_header(req,"extra-other-header"),"My header is very long and with several lines");
	FAIL_IF_NOT_EQUAL_STR(onion_request_get_header(req,"My-other-header"),"My header is very long and with several lines");
	onion_request_clean(req);

	{
		const char *query="GET / HTTP/1.0\n"
											"Host: 127.0.0.1\n\rContent-Length: 24\n"
											"Other-Header: My header is very long and with several\n lines\n"
											"My-Other-Header: My header is very long and with several\nlines\n\n";
		
		ok=onion_request_write(req,query,strlen(query));
	}
	FAIL_IF_NOT_EQUAL(ok,OCS_INTERNAL_ERROR); // No \t at my-other-header
	
	onion_request_free(req);
	
	
	END_LOCAL();
}
Esempio n. 19
0
onion_connection_status process_request(void *_, onion_request * req,
                                        onion_response * res) {
  onion_response_write0(res, "Done");

  const onion_block *data = onion_request_get_data(req);
  FAIL_IF_NOT(data);
  FAIL_IF_NOT_EQUAL_STR(onion_block_data(data),
                        "{\n   \"a\": \"10\",\n   \"b\": \"20\"\n}");

  ONION_DEBUG(onion_block_data(data));

  return OCS_PROCESSED;
}
Esempio n. 20
0
void t04_create_add_free_GET(){
	INIT_LOCAL();
	
	onion_request *req;
	int ok;
	
	req=onion_request_new(custom_io);
	FAIL_IF_EQUAL(req,NULL);
	FAIL_IF_NOT_EQUAL(req->connection.fd, -1);
	
	const char *query="GET /myurl%20/is/very/deeply/nested?test=test&query2=query%202&more_query=%20more%20query+10 HTTP/1.0\n"
													"Host: 127.0.0.1\n\r"
													"Other-Header: My header is very long and with spaces...\r\n\r\n";
	
	int i; // Straight write, with clean (keep alive like)
	for (i=0;i<10;i++){
		FAIL_IF_NOT_EQUAL_INT(req->flags,0);
		ok=REQ_WRITE(req, query);
		FAIL_IF_NOT_EQUAL_INT(ok, OCS_CLOSE_CONNECTION);
		FAIL_IF_EQUAL(req->flags,OR_GET|OR_HTTP11);
		
		FAIL_IF_EQUAL(req->headers, NULL);
		FAIL_IF_NOT_EQUAL_STR( onion_dict_get(req->headers,"Host"), "127.0.0.1");
		FAIL_IF_NOT_EQUAL_STR( onion_dict_get(req->headers,"Other-Header"), "My header is very long and with spaces...");
    FAIL_IF_NOT_EQUAL_STR( onion_dict_get(req->headers,"other-heaDER"), "My header is very long and with spaces...");
    FAIL_IF_NOT_EQUAL_STR( onion_request_get_header(req,"other-heaDER"), "My header is very long and with spaces...");

		FAIL_IF_NOT_EQUAL_STR(req->fullpath,"/myurl /is/very/deeply/nested");
		FAIL_IF_NOT_EQUAL_STR(req->path,"myurl /is/very/deeply/nested");

		FAIL_IF_EQUAL(req->GET,NULL);
		FAIL_IF_NOT_EQUAL_STR( onion_dict_get(req->GET,"test"), "test");
		FAIL_IF_NOT_EQUAL_STR( onion_dict_get(req->GET,"query2"), "query 2");
		FAIL_IF_NOT_EQUAL_STR( onion_dict_get(req->GET,"more_query"), " more query 10");
		
		onion_request_clean(req);
		FAIL_IF_NOT_EQUAL(req->GET,NULL);
	}
	
	onion_request_free(req);
	
	END_LOCAL();
}
Esempio n. 21
0
// This fixes that whenever a session is created, but no new data is added, it does not set the proper cookie
void t04_lot_of_sessionid(){
  INIT_LOCAL();

  onion *o=onion_new(O_ONE_LOOP);
  onion_server_set_write(o->server, empty_write);
  
  onion_url *url=onion_root_url(o);
  onion_url_add(url, "^.*", ask_session);
  char sessionid[256];
  char tmp[1024];
  char tmp2[4096];
  onion_request *req;
  int i;
  set_data_on_session=1;

  req=onion_request_new(o->server, NULL, NULL);
  req->fullpath="/";
  onion_request_process(req);
  FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 1);
  FAIL_IF_EQUAL_STR(lastsessionid,"");
  strcpy(sessionid, lastsessionid);
  req->fullpath=NULL;
  onion_request_free(req);

  req=onion_request_new(o->server, NULL, NULL);
  req->fullpath="/";
  snprintf(tmp,sizeof(tmp)," sessionid=xx%sxx;",lastsessionid);
  strcpy(tmp2,"Cookie:");
  for(i=0;i<64;i++)
    strncat(tmp2, tmp, sizeof(tmp2));
  snprintf(tmp,sizeof(tmp)," sessionid=%s\n",lastsessionid);
  strncat(tmp2, tmp, sizeof(tmp2));
  ONION_DEBUG("Set cookies (%d bytes): %s",strlen(tmp2),tmp2);
  strcpy(tmp,"GET /\n");
  onion_request_write(req,tmp,strlen(tmp)); // Here is the problem, at parsing too long headers
  onion_request_write(req,tmp2,strlen(tmp2)); // Here is the problem, at parsing too long headers
	onion_request_write(req,"\n",1);
  //onion_dict_add(req->headers, "Cookie", tmp2, 0);
  
  onion_request_process(req);
  FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 1);
  FAIL_IF_EQUAL_STR(lastsessionid,"");
  FAIL_IF_NOT_EQUAL_STR(lastsessionid, sessionid);
  FAIL_IF_NOT(has_set_cookie);
  onion_request_free(req);
  
  onion_free(o);
  
  END_LOCAL();
}
Esempio n. 22
0
void t14_dict_case_insensitive(){
  INIT_LOCAL();
  
  onion_dict *d=onion_dict_new();
  
  onion_dict_add(d,"Test","OK", 0);
  FAIL_IF_NOT_EQUAL(onion_dict_get(d,"test"),NULL);
  
  onion_dict_set_flags(d,OD_ICASE);
  FAIL_IF_NOT_EQUAL_STR(onion_dict_get(d,"test"),"OK");

	onion_dict_free(d);
	
  END_LOCAL();
}
Esempio n. 23
0
void t06_null_add(){
	INIT_LOCAL();
	onion_dict *dict;
	dict=onion_dict_new();
	
	onion_dict_add(dict,"b",NULL,0);
	onion_dict_add(dict,"a",NULL,0);
	onion_dict_add(dict,"c","1",0);
	
	FAIL_IF_NOT_EQUAL_STR(onion_dict_get(dict,"c"),"1");
	FAIL_IF_NOT_EQUAL(onion_dict_get(dict,"a"),NULL);
	
	onion_dict_free(dict);
	END_LOCAL();
}
Esempio n. 24
0
void t01_create_add_free(){
	INIT_LOCAL();
	
	onion_response *res;
	
	res=onion_response_new(NULL);
	FAIL_IF_NOT_EQUAL(res->code, 200);
	
	FAIL_IF_EQUAL(res,NULL);
	
	onion_response_set_length(res,1024);
	FAIL_IF_NOT_EQUAL_STR(onion_dict_get(res->headers,"Content-Length"),"1024");
	
	onion_response_free(res);
	
	END_LOCAL();
}
Esempio n. 25
0
onion_connection_status post_json_check(json_response *post, onion_request *req, onion_response *res){
	post->processed=1;
	
	FAIL_IF_NOT_EQUAL_INT(onion_request_get_flags(req)&OR_METHODS, OR_POST);
	FAIL_IF_NOT_EQUAL_STR(onion_request_get_header(req, "Content-Type"),"application/json");
	const onion_block *data=onion_request_get_data(req);
	FAIL_IF_EQUAL(data, NULL);
	if (!data)
		return OCS_INTERNAL_ERROR;
	
	FAIL_IF_NOT_EQUAL_INT(onion_block_size(data), sizeof(JSON_EXAMPLE));
	FAIL_IF_NOT_EQUAL_INT(memcmp(onion_block_data(data), JSON_EXAMPLE, sizeof(JSON_EXAMPLE)), 0);
	
	post->processed=2;
	
	return OCS_PROCESSED;
}
Esempio n. 26
0
void t13_dict_rget(){
	INIT_LOCAL();
	
	onion_dict *A=onion_dict_new();
	onion_dict *B=onion_dict_new();
	onion_dict *C=onion_dict_new();
	onion_dict *D=onion_dict_new();
	
	int i;
	for (i=0;i<16;i++){
		char tmp[9];
		sprintf(tmp,"%08X",rand());
		onion_dict_add(A, tmp, tmp, OD_DUP_ALL);
		sprintf(tmp,"%08X",rand());
		onion_dict_add(B, tmp, tmp, OD_DUP_ALL);
		sprintf(tmp,"%08X",rand());
		onion_dict_add(C, tmp, tmp, OD_DUP_ALL);
		sprintf(tmp,"%08X",rand());
		onion_dict_add(D, tmp, tmp, OD_DUP_ALL);
	}

	onion_dict_add(A, "B", B, OD_DICT|OD_FREE_VALUE);
	onion_dict_add(A, "C", C, OD_DICT|OD_FREE_VALUE);
	onion_dict_add(A, "D", D, OD_DICT|OD_FREE_VALUE);
	
	onion_dict_add(B, "C", C, OD_DICT);
	
	onion_dict_add(C, "a", "hello", 0);
	
	FAIL_IF_NOT_EQUAL(onion_dict_rget(A, "B", NULL), NULL);
	FAIL_IF_NOT_EQUAL(onion_dict_rget(A, "C", NULL), NULL);
	FAIL_IF_NOT_EQUAL(onion_dict_rget(A, "B", "C", NULL), NULL);

	FAIL_IF_NOT_EQUAL(onion_dict_rget_dict(A, "B", NULL), B);
	FAIL_IF_NOT_EQUAL(onion_dict_rget_dict(A, "C", NULL), C);
	FAIL_IF_NOT_EQUAL(onion_dict_rget_dict(A, "B", "C", NULL), C);
	
	FAIL_IF_NOT_EQUAL_STR(onion_dict_rget(A, "B", "C", "a", NULL), "hello");
	FAIL_IF_NOT_EQUAL(onion_dict_rget_dict(A, "B", "C", "a", NULL), NULL);
	
	// This should remove all the others, as they hang from it.
	onion_dict_free(A);
	
	END_LOCAL();
}
Esempio n. 27
0
void t03_create_add_free_full_flow(){
	INIT_LOCAL();
	
	onion_request *req;
	int ok;
	
	req=onion_request_new(custom_io);
	FAIL_IF_EQUAL(req,NULL);
	FAIL_IF_NOT_EQUAL(req->connection.fd, -1);
	
	ok=REQ_WRITE(req,"GET /myurl%20/is/very/deeply/nested?test=test&query2=query%202&more_query=%20more%20query+10&empty&empty2 HTTP/1.0\n");
	FAIL_IF_NOT(ok);
	ok=REQ_WRITE(req,"Host: 127.0.0.1\r\n");
	FAIL_IF_NOT(ok);
	ok=REQ_WRITE(req,"Other-Header: My header is very long and with spaces...\n");
	FAIL_IF_NOT(ok);
	ok=REQ_WRITE(req,"Final-Header: This header do not get into headers as a result of now knowing if its finished, or if its multiline.\n");
	FAIL_IF_NOT(ok);
	
	FAIL_IF_EQUAL(req->flags,OR_GET|OR_HTTP11);
	
	FAIL_IF_EQUAL(req->headers, NULL);
	FAIL_IF_NOT_EQUAL_STR( onion_dict_get(req->headers,"Host"), "127.0.0.1");
	FAIL_IF_NOT_EQUAL_STR( onion_dict_get(req->headers,"Other-Header"), "My header is very long and with spaces...");

	FAIL_IF_NOT_EQUAL_STR(req->fullpath,"/myurl /is/very/deeply/nested");
  FAIL_IF_NOT_EQUAL(req->path,NULL);
  onion_request_process(req); // this should set the req->path.
	FAIL_IF_NOT_EQUAL_STR(req->path,"myurl /is/very/deeply/nested");

	FAIL_IF_EQUAL(req->GET, NULL);
	FAIL_IF_NOT_EQUAL_STR( onion_dict_get(req->GET,"test"), "test");
	FAIL_IF_NOT_EQUAL_STR( onion_dict_get(req->GET,"query2"), "query 2");
	FAIL_IF_NOT_EQUAL_STR( onion_dict_get(req->GET,"more_query"), " more query 10");
	FAIL_IF_EQUAL(onion_request_get_query(req, "empty"), NULL);
	FAIL_IF_EQUAL(onion_request_get_query(req, "empty2"), NULL);
	FAIL_IF_NOT_EQUAL(onion_request_get_query(req, "empty3"), NULL);
	
	onion_request_free(req);
	
	END_LOCAL();
}
Esempio n. 28
0
void t01_create_add_free_10(){
	INIT_LOCAL();
	onion_dict *dict;
	const char *value;
	
	dict=onion_dict_new();
	FAIL_IF_EQUAL(dict,NULL);

	// Get before anything in
	value=onion_dict_get(dict, "Request");
	FAIL_IF_NOT_EQUAL(value,NULL);

	// basic add
	int i;
	char tmp[256];
	for (i=0;i<10;i++){
		snprintf(tmp,sizeof(tmp),"%d",(i*13)%10);
		////ONION_DEBUG("add key %s",tmp);
		onion_dict_add(dict, tmp, "GET /", OD_DUP_ALL);
		value=onion_dict_get(dict, tmp);
		FAIL_IF_NOT_EQUAL_STR(value,"GET /");
		//onion_dict_print_dot(dict);
	}
	for (i=0;i<10;i++){
		snprintf(tmp,sizeof(tmp),"%d",i);
		////ONION_DEBUG("rm key %s",tmp);
		onion_dict_remove(dict, tmp);
		value=onion_dict_get(dict, tmp);
		FAIL_IF_NOT_EQUAL(value,NULL);
		//onion_dict_print_dot(dict);
	}
	
	onion_dict_free(dict);
	
	END_LOCAL();
}
Esempio n. 29
0
void t05_preorder(){
	INIT_LOCAL();
	onion_dict *dict;
	dict=onion_dict_new();
	
	onion_dict_add(dict,"A","B",0);
	onion_dict_add(dict,"C","D",0);
	onion_dict_add(dict,"E","F",0);
	onion_dict_add(dict,"G","H",0);
	onion_dict_add(dict,"I","J",0);
	onion_dict_add(dict,"K","L",0);
	onion_dict_add(dict,"M","N",0);
	onion_dict_add(dict,"O","P",0);
	onion_dict_add(dict,"Q","R",0);
	onion_dict_add(dict,"S","T",0);
	
	char buffer[4096];
	memset(buffer,0,sizeof(buffer));
	onion_dict_preorder(dict, append_as_headers, buffer);
	FAIL_IF_NOT_EQUAL_STR(buffer,"A: B\nC: D\nE: F\nG: H\nI: J\nK: L\nM: N\nO: P\nQ: R\nS: T\n");
	
	onion_dict_free(dict);
	END_LOCAL();
}
Esempio n. 30
0
void t11_cookies(){
	INIT_LOCAL();
	
	onion_request *req;
	int ok;
	
	req=onion_request_new(custom_io);
	FAIL_IF_EQUAL(req,NULL);
	FAIL_IF_NOT_EQUAL(req->connection.fd, -1);
	
	{
		const char *query="GET / HTTP/1.0\n"
											"Content-Type: application/x-www-form-urlencoded\n"
											"Host: 127.0.0.1\n\r"
											"Cookie: key1=value1; key2=value2;\n"
											"Accept-Language: en\n"; // Missing \n caused memleak, to check with valgrind
		
		ok=onion_request_write(req,query,strlen(query));
	}
	FAIL_IF_EQUAL(ok,OCS_INTERNAL_ERROR);
	FAIL_IF_NOT_EQUAL_STR(onion_request_get_header(req,"Host"),"127.0.0.1");
	FAIL_IF_NOT_EQUAL_STR(onion_request_get_header(req,"Cookie"), "key1=value1; key2=value2;");
	
	FAIL_IF_NOT_EQUAL_STR(onion_request_get_cookie(req,"key1"), "value1");
	FAIL_IF_NOT_EQUAL_STR(onion_request_get_cookie(req,"key2"), "value2");
	FAIL_IF_EQUAL_STR(onion_request_get_cookie(req," key2"), "value2");
	
	onion_dict *cookies=onion_request_get_cookies_dict(req);
	FAIL_IF_EQUAL(cookies, NULL);
	
	FAIL_IF_NOT_EQUAL_STR(onion_dict_get(cookies,"key1"), "value1");
	FAIL_IF_NOT_EQUAL_STR(onion_dict_get(cookies,"key2"), "value2");
	
	onion_request_free(req);
	
	
	END_LOCAL();
}