Beispiel #1
0
void t05_server_timeout_threaded_ssl(){
  INIT_LOCAL();
  CURL *curl=prepare_curl("https://localhost:8081");

  ONION_DEBUG("%s",__FUNCTION__);
  o=onion_new(O_THREADED | O_DETACH_LISTEN);
  onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL));
  FAIL_IF_NOT_EQUAL_INT(onion_set_certificate(o, O_SSL_CERTIFICATE_KEY, "mycert.pem", "mycert.pem"),0);
  onion_set_port(o,"8081");
  onion_set_timeout(o,3000);
  onion_listen(o);
  sleep(1);

  int fd=connect_to("localhost","8081");
  sleep(4);
  // Should have closed the connection
  int w=write(fd,"GET /\n\n",7);
  FAIL_IF_NOT_EQUAL_INT(w,7);
  char data[256];
  FAIL_IF(read(fd, data,sizeof(data))>0);
  close(fd);

  FAIL_IF_NOT(curl_get(curl, "https://localhost:8081"));

	onion_free(o);

	curl_easy_cleanup(curl);
  END_LOCAL();
}
Beispiel #2
0
void t02_long_template(){
	INIT_LOCAL();
	int count=0;

	onion *s=onion_new(0);

	onion_set_root_handler(s, onion_handler_new((void*)AGPL_txt_handler_page, NULL, NULL));
	onion_listen_point *lp=onion_buffer_listen_point_new();
	onion_add_listen_point(s,NULL,NULL,lp);
	lp->write=count_bytes;


	onion_request *req=onion_request_new(lp);
	req->connection.listen_point->close(req);
	req->connection.user_data=&count;
	req->connection.listen_point->close=NULL;


  FAIL_IF_NOT_EQUAL_INT(onion_request_write0(req, "GET /\n\n"), OCS_REQUEST_READY);
  FAIL_IF_NOT_EQUAL_INT(onion_request_process(req), OCS_CLOSE_CONNECTION);

	FAIL_IF(count<30000);

	onion_request_free(req);
	onion_free(s);

	END_LOCAL();
}
Beispiel #3
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();
}
Beispiel #4
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();
}
Beispiel #5
0
void do_petition_set_threaded(float wait_s, float wait_c, int nrequests, char close, int nthreads){
  ONION_DEBUG("Using %d threads, %d petitions per thread",nthreads,nrequests);
  processed=0;

  params_t params;
  params.wait_s=wait_s;
  params.wait_t=wait_c;
  params.n_requests=nrequests;
  params.close_at_n=close;

  pthread_t *thread=malloc(sizeof(pthread_t*)*nthreads);
  pthread_t listen_thread;
  int i;
  pthread_create(&listen_thread, NULL, (void*)do_listen, NULL);
  for (i=0;i<nthreads;i++){
    pthread_create(&thread[i], NULL, (void*)do_requests, &params);
  }
  for (i=0;i<nthreads;i++){
    pthread_join(thread[i], NULL);
  }
  free(thread);
  if (close==2){
    usleep(wait_s*1000000);
    onion_listen_stop(o);
  }
  pthread_join(listen_thread, NULL);


  FAIL_IF_NOT_EQUAL_INT(params.n_requests * nthreads, processed);
}
Beispiel #6
0
void t04_server_timeout_threaded(){
  INIT_LOCAL();
  CURL *curl=prepare_curl("http://localhost:8082");

  o=onion_new(O_THREADED | O_DETACH_LISTEN);
  onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL));
  onion_set_port(o,"8082");
  onion_set_timeout(o,2000);
  onion_listen(o);
  sleep(1);

  int fd=connect_to("localhost","8082");
  sleep(3);
  // Should have closed the connection
  int w=write(fd,"GET /\n\n",7);
  FAIL_IF_NOT_EQUAL_INT(w,7);
  char data[256];
  FAIL_IF(read(fd, data,sizeof(data))>0);
  close(fd);

  FAIL_IF_NOT(curl_get(curl, "http://localhost:8082"));

  onion_free(o);
	curl_easy_cleanup(curl);
  END_LOCAL();
}
Beispiel #7
0
void t05_post_content_json(){
	INIT_LOCAL();

	onion *server=onion_new(0);
	onion_listen_point *lp=onion_buffer_listen_point_new();
	json_response post_json = { 0 };
	
	onion_add_listen_point(server,NULL,NULL,lp);
	onion_set_root_handler(server, onion_handler_new((void*)&post_json_check,&post_json,NULL));
	
	onion_request *req=onion_request_new(lp);
#define POST_HEADER "POST / HTTP/1.1\nContent-Type: application/json\nContent-Length: %d\n\n"
	char tmp[1024];
	int json_length=sizeof(JSON_EXAMPLE);
	ONION_DEBUG("Post size is about %d",json_length);
	snprintf(tmp, sizeof(tmp), POST_HEADER, json_length);
// 	ONION_DEBUG("%s",tmp);
	onion_request_write(req,tmp,strlen(tmp));
	onion_request_write(req,JSON_EXAMPLE,json_length);
// 	ONION_DEBUG("%s",JSON_EXAMPLE);
	
	FAIL_IF_NOT_EQUAL_INT(post_json.processed, 2);
	
	onion_request_free(req);
	onion_free(server);
	
	END_LOCAL();
}
Beispiel #8
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;
}
Beispiel #9
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();
}
Beispiel #10
0
void t03_server_https(){
  INIT_LOCAL();
  CURL *curl=prepare_curl("https://localhost:8080");

  o=onion_new(O_ONE_LOOP | O_DETACH_LISTEN);
  onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL));
  FAIL_IF_NOT_EQUAL_INT(onion_set_certificate(o, O_SSL_CERTIFICATE_KEY, "mycert.pem", "mycert.pem"),0);
  FAIL_IF_NOT_EQUAL_INT(onion_listen(o),0);
  //do_petition_set(1,1,1,1);
  sleep(1);
  //FAIL_IF_EQUAL_INT(  curl_get_to_fail("http://localhost:8080"), HTTP_OK);
  sleep(1);
  FAIL_IF_NOT_EQUAL_INT(  curl_get(curl, "https://localhost:8080"), HTTP_OK);
  sleep(1);
  onion_free(o);

	curl_easy_cleanup(curl);
  END_LOCAL();
}
Beispiel #11
0
void t07_replace(){
	INIT_LOCAL();
	onion_dict *dict=onion_dict_new();
	
	onion_dict_add(dict,"a","1", OD_DUP_ALL|OD_REPLACE);
	onion_dict_add(dict,"a","1", OD_REPLACE);
	onion_dict_add(dict,"a","1", OD_DUP_ALL|OD_REPLACE);
	onion_dict_add(dict,"a","1", OD_REPLACE);
	onion_dict_add(dict,"a","1", OD_DUP_ALL|OD_REPLACE);
	
	int n=0;
	onion_dict_preorder(dict, t07_sum, &n);
	FAIL_IF_NOT_EQUAL_INT(n,1);

	onion_dict_add(dict,"a","1", 0);
	n=0;
	onion_dict_preorder(dict, t07_sum, &n);
	FAIL_IF_NOT_EQUAL_INT(n,2);
	
	onion_dict_free(dict);
	
	END_LOCAL();
}
Beispiel #12
0
int curl_get(CURL *curl, const char *url){
  CURLcode res=curl_easy_perform(curl);
  FAIL_IF_NOT_EQUAL((int)res,0);
	if (res!=0){
		ONION_ERROR("%s",curl_easy_strerror(res));
	}
  long int http_code;
  res=curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &http_code);
  FAIL_IF_NOT_EQUAL((int)res,0);
  char buffer[1024]; size_t l;
  curl_easy_recv(curl,buffer,sizeof(buffer),&l);
  FAIL_IF_NOT_EQUAL_INT((int)http_code, HTTP_OK);

  return http_code;
}
Beispiel #13
0
void t01_websocket_server_no_ws() {
  INIT_LOCAL();

  memset(&ws_status, 0, sizeof(ws_status));
  onion *o = websocket_server_new();
  onion_request *req = onion_request_new(onion_get_listen_point(o, 0));
  onion_request_write0(req, "GET /\n\n");
  onion_request_process(req);
  FAIL_IF(ws_status.is_connected);
  FAIL_IF_NOT_EQUAL_INT(ws_status.connected, 1);

  onion_request_free(req);
  onion_free(o);

  END_LOCAL();
}
Beispiel #14
0
void t02_websocket_server_w_ws() {
  INIT_LOCAL();

  memset(&ws_status, 0, sizeof(ws_status));
  onion *o = websocket_server_new();
  onion_request *req = onion_request_new(onion_get_listen_point(o, 0));
  onion_request_write0(req,
                       "GET /\nUpgrade: websocket\nSec-Websocket-Version: 13\nSec-Websocket-Key: My-key\n\n");
  onion_request_process(req);
  FAIL_IF_NOT(ws_status.is_connected);
  FAIL_IF_NOT_EQUAL_INT(ws_status.connected, 1);

  onion_request_free(req);
  onion_free(o);

  END_LOCAL();
}
Beispiel #15
0
void do_timeout_request(){
  ONION_INFO("Start timeout requests");
  int i;
  for (i=0;i<10;i++){
    int fd=connect_to("localhost","8081");
    if ((i&1) == 1)
      usleep(500000);
    int w=write(fd,"GET /\n\n",7);
    fsync(fd);
    FAIL_IF_NOT_EQUAL_INT(w,7);
    shutdown(fd, SHUT_RDWR);
    // Should have closed the connection
    char data[256];
    FAIL_IF(read(fd, data, sizeof(data))>0);
    close(fd);
  }
}
Beispiel #16
0
void t09_very_long_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_t="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: ";
		const int longsize=strlen(query_t)+16;
		char *query=malloc(longsize); // 1MB enought?
		strcpy(query, query_t);
		int i;
		for (i=strlen(query); i<longsize -2; i++) // fill with crap
			query[i]='a'+(i%30);
		query[longsize-3]='\n';
		query[longsize-2]='\n';
		query[longsize-1]=0;
		
		FAIL_IF_NOT_EQUAL_INT(strlen(query), longsize-1);
		
		ok=onion_request_write(req, query, longsize);
		free(query);
	}
	FAIL_IF_EQUAL_INT(ok,OCS_INTERNAL_ERROR);
	
	onion_request_free(req);
	
	END_LOCAL();
}
Beispiel #17
0
/// There is a bug when posting large files. Introduced when change write 1 by 1, to write by blocks on the FILE parser
void t04_post_largefile(){
	INIT_LOCAL();
	
	int postfd=open(BIG_FILE, O_RDONLY);
	off_t filesize=lseek(postfd, 0, SEEK_END);
	lseek(postfd, 0, SEEK_SET);
	
	expected_post post={};;
	post.filename=BIG_FILE_BASE;
	post.test_ok=0; // Not ok as not called processor yet
	post.tmpfilename=NULL;
	post.size=filesize;
	
	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_new((void*)&post_check,&post,NULL));
	
	onion_request *req=onion_request_new(lp);

