cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

strlcpy.c (3226B)


      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 * Verify guac_strlcpy() behavior when the string fits the buffer without
     28 * truncation.
     29 */
     30void test_string__strlcpy() {
     31
     32    char buffer[1024];
     33
     34    memset(buffer, 0xFF, sizeof(buffer));
     35    CU_ASSERT_EQUAL(guac_strlcpy(buffer, "Guacamole", sizeof(buffer)), 9);
     36    CU_ASSERT_STRING_EQUAL(buffer, "Guacamole");
     37    CU_ASSERT_EQUAL(buffer[10], '\xFF');
     38
     39    memset(buffer, 0xFF, sizeof(buffer));
     40    CU_ASSERT_EQUAL(guac_strlcpy(buffer, "This is a test", sizeof(buffer)), 14);
     41    CU_ASSERT_STRING_EQUAL(buffer, "This is a test");
     42    CU_ASSERT_EQUAL(buffer[15], '\xFF');
     43
     44    memset(buffer, 0xFF, sizeof(buffer));
     45    CU_ASSERT_EQUAL(guac_strlcpy(buffer, "X", sizeof(buffer)), 1);
     46    CU_ASSERT_STRING_EQUAL(buffer, "X");
     47    CU_ASSERT_EQUAL(buffer[2], '\xFF');
     48
     49    memset(buffer, 0xFF, sizeof(buffer));
     50    CU_ASSERT_EQUAL(guac_strlcpy(buffer, "", sizeof(buffer)), 0);
     51    CU_ASSERT_STRING_EQUAL(buffer, "");
     52    CU_ASSERT_EQUAL(buffer[1], '\xFF');
     53
     54}
     55
     56/**
     57 * Verify guac_strlcpy() behavior when the string must be truncated to fit the
     58 * buffer.
     59 */
     60void test_string__strlcpy_truncate() {
     61
     62    char buffer[1024];
     63
     64    memset(buffer, 0xFF, sizeof(buffer));
     65    CU_ASSERT_EQUAL(guac_strlcpy(buffer, "Guacamole", 6), 9);
     66    CU_ASSERT_STRING_EQUAL(buffer, "Guaca");
     67    CU_ASSERT_EQUAL(buffer[6], '\xFF');
     68
     69    memset(buffer, 0xFF, sizeof(buffer));
     70    CU_ASSERT_EQUAL(guac_strlcpy(buffer, "This is a test", 10), 14);
     71    CU_ASSERT_STRING_EQUAL(buffer, "This is a");
     72    CU_ASSERT_EQUAL(buffer[10], '\xFF');
     73
     74    memset(buffer, 0xFF, sizeof(buffer));
     75    CU_ASSERT_EQUAL(guac_strlcpy(buffer, "This is ANOTHER test", 2), 20);
     76    CU_ASSERT_STRING_EQUAL(buffer, "T");
     77    CU_ASSERT_EQUAL(buffer[2], '\xFF');
     78
     79}
     80
     81/**
     82 * Verify guac_strlcpy() behavior with zero buffer sizes.
     83 */
     84void test_string__strlcpy_nospace() {
     85
     86    /* 0-byte buffer plus 1 guard byte (to test overrun) */
     87    char buffer[1] = { '\xFF' };
     88
     89    CU_ASSERT_EQUAL(guac_strlcpy(buffer, "Guacamole", 0), 9);
     90    CU_ASSERT_EQUAL(buffer[0], '\xFF');
     91
     92    CU_ASSERT_EQUAL(guac_strlcpy(buffer, "This is a test", 0), 14);
     93    CU_ASSERT_EQUAL(buffer[0], '\xFF');
     94
     95    CU_ASSERT_EQUAL(guac_strlcpy(buffer, "X", 0), 1);
     96    CU_ASSERT_EQUAL(buffer[0], '\xFF');
     97
     98    CU_ASSERT_EQUAL(guac_strlcpy(buffer, "", 0), 0);
     99    CU_ASSERT_EQUAL(buffer[0], '\xFF');
    100
    101}
    102