binscout 0.9.0

Headless, agent-first binary analysis: functions, CFGs, xrefs, strings, and IR over a JSON CLI or MCP server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! bytes/hexdump command: typed views, VA<->file offset, error envelopes.

mod common;

use std::io::Write;
use std::process::{Command, Stdio};

use serde_json::{Value, json};

use binscout::{BytesAs, BytesEndian, Project};
use common::{binscout_bin, cleanup, mcp_bin, temp_db, tmpdir, write_bin, write_fixture};

fn put16(v: &mut [u8], off: usize, x: u16) {
    v[off..off + 2].copy_from_slice(&x.to_le_bytes());
}

fn put32(v: &mut [u8], off: usize, x: u32) {
    v[off..off + 4].copy_from_slice(&x.to_le_bytes());
}

fn put64(v: &mut [u8], off: usize, x: u64) {
    v[off..off + 8].copy_from_slice(&x.to_le_bytes());
}

/// Minimal 64-bit LE ELF with a BSS-like tail: one `PT_LOAD` at `BASE`
/// with `p_filesz=3` but `p_memsz=16`.
fn bss_elf(base: u64) -> Vec<u8> {
    let mut f = vec![0u8; 0x78];
    f[0..4].copy_from_slice(&[0x7F, b'E', b'L', b'F']);
    f[4] = 2;
    f[5] = 1;
    f[6] = 1;
    put16(&mut f, 16, 2);
    put16(&mut f, 18, 62);
    put32(&mut f, 20, 1);
    put64(&mut f, 24, base);
    put64(&mut f, 32, 64);
    put16(&mut f, 52, 64);
    put16(&mut f, 54, 56);
    put16(&mut f, 56, 1);
    let p = 64;
    put32(&mut f, p, 1);
    put32(&mut f, p + 4, 5);
    put64(&mut f, p + 8, 0x78);
    put64(&mut f, p + 16, base);
    put64(&mut f, p + 32, 3);
    put64(&mut f, p + 40, 16);
    put64(&mut f, p + 48, 0x1000);
    f.extend_from_slice(&[0x90, 0x90, 0xC3]);
    f
}

fn open_raw(dir: &std::path::Path, tag: &str, bytes: &[u8]) -> Project {
    let bin = write_bin(dir, &format!("{tag}.bin"), bytes);
    let db = temp_db(dir, tag);
    Project::open_with_db(&bin, &db).expect("raw blob must open")
}

#[test]
fn bytes_view_u8_shape_and_provenance() {
    let dir = tmpdir("bytes-u8");
    let p = open_raw(&dir, "r", &[0x90, 0x90, 0xC3]);
    let v = p
        .bytes_view(0, 3, BytesAs::U8, BytesEndian::Le)
        .expect("range must read");
    assert_eq!(v.addr, 0);
    assert_eq!(v.bytes, vec![0x90, 0x90, 0xC3]);
    assert_eq!(v.bytes_hex(), "9090c3");
    assert_eq!(v.file_off, Some(0));
    assert_eq!(v.section, None);
    assert_eq!(
        v.values,
        vec![
            Value::from(0x90u64),
            Value::from(0x90u64),
            Value::from(0xC3u64)
        ]
    );
    let json = serde_json::to_value(&v).expect("view serializes");
    assert_eq!(json["addr"], 0);
    assert_eq!(json["addr_hex"], "0x0");
    assert_eq!(json["file_off"], 0);
    assert!(json["section"].is_null());
    assert_eq!(json["len"], 3);
    assert_eq!(json["bytes_hex"], "9090c3");
    assert_eq!(json["values"], json!([144, 144, 195]));
    cleanup(&dir);
}

