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