convert.c (4305B)
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 "common/iconv.h" 21#include "convert-test-data.h" 22 23#include <CUnit/CUnit.h> 24#include <stdio.h> 25 26/** 27 * Tests that conversion between character sets using the given guac_iconv_read 28 * and guac_iconv_write implementations matches expectations. 29 * 30 * @param reader 31 * The guac_iconv_read implementation to use to read the input string. 32 * 33 * @param in_string 34 * A pointer to the test_string structure describing the input string being 35 * tested. 36 * 37 * @param writer 38 * The guac_iconv_write implementation to use to write the output string 39 * (the converted input string). 40 * 41 * @param out_string 42 * A pointer to the test_string structure describing the expected result of 43 * the conversion. 44 */ 45static void verify_conversion( 46 guac_iconv_read* reader, test_string* in_string, 47 guac_iconv_write* writer, test_string* out_string) { 48 49 char output[4096]; 50 char input[4096]; 51 52 const char* current_input = input; 53 char* current_output = output; 54 55 memcpy(input, in_string->buffer, in_string->size); 56 guac_iconv(reader, ¤t_input, sizeof(input), 57 writer, ¤t_output, sizeof(output)); 58 59 /* Verify output length */ 60 CU_ASSERT_EQUAL(out_string->size, current_output - output); 61 62 /* Verify entire input read */ 63 CU_ASSERT_EQUAL(in_string->size, current_input - input); 64 65 /* Verify output content */ 66 CU_ASSERT_EQUAL(0, memcmp(output, out_string->buffer, out_string->size)); 67 68} 69 70/** 71 * Test which verifies that every supported encoding can be correctly converted 72 * to every other supported encoding, with all line endings preserved verbatim 73 * (not normalized). 74 */ 75void test_iconv__preserve() { 76 for (int i = 0; i < NUM_SUPPORTED_ENCODINGS; i++) { 77 for (int j = 0; j < NUM_SUPPORTED_ENCODINGS; j++) { 78 79 encoding_test_parameters* from = &test_params[i]; 80 encoding_test_parameters* to = &test_params[j]; 81 82 printf("# \"%s\" -> \"%s\" ...\n", from->name, to->name); 83 verify_conversion(from->reader, &from->test_mixed, 84 to->writer, &to->test_mixed); 85 86 } 87 } 88} 89 90/** 91 * Test which verifies that every supported encoding can be correctly converted 92 * to every other supported encoding, normalizing all line endings to 93 * Unix-style line endings. 94 */ 95void test_iconv__normalize_unix() { 96 for (int i = 0; i < NUM_SUPPORTED_ENCODINGS; i++) { 97 for (int j = 0; j < NUM_SUPPORTED_ENCODINGS; j++) { 98 99 encoding_test_parameters* from = &test_params[i]; 100 encoding_test_parameters* to = &test_params[j]; 101 102 printf("# \"%s\" -> \"%s\" ...\n", from->name, to->name); 103 verify_conversion(from->reader_normalized, &from->test_mixed, 104 to->writer, &to->test_unix); 105 106 } 107 } 108} 109 110/** 111 * Test which verifies that every supported encoding can be correctly converted 112 * to every other supported encoding, normalizing all line endings to 113 * Windows-style line endings. 114 */ 115void test_iconv__normalize_crlf() { 116 for (int i = 0; i < NUM_SUPPORTED_ENCODINGS; i++) { 117 for (int j = 0; j < NUM_SUPPORTED_ENCODINGS; j++) { 118 119 encoding_test_parameters* from = &test_params[i]; 120 encoding_test_parameters* to = &test_params[j]; 121 122 printf("# \"%s\" -> \"%s\" ...\n", from->name, to->name); 123 verify_conversion(from->reader_normalized, &from->test_mixed, 124 to->writer_crlf, &to->test_windows); 125 126 } 127 } 128} 129