#define POST_HEADER "POST / HTTP/1.1\nContent-Type: multipart/form-data; boundary=end\nContent-Length: %d\n\n--end\nContent-Disposition: text/plain; name=\"file\"; filename=\"" BIG_FILE_BASE "\"\n\n"
	char tmp[1024];
	ONION_DEBUG("Post size is about %d",filesize+73);
	snprintf(tmp, sizeof(tmp), POST_HEADER, (int)filesize+73);
	ONION_DEBUG("%s",tmp);
	onion_request_write(req,tmp,strlen(tmp));
	
	int r=read(postfd, tmp, sizeof(tmp));
	while ( r>0 ){
		onion_request_write(req, tmp, r);
		r=read(postfd, tmp, sizeof(tmp));
	}
	
	onion_request_write(req,"\n--end--",8);
	FAIL_IF_NOT_EQUAL_INT(post.test_ok,1);
#undef POST_HEADER
	onion_request_clean(req);


	//post.test_ok=0; // Not ok as not called processor yet

	lseek(postfd, 0, SEEK_SET);

	int difffd=open(post.tmpfilename, O_RDONLY);
	FAIL_IF_NOT_EQUAL_INT(difffd,-1); // Orig file is removed at handler returns. But i have a copy
	difffd=open(post.tmplink, O_RDONLY);
	FAIL_IF_EQUAL_INT(difffd,-1);
	ONION_DEBUG("tmp filename %s",post.tmpfilename);
	int r1=1, r2=1;
	char c1=0, c2=0;
	int p=0;
	while ( r1 && r2 && c1==c2){
		r1=read(difffd, &c1, 1);
		r2=read(postfd, &c2, 1);
		//ONION_DEBUG("%d %d",c1,c2);
		FAIL_IF_NOT_EQUAL_INT(c1,c2);
		p++;
	}
	if ( r1 || r2 ){
		ONION_ERROR("At %d",p);
		FAIL_IF_NOT_EQUAL_INT(r1,0);
		FAIL_IF_NOT_EQUAL_INT(r2,0);
		FAIL_IF_NOT_EQUAL_INT(c1,c2);
		FAIL("Files are different");
	}
	else
		ONION_DEBUG("Files are ok");
	
	close(difffd);
	close(postfd);

	onion_request_free(req);

	if (post.tmpfilename){
		struct stat st;
		FAIL_IF_EQUAL(stat(post.tmpfilename,&st), 0); // Should not exist
	}
	
	onion_free(server);
	if (post.tmpfilename)
		free(post.tmpfilename);
	if (post.tmplink)
		free(post.tmplink);
	
	END_LOCAL();
}
Beispiel #18
0
// This fixes that whenever a session is created, but no new data is added, it does not set the proper cookie, and should not create a new session.
void t03_bug_empty_session_is_new_session(){
  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[256];

  set_data_on_session=1;
  onion_request *req=onion_request_new(o->server, NULL, NULL);
  req->fullpath="/";
  onion_request_process(req);
  FAIL_IF_EQUAL_STR(lastsessionid,"");
  strcpy(sessionid, lastsessionid);
  req->fullpath=NULL;
  onion_request_free(req);
  FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 1);
  
  req=onion_request_new(o->server, NULL, NULL);
  req->fullpath="/";
  onion_dict_add(req->headers, "Cookie", "sessionid=xxx", 0);
  onion_request_process(req);
  FAIL_IF_EQUAL_STR(lastsessionid,"");
  FAIL_IF_EQUAL_STR(lastsessionid, sessionid);
  FAIL_IF_NOT(has_set_cookie);
  req->fullpath=NULL;
  onion_request_free(req);
  FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 2);
  
  req=onion_request_new(o->server, NULL, NULL);
  req->fullpath="/";
  snprintf(tmp,sizeof(tmp),"sessionid=%s",lastsessionid);
  onion_dict_add(req->headers, "Cookie", tmp, 0);
  onion_request_process(req);
  FAIL_IF_EQUAL_STR(lastsessionid,"");
  FAIL_IF_EQUAL_STR(lastsessionid, sessionid);
  FAIL_IF_NOT(has_set_cookie);
  strcpy(sessionid, lastsessionid);
  req->fullpath=NULL;
  onion_request_free(req);
  FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 2);
  
  req=onion_request_new(o->server, NULL, NULL);
  req->fullpath="/";
  snprintf(tmp,sizeof(tmp),"sessionid=%sxx",lastsessionid);
  onion_dict_add(req->headers, "Cookie", tmp, 0);
  onion_request_process(req);
  FAIL_IF_EQUAL_STR(lastsessionid,"");
  FAIL_IF_EQUAL_STR(lastsessionid, sessionid);
  FAIL_IF_NOT(has_set_cookie);
  req->fullpath=NULL;
  onion_request_free(req);
  FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 3);

  // Ask for new, without session data, but I will not set data on session, so session is not created.
  set_data_on_session=0;
  req=onion_request_new(o->server, NULL, NULL);
  req->fullpath="/";
  onion_request_process(req);
  FAIL_IF_EQUAL_STR(lastsessionid,"");
  strcpy(sessionid, lastsessionid);
  req->fullpath=NULL;
  FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 4); // For a moment it exists, until onion realizes is not necesary.
  onion_request_free(req);
  FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 3);

  
  onion_free(o);
  
  END_LOCAL();
}
Beispiel #19
0
void t01_call_otemplate(){
  INIT_LOCAL();


  onion *s=onion_new(0);

  onion_set_root_handler(s, onion_handler_new((void*)_13_otemplate_html_handler_page, NULL, NULL));
	onion_listen_point *lp=onion_buffer_listen_point_new();
	onion_add_listen_point(s,NULL,NULL,lp);

	struct tests_call_otemplate tests;

  onion_request *req=onion_request_new(lp);
  FAIL_IF_NOT_EQUAL_INT(onion_request_write0(req, "GET /\n\n"), OCS_REQUEST_READY);
  FAIL_IF_NOT_EQUAL_INT(onion_request_process(req), OCS_CLOSE_CONNECTION);

  ONION_INFO("Got %s",onion_buffer_listen_point_get_buffer_data(req));
  check_tests(onion_buffer_listen_point_get_buffer(req), &tests);

  FAIL_IF_NOT_EQUAL_INT(tests.ok_hello,1);
  FAIL_IF_NOT_EQUAL_INT(tests.ok_list,0);
  FAIL_IF_NOT_EQUAL_INT(tests.ok_title,0);
  FAIL_IF_NOT_EQUAL_INT(tests.ok_title_title,0);
  FAIL_IF_NOT_EQUAL_INT(tests.ok_encoding,0);


  onion_dict *d=onion_dict_new();
  onion_dict_add(d, "title", "TITLE",0);
  onion_dict_add(d, "hello", "SHOULD NOT APPEAR",0);
	onion_dict_add(d, "quoted", "<\"Hello>",0);

  onion_request_clean(req);
	onion_handler_free(onion_get_root_handler(s));
  onion_set_root_handler(s, onion_handler_new((void*)_13_otemplate_html_handler_page, d, NULL));
  FAIL_IF_NOT_EQUAL_INT(onion_request_write0(req, "GET /\n\n"), OCS_REQUEST_READY);
  FAIL_IF_NOT_EQUAL_INT(onion_request_process(req), OCS_CLOSE_CONNECTION);

  ONION_INFO("Got %s",onion_buffer_listen_point_get_buffer_data(req));
  check_tests(onion_buffer_listen_point_get_buffer(req), &tests);

  FAIL_IF_NOT_EQUAL_INT(tests.ok_hello,1);
  FAIL_IF_NOT_EQUAL_INT(tests.ok_list,0);
  FAIL_IF_NOT_EQUAL_INT(tests.ok_title,1);
  FAIL_IF_NOT_EQUAL_INT(tests.ok_title_title,1);
  FAIL_IF_NOT_EQUAL_INT(tests.ok_encoding,1);


  onion_dict *d2=onion_dict_new();
  onion_dict_add(d2,"0","LIST 1",0);
  onion_dict_add(d2,"1","LIST 2",0);
  onion_dict_add(d2,"2","LIST 3",0);
  onion_dict_add(d,"list",d2, OD_DICT|OD_FREE_VALUE);

	onion_dict *f1=onion_dict_new();
	onion_dict *f2=onion_dict_new();
	onion_dict_add(f2, "0", "internal",0);
	onion_dict_add(f2, "1", "loop",0);
	onion_dict_add(f1, "loop", f2, OD_DICT|OD_FREE_VALUE);

	onion_dict_add(d, "loop", f1, OD_DICT|OD_FREE_VALUE);

  onion_request_clean(req);
	onion_handler_free(onion_get_root_handler(s));
  onion_set_root_handler(s, onion_handler_new((void*)_13_otemplate_html_handler_page, d, (void*)onion_dict_free));
    FAIL_IF_NOT_EQUAL_INT(onion_request_write0(req, "GET /\n\n"), OCS_REQUEST_READY);
  FAIL_IF_NOT_EQUAL_INT(onion_request_process(req), OCS_CLOSE_CONNECTION);
  check_tests(onion_buffer_listen_point_get_buffer(req), &tests);
  ONION_INFO("Got %s",onion_buffer_listen_point_get_buffer_data(req));

	FAIL_IF_NOT_EQUAL_INT(tests.ok_hello,1);
  FAIL_IF_NOT_EQUAL_INT(tests.ok_list,1);
  FAIL_IF_NOT_EQUAL_INT(tests.ok_title,1);
  FAIL_IF_NOT_EQUAL_INT(tests.ok_title_title,1);
  FAIL_IF_NOT_EQUAL_INT(tests.ok_internal_loop,1);


  onion_request_free(req);
  onion_free(s);

  END_LOCAL();
}
Beispiel #20
0
void t08_sockaddr_storage(){
	INIT_LOCAL();
	onion_request *req;
	
	
	{
		struct sockaddr_storage client_addr;
		socklen_t client_len=0;
		
		req=onion_request_new(custom_io);
		FAIL_IF_EQUAL(onion_request_get_sockadd_storage(req, NULL), &client_addr);
		FAIL_IF_EQUAL(onion_request_get_sockadd_storage(req, &client_len), &client_addr);
		FAIL_IF_NOT_EQUAL_INT(client_len, 0);
		onion_request_free(req);
	}
	
	{
		struct sockaddr_storage *client_addr;
		socklen_t client_len=0;
		struct addrinfo hints;
		struct addrinfo *result, *rp;
		char hostA[128], portA[16];
		char hostB[128], portB[16];
		
		memset(&hints,0, sizeof(struct addrinfo));
		hints.ai_canonname=NULL;
		hints.ai_addr=NULL;
		hints.ai_next=NULL;
		hints.ai_socktype=SOCK_STREAM;
		hints.ai_family=AF_UNSPEC;
		hints.ai_flags=AI_PASSIVE|AI_NUMERICSERV;

		int err=getaddrinfo("localhost","8080", &hints, &result);
		FAIL_IF_NOT_EQUAL_INT(err,0);
		if (err!=0)
			goto exit;
		for(rp=result;rp!=NULL;rp=rp->ai_next){
			memset(hostA,0,sizeof(hostA));
			memset(hostB,0,sizeof(hostB));
			memset(portA,0,sizeof(portA));
			memset(portB,0,sizeof(portB));
			
			getnameinfo(rp->ai_addr, rp->ai_addrlen, hostA, sizeof(hostA), portA, sizeof(portA), NI_NUMERICHOST | NI_NUMERICSERV);
			req=onion_request_new_from_socket(NULL, 0,(struct sockaddr_storage *)rp->ai_addr, rp->ai_addrlen);
			client_addr=onion_request_get_sockadd_storage(req, &client_len);
			FAIL_IF_EQUAL(client_addr, (struct sockaddr_storage *)rp->ai_addr);
			FAIL_IF_EQUAL(client_addr, NULL);
			
			getnameinfo((struct sockaddr *)client_addr, client_len, hostB, sizeof(hostB), portB, sizeof(portB), NI_NUMERICHOST | NI_NUMERICSERV);
			
			FAIL_IF_NOT_EQUAL_STR(hostA, hostB);
			FAIL_IF_NOT_EQUAL_STR(portA, portB);
			FAIL_IF_NOT_EQUAL_STR(hostA, onion_request_get_client_description(req));
			onion_request_free(req);
		}
		freeaddrinfo(result);
		
	}
	
	{
		req=onion_request_new(custom_io); //NULL, NULL, NULL);
		struct sockaddr_storage *client_addr;
		socklen_t client_len;
		client_addr=onion_request_get_sockadd_storage(req, &client_len);
		FAIL_IF_NOT_EQUAL(client_addr, NULL);
		onion_request_free(req);
	}
	
exit:
	END_LOCAL();
}