1 /*
2 * Copyright 2011 The Chromium Authors, All Rights Reserved.
3 *
4 * utilfdt_test - Tests for utilfdt library
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdint.h>
25 #include <stdarg.h>
26
27 #include <libfdt.h>
28 #include <util.h>
29
30 #include "tests.h"
31 #include "testdata.h"
32
check(const char * fmt,int expect_type,int expect_size)33 static void check(const char *fmt, int expect_type, int expect_size)
34 {
35 int type;
36 int size;
37
38 if (utilfdt_decode_type(fmt, &type, &size))
39 FAIL("format '%s': valid format string returned failure", fmt);
40 if (expect_type != type)
41 FAIL("format '%s': expected type='%c', got type='%c'", fmt,
42 expect_type, type);
43 if (expect_size != size)
44 FAIL("format '%s': expected size=%d, got size=%d", fmt,
45 expect_size, size);
46 }
47
checkfail(const char * fmt)48 static void checkfail(const char *fmt)
49 {
50 int type;
51 int size;
52
53 if (!utilfdt_decode_type(fmt, &type, &size))
54 FAIL("format '%s': invalid format string returned success",
55 fmt);
56 }
57
58 /**
59 * Add the given modifier to each of the valid sizes, and check that we get
60 * correct values.
61 *
62 * \param modifier Modifer string to use as a prefix
63 * \param expected_size The size (in bytes) that we expect (ignored for
64 * strings)
65 */
check_sizes(char * modifier,int expected_size)66 static void check_sizes(char *modifier, int expected_size)
67 {
68 char fmt[10], *ptr;
69
70 /* set up a string with a hole in it for the format character */
71 if (strlen(modifier) + 2 >= sizeof(fmt))
72 FAIL("modifier string '%s' too long", modifier);
73 strcpy(fmt, modifier);
74 ptr = fmt + strlen(fmt);
75 ptr[1] = '\0';
76
77 /* now try each format character in turn */
78 *ptr = 'i';
79 check(fmt, 'i', expected_size);
80
81 *ptr = 'u';
82 check(fmt, 'u', expected_size);
83
84 *ptr = 'x';
85 check(fmt, 'x', expected_size);
86
87 *ptr = 's';
88 check(fmt, 's', -1);
89 }
90
test_utilfdt_decode_type(void)91 static void test_utilfdt_decode_type(void)
92 {
93 char fmt[10];
94 int ch;
95
96 /* check all the valid modifiers and sizes */
97 check_sizes("", -1);
98 check_sizes("b", 1);
99 check_sizes("hh", 1);
100 check_sizes("h", 2);
101 check_sizes("l", 4);
102
103 /* try every other character */
104 checkfail("");
105 for (ch = ' '; ch < 127; ch++) {
106 if (!strchr("iuxs", ch)) {
107 *fmt = ch;
108 fmt[1] = '\0';
109 checkfail(fmt);
110 }
111 }
112
113 /* try a few modifiers at the end */
114 checkfail("sx");
115 checkfail("ihh");
116 checkfail("xb");
117
118 /* and one for the doomsday archives */
119 checkfail("He has all the virtues I dislike and none of the vices "
120 "I admire.");
121 }
122
main(int argc,char * argv[])123 int main(int argc, char *argv[])
124 {
125 test_utilfdt_decode_type();
126 PASS();
127 }
128