25 #include <winpr/crypto.h>
26 #include <freerdp/utils/encoded_types.h>
28 #define MIN(x, y) ((x) < (y)) ? (x) : (y)
29 #define MAX(x, y) ((x) > (y)) ? (x) : (y)
31 static BOOL test_signed_integer_read_write_equal(INT32 value)
34 BYTE buffer[32] = { 0 };
36 wStream* s = Stream_StaticInit(&sbuffer, buffer,
sizeof(buffer));
39 if (!freerdp_write_four_byte_signed_integer(s, value))
41 (void)fprintf(stderr,
"[%s(%" PRId32
")] failed to write to stream\n", __func__, value);
44 if (!Stream_SetPosition(s, 0))
46 (void)fprintf(stderr,
"[%s(%" PRId32
")] failed to reset stream position\n", __func__,
50 if (!freerdp_read_four_byte_signed_integer(s, &rvalue))
52 (void)fprintf(stderr,
"[%s(%" PRId32
")] failed to read from stream\n", __func__, value);
57 (void)fprintf(stderr,
"[%s(%" PRId32
")] read invalid value %" PRId32
" from stream\n",
58 __func__, value, rvalue);
64 static BOOL test_signed_integer_write_oor(INT32 value)
66 BYTE buffer[32] = { 0 };
68 wStream* s = Stream_StaticInit(&sbuffer, buffer,
sizeof(buffer));
71 if (freerdp_write_four_byte_signed_integer(s, value))
74 "[%s(%" PRId32
")] out of range value not detected and written to stream\n",
81 static BOOL test_signed_integers(
void)
83 const INT32 outofrange[] = { FREERDP_FOUR_BYTE_SIGNED_INT_MAX + 1,
84 FREERDP_FOUR_BYTE_SIGNED_INT_MIN - 1, INT32_MAX, INT32_MIN };
85 const INT32 limits[] = { 1, 0, -1, FREERDP_FOUR_BYTE_SIGNED_INT_MAX,
86 FREERDP_FOUR_BYTE_SIGNED_INT_MIN };
88 for (
size_t x = 0; x < ARRAYSIZE(limits); x++)
90 if (!test_signed_integer_read_write_equal(limits[x]))
93 for (
size_t x = 0; x < ARRAYSIZE(outofrange); x++)
95 if (!test_signed_integer_write_oor(outofrange[x]))
98 for (
size_t x = 0; x < 100000; x++)
101 winpr_RAND(&val,
sizeof(val));
103 val = MIN(val, FREERDP_FOUR_BYTE_SIGNED_INT_MAX);
105 const INT32 nval = -val;
106 if (!test_signed_integer_read_write_equal(val))
108 if (!test_signed_integer_read_write_equal(nval))
114 static BOOL test_float_read_write_equal(
double value)
117 double rvalue = FP_NAN;
118 BYTE buffer[32] = { 0 };
120 wStream* s = Stream_StaticInit(&sbuffer, buffer,
sizeof(buffer));
123 if (!freerdp_write_four_byte_float(s, value))
125 (void)fprintf(stderr,
"[%s(%lf)] failed to write to stream\n", __func__, value);
128 if (!Stream_SetPosition(s, 0))
130 (void)fprintf(stderr,
"[%s(%lf)] failed to reset stream position\n", __func__, value);
133 if (!freerdp_read_four_byte_float_exp(s, &rvalue, &exp))
135 (void)fprintf(stderr,
"[%s(%lf)] failed to read from stream\n", __func__, value);
138 const double diff = fabs(value - rvalue);
139 const double expdiff = diff * pow(10, exp);
142 (void)fprintf(stderr,
"[%s(%lf)] read invalid value %lf from stream\n", __func__, value,
149 static BOOL test_floag_write_oor(
double value)
151 BYTE buffer[32] = { 0 };
153 wStream* s = Stream_StaticInit(&sbuffer, buffer,
sizeof(buffer));
156 if (freerdp_write_four_byte_float(s, value))
158 (void)fprintf(stderr,
"[%s(%lf)] out of range value not detected and written to stream\n",
165 static double get(
void)
170 winpr_RAND(&val,
sizeof(val));
171 }
while ((val < 0.0) || (val > FREERDP_FOUR_BYTE_FLOAT_MAX) || isnan(val) || isinf(val));
175 static BOOL test_floats(
void)
177 const double outofrange[] = { FREERDP_FOUR_BYTE_FLOAT_MAX + 1, FREERDP_FOUR_BYTE_FLOAT_MIN - 1,
179 const double limits[] = { 100045.26129238126, 1, 0, -1, FREERDP_FOUR_BYTE_FLOAT_MAX,
180 FREERDP_FOUR_BYTE_FLOAT_MIN };
182 for (
size_t x = 0; x < ARRAYSIZE(limits); x++)
184 if (!test_float_read_write_equal(limits[x]))
187 for (
size_t x = 0; x < ARRAYSIZE(outofrange); x++)
189 if (!test_floag_write_oor(outofrange[x]))
192 for (
size_t x = 0; x < 100000; x++)
196 const double nval = -val;
197 if (!test_float_read_write_equal(val))
199 if (!test_float_read_write_equal(nval))
205 int TestEncodedTypes(
int argc,
char* argv[])
210 if (!test_signed_integers())