skip to content
==> tests/snapshot.c <==
1/* status.rs's layout test. */
2#define _XOPEN_SOURCE 700 /* sexc compiles tests without the package's cflags (reported) */
3#include "mesh/abi.h"
4#include "mesh/check.h"
5#include "mesh/manifest.h"
6#include "mesh/manifest_encode.h"
7#include "mesh/prog_table.h"
8#include "mesh/snapshot.h"
9
10#include <stdlib.h>
11#include <string.h>
12
13static mesh_buf s;
14static uint32_t u(size_t o) { return (uint32_t)(uint8_t)s.data[o] | (uint32_t)(uint8_t)s.data[o + 1] << 8 | (uint32_t)(uint8_t)s.data[o + 2] << 16 | (uint32_t)(uint8_t)s.data[o + 3] << 24; }
15
16int main(void) {
17    mesh_mailbox mb = mesh_mailbox_test();
18    mesh_mailbox_set(mb, MESH_H_MAGIC, MESH_MAGIC);
19    mesh_mailbox_set(mb, MESH_H_EPOCH, 7);
20    mesh_manifest m;
21    char *err = NULL;
22    const char *text = "name = \"hello\"\nabi = 6\nview_bytes = 0x1000\nsnap_bytes = 6\nnodes = 2";
23    CHECK(mesh_manifest_parse(text, strlen(text), &m, &err));
24    mesh_image im;
25    memset(&im, 0, sizeof im);
26    im.view_off = 0x100000;
27    im.loads = 1;
28    uint8_t entry[MESH_PT_BYTES], ext[MESH_PX_BYTES];
29    mesh_manifest_encode_entry(&m, &im, entry);
30    mesh_manifest_encode_ext(&m, ext);
31    mesh_pt_write_entry(mb, 4, entry, ext, MESH_ST_RUNNING);
32    mesh_mailbox_set(mb, 0x100000, 0x4f4c4548u);
33    mesh_mailbox_set(mb, 0x100004, 0xffffffffu);
34    mesh_mailbox_set(mb, mesh_prog_ctl(4) + MESH_P_DONE, 99);
35    mesh_mailbox_set(mb, mesh_node_rec(4, 1) + MESH_N_HEARTBEAT, 5);
36    static mesh_host_block h;
37    mesh_host_block_init(&h);
38    mesh_host_block_set_u32(&h, MESH_S_EPOCH, 7);
39    mesh_snapshot(&s, mb, &h);
40    CHECK(u(MESH_SN_TOTAL) == s.len && u(MESH_SN_COUNT) == 1 && u(MESH_SN_VERSION) == MESH_SNAP_VERSION);
41    CHECK(u(MESH_SN_HEAD_BYTES + MESH_H_MAGIC) == MESH_MAGIC && u(MESH_SN_HEAD_BYTES + MESH_H_EPOCH) == 7);
42    size_t sec = MESH_SN_HEAD_BYTES + MESH_H_BYTES;
43    CHECK(u(sec + MESH_SC_SLOT) == 4 && u(sec + MESH_SC_BYTES) == mesh_section_bytes(2, 6) && u(sec + MESH_SC_NODES) == 2 &&
44          u(sec + MESH_SC_VIEW) == 6);
45    CHECK(memcmp(s.data + sec + MESH_SC_ENTRY + MESH_PT_NAME, "hello\0", 6) == 0);
46    CHECK_U(u(sec + MESH_SC_ENTRY + MESH_PT_STATE), MESH_ST_RUNNING);
47    CHECK_U(u(sec + MESH_SC_CTL + MESH_P_DONE), 99);
48    CHECK_U(u(sec + MESH_SC_NODE_RECS + MESH_NODE_STRIDE + MESH_N_HEARTBEAT), 5);
49    CHECK_U(u(sec + MESH_SC_NODE_RECS + 2 * MESH_NODE_STRIDE), 0x4f4c4548u);
50    CHECK_U(u(sec + MESH_SC_NODE_RECS + 2 * MESH_NODE_STRIDE + 4), 0xffff);
51    size_t host_at = u(MESH_SN_HOST_AT);
52    CHECK(host_at == sec + mesh_section_bytes(2, 6) && s.len - host_at == MESH_SNAP_HOST);
53    CHECK_U(u(host_at + MESH_S_EPOCH), 7);
54    mesh_manifest_free(&m);
55    mesh_buf_free(&s);
56    free(mb.base);
57    return mesh_check_done();
58}