#[test]
fn bytes_view_multibyte_endian_and_truncation() {
    let dir = tmpdir("bytes-multi");
    let bytes = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
    let p = open_raw(&dir, "r", &bytes);
    let le = p
        .bytes_view(0, 8, BytesAs::U16, BytesEndian::Le)
        .expect("u16 le");
    assert_eq!(
        Value::Array(le.values.clone()),
        json!([0x0201, 0x0403, 0x0605, 0x0807])
    );
    let be = p
        .bytes_view(0, 8, BytesAs::U16, BytesEndian::Be)
        .expect("u16 be");
    assert_eq!(
        Value::Array(be.values.clone()),
        json!([0x0102, 0x0304, 0x0506, 0x0708])
    );
    let w32 = p
        .bytes_view(0, 8, BytesAs::U32, BytesEndian::Le)
        .expect("u32 le");
    assert_eq!(
        Value::Array(w32.values.clone()),
        json!([0x04030201u64, 0x08070605u64])
    );
    let w64 = p
        .bytes_view(0, 8, BytesAs::U64, BytesEndian::Le)
        .expect("u64 le");
    assert_eq!(
        Value::Array(w64.values.clone()),
        json!([0x0807060504030201u64])
    );
    let ptr = p
        .bytes_view(0, 8, BytesAs::Ptr, BytesEndian::Le)
        .expect("ptr");
    assert_eq!(ptr.values, w64.values, "x86_64 pointers are 8 bytes");
    // Trailing partial units are dropped, never an error.
    let odd = p
        .bytes_view(0, 3, BytesAs::U16, BytesEndian::Le)
        .expect("odd u16");
    assert_eq!(Value::Array(odd.values.clone()), json!([0x0201]));
    assert_eq!(odd.bytes_hex(), "010203");
    cleanup(&dir);
}

#[test]
fn bytes_view_ptr_width_follows_arch() {
    let dir = tmpdir("bytes-ptr32");
    let bytes = [0x01, 0x02, 0x03, 0x04, 0x78, 0x56, 0x34, 0x12];
    let bin = write_bin(&dir, "r32.bin", &bytes);
    let db = temp_db(&dir, "r32");
    let p = Project::open_raw(
        &bin,
        &db,
        binscout::loader::RawOptions {
            base: 0x1000,
            arch: binscout::disasm::Arch::X86_32,
            entry: None,
        },
    )
    .expect("raw x86_32 must open");
    let v = p
        .bytes_view(0x1000, 8, BytesAs::Ptr, BytesEndian::Le)
        .expect("ptr");
    assert_eq!(
        Value::Array(v.values.clone()),
        json!([0x04030201u64, 0x12345678u64])
    );
    assert_eq!(v.file_off, Some(0));
    cleanup(&dir);
}

#[test]
fn bytes_view_cstr_and_utf16_strings() {
    let dir = tmpdir("bytes-str");
    let p = open_raw(&dir, "r", b"Hi\0trailing");
    let v = p
        .bytes_view(0, 11, BytesAs::CStr, BytesEndian::Le)
        .expect("cstr");
    assert_eq!(Value::Array(v.values.clone()), json!(["Hi"]));
    // No NUL in range: the whole range decodes.
    let v = p
        .bytes_view(3, 8, BytesAs::CStr, BytesEndian::Le)
        .expect("cstr");
    assert_eq!(Value::Array(v.values.clone()), json!(["trailing"]));
    // Lossy on invalid UTF-8.
    let q = open_raw(&dir, "q", &[0x41, 0xFF, 0x00]);
    let v = q
        .bytes_view(0, 3, BytesAs::CStr, BytesEndian::Le)
        .expect("cstr");
    assert_eq!(Value::Array(v.values.clone()), json!(["A\u{fffd}"]));

    let u = open_raw(&dir, "u", &[0x48, 0x00, 0x69, 0x00, 0x00, 0x00, 0x41, 0x00]);
    let v = u
        .bytes_view(0, 8, BytesAs::Utf16, BytesEndian::Le)
        .expect("utf16 le");
    assert_eq!(Value::Array(v.values.clone()), json!(["Hi"]));
    let b = open_raw(&dir, "b", &[0x00, 0x48, 0x00, 0x69, 0x00, 0x00]);
    let v = b
        .bytes_view(0, 6, BytesAs::Utf16, BytesEndian::Be)
        .expect("utf16 be");
    assert_eq!(Value::Array(v.values.clone()), json!(["Hi"]));
    cleanup(&dir);
}

#[test]
fn parse_bytes_as_and_endian() {
    use binscout::{parse_bytes_as, parse_bytes_endian};
    for (s, want) in [
        ("u8", BytesAs::U8),
        ("U16", BytesAs::U16),
        ("u32", BytesAs::U32),
        ("u64", BytesAs::U64),
        ("ptr", BytesAs::Ptr),
        ("CSTR", BytesAs::CStr),
        ("utf16", BytesAs::Utf16),
    ] {
        assert_eq!(parse_bytes_as(s).expect("known view"), want);
    }
    assert!(parse_bytes_as("u128").is_err());
    assert!(parse_bytes_as("").is_err());
    assert_eq!(parse_bytes_endian("LE").expect("le"), BytesEndian::Le);
    assert_eq!(parse_bytes_endian("be").expect("be"), BytesEndian::Be);
    assert!(parse_bytes_endian("mid").is_err());
}

