append.c (2553B)
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 <stdio.h> 24#include <stdlib.h> 25 26/** 27 * Verifies that guac_kubernetes_append_endpoint_param() correctly appends 28 * parameters to URLs that do not already have a query string. 29 */ 30void test_url__append_no_query() { 31 32 char url[256] = "http://example.net"; 33 34 CU_ASSERT(!guac_kubernetes_append_endpoint_param(url, sizeof(url), "foo", "100% test value")); 35 CU_ASSERT_STRING_EQUAL(url, "http://example.net?foo=100%25%20test%20value"); 36 37} 38 39/** 40 * Verifies that guac_kubernetes_append_endpoint_param() correctly appends 41 * parameters to URLs that already have a query string. 42 */ 43void test_url__append_existing_query() { 44 45 char url[256] = "http://example.net?foo=test%20value"; 46 47 CU_ASSERT(!guac_kubernetes_append_endpoint_param(url, sizeof(url), "foo2", "yet&another/test\\value")); 48 CU_ASSERT_STRING_EQUAL(url, "http://example.net?foo=test%20value&foo2=yet%26another%2Ftest%5Cvalue"); 49 50} 51 52/** 53 * Verifies that guac_kubernetes_append_endpoint_param() refuses to overflow 54 * the bounds of the provided buffer. 55 */ 56void test_url__append_bounds() { 57 58 char url[256]; 59 60 /* Appending "?a=1" to the 18-character string "http://example.net" should 61 * fail for all buffer sizes with 22 bytes or less, with a 22-byte buffer 62 * lacking space for the null terminator */ 63 for (int length = 18; length <= 22; length++) { 64 strcpy(url, "http://example.net"); 65 printf("Testing buffer with length %i ...\n", length); 66 CU_ASSERT(guac_kubernetes_append_endpoint_param(url, length, "a", "1")); 67 } 68 69 /* A 23-byte buffer should be sufficient */ 70 strcpy(url, "http://example.net"); 71 CU_ASSERT(!guac_kubernetes_append_endpoint_param(url, 23, "a", "1")); 72 73} 74