| 1 | /* |
| 2 | * Copyright 2015 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * Jan Černý <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include "common/oscap_string.h" |
| 31 | #include "common/oscap_buffer.h" |
| 32 | |
| 33 | int test_append_char(void); |
| 34 | int test_append_string(void); |
| 35 | int test_append_binary_data(void); |
| 36 | int test_null_parameters(void); |
| 37 | int test_buffer_clear(void); |
| 38 | |
| 39 | int test_append_char() |
| 40 | { |
| 41 | const unsigned int limit = 1000; |
| 42 | int retval = 0; |
| 43 | struct oscap_string *s = oscap_string_new(); |
| 44 | for (unsigned int i = 0; i < limit; i++) { |
| 45 | oscap_string_append_char(s, 'a'); |
| 46 | } |
| 47 | if (strlen(oscap_string_get_cstr(s)) != limit) { |
| 48 | fprintf(stderr, "Length of result does not match the required length.\n"); |
| 49 | retval = 1; |
| 50 | } |
| 51 | oscap_string_free(s); |
| 52 | return retval; |
| 53 | } |
| 54 | |
| 55 | int test_append_string() |
| 56 | { |
| 57 | const int count_of_strings = 7; |
| 58 | const char *strings[] = { |
| 59 | "", |
| 60 | "A", |
| 61 | "ABCD", |
| 62 | "ABCDEFGH", |
| 63 | "012345678901234567890123456789012345678901234567890123456789012", |
| 64 | "0123456789012345678901234567890123456789012345678901234567890123", |
| 65 | "01234567890123456789012345678901234567890123456789012345678901234" |
| 66 | }; |
| 67 | unsigned int length = 0; |
| 68 | int retval = 0; |
| 69 | struct oscap_string *s = oscap_string_new(); |
| 70 | for (int i = 0; i < count_of_strings; i++) { |
| 71 | oscap_string_append_string(s, strings[i]); |
| 72 | length += strlen(strings[i]); |
| 73 | } |
| 74 | if (strlen(oscap_string_get_cstr(s)) != length) { |
| 75 | fprintf(stderr, "Length of result does not match the required length.\n"); |
| 76 | retval = 1; |
| 77 | } |
| 78 | oscap_string_free(s); |
| 79 | return retval; |
| 80 | } |
| 81 | |
| 82 | #define ASSERT_LENGTH(STR, LENGTH) if (oscap_buffer_get_length(STR) != (LENGTH)) { fprintf(stderr, "Length of result does not match the required length.\n"); oscap_buffer_free(STR); return 1;} |
| 83 | |
| 84 | int test_append_binary_data() |
| 85 | { |
| 86 | static const char* data[] = { |
| 87 | "\0", |
| 88 | "\0A", |
| 89 | "ABC" |
| 90 | }; |
| 91 | |
| 92 | struct oscap_buffer *s = oscap_buffer_new(); |
| 93 | ASSERT_LENGTH(s, 0); |
| 94 | |
| 95 | oscap_buffer_append_binary_data(s, data[0], 1); |
| 96 | ASSERT_LENGTH(s, 1); |
| 97 | |
| 98 | oscap_buffer_append_binary_data(s, data[1], 2); |
| 99 | ASSERT_LENGTH(s, 3); |
| 100 | |
| 101 | oscap_buffer_append_binary_data(s, data[0], 0); // do not increase length |
| 102 | ASSERT_LENGTH(s, 3); |
| 103 | |
| 104 | oscap_buffer_append_binary_data(s, data[2], 3); |
| 105 | ASSERT_LENGTH(s, 6); |
| 106 | |
| 107 | |
| 108 | // Check output data |
| 109 | int length = oscap_buffer_get_length(s); // length is valid due to previous ASSERT_LENGTH |
| 110 | static const char expected_result[] = {'\0', '\0', 'A', 'A', 'B', 'C'}; |
| 111 | const char* result = oscap_buffer_get_raw(s); |
| 112 | for (int i = 0; i < length; ++i) { |
| 113 | char expected = expected_result[i]; |
| 114 | char got = result[i]; |
| 115 | if ( expected != got ) { |
| 116 | oscap_buffer_free(s); |
| 117 | fprintf(stderr, "Invalid character at index %d, expected %x and got %x\n", i, expected, got); |
| 118 | return 1; |
| 119 | } |
| 120 | } |
| 121 | oscap_buffer_free(s); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int test_null_parameters() { |
| 127 | struct oscap_buffer *s = oscap_buffer_new(); |
| 128 | oscap_buffer_append_binary_data(s, NULL, 4); |
| 129 | |
| 130 | if (oscap_buffer_get_length(s) != 0) { |
| 131 | fprintf(stderr, "Appended NULL changed the length of string\n"); |
| 132 | oscap_buffer_free(s); |
| 133 | return 1; |
| 134 | } |
| 135 | oscap_buffer_free(NULL); |
| 136 | oscap_buffer_free(s); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | int test_buffer_clear() |
| 141 | { |
| 142 | struct oscap_buffer *s = oscap_buffer_new(); |
| 143 | ASSERT_LENGTH(s, 0); |
| 144 | |
| 145 | oscap_buffer_clear(s); |
| 146 | ASSERT_LENGTH(s, 0); |
| 147 | |
| 148 | char data = 'A'; |
| 149 | oscap_buffer_append_binary_data(s, &data, 1); |
| 150 | ASSERT_LENGTH(s, 1); |
| 151 | |
| 152 | oscap_buffer_clear(s); |
| 153 | ASSERT_LENGTH(s, 0); |
| 154 | |
| 155 | oscap_buffer_free(s); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | int main (int argc, char *argv[]) |
| 160 | { |
| 161 | int retval = 0; |
| 162 | if ((retval = test_append_char()) != 0 ) { |
| 163 | return retval; |
| 164 | } |
| 165 | |
| 166 | if ((retval = test_append_string()) != 0 ) { |
| 167 | return retval; |
| 168 | } |
| 169 | |
| 170 | if ((retval = test_append_binary_data()) != 0 ) { |
| 171 | return retval; |
| 172 | } |
| 173 | |
| 174 | if ((retval = test_null_parameters()) != 0 ) { |
| 175 | return retval; |
| 176 | } |
| 177 | |
| 178 | if ((retval = test_buffer_clear()) != 0 ) { |
| 179 | return retval; |
| 180 | } |
| 181 | |
| 182 | return retval; |
| 183 | } |