#[test]
fn file_off_and_va_round_trip_on_pe() {
    use common::builders::{BASIC_STRING_RDATA_OFF, BASIC_STRING_VA, TEXT_VA};
    let dir = tmpdir("bytes-off");
    let p = common::open_test_project(&dir, "pe32-basic.exe");
    assert_eq!(p.file_off_of(TEXT_VA), Some(0x200));
    assert_eq!(p.va_of_file_off(0x200), Some(TEXT_VA));
    assert_eq!(p.section_of(TEXT_VA), Some(".text"));
    let rdata_off = 0x400 + BASIC_STRING_RDATA_OFF as u64;
    assert_eq!(p.file_off_of(BASIC_STRING_VA), Some(rdata_off));
    assert_eq!(p.va_of_file_off(rdata_off), Some(BASIC_STRING_VA));
    assert_eq!(p.section_of(BASIC_STRING_VA), Some(".rdata"));
    // Unmapped VA and past-end offsets have no provenance.
    assert_eq!(p.file_off_of(0xdead_beef), None);
    assert_eq!(p.section_of(0xdead_beef), None);
    assert_eq!(p.va_of_file_off(u64::MAX), None);
    // Round trip both ways through the view.
    let v = p
        .bytes_view(TEXT_VA, 3, BytesAs::U8, BytesEndian::Le)
        .expect("text view");
    assert_eq!(v.file_off, Some(0x200));
    assert_eq!(v.section.as_deref(), Some(".text"));
    assert_eq!(p.va_of_file_off(v.file_off.expect("backed")), Some(TEXT_VA));
    cleanup(&dir);
}

#[test]
fn bss_tail_reads_zeros_with_null_provenance() {
    const BASE: u64 = 0x400000;
    let dir = tmpdir("bytes-bss");
    let bin = write_bin(&dir, "bss.elf", &bss_elf(BASE));
    let db = temp_db(&dir, "bss");
    let p = Project::open_with_db(&bin, &db).expect("bss ELF must open");
    // Wholly zero-filled tail: zeros with null file_off/section.
    let v = p
        .bytes_view(BASE + 8, 4, BytesAs::U8, BytesEndian::Le)
        .expect("tail must read");
    assert_eq!(v.bytes, vec![0u8; 4]);
    assert_eq!(Value::Array(v.values.clone()), json!([0, 0, 0, 0]));
    assert_eq!(v.file_off, None);
    assert_eq!(v.section, None);
    assert_eq!(p.file_off_of(BASE + 8), None);
    assert_eq!(p.section_of(BASE + 8), None);
    // Mixed file/zero range: starts backed, tails zero.
    let v = p
        .bytes_view(BASE + 1, 5, BytesAs::U8, BytesEndian::Le)
        .expect("mixed must read");
    assert_eq!(v.bytes, vec![0x90, 0xC3, 0x00, 0x00, 0x00]);
    assert_eq!(p.file_off_of(BASE + 1), p.file_off_of(BASE).map(|o| o + 1));
    cleanup(&dir);
}

/// Run the CLI; stdout must be exactly one JSON value.
fn run(args: &[&str]) -> (bool, Value, String) {
    let out = Command::new(binscout_bin())
        .args(args)
        .output()
        .expect("spawn binscout");
    let text = String::from_utf8(out.stdout).expect("stdout is utf-8");
    let value: Value = serde_json::from_str(text.trim_end())
        .unwrap_or_else(|_| panic!("stdout must be one JSON value, got: {text:?}"));
    (out.status.success(), value, text)
}

