Branch data Line data Source code
1 : : #include "test_libmem_all.h"
2 : :
3 : : /**
4 : : * @brief Capture invalid descriptor cases for the soft string view helper
5 : : *
6 : : * @return Return describing success or failure
7 : : */
8 : 1 : static Return capture_invalid_mem_string_descriptors(void)
9 : : {
10 : 1 : INITTEST;
11 : :
12 : 1 : memory zero_sized_string = m_init(char,MEMORY_STRING);
13 : 1 : memory data_descriptor = m_init(char,MEMORY_DATA);
14 : 1 : memory missing_data_string = m_init(char,MEMORY_STRING);
15 : 1 : memory invalid_cached_length = m_init(char,MEMORY_STRING);
16 : 1 : char cached_text[] = "ab";
17 : :
18 : 1 : zero_sized_string.single_element_size = 0;
19 : 1 : missing_data_string.length = 3;
20 : 1 : missing_data_string.string_length = 1;
21 : 1 : invalid_cached_length.data = cached_text;
22 : 1 : invalid_cached_length.length = sizeof(cached_text);
23 : 1 : invalid_cached_length.string_length = invalid_cached_length.length;
24 : :
25 : 1 : const unsigned char *empty_view = (const unsigned char *)m_string(NULL);
26 : 1 : ASSERT(empty_view != NULL);
27 : :
28 : 1 : IF(empty_view != NULL)
29 : : {
30 : 1 : ASSERT(empty_view[0] == 0U);
31 : : }
32 : :
33 : 1 : empty_view = (const unsigned char *)m_string(&zero_sized_string);
34 : 1 : ASSERT(empty_view != NULL);
35 : :
36 : 1 : IF(empty_view != NULL)
37 : : {
38 : 1 : ASSERT(empty_view[0] == 0U);
39 : : }
40 : :
41 : 1 : empty_view = (const unsigned char *)m_string(&data_descriptor);
42 : 1 : ASSERT(empty_view != NULL);
43 : :
44 : 1 : IF(empty_view != NULL)
45 : : {
46 : 1 : ASSERT(empty_view[0] == 0U);
47 : : }
48 : :
49 : 1 : empty_view = (const unsigned char *)m_string(&missing_data_string);
50 : 1 : ASSERT(empty_view != NULL);
51 : :
52 : 1 : IF(empty_view != NULL)
53 : : {
54 : 1 : ASSERT(empty_view[0] == 0U);
55 : : }
56 : :
57 : 1 : empty_view = (const unsigned char *)m_string(&invalid_cached_length);
58 : 1 : ASSERT(empty_view != NULL);
59 : :
60 : 1 : IF(empty_view != NULL)
61 : : {
62 : 1 : ASSERT(empty_view[0] == 0U);
63 : : }
64 : :
65 : 1 : deliver(status);
66 : : }
67 : :
68 : : /**
69 : : * @brief Check soft read-only string access for byte and multi-byte descriptors
70 : : *
71 : : * @return Return describing success or failure
72 : : */
73 : 1 : Return test_libmem_0067(void)
74 : : {
75 : 1 : INITTEST;
76 : :
77 : : static const char expected_stderr_pattern_libmem_0067[] =
78 : : "\\A.*Soft string view requires a non-NULL descriptor"
79 : : ".*Soft string view requires a non-zero element size"
80 : : ".*Soft string view requires a string descriptor"
81 : : ".*Descriptor has non-zero length with NULL data pointer"
82 : : ".*Soft string view requires string_length to stay below length.*\\Z";
83 : :
84 : : static const char alpha_text[] = "alpha";
85 : : static const uint32_t wide_text[] = {
86 : : UINT32_C(100),
87 : : UINT32_C(200),
88 : : UINT32_C(0)
89 : : };
90 : :
91 : 1 : m_create(char,byte_string,MEMORY_STRING);
92 : 1 : m_create(uint32_t,wide_string,MEMORY_STRING);
93 : :
94 : 1 : ASSERT(SUCCESS == m_copy_fixed_string(byte_string,sizeof(alpha_text),alpha_text));
95 : :
96 : 1 : const char *byte_view = (const char *)m_string(byte_string);
97 : 1 : ASSERT(byte_view != NULL);
98 : :
99 : 1 : IF(byte_view != NULL)
100 : : {
101 : 1 : ASSERT(0 == strcmp(byte_view,"alpha"));
102 : : }
103 : :
104 : 1 : const char *text_view = m_text(byte_string);
105 : 1 : ASSERT(text_view != NULL);
106 : :
107 : 1 : IF(text_view != NULL)
108 : : {
109 : 1 : ASSERT(0 == strcmp(text_view,"alpha"));
110 : : }
111 : :
112 : 1 : ASSERT(byte_string->is_string == true);
113 : 1 : ASSERT(byte_string->string_length == strlen("alpha"));
114 : :
115 : 1 : ASSERT(SUCCESS == m_copy_fixed_string(wide_string,sizeof(wide_text),wide_text));
116 : :
117 : 1 : const uint32_t *wide_view = (const uint32_t *)m_string(wide_string);
118 : 1 : ASSERT(wide_view != NULL);
119 : :
120 : 1 : IF(wide_view != NULL)
121 : : {
122 : 1 : ASSERT(wide_view[0] == UINT32_C(100));
123 : 1 : ASSERT(wide_view[1] == UINT32_C(200));
124 : 1 : ASSERT(wide_view[2] == UINT32_C(0));
125 : : }
126 : :
127 : 1 : ASSERT(wide_string->is_string == true);
128 : 1 : ASSERT(wide_string->string_length == 2);
129 : :
130 : 1 : call(m_del(wide_string));
131 : :
132 : 1 : m_create(uint32_t,empty_wide_string,MEMORY_STRING);
133 : :
134 : 1 : const uint32_t *empty_wide_view = (const uint32_t *)m_string(empty_wide_string);
135 : 1 : ASSERT(empty_wide_view != NULL);
136 : :
137 : 1 : IF(empty_wide_view != NULL)
138 : : {
139 : 1 : ASSERT(empty_wide_view[0] == UINT32_C(0));
140 : : }
141 : :
142 : 1 : ASSERT(SUCCESS == match_function_output(NULL,expected_stderr_pattern_libmem_0067,capture_invalid_mem_string_descriptors));
143 : :
144 : 1 : call(m_del(empty_wide_string));
145 : 1 : call(m_del(byte_string));
146 : :
147 : 1 : RETURN_STATUS;
148 : : }
|