END_TEST /** * \brief Test packing and unpacking of negotiate command with multiple values */ START_TEST ( test_pack_unpack_negotiate_cmd_multiple_values ) { union VSystemCommands send_sys_cmd[1], recv_sys_cmd[1]; uint8 cmd_op_code = CMD_CONFIRM_L_ID; uint8 ftr_op_code = FTR_FC_ID; uint8 values[2] = {FC_TCP_LIKE, FC_NONE}; char buffer[255]; int ret, buffer_pos = 0, cmd_len; /* Create negotiate command */ ret = v_add_negotiate_cmd(send_sys_cmd, 0, cmd_op_code, ftr_op_code, &values[0], &values[1], NULL); fail_unless( ret == 1, "Adding negotiate command failed"); /* Pack negotiate command */ buffer_pos += v_raw_pack_negotiate_cmd(buffer, &send_sys_cmd[0].negotiate_cmd); fail_unless( buffer_pos == 5, "Length of packed cmd: %d != %d", buffer_pos, 5); /* Unpack system command */ cmd_len = v_raw_unpack_negotiate_cmd(buffer, buffer_pos, &recv_sys_cmd[0].negotiate_cmd); fail_unless( cmd_len == buffer_pos, "Length of packed and unpacked cmd: %d != %d", buffer_pos, cmd_len); fail_unless( recv_sys_cmd->negotiate_cmd.id == cmd_op_code, "Negotiate command OpCode: %d != %d", recv_sys_cmd->negotiate_cmd.id, cmd_op_code); fail_unless( recv_sys_cmd->negotiate_cmd.feature == ftr_op_code, "Negotiate command feature: %d != %d", recv_sys_cmd->negotiate_cmd.feature, ftr_op_code); fail_unless( recv_sys_cmd->negotiate_cmd.count == 2, "Negotiate command feature count: %d != %d", recv_sys_cmd->negotiate_cmd.count, 2); fail_unless( recv_sys_cmd->negotiate_cmd.value[0].uint8 == values[0], "Negotiate command value[0]: %d != %d", recv_sys_cmd->negotiate_cmd.value[0].uint8, values[0]); fail_unless( recv_sys_cmd->negotiate_cmd.value[1].uint8 == values[1], "Negotiate command value[1]: %d != %d", recv_sys_cmd->negotiate_cmd.value[1].uint8, values[1]); }
END_TEST /** * \brief Test of packing and unpacking negotiate command with string value. */ START_TEST ( test_pack_unpack_negotiate_cmd_string_value ) { union VSystemCommands send_sys_cmd[1], recv_sys_cmd[1]; uint8 cmd_op_code = CMD_CHANGE_R_ID; uint8 ftr_op_code = FTR_CLIENT_NAME; char string[5] = {'a', 'h', 'o', 'y', '\0'}; char buffer[255]; int ret, buffer_pos = 0, cmd_len; ret = v_add_negotiate_cmd(send_sys_cmd, 0, cmd_op_code, ftr_op_code, string, NULL); fail_unless( ret == 1, "Adding negotiate command failed"); /* Pack negotiate command */ buffer_pos += v_raw_pack_negotiate_cmd(buffer, &send_sys_cmd[0].negotiate_cmd); fail_unless( buffer_pos == 8, "Length of packed cmd: %d != %d", buffer_pos, 8); /* Unpack system command */ cmd_len = v_raw_unpack_negotiate_cmd(buffer, buffer_pos, &recv_sys_cmd[0].negotiate_cmd); fail_unless( cmd_len == buffer_pos, "Length of packed and unpacked cmd: %d != %d", buffer_pos, cmd_len); fail_unless( recv_sys_cmd->negotiate_cmd.id == cmd_op_code, "Negotiate command OpCode: %d != %d", recv_sys_cmd->negotiate_cmd.id, cmd_op_code); fail_unless( recv_sys_cmd->negotiate_cmd.feature == ftr_op_code, "Negotiate command feature: %d != %d", recv_sys_cmd->negotiate_cmd.feature, ftr_op_code); fail_unless( recv_sys_cmd->negotiate_cmd.count == 1, "Negotiate command feature count: %d != %d", recv_sys_cmd->negotiate_cmd.count, 1); fail_unless( recv_sys_cmd->negotiate_cmd.value[0].string8.length == 4, "Negotiate command value (string length): %d != %d", recv_sys_cmd->negotiate_cmd.value[0].string8.length, 4); fail_unless( strcmp((char*)recv_sys_cmd->negotiate_cmd.value[0].string8.str, "ahoy") == 0, "Negotiate command value (string): %s != ahoy", recv_sys_cmd->negotiate_cmd.value[0].string8.str); }
END_TEST /** * \brief Test simple packing and unpacking of negotiate command */ START_TEST ( test_pack_unpack_negotiate_cmd_float_value ) { union VSystemCommands send_sys_cmd[1], recv_sys_cmd[1]; uint8 cmd_op_code = CMD_CHANGE_L_ID; uint8 ftr_op_code = FTR_FPS; real32 value = 60.0f; char buffer[255]; int ret, buffer_pos = 0, cmd_len; /* Create negotiate command */ ret = v_add_negotiate_cmd(send_sys_cmd, 0, cmd_op_code, ftr_op_code, &value, NULL); fail_unless( ret == 1, "Adding negotiate command failed"); /* Pack negotiate command */ buffer_pos += v_raw_pack_negotiate_cmd(buffer, &send_sys_cmd[0].negotiate_cmd); fail_unless( buffer_pos == 7, "Length of packed cmd: %d != %d", buffer_pos, 7); /* Unpack system command */ cmd_len = v_raw_unpack_negotiate_cmd(buffer, buffer_pos, &recv_sys_cmd[0].negotiate_cmd); fail_unless( cmd_len == buffer_pos, "Length of packed and unpacked cmd: %d != %d", buffer_pos, cmd_len); fail_unless( recv_sys_cmd->negotiate_cmd.id == cmd_op_code, "Negotiate command OpCode: %d != %d", recv_sys_cmd->negotiate_cmd.id, cmd_op_code); fail_unless( recv_sys_cmd->negotiate_cmd.feature == ftr_op_code, "Negotiate command feature: %d != %d", recv_sys_cmd->negotiate_cmd.feature, ftr_op_code); fail_unless( recv_sys_cmd->negotiate_cmd.count == 1, "Negotiate command feature count: %d != %d", recv_sys_cmd->negotiate_cmd.count, 1); fail_unless( recv_sys_cmd->negotiate_cmd.value[0].real32 == value, "Negotiate command value: %d != %d", recv_sys_cmd->negotiate_cmd.value[0].real32, value); }
/** * \brief Get system commands from received buffer and store them in VMessage * \param[in] *buffer The received buffer * \param[in] buffer_len The size of received buffer * \param[out] *vmessage The structure of message, that will be filled with * information from the buffer. * \return This function returns relative buffer position of buffer proceeding. */ int v_unpack_message_system_commands(const char *buffer, unsigned short buffer_len, struct VMessage *vmessage) { unsigned short not_used = 0, buffer_pos = 0; unsigned char length, cmd_id=CMD_RESERVED_ID; int i=0; if(buffer_len<1) { vmessage->sys_cmd[0].cmd.id = CMD_RESERVED_ID; return 0; /* Corrupted data was received ... buffer can not be proceeded further */ } else { while(buffer_pos<buffer_len && cmd_id<=MAX_SYS_CMD_ID) { /* System command IDs are in range 0-31 */ /* Unpack Command ID */ not_used += vnp_raw_unpack_uint8(&buffer[buffer_pos], &cmd_id); /* Is it still system command or is it node command */ if(cmd_id>MAX_SYS_CMD_ID) { vmessage->sys_cmd[i].cmd.id = CMD_RESERVED_ID; break; } else { vmessage->sys_cmd[i].cmd.id = cmd_id; vmessage->sys_cmd[i+1].cmd.id = CMD_RESERVED_ID; switch(cmd_id) { case CMD_USER_AUTH_REQUEST: buffer_pos += v_raw_unpack_user_auth_request(&buffer[buffer_pos], buffer_len - buffer_pos, &vmessage->sys_cmd[i].ua_req); break; case CMD_USER_AUTH_FAILURE: buffer_pos += v_raw_unpack_user_auth_failure(&buffer[buffer_pos], buffer_len - buffer_pos, &vmessage->sys_cmd[i].ua_fail); break; case CMD_USER_AUTH_SUCCESS: buffer_pos += v_raw_unpack_user_auth_success(&buffer[buffer_pos], buffer_len - buffer_pos, &vmessage->sys_cmd[i].ua_succ); break; case CMD_CHANGE_L_ID: case CMD_CONFIRM_L_ID: case CMD_CHANGE_R_ID: case CMD_CONFIRM_R_ID: buffer_pos += v_raw_unpack_negotiate_cmd(&buffer[buffer_pos], buffer_len - buffer_pos, &vmessage->sys_cmd[i].negotiate_cmd); break; default: /* Unknown system command. */ /* Unpack length of the command */ not_used = vnp_raw_unpack_uint8(&buffer[buffer_pos+1], &length); /* Warning print */ v_print_log(VRS_PRINT_WARNING, "Unknown system command ID: %d, Length: %d\n", cmd_id, length); /* Skip this command */ if(length < (buffer_len - buffer_pos)) buffer_pos += length; else buffer_pos = buffer_len; break; } i++; } } } return buffer_pos; }
/** * \brief Get system commands from received buffer and store them in VPacket * \param[in] *buffer The received buffer * \param[in] buffer_len The size of received buffer * \param[out] *vpacket The structure of packet, that will be filled with * information from the buffer. * \return This function returns relative buffer position of buffer proceeding. */ int v_unpack_packet_system_commands(const char *buffer, unsigned short buffer_len, struct VPacket *vpacket) { unsigned short not_used=0, buffer_pos=VERSE_PACKET_HEADER_SIZE; unsigned char length, cmd_id=CMD_RESERVED_ID; int i=0; if(buffer_len<VERSE_PACKET_HEADER_SIZE) { vpacket->sys_cmd[0].cmd.id = CMD_RESERVED_ID; return -1; /* Corrupted data was received ... buffer can not be proceeded further */ } else if(buffer_len==VERSE_PACKET_HEADER_SIZE) { vpacket->sys_cmd[0].cmd.id = CMD_RESERVED_ID; return VERSE_PACKET_HEADER_SIZE; } else { while(buffer_pos<buffer_len && cmd_id<=MAX_SYS_CMD_ID && /* System command IDs are in range 0-31 */ i < MAX_SYSTEM_COMMAND_COUNT-1 ) { /* Unpack Command ID */ not_used += vnp_raw_unpack_uint8(&buffer[buffer_pos], &cmd_id); /* Is it still system command or is it node command */ if(cmd_id>MAX_SYS_CMD_ID) { vpacket->sys_cmd[i].cmd.id = CMD_RESERVED_ID; break; } else { vpacket->sys_cmd[i].cmd.id = cmd_id; vpacket->sys_cmd[i+1].cmd.id = CMD_RESERVED_ID; switch(cmd_id) { case CMD_ACK_ID: buffer_pos += v_raw_unpack_ack_nak_cmd(&buffer[buffer_pos], &vpacket->sys_cmd[i].ack_cmd); break; case CMD_NAK_ID: buffer_pos += v_raw_unpack_ack_nak_cmd(&buffer[buffer_pos], &vpacket->sys_cmd[i].nak_cmd); break; case CMD_CHANGE_L_ID: case CMD_CONFIRM_L_ID: case CMD_CHANGE_R_ID: case CMD_CONFIRM_R_ID: buffer_pos += v_raw_unpack_negotiate_cmd(&buffer[buffer_pos], buffer_len - buffer_pos, &vpacket->sys_cmd[i].negotiate_cmd); break; default: /* This is unknown system command. Unpack length of * the command and skip this command. */ not_used = vnp_raw_unpack_uint8(&buffer[buffer_pos+1], &length); /* Warning print */ v_print_log(VRS_PRINT_WARNING, "Unknown system command ID: %d, Length: %d\n", cmd_id, length); /* Skip this command */ if(length < (buffer_len - buffer_pos)) buffer_pos += length; else buffer_pos = buffer_len; break; } } i++; } } return buffer_pos; }
END_TEST /** * \brief Test unpacking of corrupted negotiate command */ START_TEST ( test_unpack_wrong_negotiate_cmd ) { union VSystemCommands recv_sys_cmd[1]; char buffer[255]; int buffer_size, cmd_len; /* Create buffer with corrupted negotiate command (wrong command length) */ buffer[0] = CMD_CHANGE_L_ID; /* Command OpCode */ buffer[1] = 0; /* Wrong Command Length */ buffer_size = 2; /* Unpack system command */ cmd_len = v_raw_unpack_negotiate_cmd(buffer, buffer_size, &recv_sys_cmd[0].negotiate_cmd); /* Create buffer with corrupted negotiate command (small buffer size) */ buffer[0] = CMD_CHANGE_L_ID; /* Command OpCode */ buffer[1] = 4; /* Command Length */ buffer[2] = FTR_CC_ID; /* Feature (Congestion) */ buffer[3] = CC_TCP_LIKE; /* Type of congestion */ buffer_size = 3; /* Unpack system command */ cmd_len = v_raw_unpack_negotiate_cmd(buffer, buffer_size, &recv_sys_cmd[0].negotiate_cmd); fail_unless( cmd_len == buffer_size, "Length of packed and unpacked cmd: %d != %d", buffer_size, cmd_len); /* Create buffer with corrupted negotiate command * (wrong command feature number) */ buffer[0] = CMD_CHANGE_L_ID; /* Command OpCode */ buffer[1] = 3; /* Command Length */ buffer[2] = FTR_RSV_ID; /* Wrong Command Feature */ buffer_size = 3; /* Unpack system command */ cmd_len = v_raw_unpack_negotiate_cmd(buffer, buffer_size, &recv_sys_cmd[0].negotiate_cmd); fail_unless( cmd_len == buffer_size, "Length of packed and unpacked cmd: %d != %d", buffer_size, cmd_len); /* Create buffer with corrupted negotiate command * (wrong command feature number) */ buffer[0] = CMD_CHANGE_L_ID; /* Command OpCode */ buffer[1] = 3; /* Command Length */ buffer[2] = 100; /* Wrong Command Feature */ buffer_size = 3; /* Unpack system command */ cmd_len = v_raw_unpack_negotiate_cmd(buffer, buffer_size, &recv_sys_cmd[0].negotiate_cmd); fail_unless( cmd_len == buffer_size, "Length of packed and unpacked cmd: %d != %d", buffer_size, cmd_len); /* Create buffer with corrupted negotiate command * (wrong size of negotiated string) */ buffer[0] = CMD_CHANGE_L_ID; /* Command OpCode */ buffer[1] = 8; /* Command Length */ buffer[2] = FTR_CLIENT_NAME; /* Command Feature */ buffer[3] = 5; /* Wrong String length */ buffer[4] = 'a'; /* String ... */ buffer[4] = 'h'; buffer[4] = 'o'; buffer[4] = 'y'; buffer_size = 8; /* Unpack system command */ cmd_len = v_raw_unpack_negotiate_cmd(buffer, buffer_size, &recv_sys_cmd[0].negotiate_cmd); fail_unless( cmd_len == buffer_size, "Length of packed and unpacked cmd: %d != %d", buffer_size, cmd_len); /* Create buffer with corrupted negotiate command * (wrong size of command including negotiated string) */ buffer[0] = CMD_CHANGE_L_ID; /* Command OpCode */ buffer[1] = 7; /* Command Length */ buffer[2] = FTR_CLIENT_NAME; /* Command Feature */ buffer[3] = 4; /* Wrong String length */ buffer[4] = 'a'; /* String ... */ buffer[4] = 'h'; buffer[4] = 'o'; buffer[4] = 'y'; buffer_size = 8; /* Unpack system command */ cmd_len = v_raw_unpack_negotiate_cmd(buffer, buffer_size, &recv_sys_cmd[0].negotiate_cmd); fail_unless( cmd_len == 7, "Length of packed and unpacked cmd: %d != %d", buffer_size, cmd_len); }
END_TEST /** * \brief Test of packing and unpacking negotiate command with string value. */ START_TEST ( test_pack_unpack_negotiate_cmd_long_string_value ) { union VSystemCommands send_sys_cmd[1], recv_sys_cmd[1]; uint8 cmd_op_code = CMD_CHANGE_R_ID; uint8 ftr_op_code = FTR_CLIENT_NAME; char string[470] = "Lorem ipsum dolor sit amet, consectetuer adipiscing " "elit. Ut enim ad minim veniam, quis nostrud exercitation ullamco " "laboris nisi ut aliquip ex ea commodo consequat. Nullam at arcu " "a est sollicitudin euismod. Fusce consectetuer risus a nunc. " "Cras pede libero, dapibus nec, pretium sit amet, tempor quis. " "Etiam dictum tincidunt diam. Nullam lectus justo, vulputate " "eget mollis sed, tempor sed magna. Vivamus porttitor turpis ac " "leo. Suspendisse sagittis ultrices augue."; char buffer[512]; int ret, buffer_pos = 0, cmd_len; ret = v_add_negotiate_cmd(send_sys_cmd, 0, cmd_op_code, ftr_op_code, string, NULL); fail_unless( ret == 1, "Adding negotiate command failed"); /* Pack negotiate command */ buffer_pos += v_raw_pack_negotiate_cmd(buffer, &send_sys_cmd[0].negotiate_cmd); fail_unless( buffer_pos == 261, "Length of packed cmd: %d != %d", buffer_pos, 8); /* Unpack system command */ cmd_len = v_raw_unpack_negotiate_cmd(buffer, buffer_pos, &recv_sys_cmd[0].negotiate_cmd); fail_unless( cmd_len == buffer_pos, "Length of packed and unpacked cmd: %d != %d", buffer_pos, cmd_len); fail_unless( recv_sys_cmd->negotiate_cmd.id == cmd_op_code, "Negotiate command OpCode: %d != %d", recv_sys_cmd->negotiate_cmd.id, cmd_op_code); fail_unless( recv_sys_cmd->negotiate_cmd.feature == ftr_op_code, "Negotiate command feature: %d != %d", recv_sys_cmd->negotiate_cmd.feature, ftr_op_code); fail_unless( recv_sys_cmd->negotiate_cmd.count == 1, "Negotiate command feature count: %d != %d", recv_sys_cmd->negotiate_cmd.count, 1); fail_unless( recv_sys_cmd->negotiate_cmd.value[0].string8.length == 255, "Negotiate command value (string length): %d != %d", recv_sys_cmd->negotiate_cmd.value[0].string8.length, 255); /* Make original string 255 bytes long */ string[255] = '\0'; fail_unless( strncmp((char*)recv_sys_cmd->negotiate_cmd.value[0].string8.str, string, 255) == 0, "Negotiate command value (string): %s != %s", recv_sys_cmd->negotiate_cmd.value[0].string8.str, string); }