strljoin.c (5342B)
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/string.h> 22 23#include <stdlib.h> 24#include <string.h> 25 26/** 27 * Array of test elements containing the strings "Apache" and "Guacamole". 28 */ 29const char* const apache_guacamole[] = { "Apache", "Guacamole" }; 30 31/** 32 * Array of test elements containing the strings "This", "is", "a", and "test". 33 */ 34const char* const this_is_a_test[] = { "This", "is", "a", "test" }; 35 36/** 37 * Array of four test elements containing the strings "A" and "B", each 38 * preceded by an empty string (""). 39 */ 40const char* const empty_a_empty_b[] = { "", "A", "", "B" }; 41 42/** 43 * Array of test elements containing ten empty strings. 44 */ 45const char* const empty_x10[] = { "", "", "", "", "", "", "", "", "", "" }; 46 47/** 48 * Verify guac_strljoin() behavior when the string fits the buffer without 49 * truncation. The return value of each call should be the length of the 50 * resulting string. Each resulting string should contain the full result of 51 * the join operation, including null terminator. 52 */ 53void test_string__strljoin() { 54 55 char buffer[1024]; 56 57 memset(buffer, 0xFF, sizeof(buffer)); 58 CU_ASSERT_EQUAL(guac_strljoin(buffer, apache_guacamole, 2, " ", sizeof(buffer)), 16); 59 CU_ASSERT_STRING_EQUAL(buffer, "Apache Guacamole"); 60 CU_ASSERT_EQUAL(buffer[17], '\xFF'); 61 62 memset(buffer, 0xFF, sizeof(buffer)); 63 CU_ASSERT_EQUAL(guac_strljoin(buffer, this_is_a_test, 4, "", sizeof(buffer)), 11); 64 CU_ASSERT_STRING_EQUAL(buffer, "Thisisatest"); 65 CU_ASSERT_EQUAL(buffer[12], '\xFF'); 66 67 memset(buffer, 0xFF, sizeof(buffer)); 68 CU_ASSERT_EQUAL(guac_strljoin(buffer, this_is_a_test, 4, "-/-", sizeof(buffer)), 20); 69 CU_ASSERT_STRING_EQUAL(buffer, "This-/-is-/-a-/-test"); 70 CU_ASSERT_EQUAL(buffer[21], '\xFF'); 71 72 memset(buffer, 0xFF, sizeof(buffer)); 73 CU_ASSERT_EQUAL(guac_strljoin(buffer, empty_a_empty_b, 4, "/", sizeof(buffer)), 5); 74 CU_ASSERT_STRING_EQUAL(buffer, "/A//B"); 75 CU_ASSERT_EQUAL(buffer[6], '\xFF'); 76 77 memset(buffer, 0xFF, sizeof(buffer)); 78 CU_ASSERT_EQUAL(guac_strljoin(buffer, empty_x10, 10, "/", sizeof(buffer)), 9); 79 CU_ASSERT_STRING_EQUAL(buffer, "/////////"); 80 CU_ASSERT_EQUAL(buffer[10], '\xFF'); 81 82} 83 84/** 85 * Verify guac_strljoin() behavior when the string must be truncated to fit the 86 * buffer. The return value of each call should be the length that would result 87 * from joining the strings given an infinite buffer, however only as many 88 * characters as can fit should be appended to the string within the buffer, 89 * and the buffer should be null-terminated. 90 */ 91void test_string__strljoin_truncate() { 92 93 char buffer[1024]; 94 95 memset(buffer, 0xFF, sizeof(buffer)); 96 CU_ASSERT_EQUAL(guac_strljoin(buffer, apache_guacamole, 2, " ", 9), 16); 97 CU_ASSERT_STRING_EQUAL(buffer, "Apache G"); 98 CU_ASSERT_EQUAL(buffer[9], '\xFF'); 99 100 memset(buffer, 0xFF, sizeof(buffer)); 101 CU_ASSERT_EQUAL(guac_strljoin(buffer, this_is_a_test, 4, "", 8), 11); 102 CU_ASSERT_STRING_EQUAL(buffer, "Thisisa"); 103 CU_ASSERT_EQUAL(buffer[8], '\xFF'); 104 105 memset(buffer, 0xFF, sizeof(buffer)); 106 CU_ASSERT_EQUAL(guac_strljoin(buffer, this_is_a_test, 4, "-/-", 12), 20); 107 CU_ASSERT_STRING_EQUAL(buffer, "This-/-is-/"); 108 CU_ASSERT_EQUAL(buffer[12], '\xFF'); 109 110 memset(buffer, 0xFF, sizeof(buffer)); 111 CU_ASSERT_EQUAL(guac_strljoin(buffer, empty_a_empty_b, 4, "/", 2), 5); 112 CU_ASSERT_STRING_EQUAL(buffer, "/"); 113 CU_ASSERT_EQUAL(buffer[2], '\xFF'); 114 115 memset(buffer, 0xFF, sizeof(buffer)); 116 CU_ASSERT_EQUAL(guac_strljoin(buffer, empty_x10, 10, "/", 7), 9); 117 CU_ASSERT_STRING_EQUAL(buffer, "//////"); 118 CU_ASSERT_EQUAL(buffer[7], '\xFF'); 119 120} 121 122/** 123 * Verify guac_strljoin() behavior with zero buffer sizes. The return value of 124 * each call should be the size of the input string, while the buffer remains 125 * untouched. 126 */ 127void test_string__strljoin_nospace() { 128 129 /* 0-byte buffer plus 1 guard byte (to test overrun) */ 130 char buffer[1] = { '\xFF' }; 131 132 CU_ASSERT_EQUAL(guac_strljoin(buffer, apache_guacamole, 2, " ", 0), 16); 133 CU_ASSERT_EQUAL(buffer[0], '\xFF'); 134 135 CU_ASSERT_EQUAL(guac_strljoin(buffer, this_is_a_test, 4, "", 0), 11); 136 CU_ASSERT_EQUAL(buffer[0], '\xFF'); 137 138 CU_ASSERT_EQUAL(guac_strljoin(buffer, this_is_a_test, 4, "-/-", 0), 20); 139 CU_ASSERT_EQUAL(buffer[0], '\xFF'); 140 141 CU_ASSERT_EQUAL(guac_strljoin(buffer, empty_a_empty_b, 4, "/", 0), 5); 142 CU_ASSERT_EQUAL(buffer[0], '\xFF'); 143 144 CU_ASSERT_EQUAL(guac_strljoin(buffer, empty_x10, 10, "/", 0), 9); 145 CU_ASSERT_EQUAL(buffer[0], '\xFF'); 146 147} 148