rns-git 0.3.1

Reticulum Git transport tools
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

use rns_core::msgpack::{self, Value};
use rns_git::acl::Access;
use rns_git::config::ServerConfig;
use rns_git::logging;
use rns_git::{pages, protocol, release, server};
use rns_net::RequestResponse;

const REMOTE: [u8; 16] = [
    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
];
const REMOTE_SIG: [u8; 64] = [0x42; 64];

#[test]
fn stats_permission_is_required_for_links_and_stats_page() {
    let tmp = tempfile::tempdir().unwrap();
    let mut config = cfg(tmp.path());
    config.allow_stats = vec!["none".into()];
    create_repo(
        config.repositories_dir.join("public/alpha"),
        "README.md",
        "# Alpha\n",
    );
    let access_rules = access(&config);

    let repo_page = pages::render_page(
        pages::PATH_REPO,
        &config,
        &access_rules,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();
    assert!(!repo_page.contains("[Stats`:/page/stats.mu"));
    assert!(!repo_page.contains(pages::PATH_STATS));

    let stats_page = pages::render_page(
        pages::PATH_STATS,
        &config,
        &access_rules,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();
    assert!(stats_page.contains(">Error"));
    assert!(stats_page.contains("repository was not found"));
}

#[test]
fn page_fetch_and_push_stats_are_recorded_and_rendered() {
    let tmp = tempfile::tempdir().unwrap();
    let config = cfg(tmp.path());
    create_repo(
        config.repositories_dir.join("public/alpha"),
        "README.md",
        "# Alpha\n",
    );
    let access_rules = access(&config);
    let remote = Some(&(REMOTE, REMOTE_SIG));

    pages::render_page(
        pages::PATH_INDEX,
        &config,
        &access_rules,
        &page_request(&[]),
        Some(&REMOTE),
    )
    .unwrap();
    pages::render_page(
        pages::PATH_GROUP,
        &config,
        &access_rules,
        &page_request(&[("var_g", "public")]),
        Some(&REMOTE),
    )
    .unwrap();
    pages::render_page(
        pages::PATH_REPO,
        &config,
        &access_rules,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();

    let fetch = server::handle_fetch(
        &config,
        &access_rules,
        &protocol::fetch_request("public/alpha", &[]),
        remote,
    )
    .unwrap();
    assert_fetch_ok(fetch);

    let push = server::handle_push(
        &config,
        &access_rules,
        &protocol::push_request("public/beta", Vec::new(), Vec::new()),
        remote,
    )
    .unwrap();
    assert_eq!(push[0], protocol::RES_OK);

    let alpha_stats = pages::render_page(
        pages::PATH_STATS,
        &config,
        &access_rules,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();
    assert!(alpha_stats.contains("/ stats\n\n>Stats for alpha"));
    assert!(alpha_stats.contains("Views"));
    assert!(alpha_stats.contains("Fetches"));
    assert!(alpha_stats.contains("Views`f     :     1  total"));
    assert!(alpha_stats.contains("Fetches`f   :     1  total"));
    assert!(alpha_stats.contains("Pushes`f    :     0  total"));
    assert!(alpha_stats.contains("Activity`f  :     2 points"));
    assert!(alpha_stats.contains("today:   1  peak:   1"));
    assert!(alpha_stats.contains("Low activity"));
    let persisted = msgpack::unpack_exact(&fs::read(config.dir.join("stats")).unwrap()).unwrap();
    assert_eq!(sum_counter(&persisted, &["pages", "front"]), 1);
    assert_eq!(sum_counter(&persisted, &["groups", "public", "view"]), 1);
    assert_eq!(
        sum_counter(
            &persisted,
            &["groups", "public", "repositories", "alpha", "view"]
        ),
        1
    );
    assert_eq!(
        sum_counter(
            &persisted,
            &["groups", "public", "repositories", "alpha", "fetch"]
        ),
        1
    );

    let beta_stats = pages::render_page(
        pages::PATH_STATS,
        &config,
        &access_rules,
        &page_request(&[("var_g", "public"), ("var_r", "beta")]),
        Some(&REMOTE),
    )
    .unwrap();
    assert!(beta_stats.contains(">Stats for beta"));
    assert!(beta_stats.contains("Pushes`f    :     1  total"));
    assert!(beta_stats.contains("Activity`f  :     5 points"));

    assert!(config.dir.join("stats").exists());
}

#[test]
fn download_stats_are_recorded_and_rendered() {
    let tmp = tempfile::tempdir().unwrap();
    let config = cfg(tmp.path());
    let repo_path = create_repo(
        config.repositories_dir.join("public/alpha"),
        "payload.txt",
        "blob bytes\n",
    );
    let releases_path = release::release_sidecar_path(&repo_path);
    let release_dir = releases_path.join("v1");
    fs::create_dir_all(release_dir.join("artifacts")).unwrap();
    fs::write(
        release_dir.join("META"),
        "tag = v1\ncreated = 1\nstatus = published\ncreated_by = test\n",
    )
    .unwrap();
    fs::write(release_dir.join("artifacts/dist.tar"), b"artifact bytes").unwrap();
    let access_rules = access(&config);

    let blob = pages::download_file(
        &config,
        &access_rules,
        &page_request(&[
            ("var_g", "public"),
            ("var_r", "alpha"),
            ("var_ref", "HEAD"),
            ("var_path", "payload.txt"),
        ]),
        Some(&REMOTE),
    )
    .unwrap();
    assert_resource_bytes(blob, b"blob bytes\n");

    let artifact = pages::download_file(
        &config,
        &access_rules,
        &page_request(&[
            ("var_g", "public"),
            ("var_r", "alpha"),
            ("var_tag", "v1"),
            ("var_artifact", "dist.tar"),
        ]),
        Some(&REMOTE),
    )
    .unwrap();
    assert_resource_bytes(artifact, b"artifact bytes");

    let alpha_stats = pages::render_page(
        pages::PATH_STATS,
        &config,
        &access_rules,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();
    assert!(alpha_stats.contains("Downloads`f :     2  total"));
    assert!(alpha_stats.contains(">Downloads"));
    assert!(alpha_stats.contains("Peak: 2"));
    assert!(!alpha_stats.contains("90 pts"));
    assert!(alpha_stats.contains('â–€') || alpha_stats.contains('â–„'));

    let persisted = msgpack::unpack_exact(&fs::read(config.dir.join("stats")).unwrap()).unwrap();
    assert_eq!(
        sum_counter(
            &persisted,
            &["groups", "public", "repositories", "alpha", "download"]
        ),
        1
    );
    assert_eq!(
        sum_counter(
            &persisted,
            &[
                "groups",
                "public",
                "repositories",
                "alpha",
                "release_download",
            ]
        ),
        1
    );
}

#[test]
fn denied_and_failed_operations_do_not_record_stats() {
    let tmp = tempfile::tempdir().unwrap();
    let mut config = cfg(tmp.path());
    create_repo(
        config.repositories_dir.join("public/alpha"),
        "README.md",
        "# Alpha\n",
    );

    config.allow_read = vec!["none".into()];
    let read_denied = access(&config);
    let denied_fetch = server::handle_fetch(
        &config,
        &read_denied,
        &protocol::fetch_request("public/alpha", &[]),
        Some(&(REMOTE, REMOTE_SIG)),
    )
    .unwrap();
    match denied_fetch {
        RequestResponse::Bytes(bytes) => assert_eq!(bytes[0], protocol::RES_DISALLOWED),
        RequestResponse::Resource { .. } | RequestResponse::File { .. } => {
            panic!("denied fetch unexpectedly returned resource")
        }
    }

    config.allow_read = vec!["all".into()];
    config.allow_write = vec!["none".into()];
    config.allow_create = vec!["none".into()];
    let write_denied = access(&config);
    let denied_push = server::handle_push(
        &config,
        &write_denied,
        &protocol::push_request("public/beta", Vec::new(), Vec::new()),
        Some(&(REMOTE, REMOTE_SIG)),
    )
    .unwrap();
    assert_eq!(denied_push[0], protocol::RES_DISALLOWED);

    let missing_fetch = server::handle_fetch(
        &config,
        &write_denied,
        &protocol::fetch_request("public/missing", &[]),
        Some(&(REMOTE, REMOTE_SIG)),
    )
    .unwrap();
    match missing_fetch {
        RequestResponse::Bytes(bytes) => assert_eq!(bytes[0], protocol::RES_NOT_FOUND),
        RequestResponse::Resource { .. } | RequestResponse::File { .. } => {
            panic!("missing fetch unexpectedly returned resource")
        }
    }

    assert!(!config.dir.join("stats").exists());
}

#[test]
fn stats_persist_across_config_reloads() {
    let tmp = tempfile::tempdir().unwrap();
    let config = cfg(tmp.path());
    create_repo(
        config.repositories_dir.join("public/alpha"),
        "README.md",
        "# Alpha\n",
    );
    let access_rules = access(&config);

    pages::render_page(
        pages::PATH_REPO,
        &config,
        &access_rules,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();

    let reloaded = cfg(tmp.path());
    let reloaded_access = access(&reloaded);
    let stats = pages::render_page(
        pages::PATH_STATS,
        &reloaded,
        &reloaded_access,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();

    assert!(stats.contains("Views`f     :     1  total"));
}

#[test]
fn ignored_identities_and_disabled_recording_do_not_mutate_stats() {
    let tmp = tempfile::tempdir().unwrap();
    let mut ignored = cfg(tmp.path());
    ignored.stats_ignore_identities = vec![REMOTE];
    create_repo(
        ignored.repositories_dir.join("public/alpha"),
        "README.md",
        "# Alpha\n",
    );
    let ignored_access = access(&ignored);

    pages::render_page(
        pages::PATH_REPO,
        &ignored,
        &ignored_access,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();
    let fetch = server::handle_fetch(
        &ignored,
        &ignored_access,
        &protocol::fetch_request("public/alpha", &[]),
        Some(&(REMOTE, REMOTE_SIG)),
    )
    .unwrap();
    assert_fetch_ok(fetch);
    let push = server::handle_push(
        &ignored,
        &ignored_access,
        &protocol::push_request("public/beta", Vec::new(), Vec::new()),
        Some(&(REMOTE, REMOTE_SIG)),
    )
    .unwrap();
    assert_eq!(push[0], protocol::RES_OK);

    let ignored_stats = pages::render_page(
        pages::PATH_STATS,
        &ignored,
        &ignored_access,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();
    assert!(ignored_stats.contains("Views`f     :     0  total"));
    assert!(ignored_stats.contains(
        "No development activity recorded for this repository in the selected time period.\n\n`*"
    ));
    let ignored_beta = pages::render_page(
        pages::PATH_STATS,
        &ignored,
        &ignored_access,
        &page_request(&[("var_g", "public"), ("var_r", "beta")]),
        Some(&REMOTE),
    )
    .unwrap();
    assert!(ignored_beta.contains("Pushes`f    :     0  total"));

    let tmp = tempfile::tempdir().unwrap();
    let mut push_ignored = cfg(tmp.path());
    push_ignored.stats_push_ignore_identities = vec![REMOTE];
    create_repo(
        push_ignored.repositories_dir.join("public/alpha"),
        "README.md",
        "# Alpha\n",
    );
    let push_ignored_access = access(&push_ignored);
    pages::render_page(
        pages::PATH_REPO,
        &push_ignored,
        &push_ignored_access,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();
    let push = server::handle_push(
        &push_ignored,
        &push_ignored_access,
        &protocol::push_request("public/gamma", Vec::new(), Vec::new()),
        Some(&(REMOTE, REMOTE_SIG)),
    )
    .unwrap();
    assert_eq!(push[0], protocol::RES_OK);
    let push_ignored_alpha = pages::render_page(
        pages::PATH_STATS,
        &push_ignored,
        &push_ignored_access,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();
    assert!(push_ignored_alpha.contains("Views`f     :     1  total"));
    let push_ignored_gamma = pages::render_page(
        pages::PATH_STATS,
        &push_ignored,
        &push_ignored_access,
        &page_request(&[("var_g", "public"), ("var_r", "gamma")]),
        Some(&REMOTE),
    )
    .unwrap();
    assert!(push_ignored_gamma.contains("Pushes`f    :     0  total"));

    let tmp = tempfile::tempdir().unwrap();
    let mut disabled = cfg(tmp.path());
    disabled.record_stats = false;
    create_repo(
        disabled.repositories_dir.join("public/alpha"),
        "README.md",
        "# Alpha\n",
    );
    let disabled_access = access(&disabled);

    pages::render_page(
        pages::PATH_REPO,
        &disabled,
        &disabled_access,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();

    let disabled_stats = pages::render_page(
        pages::PATH_STATS,
        &disabled,
        &disabled_access,
        &page_request(&[("var_g", "public"), ("var_r", "alpha")]),
        Some(&REMOTE),
    )
    .unwrap();
    assert!(disabled_stats.contains("Views`f     :     0  total"));
    assert!(!disabled.dir.join("stats").exists());
}

fn cfg(root: &Path) -> ServerConfig {
    ServerConfig {
        dir: root.to_path_buf(),
        reticulum_dir: None,
        repositories_dir: root.join("repositories"),
        identity_path: root.join("repositories_identity"),
        client_identity_path: root.join("client_identity"),
        node_name: "Stats Test Node".into(),
        announce_interval_secs: 300,
        serve_nomadnet: true,
        templates_dir: root.join("templates"),
        unicode_icons: false,
        media_conversion: true,
        record_stats: true,
        stats_ignore_identities: Vec::new(),
        stats_push_ignore_identities: Vec::new(),
        blocked_identities: Vec::new(),
        identity_aliases: std::collections::BTreeMap::new(),
        allow_read: vec!["all".into()],
        allow_write: vec!["all".into()],
        allow_create: vec!["all".into()],
        allow_stats: vec!["all".into()],
        allow_release: vec!["none".into()],
        allow_interact: vec!["none".into()],
        allow_propose: vec!["none".into()],
        allow_admin: vec!["none".into()],
        log_level: logging::DEFAULT_LOG_LEVEL,
    }
}

fn access(config: &ServerConfig) -> Access {
    Access::new(
        &config.allow_read,
        &config.allow_write,
        &config.allow_create,
        &config.allow_stats,
        &config.allow_release,
        &config.allow_interact,
        &config.allow_admin,
        config.repositories_dir.clone(),
    )
    .unwrap()
}

fn page_request(fields: &[(&str, &str)]) -> Vec<u8> {
    msgpack::pack(&Value::Map(
        fields
            .iter()
            .map(|(k, v)| (Value::Str((*k).into()), Value::Str((*v).into())))
            .collect(),
    ))
}

fn create_repo(path: PathBuf, file: &str, content: &str) -> PathBuf {
    let work = path.with_extension("work");
    fs::create_dir_all(&work).unwrap();
    run_git(Command::new("git").arg("init").arg(&work));
    let file_path = work.join(file);
    fs::create_dir_all(file_path.parent().unwrap()).unwrap();
    fs::write(&file_path, content).unwrap();
    run_git(Command::new("git").arg("-C").arg(&work).arg("add").arg("."));
    run_git(
        Command::new("git")
            .arg("-C")
            .arg(&work)
            .arg("-c")
            .arg("user.name=RNS Stats Test")
            .arg("-c")
            .arg("user.email=rns-stats-test@example.invalid")
            .arg("commit")
            .arg("-m")
            .arg("initial"),
    );
    run_git(
        Command::new("git")
            .arg("-C")
            .arg(&work)
            .args(["branch", "-M", "main"]),
    );
    run_git(
        Command::new("git")
            .arg("clone")
            .arg("--bare")
            .arg(&work)
            .arg(&path),
    );
    path
}

fn run_git(cmd: &mut Command) -> String {
    let output = cmd.output().unwrap();
    assert!(
        output.status.success(),
        "git command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).into_owned()
}

fn assert_fetch_ok(response: RequestResponse) {
    match response {
        RequestResponse::File { .. } => panic!("fetch should return a value"),
        RequestResponse::Resource { metadata, .. } => {
            assert_eq!(
                metadata.as_deref(),
                Some(&protocol::metadata_status(protocol::RES_OK)[..])
            );
        }
        RequestResponse::Bytes(bytes) => assert_eq!(bytes[0], protocol::RES_OK),
    }
}

fn assert_resource_bytes(response: RequestResponse, expected: &[u8]) {
    match response {
        RequestResponse::Resource { data, metadata, .. } => {
            assert_eq!(data, expected);
            assert_eq!(
                metadata.as_deref(),
                Some(&protocol::metadata_status(protocol::RES_OK)[..])
            );
        }
        RequestResponse::Bytes(bytes) => panic!("expected resource response, got {bytes:?}"),
        RequestResponse::File { .. } => panic!("expected value response, got file"),
    }
}

fn sum_counter(value: &Value, path: &[&str]) -> u64 {
    let mut cursor = value;
    for key in path {
        cursor = cursor.map_get(key).expect("stats path component");
    }
    cursor
        .as_map()
        .unwrap()
        .iter()
        .map(|(_, value)| value.as_integer().unwrap() as u64)
        .sum()
}