FreeRDP
Loading...
Searching...
No Matches
TestFreeRDPHelpers.c
1
20#include <errno.h>
21#include <stdio.h>
22
23#include <winpr/path.h>
24#include <winpr/file.h>
25#include <winpr/debug.h>
26#include <winpr/print.h>
27
28#include "TestFreeRDPHelpers.h"
29
30static char* get_path(const char* codec, const char* type, const char* name)
31{
32 char path[500] = { 0 };
33 (void)snprintf(path, sizeof(path), "%s-%s-%s.bin", codec, type, name);
34 char* s1 = GetCombinedPath(CMAKE_CURRENT_SOURCE_DIR, codec);
35 if (!s1)
36 return NULL;
37
38 char* s2 = GetCombinedPath(s1, path);
39 free(s1);
40 return s2;
41}
42
43static FILE* open_path(const char* codec, const char* type, const char* name, const char* mode)
44{
45 WINPR_ASSERT(type);
46 WINPR_ASSERT(name);
47 WINPR_ASSERT(mode);
48
49 char* path = get_path(codec, type, name);
50 if (!path)
51 {
52 (void)printf("%s: get_path %s %s failed\n", __func__, type, name);
53 return NULL;
54 }
55
56 FILE* fp = winpr_fopen(path, mode);
57 if (!fp)
58 {
59 char buffer[128] = { 0 };
60 (void)printf("%s: %s %s: fopen(%s, %s) failed: %s\n", __func__, type, name, path, mode,
61 winpr_strerror(errno, buffer, sizeof(buffer)));
62 }
63 free(path);
64 return fp;
65}
66
67void* test_codec_helper_read_data(const char* codec, const char* type, const char* name,
68 size_t* plength)
69{
70 WINPR_ASSERT(type);
71 WINPR_ASSERT(name);
72 WINPR_ASSERT(plength);
73
74 void* rc = NULL;
75 void* cmp = NULL;
76
77 *plength = 0;
78 FILE* fp = open_path(codec, type, name, "rb");
79 if (!fp)
80 goto fail;
81
82 if (_fseeki64(fp, 0, SEEK_END) != 0)
83 goto fail;
84
85 const size_t pos = _ftelli64(fp);
86
87 if (_fseeki64(fp, 0, SEEK_SET) != 0)
88 goto fail;
89
90 cmp = calloc(pos, 1);
91 if (!cmp)
92 goto fail;
93
94 if (fread(cmp, 1, pos, fp) != pos)
95 goto fail;
96
97 *plength = pos;
98 rc = cmp;
99 cmp = NULL;
100
101fail:
102 (void)printf("%s: [%s] %s %s -> %p\n", __func__, codec, type, name, rc);
103 free(cmp);
104 if (fp)
105 (void)fclose(fp);
106 return rc;
107}
108
109void test_codec_helper_write_data(const char* codec, const char* type, const char* name,
110 const void* data, size_t length)
111{
112 FILE* fp = open_path(codec, type, name, "wb");
113 if (!fp)
114 return;
115
116 if (fwrite(data, 1, length, fp) != length)
117 goto fail;
118
119fail:
120 fclose(fp);
121}
122
123bool test_codec_helper_compare(const char* codec, const char* type, const char* name,
124 const void* data, size_t length)
125{
126 bool rc = false;
127 size_t cmplen = 0;
128 void* cmp = test_codec_helper_read_data(codec, type, name, &cmplen);
129 if (!cmp)
130 goto fail;
131 if (cmplen != length)
132 {
133 (void)printf("%s: [%s] %s %s: length mismatch: %" PRIuz " vs %" PRIuz "\n", __func__, codec,
134 type, name, cmplen, length);
135 goto fail;
136 }
137 if (memcmp(data, cmp, length) != 0)
138 {
139 (void)printf("%s: [%s] %s %s: data mismatch\n", __func__, codec, type, name);
140 winpr_HexDump(__func__, WLOG_WARN, data, length);
141 winpr_HexDump(__func__, WLOG_WARN, cmp, cmplen);
142 goto fail;
143 }
144 rc = true;
145fail:
146 (void)printf("%s: [%s] %s %s -> %s\n", __func__, codec, type, name, rc ? "SUCCESS" : "FAILED");
147 free(cmp);
148 return rc;
149}