#[test]
fn cli_bytes_json_shape_and_no_sidecar() {
    let dir = tmpdir("cli-bytes");
    let before = std::fs::read("tests/fixtures/raw-x64.bin").expect("fixture");
    let bin = write_bin(&dir, "a.bin", &before);
    let bin_s = bin.display().to_string();
    let db = temp_db(&dir, "a").display().to_string();
    let d = db.as_str();

    let (ok, v, text) = run(&["--db", d, "bytes", &bin_s, "0x0", "--len", "3"]);
    assert!(ok, "{v}");
    assert_eq!(text.trim_end().lines().count(), 1);
    assert_eq!(v["addr"], 0);
    assert_eq!(v["addr_hex"], "0x0");
    assert_eq!(v["file_off"], 0);
    assert!(v["section"].is_null(), "{v}");
    assert_eq!(v["len"], 3);
    assert_eq!(v["bytes_hex"], "9090c3");
    assert_eq!(v["values"], json!([144, 144, 195]));

    // Typed views and decimal addresses agree with hex.
    let (ok, v, _) = run(&[
        "--db", d, "bytes", &bin_s, "0", "--len", "3", "--as", "u16", "--endian", "be",
    ]);
    assert!(ok, "{v}");
    assert_eq!(v["values"], json!([0x9090]));

    // PE string decodes as one cstr row with section provenance.
    let pe = write_fixture(&dir, "pe32-basic.exe");
    let pe_s = pe.display().to_string();
    let hex = format!("{:#x}", common::builders::BASIC_STRING_VA);
    let (ok, v, _) = run(&[
        "--db", d, "bytes", &pe_s, &hex, "--len", "64", "--as", "cstr",
    ]);
    assert!(ok, "{v}");
    assert_eq!(v["section"], ".rdata");
    assert!(v["file_off"].is_number(), "{v}");
    assert_eq!(v["values"], json!([common::builders::BASIC_STRING]));

    assert!(
        !std::path::Path::new(d).exists(),
        "bytes reads never create the sidecar"
    );
    assert_eq!(
        std::fs::read(&bin).expect("reread"),
        before,
        "binary untouched"
    );
    cleanup(&dir);
}

#[test]
fn cli_bytes_errors_are_json_envelopes() {
    let dir = tmpdir("cli-bytes-err");
    let bin = write_bin(&dir, "a.bin", &[0x90, 0x90, 0xC3]);
    let bin_s = bin.display().to_string();
    let db = temp_db(&dir, "a").display().to_string();
    let d = db.as_str();

    // Unmapped range.
    let (ok, v, text) = run(&["--db", d, "bytes", &bin_s, "0xdead", "--len", "3"]);
    assert!(!ok && v.get("error").is_some(), "{v}");
    assert_eq!(text.trim_end().lines().count(), 1);
    // Zero and over-cap lengths (1 MiB cap, like disasm/ir).
    let (ok, v, _) = run(&["--db", d, "bytes", &bin_s, "0x0", "--len", "0"]);
    assert!(!ok && v.get("error").is_some(), "{v}");
    let (ok, v, _) = run(&["--db", d, "bytes", &bin_s, "0x0", "--len", "1048577"]);
    assert!(!ok && v.get("error").is_some(), "over-cap: {v}");
    // Bad view, endian, and address strings.
    for args in [
        vec![
            "--db", d, "bytes", &bin_s, "0x0", "--len", "3", "--as", "u128",
        ],
        vec![
            "--db", d, "bytes", &bin_s, "0x0", "--len", "3", "--endian", "mid",
        ],
        vec!["--db", d, "bytes", &bin_s, "zz", "--len", "3"],
    ] {
        let (ok, v, text) = run(&args);
        assert!(!ok && v.get("error").is_some(), "{args:?}: {v}");
        assert_eq!(text.trim_end().lines().count(), 1);
    }
    assert!(
        !std::path::Path::new(d).exists(),
        "failed bytes reads never create the sidecar"
    );
    cleanup(&dir);
}

#[test]
fn cli_bytes_bss_tail_is_zeros_with_null_file_off() {
    const BASE: u64 = 0x400000;
    let dir = tmpdir("cli-bytes-bss");
    let bin = write_bin(&dir, "bss.elf", &bss_elf(BASE));
    let before = std::fs::read(&bin).expect("read back");
    let bin_s = bin.display().to_string();
    let db = temp_db(&dir, "bss").display().to_string();
    let d = db.as_str();
    let tail = format!("{:#x}", BASE + 8);

    let (ok, v, text) = run(&["--db", d, "bytes", &bin_s, &tail, "--len", "4"]);
    assert!(ok, "{v}");
    assert_eq!(text.trim_end().lines().count(), 1);
    assert_eq!(v["bytes_hex"], "00000000");
    assert_eq!(v["values"], json!([0, 0, 0, 0]));
    assert!(v["file_off"].is_null(), "{v}");
    assert!(v["section"].is_null(), "{v}");

    assert_eq!(
        std::fs::read(&bin).expect("reread"),
        before,
        "binary untouched"
    );
    assert!(!std::path::Path::new(d).exists(), "no sidecar written");
    cleanup(&dir);
}

