base64_decode.c (1955B)
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/protocol.h> 22 23/** 24 * Tests that libguac's in-place base64 decoding function properly decodes 25 * valid base64 and fails for invalid base64. 26 */ 27void test_protocol__decode_base64() { 28 29 /* Test strings */ 30 char test_HELLO[] = "SEVMTE8="; 31 char test_AVOCADO[] = "QVZPQ0FETw=="; 32 char test_GUACAMOLE[] = "R1VBQ0FNT0xF"; 33 34 /* Invalid strings */ 35 char invalid1[] = "===="; 36 char invalid2[] = ""; 37 38 /* Test one character of padding */ 39 CU_ASSERT_EQUAL(guac_protocol_decode_base64(test_HELLO), 5); 40 CU_ASSERT_NSTRING_EQUAL(test_HELLO, "HELLO", 5); 41 42 /* Test two characters of padding */ 43 CU_ASSERT_EQUAL(guac_protocol_decode_base64(test_AVOCADO), 7); 44 CU_ASSERT_NSTRING_EQUAL(test_AVOCADO, "AVOCADO", 7); 45 46 /* Test three characters of padding */ 47 CU_ASSERT_EQUAL(guac_protocol_decode_base64(test_GUACAMOLE), 9); 48 CU_ASSERT_NSTRING_EQUAL(test_GUACAMOLE, "GUACAMOLE", 9); 49 50 /* Verify invalid strings stop early as expected */ 51 CU_ASSERT_EQUAL(guac_protocol_decode_base64(invalid1), 0); 52 CU_ASSERT_EQUAL(guac_protocol_decode_base64(invalid2), 0); 53 54} 55