1/*
2 * Copyright (c) 2013, Oracle and/or its affiliates.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <assert.h>
28#include <X11/Xdmcp.h>
29#include <inttypes.h>
30
31/* Test what happens if you try to allocate an array with too many entries */
32#define TestAllocOversize(type, len) {                          \
33    type newArray = { -1, (void *) -1 };                        \
34    int result;                                                 \
35    printf("Checking XdmcpAlloc%s(%d)...\n", #type, len);       \
36    result = XdmcpAlloc##type(&newArray, len)    ;              \
37    assert(result == FALSE);                                    \
38    assert(newArray.length == 0);                               \
39    assert(newArray.data == NULL);                              \
40    printf("Checking XdmcpRealloc%s(%d)...\n", #type, len);     \
41    result = XdmcpRealloc##type(&newArray, len);                \
42    assert(result == FALSE);                                    \
43    assert(newArray.length == 0);                               \
44    assert(newArray.data == NULL);                              \
45    XdmcpDispose##type(&newArray);                              \
46}
47
48static void
49TestAllocOversizeArrays(void)
50{
51    TestAllocOversize(ARRAY8, UINT16_MAX + 1);
52    TestAllocOversize(ARRAY16, UINT8_MAX + 1);
53    TestAllocOversize(ARRAY32, UINT8_MAX + 1);
54    TestAllocOversize(ARRAYofARRAY8, UINT8_MAX + 1);
55    TestAllocOversize(ARRAY8, -1);
56    TestAllocOversize(ARRAY16, -1);
57    TestAllocOversize(ARRAY32, -1);
58    TestAllocOversize(ARRAYofARRAY8, -1);
59}
60
61static void
62TestZeroFillARRAYofARRAY8(void)
63{
64    ARRAYofARRAY8 aa;
65    int result;
66    char *noise;
67
68    printf("Checking XdmcpAllocARRAYofARRAY8 zero fills array...\n");
69    /* prefill memory with junk - hopefully next malloc will pick up some */
70    noise = malloc(32 * sizeof(ARRAY8));
71    memset(noise, 0xdeadbeef, 32 * sizeof(ARRAY8));
72    free(noise);
73    result = XdmcpAllocARRAYofARRAY8(&aa, 32);
74    assert(result == TRUE);
75    assert(aa.length == 32);
76    assert(aa.data[4].data == NULL);
77    printf("Checking XdmcpReallocARRAYofARRAY8 zero fills array...\n");
78    result = XdmcpAllocARRAYofARRAY8(&aa, 48);
79    assert(result == TRUE);
80    assert(aa.length == 48);
81    assert(aa.data[40].data == NULL);
82    XdmcpDisposeARRAYofARRAY8(&aa);
83}
84
85int
86main(int argc, char **argv)
87{
88    TestAllocOversizeArrays();
89    TestZeroFillARRAYofARRAY8();
90
91    exit(0);
92}
93