1 /*
2  * Copyright (c) 2013, NLNet Labs, Verisign, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  *   notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  *   notice, this list of conditions and the following disclaimer in the
11  *   documentation and/or other materials provided with the distribution.
12  * * Neither the names of the copyright holders nor the
13  *   names of its contributors may be used to endorse or promote products
14  *   derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 #ifndef _check_getdns_list_get_bindata_h_
28 #define _check_getdns_list_get_bindata_h_
29 
30     /*
31      **************************************************************************
32      *                                                                        *
33      *  T E S T S  F O R  G E T D N S _ L I S T _ G E T _ N A M E   *
34      *                                                                        *
35      **************************************************************************
36     */
37 
START_TEST(getdns_list_get_bindata_1)38     START_TEST (getdns_list_get_bindata_1)
39     {
40      /*
41       *  list = NULL
42       *  expect: GETDNS_RETURN_INVALID_PARAMETER
43       */
44       struct getdns_list *list = NULL;
45       size_t index = 0;
46       struct getdns_bindata *answer = NULL;
47 
48       ASSERT_RC(getdns_list_get_bindata(list, index, &answer),
49         GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_names()");
50 
51     }
52     END_TEST
53 
START_TEST(getdns_list_get_bindata_2)54     START_TEST (getdns_list_get_bindata_2)
55     {
56      /* index is out of range
57       * create a list and and set index 0 to an int with a value of 100
58       * Call getdns_get_list() for index 1
59       * expect: GETDNS_RETURN_INVALID_PARAMETER
60      */
61       struct getdns_list *list = NULL;
62       size_t index = 0;
63 
64       LIST_CREATE(list);
65 
66       ASSERT_RC(getdns_list_set_int(list, index, 1),
67         GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()");
68 
69       index++;
70       ASSERT_RC(getdns_list_get_bindata(list, index, NULL),
71       	GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_bindata()");
72 
73       LIST_DESTROY(list);
74     }
75     END_TEST
76 
START_TEST(getdns_list_get_bindata_3)77     START_TEST (getdns_list_get_bindata_3)
78     {
79      /* data type at index is not int
80       * create a list and set index 0 to an int with a value of 100
81       * Call getdns_list_get_bindata() for index 0
82       * expect: GETDNS_RETURN_WRONG_TYPE_REQUESTED
83       */
84       struct getdns_list *list = NULL;
85       size_t index = 0;
86       struct getdns_bindata *answer = NULL;
87 
88       LIST_CREATE(list);
89 
90       ASSERT_RC(getdns_list_set_int(list, index, 1),
91         GETDNS_RETURN_GOOD, "Return code from getdns_list_set_int()");
92 
93 
94       ASSERT_RC(getdns_list_get_bindata(list, index, &answer),
95         GETDNS_RETURN_WRONG_TYPE_REQUESTED, "Return code from getdns_list_get_bindata()");
96 
97       LIST_DESTROY(list);
98     }
99     END_TEST
100 
START_TEST(getdns_list_get_bindata_4)101     START_TEST (getdns_list_get_bindata_4)
102     {
103      /* answer is NULL
104       * expect: GETDNS_RETURN_INVALID_PARAMETER
105       */
106       struct getdns_list *list = NULL;
107       size_t index = 0;
108 
109       ASSERT_RC(getdns_list_get_bindata(list, index, NULL),
110         GETDNS_RETURN_INVALID_PARAMETER, "Return code from getdns_list_get_bindata()");
111     }
112     END_TEST
113 
START_TEST(getdns_list_get_bindata_5)114     START_TEST (getdns_list_get_bindata_5)
115     {
116      /*
117       * create a list
118       * Create some bindata containing "bindata" and add it to the list with name = "bindata"
119       * Set list value at index 0 via getdns_list_set_list() to the bindata
120       * Call getdns_list_get_list() for index 0
121       * expect: GETDNS_RETURN_GOOD
122       */
123       struct getdns_list *list = NULL;
124       struct getdns_bindata bindata = { 8, (void *)"bindata" };
125       size_t index = 0;
126       struct getdns_bindata *answer = NULL;
127 
128       LIST_CREATE(list);
129 
130       ASSERT_RC(getdns_list_set_bindata(list, index, &bindata),
131         GETDNS_RETURN_GOOD, "Return code from getdns_list_set_bindata()");
132 
133 
134       ASSERT_RC(getdns_list_get_bindata(list, index, &answer),
135         GETDNS_RETURN_GOOD, "Return code from getdns_list_get_bindata()");
136 
137       ck_assert_msg(strcmp((char *)answer->data, (char *)bindata.data) == 0,
138         "Expected bindata data to be \"%s\", got: \"%s\"",
139         (char *)bindata.data, (char *)answer->data);
140 
141       LIST_DESTROY(list);
142     }
143     END_TEST
144 
145 
146 
147 
148 
149 
150     Suite *
getdns_list_get_bindata_suite(void)151     getdns_list_get_bindata_suite (void)
152     {
153       Suite *s = suite_create ("getdns_list_get_bindata()");
154 
155       /* Negative test caseis */
156       TCase *tc_neg = tcase_create("Negative");
157       tcase_add_test(tc_neg, getdns_list_get_bindata_1);
158       tcase_add_test(tc_neg, getdns_list_get_bindata_2);
159       tcase_add_test(tc_neg, getdns_list_get_bindata_3);
160       tcase_add_test(tc_neg, getdns_list_get_bindata_4);
161       suite_add_tcase(s, tc_neg);
162 
163       /* Positive test cases */
164        TCase *tc_pos = tcase_create("Positive");
165        tcase_add_test(tc_pos, getdns_list_get_bindata_5);
166        suite_add_tcase(s, tc_pos);
167 
168       return s;
169     }
170 
171 #endif
172