cscg24-guacamole

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

escape.c (2353B)


      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 "url.h"
     21
     22#include <CUnit/CUnit.h>
     23#include <stdlib.h>
     24
     25/**
     26 * Verifies that guac_kubernetes_escape_url_component() correctly escapes
     27 * characters that would otherwise have special meaning within URLs.
     28 */
     29void test_url__escape_special() {
     30
     31    char value[256];
     32
     33    CU_ASSERT(!guac_kubernetes_escape_url_component(value, sizeof(value), "?foo%20bar\\1/2&3=4"));
     34    CU_ASSERT_STRING_EQUAL(value, "%3Ffoo%2520bar%5C1%2F2%263%3D4");
     35
     36}
     37
     38/**
     39 * Verifies that guac_kubernetes_escape_url_component() leaves strings
     40 * untouched if they contain no characters requiring escaping.
     41 */
     42void test_url__escape_nospecial() {
     43
     44    char value[256];
     45
     46    CU_ASSERT(!guac_kubernetes_escape_url_component(value, sizeof(value), "potato"));
     47    CU_ASSERT_STRING_EQUAL(value, "potato");
     48
     49}
     50
     51/**
     52 * Verifies that guac_kubernetes_escape_url_component() refuses to overflow the
     53 * bounds of the provided buffer.
     54 */
     55void test_url__escape_bounds() {
     56
     57    char value[256];
     58
     59    /* Escaping "?potato" (or "potato?") should fail for all buffer sizes with
     60     * 9 bytes or less, with a 9-byte buffer lacking space for the null
     61     * terminator */
     62    for (int length = 0; length <= 9; length++) {
     63        printf("Testing buffer with length %i ...\n", length);
     64        CU_ASSERT(guac_kubernetes_escape_url_component(value, length, "?potato"));
     65        CU_ASSERT(guac_kubernetes_escape_url_component(value, length, "potato?"));
     66    }
     67
     68    /* A 10-byte buffer should be sufficient */
     69    CU_ASSERT(!guac_kubernetes_escape_url_component(value, 10, "?potato"));
     70
     71}
     72