Split test_protocol.c into multiple files, one per test.

This commit is contained in:
Michael Jumper 2012-07-23 15:37:36 -07:00
parent af4cb160cb
commit f8eb590ea3
6 changed files with 366 additions and 174 deletions

View File

@ -40,6 +40,12 @@ AM_CFLAGS = -Werror -Wall -pedantic -I../include
TESTS = test_protocol
check_PROGRAMS = test_protocol
test_protocol_SOURCES = test_protocol.c
test_protocol_SOURCES = \
test_protocol.c \
protocol/suite.c \
protocol/instruction_read.c \
protocol/instruction_write.c
test_protocol_LDADD = $(top_builddir)/src/libguac.la @CUNIT_LIBS@

View File

@ -0,0 +1,118 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is libguac.
*
* The Initial Developer of the Original Code is
* Michael Jumper.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include "socket.h"
#include "protocol.h"
#include "error.h"
#include "suite.h"
void test_instruction_read() {
int rfd, wfd;
int fd[2], childpid;
char test_string[] = "4.test,3.a" UTF8_DOG "b,"
"5.12345,4.a" UTF8_DOG UTF8_DOG "c;"
"5.test2,10.hellohello,15.worldworldworld;";
/* Create pipe */
pipe(fd);
/* File descriptors */
rfd = fd[0];
wfd = fd[1];
/* Fork */
if ((childpid = fork()) == -1) {
/* ERROR */
perror("fork");
return;
}
/* Child (pipe writer) */
if (childpid != 0) {
close(rfd);
write(wfd, test_string, sizeof(test_string));
exit(0);
}
/* Parent (unit test) */
else {
guac_socket* socket;
guac_instruction* instruction;
close(wfd);
/* Open guac socket */
socket = guac_socket_open(rfd);
CU_ASSERT_PTR_NOT_NULL_FATAL(socket);
/* Read instruction */
instruction = guac_protocol_read_instruction(socket, 1000000);
CU_ASSERT_PTR_NOT_NULL_FATAL(instruction);
/* Validate contents */
CU_ASSERT_STRING_EQUAL(instruction->opcode, "test");
CU_ASSERT_EQUAL_FATAL(instruction->argc, 3);
CU_ASSERT_STRING_EQUAL(instruction->argv[0], "a" UTF8_DOG "b");
CU_ASSERT_STRING_EQUAL(instruction->argv[1], "12345");
CU_ASSERT_STRING_EQUAL(instruction->argv[2], "a" UTF8_DOG UTF8_DOG "c");
/* Read another instruction */
guac_instruction_free(instruction);
instruction = guac_protocol_read_instruction(socket, 1000000);
/* Validate contents */
CU_ASSERT_STRING_EQUAL(instruction->opcode, "test2");
CU_ASSERT_EQUAL_FATAL(instruction->argc, 2);
CU_ASSERT_STRING_EQUAL(instruction->argv[0], "hellohello");
CU_ASSERT_STRING_EQUAL(instruction->argv[1], "worldworldworld");
guac_instruction_free(instruction);
guac_socket_close(socket);
}
}

View File

@ -0,0 +1,117 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is libguac.
*
* The Initial Developer of the Original Code is
* Michael Jumper.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include "socket.h"
#include "protocol.h"
#include "error.h"
#include "suite.h"
void test_instruction_write() {
int rfd, wfd;
int fd[2], childpid;
/* Create pipe */
pipe(fd);
/* File descriptors */
rfd = fd[0];
wfd = fd[1];
/* Fork */
if ((childpid = fork()) == -1) {
/* ERROR */
perror("fork");
return;
}
/* Child (pipe writer) */
if (childpid != 0) {
guac_socket* socket;
close(rfd);
/* Open guac socket */
socket = guac_socket_open(wfd);
/* Write instruction */
guac_protocol_send_clipboard(socket, "a" UTF8_DOG "b" UTF8_DOG "c");
guac_protocol_send_sync(socket, 12345);
guac_socket_flush(socket);
guac_socket_close(socket);
exit(0);
}
/* Parent (unit test) */
else {
char expected[] =
"9.clipboard,5.a" UTF8_DOG "b" UTF8_DOG "c;"
"4.sync,5.12345;";
int numread;
char buffer[1024];
int offset = 0;
close(wfd);
/* Read everything available into buffer */
while ((numread =
read(rfd,
&(buffer[offset]),
sizeof(buffer)-offset)) != 0) {
offset += numread;
}
/* Add NULL terminator */
buffer[offset] = '\0';
/* Read value should be equal to expected value */
CU_ASSERT_STRING_EQUAL(buffer, expected);
}
}

View File

