void cmd_connect_fail_message(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "*****@*****.**", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); expect_any(accounts_get_account, name); will_return(accounts_get_account, NULL); will_return(ui_ask_password, strdup("password")); expect_cons_show("Connecting as [email protected]"); expect_any(jabber_connect_with_details, jid); expect_any(jabber_connect_with_details, passwd); expect_any(jabber_connect_with_details, altdomain); expect_any(jabber_connect_with_details, port); will_return(jabber_connect_with_details, JABBER_DISCONNECTED); expect_cons_show_error("Connection attempt for [email protected] failed."); gboolean result = cmd_connect(NULL, args, *help); assert_true(result); free(help); }
/** * Handle input command. * * This function should return 0, even if the command failed. * Return of != 0 means the event loop will quit. */ static int handle_input_command(const dstring* string) { dstrlist* list; dstring** argv; unsigned int argc; int error; ENetPacket* packet; if (string->len == 0) { /* empty command */ return 0; } else if (string->data[0] == '/') { /* local command */ /* parse the command */ list = dstrlex_parse(string, &error); if (list == NULL) { fprintf(stderr, "Failed to parse command string '%s': %s\n", string->data, dstrlex_errstr(error)); return 0; } /* convert list to vector */ argc = list->size; argv = dlist_tovector(list); dlist_free(list); /* select command to execute */ if (dcmpcs(argv[0], "/connect") == 0) { error = cmd_connect(argc, argv); } else if (dcmpcs(argv[0], "/disconnect") == 0) { error = cmd_disconnect(argc, argv); } else if (dcmpcs(argv[0], "/quit") == 0) { error = cmd_quit(argc, argv); } else if (dcmpcs(argv[0], "/mute") == 0) { error = cmd_mute(argc, argv); } else if (dcmpcs(argv[0], "/deafen") == 0) { error = cmd_deafen(argc, argv); } else { fprintf(stderr, "Unknown command '%s'\n", argv[0]->data); error = 0; } dvec_free(argv); return error; } else if (client.state == SVCECLIENT_STATE_CONNECTED) { /* send to server if connected */ mutex_lock(&client.network_lock); packet = enet_packet_create(string->data, string->len, ENET_PACKET_FLAG_RELIABLE); enet_peer_send(client.client, 0, packet); mutex_unlock(&client.network_lock); } else if (client.state == SVCECLIENT_STATE_CONNECTING) { /* server command but still connecting */ fprintf(stderr, "Can't send command to server, still connecting.\n"); } else { /* server command but not connected */ fprintf(stderr, "Can't send command to server, not connected.\n"); } return 0; }
void cmd_connect_lowercases_argument(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "*****@*****.**", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); expect_string(accounts_get_account, name, "*****@*****.**"); will_return(accounts_get_account, NULL); will_return(ui_ask_password, strdup("password")); expect_cons_show("Connecting as [email protected]"); expect_any(jabber_connect_with_details, jid); expect_any(jabber_connect_with_details, passwd); expect_any(jabber_connect_with_details, altdomain); expect_any(jabber_connect_with_details, port); will_return(jabber_connect_with_details, JABBER_CONNECTING); gboolean result = cmd_connect(NULL, args, *help); assert_true(result); free(help); }
void cmd_connect_with_server_and_port_when_provided(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "*****@*****.**", "port", "5432", "server", "aserver", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); expect_string(accounts_get_account, name, "*****@*****.**"); will_return(accounts_get_account, NULL); will_return(ui_ask_password, strdup("password")); expect_cons_show("Connecting as [email protected]"); expect_string(jabber_connect_with_details, jid, "*****@*****.**"); expect_string(jabber_connect_with_details, passwd, "password"); expect_string(jabber_connect_with_details, altdomain, "aserver"); expect_value(jabber_connect_with_details, port, 5432); will_return(jabber_connect_with_details, JABBER_CONNECTING); gboolean result = cmd_connect(NULL, args, *help); assert_true(result); free(help); }
int main(int argc, const char *argv[]) { if (cpn_global_init() < 0) return -1; if (cpn_opts_parse_cmd(opts, argc, argv) < 0) { return -1; } switch (cpn_opts_get(opts, 'v', NULL)->counter) { case 0: cpn_log_set_level(LOG_LEVEL_ERROR); break; case 1: cpn_log_set_level(LOG_LEVEL_WARNING); break; case 2: cpn_log_set_level(LOG_LEVEL_VERBOSE); break; case 3: cpn_log_set_level(LOG_LEVEL_TRACE); break; default: break; } if (cpn_cfg_parse(&cfg, cpn_opts_get(opts, 'c', NULL)->string) < 0) { printf("Could not parse config '%s", cpn_opts_get(opts, 'c', NULL)->string); return -1; } if (cpn_sign_keys_from_config(&local_keys, &cfg) < 0) { puts("Could not keys from config"); return -1; } memcpy(&remote_key, &cpn_opts_get(opts, 0, "--remote-key")->sigkey, sizeof(struct cpn_sign_pk)); remote_host = cpn_opts_get(opts, 0, "--remote-host")->string; remote_port = cpn_opts_get(opts, 0, "--remote-port")->uint32; if (cpn_opts_get(opts, 0, "query")) return cmd_query(); else if (cpn_opts_get(opts, 0, "request")) return cmd_request(cpn_opts_get(request_opts, 0, "--service-type")->string, &cpn_opts_get(request_opts, 0, "--parameters")->stringlist); else if (cpn_opts_get(opts, 0, "connect")) return cmd_connect(cpn_opts_get(connect_opts, 0, "--service-type")->string, cpn_opts_get(connect_opts, 0, "--session-id")->uint32, cpn_opts_get(connect_opts, 0, "--session-cap")->string); else if (cpn_opts_get(opts, 0, "terminate")) return cmd_terminate(cpn_opts_get(terminate_opts, 0, "--session-id")->uint32, cpn_opts_get(terminate_opts, 0, "--session-cap")->string); else puts("No action specified"); return 0; }
static void test_with_connection_status(jabber_conn_status_t status) { CommandHelp *help = malloc(sizeof(CommandHelp)); will_return(jabber_get_connection_status, status); expect_cons_show("You are either connected already, or a login is in process."); gboolean result = cmd_connect(NULL, NULL, *help); assert_true(result); free(help); }
void cmd_connect_shows_message_when_port_contains_chars(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "*****@*****.**", "port", "52f66", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); expect_cons_show("Could not convert \"52f66\" to a number."); expect_cons_show(""); gboolean result = cmd_connect(NULL, args, *help); assert_true(result); free(help); }
void cmd_connect_shows_message_when_port_65536(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "*****@*****.**", "port", "65536", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); expect_cons_show("Value 65536 out of range. Must be in 1..65535."); expect_cons_show(""); gboolean result = cmd_connect(NULL, args, *help); assert_true(result); free(help); }
void cmd_connect_shows_usage_when_no_port_value(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); help->usage = "some usage"; gchar *args[] = { "*****@*****.**", "port", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); expect_cons_show("Usage: some usage"); expect_cons_show(""); gboolean result = cmd_connect(NULL, args, *help); assert_true(result); free(help); }
void cmd_connect_connects_with_account(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "jabber_org", NULL }; ProfAccount *account = account_new("jabber_org", "*****@*****.**", "password", NULL, TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL); will_return(jabber_get_connection_status, JABBER_DISCONNECTED); expect_any(accounts_get_account, name); will_return(accounts_get_account, account); expect_cons_show("Connecting with account jabber_org as [email protected]"); expect_memory(jabber_connect_with_account, account, account, sizeof(account)); will_return(jabber_connect_with_account, JABBER_CONNECTING); gboolean result = cmd_connect(NULL, args, *help); assert_true(result); free(help); }
int main(void) { int cmd; /* initialize window size to defult */ window_size = DEFAULT_WINDOW_SIZE; message_to_send = NULL; /*pthread_t pthread_id[2]; pthread_attr_t attr;*/ /* * start named pipe, this is where the host will receive user commands. * it will be a producer/consumer design. pipe name should be passed as * command-line argument */ create_pipe(PIPE_NAME); int pipe = open_pipe(PIPE_NAME, O_RDONLY); /* * open a socket to do the communication. packet transaction will be * simulated above a normal connection. */ if (HOST == HOST_A) { open_socket(); send_message_to_other("Hello Client! You can start your simulation."); get_message_from_other(); printf("\n\n\n"); } else { open_connection(); get_message_from_other(); send_message_to_other("Hi Server."); printf("\n\n\n"); } /* * once connection is stablished we can start the SIM STATE MACHINE. * our state machine will start in CLOSED */ host_state = STATE_CLOSED; host_seq_number = rand(); /* * at this point we need to start our threads that will exchange * messages with the other host. */ /* thread to construct packets */ pthread_t pthread_id[10]; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_create(&pthread_id[0], &attr, (void *) &send_stuff_thread, (void *) NULL); /* pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_create(&pthread_id[0], &attr, (void *)&receive_tcp_packet_from_other, NULL); */ /* loop until gets work to do */ while (TRUE) { /* * get work from console. this is the link we have with the * outside world (client) */ fflush(stdout); cmd = get_work(pipe); int read_size; char buf[MAX_PIPE_DATA_SIZE]; switch (cmd) { case CMD_LISTEN: printf("got CMD_LISTEN\n"); cmd_listen(); break; case CMD_CONNECT: printf("got CMD_CONNECT\n"); cmd_connect(); break; case CMD_SEND_PKT: printf("got CMD_SEND_PKT\n"); sleep(1); /* get message to send over */ while ((read_size = pipe_read(pipe, buf)) < 1); message_to_send = malloc(read_size * sizeof(char)); strncpy(buf, message_to_send, read_size); break; case CMD_CHNG_WINDOW_SIZE: printf("changed window size to %d\n", window_size); break; case CMD_CLOSE: break; /* ACK packet */ default: printf("did not recognize cmd!\n"); } } }
static void mapidcom() //Communicates with clients through IPC { fd_set active_fd_set; // active file descriptor list fd_set read_fd_set; // temp file descriptor list for select() int fdmax; // maximum file descriptor number int yes = 1; // for setsockopt() SO_REUSEADDR, below struct sockaddr_un mapidaddr; struct sockaddr_un remoteaddr; struct mapiipcbuf qbuf; socklen_t addrlen; int nbytes, len, s; struct client *cl= NULL; flist_node_t *tmpnode; conf_category_entry_t *cat=NULL; char *local; struct group *mapi_group; conf_category_t *conf; mapidsocket=strdup(MAPIDSOCKGLOBAL); if ((conf = pc_load(mapid_conf)) != NULL) { cat = pc_get_category(conf, ""); local=pc_get_param(cat, "local"); if (local!=NULL && strcmp(local, "1")==0) { free(mapidsocket); mapidsocket=printf_string(MAPIDSOCKHOME, getenv("HOME") ); } pc_close(conf); } // create the listener socket if ((listenerfd = socket(AF_LOCAL, SOCK_STREAM, 0)) == -1) { DEBUG_CMD(Debug_Message("ERROR: socket: %s", strerror(errno))); exit(EXIT_FAILURE); } // avoid "address already in use" error message if (setsockopt(listenerfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { DEBUG_CMD(Debug_Message("ERROR: setsockopt: %s", strerror(errno))); exit(EXIT_FAILURE); } // set up the address we will be binding to memset(&mapidaddr, 0, sizeof (mapidaddr)); mapidaddr.sun_family = AF_LOCAL; memcpy(mapidaddr.sun_path, mapidsocket, strlen(mapidsocket)+1); unlink(mapidsocket); len = sizeof mapidaddr.sun_family + strlen(mapidaddr.sun_path); if (bind(listenerfd, (struct sockaddr *) &mapidaddr, len)) { DEBUG_CMD(Debug_Message("ERROR: bind: %s", strerror(errno))); exit(EXIT_FAILURE); } // allow any member of our own group to connect chmod(mapidsocket, S_IRWXU | S_IRWXG); // if a mapi user group exists, set group permissions accordingly, // otherwise the group ID will be equal to the user ID of the user that // invoked mapid mapi_group = getgrnam(MAPI_GROUP_NAME); if (mapi_group != NULL) { chown(mapidsocket, -1, mapi_group->gr_gid); } if (listen(listenerfd, BACKLOG) == -1) { DEBUG_CMD(Debug_Message("ERROR: listen: %s", strerror(errno))); exit(EXIT_FAILURE); } FD_ZERO (&active_fd_set); // add the listener to the active set FD_SET (listenerfd, &active_fd_set) ; // keep track of the biggest file descriptor fdmax = listenerfd; // so far, it's just this one // wait for commands from the mapi stub, for ever... while (1) { read_fd_set = active_fd_set; // copy it if (select(fdmax + 1, &read_fd_set, NULL, NULL, NULL) == -1) { DEBUG_CMD(Debug_Message("ERROR: select: %s", strerror(errno))); break; } // run through the existing connections for (s = 0; s <= fdmax; s++) { if (FD_ISSET (s, &read_fd_set)) { // connection on the original listener socket if (s == listenerfd) { addrlen = sizeof (remoteaddr); if ((newfd = accept(listenerfd, (struct sockaddr *) &remoteaddr, &addrlen)) == -1) { DEBUG_CMD(Debug_Message("accept: %s", strerror(errno))); } else { FD_SET (newfd, &active_fd_set) ; // add to active set if (newfd > fdmax) { // keep track of the maximum fdmax = newfd; } } } // handle data from an existing client else { if ((nbytes = recv (s, &qbuf, MAX_SEND_SIZE, 0)) <= 0) { if (nbytes == 0) { // connection closed - find client's pid while(__sync_lock_test_and_set(&clientlist_lock,1)); tmpnode = (flist_node_t *) flist_head (clientlist); cl = NULL; while (tmpnode != NULL) { if (((struct client *) tmpnode->data)->sock == s) { cl = (struct client *) tmpnode->data; break; } tmpnode = flist_next (tmpnode); } clientlist_lock = 0; if (cl == NULL) {/* This is not interesting, as it will occur upon requests from clients without flows etc. WARNING_CMD (printf ("WARNING: Zero bytes from socket %d [%s:%d]\n", s, __FILE__, __LINE__)); */ /* shouldn't really exit here? * this will cause the program to exit on errors with an empty * client-list which isn't really an error (IMHO) */ //exit(EXIT_FAILURE); } else { while(__sync_lock_test_and_set(&clientlist_lock,1)); //clean up any remaining flows tmpnode = (flist_node_t *) flist_head (cl->flowlist); while (tmpnode != NULL) { cleanup_flow ((struct flow *) tmpnode->data); tmpnode = flist_next (tmpnode); } tmpnode = (flist_node_t *) flist_head (cl->devicelist); while (tmpnode != NULL) { mapidrv_delete_device = get_drv_funct (tmpnode->data, "mapidrv_delete_device"); mapidrv_delete_device (tmpnode->id); tmpnode = flist_next (tmpnode); } flist_destroy (cl->flowlist); flist_destroy (cl->devicelist); free(cl->devicelist); free (cl->flowlist); //remove this client from global client list free(flist_remove (clientlist, cl->pid)); clientlist_lock = 0; } } else { DEBUG_CMD(Debug_Message("WARNING: recv: %s at", strerror (errno))); } close (s); FD_CLR (s, &active_fd_set); // remove it from active set } else { // we got some data from a client: process request switch (qbuf.cmd) { case GET_LIBS: cmd_get_libs (qbuf.pid, s); break; case GET_LIBPATH: cmd_get_libpath (qbuf.pid, s); break; case CREATE_FLOW: cmd_create_flow ((char *)qbuf.data, qbuf.pid, qbuf.uid, s); break; case CLOSE_FLOW: cmd_close_flow (qbuf.fd, qbuf.pid, s, 1); break; case GET_FLOW_INFO: cmd_get_flow_info (qbuf.fd, qbuf.pid, s); break; case GET_FUNCTION_INFO: cmd_get_function_info (qbuf.fd, qbuf.fid, qbuf.pid, s); break; case GET_NEXT_FUNCTION_INFO: cmd_get_next_function_info (qbuf.fd, qbuf.fid, qbuf.pid, s); break; case GET_NEXT_FLOW_INFO: cmd_get_next_flow_info (qbuf.fd, qbuf.pid, s); break; case GET_NEXT_DEVICE_INFO: cmd_get_next_device_info (qbuf.fd, qbuf.pid, s); break; case GET_DEVICE_INFO: cmd_get_device_info (qbuf.fd, qbuf.pid, s); break; case MAPI_STATS: cmd_stats ((char *)qbuf.data, qbuf.pid, qbuf.uid, s); break; case APPLY_FUNCTION: cmd_apply_function (&qbuf, qbuf.pid, s); break; case READ_RESULT: cmd_read_results (qbuf.fd, qbuf.fid, qbuf.pid, s); break; case CONNECT: cmd_connect (qbuf.fd, qbuf.pid, s); break; case CREATE_FLOW_ACK: case APPLY_FUNCTION_ACK: case READ_RESULT_ACK: case CONNECT_ACK: case CLOSE_FLOW_ACK: case SET_AUTHDATA_ACK: break; case READ_ERROR_ACK: break; case ERROR_ACK: break; case CREATE_OFFLINE_DEVICE: cmd_create_offline_device ((char *)qbuf.data, qbuf.fid, qbuf.pid, s); break; case CREATE_OFFLINE_DEVICE_ACK: break; case START_OFFLINE_DEVICE: cmd_start_offline_device ((char *)qbuf.data, qbuf.pid, s); break; case START_OFFLINE_DEVICE_ACK: break; case DELETE_OFFLINE_DEVICE: cmd_delete_offline_device ((char *)qbuf.data, qbuf.pid, s); break; case DELETE_OFFLINE_DEVICE_ACK: break; case CREATE_OFFLINE_FLOW_ACK: break; case LOAD_LIBRARY: cmd_load_library ((char *)qbuf.data, qbuf.pid, s); default: break; } } } } } // for } // while(1) }
int main(int argc, char **argv) { char addr[18]; char *handle, *value; int ret, choice; printf("*********************************\n"); printf("**** Nod Labs test framework ****\n"); printf("*********************************\n"); /* initialize a glib event loop */ event_loop = g_main_loop_new(NULL, FALSE); printf("Scanning for all BTLE devices in proximity..."); printf("Press ^C to stop scanning\n"); if(ret = cmd_lescan(0) < 0) { printf("Scanning failed!! Quitting %d\n", ret); exit(-1); } printf("Enter the device to connect: "); scanf("%s", addr); ret = 0; if (ret = cmd_connect(addr, NULL) < 0) { printf("Connection failed %d\n", ret); exit(-2); } else { printf("Connected to [%s]\n", addr); } printf("SERVICES:\n"); discover_services((gpointer)attrib); printf("\nCHARACTERISTICS:\n"); discover_characteristics((gpointer)attrib, CHAR_START, CHAR_END); /* IMP: If the native bluez stack is running while this test runs, then the * keys get cached in the system(on which this is running). Next time, when this * test case is being run, it will fail to change the security level to * anything other than "low". To resolve this issue, we need to check for * the paired devices in bluetoothctl (using cmd 'show devices') and remove * the device from bluez device cache. * * HIGH security level is needed for initiating the bonding with the device. This * is important as HID services need the link to be encrypted. */ if (cmd_set_sec_level("high") < 0) { printf("Failed to set the security level to HIGH\n"); exit(-1); } else { printf("Security level set to HIGH\n"); } /* Now, enable battery and nControl notifications in the ring */ control_service(non_os_indexes, GET_SZ(non_os_indexes), ENABLE_NOTIFICATION); /* * TODO: Make sure the below mode list is up-to-date with * the numbers used in the f/w */ ret = 0; printf("Which mode you want to enable?\n"); printf("1. TTM\n"); printf("2. Free pointer\n"); printf("3. OpenSpatial (Pose6D & Buttons)\n"); printf("0. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); printf("\n"); if (!choice) { exit(0); } /* update the mode accordingly */ change_mode((gpointer)attrib, choice); /* We have an active connection, with the data ready to be received */ set_state(STATE_CONNACTIVE); /* let the signal handler do necessary cleanup before proceeding to disconnect */ wait_for_cleanup = 1; /* below loop will control the event loop for receiving the data */ g_main_loop_run(event_loop); while (wait_for_cleanup); printf("Disconnecting from device [%s]\n", addr); cmd_disconnect(); /* un-initialize glib event loop */ g_main_loop_unref(event_loop); return 0; }