Files
story/docs/device-protocol-c/test_protocol.c
T
Codex Local bf1233d5df
ci / verify (push) Canceled after 0s
feat: expand companion platform, device controls, and memoir workflows
Add customer and operator login, BLE provisioning, device fleet controls, call memory, memoir generation, and refreshed family and maintenance portals. Include API contracts, migrations, firmware sources, tests, assets, and deployment documentation.
2026-10-08 10:06:44 +08:00

73 lines
3.5 KiB
C

#include "vl_protocol.h"
#include "vectors.generated.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
static int test_rng(void *context, uint8_t *out, size_t n) {
(void)context;
for (size_t i = 0; i < n; i++) out[i] = (uint8_t)i;
return 0; /* Fixed bytes are ONLY for this offline test. */
}
static int failed_rng(void *context, uint8_t *out, size_t n) {
(void)context; (void)out; (void)n; return -1;
}
static int read_abc(void *context, uint8_t *out, size_t cap, size_t *n) {
size_t *offset = context;
if (!cap) return -1;
*n = *offset < 3 ? 1 : 0;
if (*n) out[0] = (uint8_t)"abc"[(*offset)++];
return 0;
}
static int read_error(void *context, uint8_t *out, size_t cap, size_t *n) {
(void)context; (void)out; (void)cap; (void)n; return -1;
}
int main(void) {
uint8_t key[32], decoded[4096], frame[2050];
char hash[65], signature[65], nonce[23], uuid[37];
size_t n;
assert(vl_key_from_hex(TEST_KEY, key) == 0);
assert(vl_key_from_hex("short", key) != 0);
assert(vl_key_from_hex(NULL, key) != 0);
assert(vl_sha256_hex("{}", 2, hash) == 0 && !strcmp(hash, BODY_SHA));
assert(vl_sha256_hex(NULL, 0, hash) == 0 && !strcmp(hash, EMPTY_SHA));
assert(vl_http_sign(key, "POST", "/api/v1/device/ble-state", "KP01-020000260914",
"1", "1789372800000", "AAECAwQFBgcICQoLDA0ODw", "12345678-1234-4234-8234-123456789abc",
"{}", 2, signature) == 0 && !strcmp(signature, HTTP_SIGNATURE));
assert(vl_http_sign(key, "POST", "/api/v1/device/ble-state", "KP01-020000260914",
"1", "1789372800000", "AAECAwQFBgcICQoLDA0ODw", "12345678-1234-4234-8234-123456789abc",
"{ }", 3, signature) == 0 && strcmp(signature, HTTP_SIGNATURE));
assert(vl_http_sign(key, "POST", "/bad\npath", "device", "1", "1", "nonce", "id",
"{}", 2, signature) != 0);
assert(vl_nonce(test_rng, NULL, nonce) == 0 && !strcmp(nonce, "AAECAwQFBgcICQoLDA0ODw"));
assert(vl_uuid4(test_rng, NULL, uuid) == 0 && !strcmp(uuid, "00010203-0405-4607-8809-0a0b0c0d0e0f"));
assert(vl_nonce(failed_rng, NULL, nonce) != 0 && nonce[0] == 0);
assert(vl_uuid4(failed_rng, NULL, uuid) != 0 && uuid[0] == 0);
const char *json = "{\"cmd\":\"get_info\"}";
assert(vl_ble_pack(json, strlen(json), frame, sizeof(frame), &n) == 0);
assert(n == 20 && frame[0] == 18 && frame[1] == 0 && !memcmp(frame + 2, json, 18));
assert(vl_ble_pack(json, strlen(json), frame, 19, &n) != 0 && n == 0);
memset(decoded, 'a', 2049);
assert(vl_ble_pack(decoded, 2048, frame, sizeof(frame), &n) == 0 && n == 2050);
assert(frame[0] == 0 && frame[1] == 8);
assert(vl_ble_pack(decoded, 2049, frame, sizeof(frame), &n) != 0);
assert(vl_ble_pack("\xe4\xbd\xa0", 3, frame, sizeof(frame), &n) == 0 && frame[0] == 3);
mbedtls_md_context_t sha;
assert(vl_sha256_begin(&sha) == 0);
assert(vl_sha256_update(&sha, "a", 1) == 0);
assert(vl_sha256_update(&sha, "bc", 2) == 0);
assert(vl_sha256_finish(&sha, hash) == 0 && !strcmp(hash, ABC_SHA));
mbedtls_md_free(&sha);
size_t offset = 0;
assert(vl_ota_verify(read_abc, &offset, 3, ABC_SHA) == 0);
offset = 0;
assert(vl_ota_verify(read_abc, &offset, 2, ABC_SHA) != 0);
offset = 0;
assert(vl_ota_verify(read_abc, &offset, 4, ABC_SHA) != 0);
offset = 0;
assert(vl_ota_verify(read_abc, &offset, 3, EMPTY_SHA) != 0);
assert(vl_ota_verify(read_error, NULL, 3, ABC_SHA) != 0);
puts("PASS: HTTP signature, SHA256/OTA streaming, UUID/nonce, BLE framing and error cases");
return 0;
}