generate.c (2313B)
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 "guacamole/mem.h" 21#include "id.h" 22 23#include <CUnit/CUnit.h> 24#include <stdlib.h> 25#include <stdio.h> 26#include <string.h> 27 28/** 29 * Test which verifies that each call to guac_generate_id() produces a 30 * different string. 31 */ 32void test_id__unique() { 33 34 char* id1 = guac_generate_id('x'); 35 char* id2 = guac_generate_id('x'); 36 37 /* Neither string may be NULL */ 38 CU_ASSERT_PTR_NOT_NULL_FATAL(id1); 39 CU_ASSERT_PTR_NOT_NULL_FATAL(id2); 40 41 /* Both strings should be different */ 42 CU_ASSERT_STRING_NOT_EQUAL(id1, id2); 43 44 guac_mem_free(id1); 45 guac_mem_free(id2); 46 47} 48 49/** 50 * Test which verifies that guac_generate_id() produces strings are in the 51 * correc UUID-based format. 52 */ 53void test_id__format() { 54 55 unsigned int ignore; 56 57 char* id = guac_generate_id('x'); 58 CU_ASSERT_PTR_NOT_NULL_FATAL(id); 59 60 int items_read = sscanf(id, "x%08x-%04x-%04x-%04x-%08x%04x", 61 &ignore, &ignore, &ignore, &ignore, &ignore, &ignore); 62 63 CU_ASSERT_EQUAL(items_read, 6); 64 CU_ASSERT_EQUAL(strlen(id), 37); 65 66 guac_mem_free(id); 67 68} 69 70/** 71 * Test which verifies that guac_generate_id() takes the specified prefix 72 * character into account when generating the ID string. 73 */ 74void test_id__prefix() { 75 76 char* id; 77 78 id = guac_generate_id('a'); 79 CU_ASSERT_PTR_NOT_NULL_FATAL(id); 80 CU_ASSERT_EQUAL(id[0], 'a'); 81 guac_mem_free(id); 82 83 id = guac_generate_id('b'); 84 CU_ASSERT_PTR_NOT_NULL_FATAL(id); 85 CU_ASSERT_EQUAL(id[0], 'b'); 86 guac_mem_free(id); 87 88} 89