/// One MCP request line plus notification; response lines in order.
fn mcp_session(lines: &[String]) -> Vec<Value> {
    let mut child = Command::new(mcp_bin())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
        .expect("spawn binscout-mcp");
    {
        let stdin = child.stdin.as_mut().expect("piped stdin");
        for line in lines {
            writeln!(stdin, "{line}").expect("write request");
        }
    }
    let out = child.wait_with_output().expect("server exits on stdin EOF");
    assert!(out.status.success(), "server exit: {}", out.status);
    let text = String::from_utf8(out.stdout).expect("stdout utf-8");
    text.lines()
        .filter(|l| !l.trim().is_empty())
        .map(|l| {
            serde_json::from_str(l).unwrap_or_else(|_| panic!("response line must be JSON: {l:?}"))
        })
        .collect()
}

fn mcp_req(id: u64, method: &str, params: Value) -> String {
    serde_json::to_string(&json!({"jsonrpc": "2.0", "id": id, "method": method, "params": params}))
        .expect("request serializes")
}

fn mcp_call(id: u64, name: &str, args: Value) -> String {
    mcp_req(id, "tools/call", json!({"name": name, "arguments": args}))
}

/// One `bytes`-capable round trip: returns the tool result text parsed as JSON.
fn bytes_text(args: Value) -> Value {
    let lines = vec![
        mcp_req(
            1,
            "initialize",
            json!({"protocolVersion": "2025-11-25", "capabilities": {}, "clientInfo": {"name": "t", "version": "0"}}),
        ),
        serde_json::to_string(&json!({"jsonrpc": "2.0", "method": "notifications/initialized"}))
            .expect("n"),
        mcp_call(10, "bytes", args),
    ];
    let resps = mcp_session(&lines);
    assert_eq!(resps.len(), 2, "{resps:?}");
    let by_id: std::collections::HashMap<u64, &Value> = resps
        .iter()
        .map(|r| (r["id"].as_u64().expect("numeric id"), r))
        .collect();
    let result = by_id[&10]
        .get("result")
        .unwrap_or_else(|| panic!("bytes must return result: {:?}", by_id[&10]));
    assert_ne!(
        result.get("isError"),
        Some(&Value::Bool(true)),
        "tool error: {result}"
    );
    let text = result["content"][0]["text"].as_str().expect("text content");
    serde_json::from_str(text).expect("tool json")
}

#[test]
fn mcp_bytes_typed_views_and_defaults() {
    let dir = tmpdir("mcp-bytes");
    let bin = write_bin(&dir, "m.bin", &[0x01, 0x02, 0x03, 0x04]);
    let bin_s = bin.display().to_string();
    let db = temp_db(&dir, "m").display().to_string();

    // Defaults: u8 LE.
    let v = bytes_text(json!({"binary": bin_s, "db": db, "addr": "0x0", "len": 4}));
    assert_eq!(v["addr"], 0);
    assert_eq!(v["addr_hex"], "0x0");
    assert_eq!(v["file_off"], 0);
    assert!(v["section"].is_null(), "{v}");
    assert_eq!(v["bytes_hex"], "01020304");
    assert_eq!(v["values"], json!([1, 2, 3, 4]));

    // Explicit view: u32 BE over the same bytes.
    let v = bytes_text(
        json!({"binary": bin_s, "db": db, "addr": "0", "len": 4, "as": "u32", "endian": "be"}),
    );
    assert_eq!(v["values"], json!([0x01020304u64]));

    assert!(!std::path::Path::new(&db).exists(), "reads never save");
    cleanup(&dir);
}

