style: 应用 clang-format
This commit is contained in:
1 parent
016a45bc6e
commit
d1612f1d31
65 files changed
+7288
-5983
No files matched your search
+76
-60
@@ -5,69 +5,77 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int failures = 0;
|
||||
static int checks = 0;
|
||||
|
||||
#define CHECK(cond, ...) \
|
||||
do { \
|
||||
checks++; \
|
||||
if (!(cond)) { \
|
||||
failures++; \
|
||||
printf(" ✗ %s:%d: ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} \
|
||||
#define CHECK(cond, ...) \
|
||||
do { \
|
||||
checks++; \
|
||||
if (!(cond)) { \
|
||||
failures++; \
|
||||
printf(" ✗ %s:%d: ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static const char PNG[] = "\x89PNG\r\n\x1a\n rest";
|
||||
static const char PNG[] = "\x89PNG\r\n\x1a\n rest";
|
||||
static const char JPEG[] = "\xff\xd8\xff\xe0 rest";
|
||||
static const char GIF87[] = "GIF87a rest";
|
||||
static const char GIF89[] = "GIF89a rest";
|
||||
static const char WEBP[] = "RIFF\x00\x00\x00\x00WEBPVP8 ";
|
||||
|
||||
static void test_sniff(void)
|
||||
{
|
||||
static void test_sniff(void) {
|
||||
puts("按文件内容认格式 (不看扩展名)");
|
||||
const char *ext = NULL, *mime = NULL;
|
||||
|
||||
CHECK(image_sniff((const unsigned char *)PNG, 14, &ext, &mime) == 0 &&
|
||||
strcmp(ext, "png") == 0 && strcmp(mime, "image/png") == 0, "PNG");
|
||||
strcmp(ext, "png") == 0 && strcmp(mime, "image/png") == 0,
|
||||
"PNG");
|
||||
CHECK(image_sniff((const unsigned char *)JPEG, 7, &ext, &mime) == 0 &&
|
||||
strcmp(ext, "jpg") == 0 && strcmp(mime, "image/jpeg") == 0, "JPEG");
|
||||
strcmp(ext, "jpg") == 0 && strcmp(mime, "image/jpeg") == 0,
|
||||
"JPEG");
|
||||
CHECK(image_sniff((const unsigned char *)GIF87, 10, &ext, &mime) == 0 &&
|
||||
strcmp(ext, "gif") == 0, "GIF87a");
|
||||
strcmp(ext, "gif") == 0,
|
||||
"GIF87a");
|
||||
CHECK(image_sniff((const unsigned char *)GIF89, 10, &ext, &mime) == 0 &&
|
||||
strcmp(ext, "gif") == 0, "GIF89a");
|
||||
strcmp(ext, "gif") == 0,
|
||||
"GIF89a");
|
||||
CHECK(image_sniff((const unsigned char *)WEBP, 16, &ext, &mime) == 0 &&
|
||||
strcmp(ext, "webp") == 0 && strcmp(mime, "image/webp") == 0, "WebP");
|
||||
strcmp(ext, "webp") == 0 && strcmp(mime, "image/webp") == 0,
|
||||
"WebP");
|
||||
|
||||
/* 认不出来要明确失败, 别猜一个格式报上去 */
|
||||
CHECK(image_sniff((const unsigned char *)"not an image", 12, &ext, &mime) != 0,
|
||||
CHECK(image_sniff((const unsigned char *)"not an image", 12, &ext, &mime) !=
|
||||
0,
|
||||
"杂文本不该被认成图片");
|
||||
CHECK(image_sniff((const unsigned char *)"\x89PNG", 4, &ext, &mime) != 0,
|
||||
"截断的 PNG 头不该被认出来");
|
||||
CHECK(image_sniff((const unsigned char *)"RIFFxxxxNOPE", 12, &ext, &mime) != 0,
|
||||
CHECK(image_sniff((const unsigned char *)"RIFFxxxxNOPE", 12, &ext, &mime) !=
|
||||
0,
|
||||
"RIFF 但不是 WEBP 不该被认成 WebP");
|
||||
CHECK(image_sniff(NULL, 0, &ext, &mime) != 0, "NULL 不该崩");
|
||||
}
|
||||
|
||||
static void test_backup(void)
|
||||
{
|
||||
static void test_backup(void) {
|
||||
puts("本地备份: 命名、内容、不重复");
|
||||
char tmpl[] = "/tmp/ligent_img_XXXXXX";
|
||||
char *dir = mkdtemp(tmpl);
|
||||
CHECK(dir != NULL, "建临时目录失败");
|
||||
if (!dir) return;
|
||||
if (!dir)
|
||||
return;
|
||||
|
||||
const unsigned char data[] = "\x89PNG\r\n\x1a\n payload";
|
||||
size_t n = sizeof data - 1;
|
||||
|
||||
char *b1 = image_backup_save(dir, data, n, "png");
|
||||
CHECK(b1 != NULL, "备份应成功");
|
||||
if (!b1) { return; }
|
||||
if (!b1) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* 名字形如 <上传时间戳>_<4位随机>.<ext> */
|
||||
CHECK(strlen(b1) > 5, "名字不该太短: %s", b1);
|
||||
@@ -79,34 +87,39 @@ static void test_backup(void)
|
||||
char *p1 = image_backup_path(dir, b1);
|
||||
size_t len = 0;
|
||||
char *back = read_entire_file(p1, &len);
|
||||
CHECK(back && len == n && memcmp(back, data, n) == 0, "备份内容应逐字节一致");
|
||||
free(back); free(p1);
|
||||
CHECK(back && len == n && memcmp(back, data, n) == 0,
|
||||
"备份内容应逐字节一致");
|
||||
free(back);
|
||||
free(p1);
|
||||
|
||||
/* 再存一次应该拿到不同的名字 (随机后缀), 而不是互相覆盖 */
|
||||
char *b2 = image_backup_save(dir, data, n, "png");
|
||||
CHECK(b2 && strcmp(b1, b2) != 0, "两次备份不该同号: %s / %s",
|
||||
b1, b2 ? b2 : "(null)");
|
||||
CHECK(b2 && strcmp(b1, b2) != 0, "两次备份不该同号: %s / %s", b1,
|
||||
b2 ? b2 : "(null)");
|
||||
free(b2);
|
||||
|
||||
free(b1);
|
||||
/* 清场 */
|
||||
char *cmd = xasprintf("rm -rf %s", dir);
|
||||
if (system(cmd) != 0) { /* 清理失败不影响断言 */ }
|
||||
if (system(cmd) != 0) { /* 清理失败不影响断言 */
|
||||
}
|
||||
free(cmd);
|
||||
}
|
||||
|
||||
static void test_base64(void)
|
||||
{
|
||||
static void test_base64(void) {
|
||||
puts("base64: 长度、补位、往返");
|
||||
/* 用几组长度覆盖三种补位情况: 3n、3n+1、3n+2 */
|
||||
struct { const char *in; const char *want; } cases[] = {
|
||||
{ "", "" },
|
||||
{ "f", "Zg==" },
|
||||
{ "fo", "Zm8=" },
|
||||
{ "foo", "Zm9v" },
|
||||
{ "foob", "Zm9vYg==" },
|
||||
{ "fooba", "Zm9vYmE=" },
|
||||
{ "foobar", "Zm9vYmFy" },
|
||||
struct {
|
||||
const char *in;
|
||||
const char *want;
|
||||
} cases[] = {
|
||||
{"", ""},
|
||||
{"f", "Zg=="},
|
||||
{"fo", "Zm8="},
|
||||
{"foo", "Zm9v"},
|
||||
{"foob", "Zm9vYg=="},
|
||||
{"fooba", "Zm9vYmE="},
|
||||
{"foobar", "Zm9vYmFy"},
|
||||
};
|
||||
for (size_t i = 0; i < sizeof cases / sizeof cases[0]; i++) {
|
||||
size_t n = strlen(cases[i].in);
|
||||
@@ -119,7 +132,7 @@ static void test_base64(void)
|
||||
free(got);
|
||||
}
|
||||
/* 二进制字节 (0x00 / 0xFF 这类不能被当字符串处理) */
|
||||
const unsigned char bin[] = { 0x00, 0xFF, 0x10, 0x80 };
|
||||
const unsigned char bin[] = {0x00, 0xFF, 0x10, 0x80};
|
||||
char *b = base64_encode(bin, sizeof bin);
|
||||
CHECK(b && strcmp(b, "AP8QgA==") == 0, "二进制应正确编码, 实际 %s",
|
||||
b ? b : "(null)");
|
||||
@@ -127,21 +140,21 @@ static void test_base64(void)
|
||||
}
|
||||
|
||||
/* 造一张内容合法 (靠文件头就能嗅探出来)的假图, 返回字节数 */
|
||||
static size_t fake_png(unsigned char *out, size_t n)
|
||||
{
|
||||
static size_t fake_png(unsigned char *out, size_t n) {
|
||||
static const char magic[] = "\x89PNG\r\n\x1a\n";
|
||||
size_t m = sizeof magic - 1;
|
||||
if (n < m) return 0;
|
||||
if (n < m)
|
||||
return 0;
|
||||
memcpy(out, magic, m);
|
||||
for (size_t i = m; i < n; i++) out[i] = (unsigned char)(i & 0xFF);
|
||||
for (size_t i = m; i < n; i++)
|
||||
out[i] = (unsigned char)(i & 0xFF);
|
||||
return n;
|
||||
}
|
||||
|
||||
/* 把一段备份引用塞进一条 user 消息, 模拟 read_image 之后的历史 */
|
||||
static const char *ref_message_type = "ligent_image";
|
||||
|
||||
static JsonValue *history_with_ref(const char *backup)
|
||||
{
|
||||
static JsonValue *history_with_ref(const char *backup) {
|
||||
JsonValue *msgs = json_new_array();
|
||||
|
||||
JsonValue *sys = json_new_object();
|
||||
@@ -162,20 +175,23 @@ static JsonValue *history_with_ref(const char *backup)
|
||||
return msgs;
|
||||
}
|
||||
|
||||
static void test_expand(void)
|
||||
{
|
||||
static void test_expand(void) {
|
||||
puts("请求前展开: 引用 -> image_url + base64");
|
||||
char tmpl[] = "/tmp/ligent_imgx_XXXXXX";
|
||||
char *dir = mkdtemp(tmpl);
|
||||
CHECK(dir != NULL, "建临时目录失败");
|
||||
if (!dir) return;
|
||||
if (!dir)
|
||||
return;
|
||||
image_set_bindir(dir);
|
||||
|
||||
unsigned char png[256];
|
||||
size_t n = fake_png(png, sizeof png);
|
||||
char *backup = image_backup_save(dir, png, n, "png");
|
||||
CHECK(backup != NULL, "备份应成功");
|
||||
if (!backup) { image_set_bindir("."); return; }
|
||||
if (!backup) {
|
||||
image_set_bindir(".");
|
||||
return;
|
||||
}
|
||||
|
||||
/* --- 正常展开 --- */
|
||||
JsonValue *msgs = history_with_ref(backup);
|
||||
@@ -191,14 +207,14 @@ static void test_expand(void)
|
||||
"附件消息应有一个 content 块");
|
||||
JsonValue *wp = json_array_get(wc, 0);
|
||||
CHECK(strcmp(json_get_string(wp, "type"), "image_url") == 0,
|
||||
"发送形态的类型应是 image_url, 实际 %s",
|
||||
json_get_string(wp, "type"));
|
||||
"发送形态的类型应是 image_url, 实际 %s", json_get_string(wp, "type"));
|
||||
JsonValue *iu = json_object_get(wp, "image_url");
|
||||
const char *url = iu ? json_get_string(iu, "url") : NULL;
|
||||
CHECK(url && strncmp(url, "data:image/png;base64,", 22) == 0,
|
||||
"应是 data: URL, 实际 %.40s", url ? url : "(null)");
|
||||
|
||||
/* base64 段应能解回原字节 —— 这里只验长度与首尾, 避免在测试里再写一个解码器 */
|
||||
/* base64 段应能解回原字节 —— 这里只验长度与首尾, 避免在测试里再写一个解码器
|
||||
*/
|
||||
CHECK(url && strlen(url) == 22 + base64_encoded_len(n),
|
||||
"data URL 长度应等于前缀 + base64 长度");
|
||||
|
||||
@@ -274,14 +290,14 @@ static void test_expand(void)
|
||||
free(backup);
|
||||
image_set_bindir(".");
|
||||
char *cmd = xasprintf("rm -rf %s", tmpl);
|
||||
if (system(cmd) != 0) { }
|
||||
if (system(cmd) != 0) {
|
||||
}
|
||||
free(cmd);
|
||||
}
|
||||
|
||||
static void test_pending(void)
|
||||
{
|
||||
static void test_pending(void) {
|
||||
puts("待发附件: 取走即清空");
|
||||
image_pending_take(); /* 先清干净 */
|
||||
image_pending_take(); /* 先清干净 */
|
||||
|
||||
image_pending_add("1700000000_aaaa.png");
|
||||
image_pending_add("1700000001_bbbb.png");
|
||||
@@ -296,7 +312,8 @@ static void test_pending(void)
|
||||
CHECK(json_get_string(p0, "file_id") == NULL,
|
||||
"不该再有 file_id 这种协议字段");
|
||||
CHECK(strcmp(json_get_string(json_array_get(arr, 1), "backup"),
|
||||
"1700000001_bbbb.png") == 0, "第二个备份名");
|
||||
"1700000001_bbbb.png") == 0,
|
||||
"第二个备份名");
|
||||
}
|
||||
json_free(arr);
|
||||
|
||||
@@ -306,8 +323,7 @@ static void test_pending(void)
|
||||
CHECK(image_pending_take() == NULL, "空备份名不该被记下");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int main(void) {
|
||||
test_sniff();
|
||||
test_backup();
|
||||
test_base64();
|
||||
|
||||
Reference in new issue
Block a user