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