#[test]
fn mcp_bytes_unmapped_is_protocol_error() {
    let dir = tmpdir("mcp-bytes-err");
    let bin = write_bin(&dir, "m.bin", &[0x90, 0x90, 0xC3]);
    let bin_s = bin.display().to_string();
    let db = temp_db(&dir, "m").display().to_string();
    let lines = vec![
        mcp_req(
            1,
            "initialize",
            json!({"protocolVersion": "2025-11-25", "capabilities": {}, "clientInfo": {"name": "t", "version": "0"}}),
        ),
        serde_json::to_string(&json!({"jsonrpc": "2.0", "method": "notifications/initialized"}))
            .expect("n"),
        mcp_call(
            10,
            "bytes",
            json!({"binary": bin_s, "db": db, "addr": "0xdead", "len": 3}),
        ),
    ];
    let resps = mcp_session(&lines);
    assert_eq!(resps.len(), 2, "{resps:?}");
    let by_id: std::collections::HashMap<u64, &Value> = resps
        .iter()
        .map(|r| (r["id"].as_u64().expect("numeric id"), r))
        .collect();
    assert!(by_id[&10].get("error").is_some(), "{:?}", by_id[&10]);
    assert!(by_id[&10].get("result").is_none());
    cleanup(&dir);
}

#[test]
fn bytes_view_values_hex_twins_integer_views() {
    let dir = tmpdir("bytes-values-hex");
    let wide = 0x8010_0000_0000_0001u64;
    let p = open_raw(&dir, "w", &wide.to_le_bytes());
    let v = p
        .bytes_view(0, 8, BytesAs::U64, BytesEndian::Le)
        .expect("u64 view");
    assert_eq!(Value::Array(v.values.clone()), json!([wide]));
    assert_eq!(v.values_hex, Some(vec!["0x8010000000000001".to_string()]));
    let json = serde_json::to_value(&v).expect("view serializes");
    assert_eq!(json["values_hex"], json!(["0x8010000000000001"]));
    let ptr = p
        .bytes_view(0, 8, BytesAs::Ptr, BytesEndian::Le)
        .expect("ptr view");
    assert_eq!(ptr.values_hex, Some(vec!["0x8010000000000001".to_string()]));
    let q = open_raw(&dir, "b", &[0x90, 0x90, 0xC3]);
    let v = q
        .bytes_view(0, 3, BytesAs::U8, BytesEndian::Le)
        .expect("u8 view");
    assert_eq!(Value::Array(v.values.clone()), json!([144, 144, 195]));
    assert_eq!(
        v.values_hex,
        Some(vec![
            "0x90".to_string(),
            "0x90".to_string(),
            "0xc3".to_string()
        ])
    );
    let s = q
        .bytes_view(0, 3, BytesAs::CStr, BytesEndian::Le)
        .expect("cstr view");
    assert_eq!(s.values_hex, None);
    let json = serde_json::to_value(&s).expect("view serializes");
    assert!(json.get("values_hex").is_none(), "{json}");
    cleanup(&dir);
}

#[test]
fn cli_bytes_values_hex_shape() {
    let dir = tmpdir("cli-bytes-hex");
    let wide = 0x8010_0000_0000_0001u64;
    let bin = write_bin(&dir, "w.bin", &wide.to_le_bytes());
    let bin_s = bin.display().to_string();
    let db = temp_db(&dir, "w").display().to_string();
    let d = db.as_str();
    let (ok, v, text) = run(&[
        "--db", d, "bytes", &bin_s, "0x0", "--len", "8", "--as", "u64",
    ]);
    assert!(ok, "{v}");
    assert_eq!(text.trim_end().lines().count(), 1);
    assert_eq!(v["values"], json!([wide]));
    assert_eq!(v["values_hex"], json!(["0x8010000000000001"]));
    let bin2 = write_bin(&dir, "b.bin", &[0x90, 0x90, 0xC3]);
    let bin2_s = bin2.display().to_string();
    let (ok, v, _) = run(&["--db", d, "bytes", &bin2_s, "0x0", "--len", "3"]);
    assert!(ok, "{v}");
    assert_eq!(v["values"], json!([144, 144, 195]));
    assert_eq!(v["values_hex"], json!(["0x90", "0x90", "0xc3"]));
    let (ok, v, _) = run(&[
        "--db", d, "bytes", &bin2_s, "0x0", "--len", "3", "--as", "cstr",
    ]);
    assert!(ok, "{v}");
    assert!(v.get("values_hex").is_none(), "{v}");
    cleanup(&dir);
}