fd_send_instruction.c (4062B)
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20#include <CUnit/CUnit.h> 21#include <guacamole/protocol.h> 22#include <guacamole/socket.h> 23 24#include <stdlib.h> 25#include <unistd.h> 26 27/** 28 * Test string which contains exactly four Unicode characters encoded in UTF-8. 29 * This particular test string uses several characters which encode to multiple 30 * bytes in UTF-8. 31 */ 32#define UTF8_4 "\xe7\x8a\xac\xf0\x90\xac\x80z\xc3\xa1" 33 34/** 35 * Writes a series of Guacamole instructions using a normal guac_socket 36 * wrapping the given file descriptor. The instructions written correspond to 37 * the instructions verified by read_expected_instructions(). The given file 38 * descriptor is automatically closed as a result of calling this function. 39 * 40 * @param fd 41 * The file descriptor to write instructions to. 42 */ 43static void write_instructions(int fd) { 44 45 /* Open guac socket */ 46 guac_socket* socket = guac_socket_open(fd); 47 48 /* Write nothing if socket cannot be allocated (test will fail in parent 49 * process due to failure to read) */ 50 if (socket == NULL) { 51 close(fd); 52 return; 53 } 54 55 /* Write instructions */ 56 guac_protocol_send_name(socket, "a" UTF8_4 "b" UTF8_4 "c"); 57 guac_protocol_send_sync(socket, 12345); 58 guac_socket_flush(socket); 59 60 /* Close and free socket */ 61 guac_socket_free(socket); 62 63} 64 65/** 66 * Reads raw bytes from the given file descriptor until no further bytes 67 * remain, verifying that those bytes represent the series of Guacamole 68 * instructions expected to be written by write_instructions(). The given 69 * file descriptor is automatically closed as a result of calling this 70 * function. 71 * 72 * @param fd 73 * The file descriptor to read data from. 74 */ 75static void read_expected_instructions(int fd) { 76 77 char expected[] = 78 "4.name,11.a" UTF8_4 "b" UTF8_4 "c;" 79 "4.sync,5.12345;"; 80 81 int numread; 82 char buffer[1024]; 83 int offset = 0; 84 85 /* Read everything available into buffer */ 86 while ((numread = read(fd, &(buffer[offset]), 87 sizeof(buffer) - offset)) > 0) { 88 offset += numread; 89 } 90 91 /* Verify length of read data */ 92 CU_ASSERT_EQUAL(offset, strlen(expected)); 93 94 /* Add NULL terminator */ 95 buffer[offset] = '\0'; 96 97 /* Read value should be equal to expected value */ 98 CU_ASSERT_STRING_EQUAL(buffer, expected); 99 100 /* File descriptor is no longer needed */ 101 close(fd); 102 103} 104 105/** 106 * Tests that the file descriptor implementation of guac_socket properly 107 * implements writing of instructions. A child process is forked to write a 108 * series of instructions which are read and verified by the parent process. 109 */ 110void test_socket__fd_send_instruction() { 111 112 int fd[2]; 113 114 /* Create pipe */ 115 CU_ASSERT_EQUAL_FATAL(pipe(fd), 0); 116 117 int read_fd = fd[0]; 118 int write_fd = fd[1]; 119 120 /* Fork into writer process (child) and reader process (parent) */ 121 int childpid; 122 CU_ASSERT_NOT_EQUAL_FATAL((childpid = fork()), -1); 123 124 /* Attempt to write a series of instructions within the child process */ 125 if (childpid == 0) { 126 close(read_fd); 127 write_instructions(write_fd); 128 exit(0); 129 } 130 131 /* Read and verify the expected instructions within the parent process */ 132 close(write_fd); 133 read_expected_instructions(read_fd); 134 135} 136