FreeRDP
Loading...
Searching...
No Matches
TestStreamDump.c
1#include <stdio.h>
2
3#include <winpr/stream.h>
4#include <winpr/path.h>
5#include <winpr/crypto.h>
6
7#include <freerdp/freerdp.h>
8#include <freerdp/streamdump.h>
9
10#include "../streamdump.h"
11
12static BOOL test_entry_read_write(void)
13{
14 BOOL rc = FALSE;
15 FILE* fp = nullptr;
16 wStream* sw = nullptr;
17 wStream* sr = nullptr;
18 size_t offset = 0;
19 UINT64 ts = 0;
20 UINT32 flags = 0;
21 BYTE tmp[16] = WINPR_C_ARRAY_INIT;
22 char tmp2[64] = WINPR_C_ARRAY_INIT;
23 char* name = nullptr;
24 size_t entrysize = sizeof(UINT64) /* timestamp */ + sizeof(BYTE) /* direction */ +
25 sizeof(UINT32) /* CRC */ + sizeof(UINT64) /* size */;
26
27 if (winpr_RAND(tmp, sizeof(tmp)) < 0)
28 goto fail;
29
30 for (size_t x = 0; x < sizeof(tmp); x++)
31 (void)_snprintf(&tmp2[x * 2], sizeof(tmp2) - 2 * x, "%02" PRIx8, tmp[x]);
32 name = GetKnownSubPath(KNOWN_PATH_TEMP, tmp2);
33 if (!name)
34 {
35 (void)fprintf(stderr, "[%s] Could not create temporary path\n", __func__);
36 goto fail;
37 }
38
39 sw = Stream_New(nullptr, 8123);
40 sr = Stream_New(nullptr, 1024);
41 if (!sr || !sw)
42 {
43 (void)fprintf(stderr, "[%s] Could not create iostreams sw=%p, sr=%p\n", __func__, (void*)sw,
44 (void*)sr);
45 goto fail;
46 }
47
48 if (winpr_RAND(Stream_Buffer(sw), Stream_Capacity(sw)) < 0)
49 goto fail;
50 entrysize += Stream_Capacity(sw);
51 Stream_SetLength(sw, Stream_Capacity(sw));
52
53 fp = fopen(name, "wb");
54 if (!fp)
55 goto fail;
56 if (!stream_dump_write_line(fp, 0, sw))
57 goto fail;
58 (void)fclose(fp);
59
60 fp = fopen(name, "rb");
61 if (!fp)
62 goto fail;
63 if (!stream_dump_read_line(fp, sr, &ts, &offset, &flags))
64 goto fail;
65
66 if (entrysize != offset)
67 {
68 (void)fprintf(stderr, "[%s] offset %" PRIuz " bytes, entrysize %" PRIuz " bytes\n",
69 __func__, offset, entrysize);
70 goto fail;
71 }
72
73 if (Stream_Length(sr) != Stream_Capacity(sw))
74 {
75 (void)fprintf(stderr, "[%s] Written %" PRIuz " bytes, read %" PRIuz " bytes\n", __func__,
76 Stream_Length(sr), Stream_Capacity(sw));
77 goto fail;
78 }
79
80 if (memcmp(Stream_Buffer(sw), Stream_Buffer(sr), Stream_Capacity(sw)) != 0)
81 {
82 (void)fprintf(stderr, "[%s] Written data does not match data read back\n", __func__);
83 goto fail;
84 }
85 rc = TRUE;
86fail:
87 Stream_Free(sr, TRUE);
88 Stream_Free(sw, TRUE);
89 if (fp)
90 (void)fclose(fp);
91 if (name)
92 winpr_DeleteFile(name);
93 free(name);
94 (void)fprintf(stderr, "xxxxxxxxxxxxx %d\n", rc);
95 return rc;
96}
97
98int TestStreamDump(int argc, char* argv[])
99{
100 WINPR_UNUSED(argc);
101 WINPR_UNUSED(argv);
102
103 if (!test_entry_read_write())
104 return -1;
105 return 0;
106}