@ -0,0 +1,72 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is libguac.
*
* The Initial Developer of the Original Code is
* Michael Jumper.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include <CUnit/Basic.h>
#include "suite.h"
int protocol_suite_init() {
return 0;
}
int protocol_suite_cleanup() {
return 0;
}
int register_protocol_suite() {
/* Add protocol test suite */
CU_pSuite suite = CU_add_suite("protocol",
protocol_suite_init, protocol_suite_cleanup);
if (suite == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
/* Add tests */
if (
CU_add_test(suite, "instruction-read", test_instruction_read) == NULL
|| CU_add_test(suite, "instruction-write", test_instruction_write) == NULL
) {
CU_cleanup_registry();
return CU_get_error();
}
return 0;
}

View File

@ -0,0 +1,49 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is libguac.
*
* The Initial Developer of the Original Code is
* Michael Jumper.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef _GUAC_TEST_PROTOCOL_SUITE_H
#define _GUAC_TEST_PROTOCOL_SUITE_H
#define UTF8_DOG "\xe7\x8a\xac"
int register_protocol_suite();
void test_instruction_read();
void test_instruction_write();
#endif

View File

@ -35,188 +35,18 @@
*
* ***** END LICENSE BLOCK ***** */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include "socket.h"
#include "protocol.h"
#include "error.h"
#define UTF8_DOG "\xe7\x8a\xac"
int init_suite() {
return 0;
}
int cleanup_suite() {
return 0;
}
void test_instruction_write() {
int rfd, wfd;
int fd[2], childpid;
/* Create pipe */
pipe(fd);
/* File descriptors */
rfd = fd[0];
wfd = fd[1];
/* Fork */
if ((childpid = fork()) == -1) {
/* ERROR */
perror("fork");
return;
}
/* Child (pipe writer) */
if (childpid != 0) {
guac_socket* socket;
close(rfd);
/* Open guac socket */
socket = guac_socket_open(wfd);
/* Write instruction */
guac_protocol_send_clipboard(socket, "a" UTF8_DOG "b" UTF8_DOG "c");
guac_protocol_send_sync(socket, 12345);
guac_socket_flush(socket);
guac_socket_close(socket);
exit(0);
}
/* Parent (unit test) */
else {
char expected[] =
"9.clipboard,5.a" UTF8_DOG "b" UTF8_DOG "c;"
"4.sync,5.12345;";
int numread;
char buffer[1024];
int offset = 0;
close(wfd);
/* Read everything available into buffer */
while ((numread =
read(rfd,
&(buffer[offset]),
sizeof(buffer)-offset)) != 0) {
offset += numread;
}
/* Add NULL terminator */
buffer[offset] = '\0';
/* Read value should be equal to expected value */
CU_ASSERT_STRING_EQUAL(buffer, expected);
}
}
void test_instruction_read() {
int rfd, wfd;
int fd[2], childpid;
char test_string[] = "4.test,3.a" UTF8_DOG "b,"
"5.12345,4.a" UTF8_DOG UTF8_DOG "c;"
"5.test2,10.hellohello,15.worldworldworld;";
/* Create pipe */
pipe(fd);
/* File descriptors */
rfd = fd[0];
wfd = fd[1];
/* Fork */
if ((childpid = fork()) == -1) {
/* ERROR */
perror("fork");
return;
}
/* Child (pipe writer) */
if (childpid != 0) {
close(rfd);
write(wfd, test_string, sizeof(test_string));
exit(0);
}
/* Parent (unit test) */
else {
guac_socket* socket;
guac_instruction* instruction;
close(wfd);
/* Open guac socket */
socket = guac_socket_open(rfd);
CU_ASSERT_PTR_NOT_NULL_FATAL(socket);
/* Read instruction */
instruction = guac_protocol_read_instruction(socket, 1000000);
CU_ASSERT_PTR_NOT_NULL_FATAL(instruction);
/* Validate contents */
CU_ASSERT_STRING_EQUAL(instruction->opcode, "test");
CU_ASSERT_EQUAL_FATAL(instruction->argc, 3);
CU_ASSERT_STRING_EQUAL(instruction->argv[0], "a" UTF8_DOG "b");
CU_ASSERT_STRING_EQUAL(instruction->argv[1], "12345");
CU_ASSERT_STRING_EQUAL(instruction->argv[2], "a" UTF8_DOG UTF8_DOG "c");
/* Read another instruction */
guac_instruction_free(instruction);
instruction = guac_protocol_read_instruction(socket, 1000000);
/* Validate contents */
CU_ASSERT_STRING_EQUAL(instruction->opcode, "test2");
CU_ASSERT_EQUAL_FATAL(instruction->argc, 2);
CU_ASSERT_STRING_EQUAL(instruction->argv[0], "hellohello");
CU_ASSERT_STRING_EQUAL(instruction->argv[1], "worldworldworld");
guac_instruction_free(instruction);
guac_socket_close(socket);
}
}
#include "protocol/suite.h"
int main() {
CU_pSuite suite;
/* Init registry */
if (CU_initialize_registry() != CUE_SUCCESS)
return CU_get_error();
/* Add protocol test suite */
suite = CU_add_suite("protocol", init_suite, cleanup_suite);
if (suite == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
/* Add tests */
if (
CU_add_test(suite, "instruction-read", test_instruction_read) == NULL
|| CU_add_test(suite, "instruction-write", test_instruction_write) == NULL
) {
CU_cleanup_registry();
return CU_get_error();
}
/* Register suites */
register_protocol_suite();
/* Run tests */
CU_basic_set_mode(CU_BRM_VERBOSE);