vcon-server 0.4.12

Stores vCons encrypted at rest, on local disk or in an S3 bucket (as objects or Parquet files), and returns them to authorized readers.
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
//! Schema version 8: the server numbers every row that has a `seq`,
//! whatever the statement says, and the two audit logs take new records
//! and one more in the count of a counted record, and nothing else, from
//! the application role and from the owner. Each live test of the
//! migration starts from a database at version 7. Version 9 (v9.rs)
//! replaced "one more" with "a larger count"; the live tests here that
//! start from a migrated database hold the rule as it is now.

use super::super::super::schema::{MIGRATIONS, SCHEMA_VERSION, version_refusal};

/// The position of this version's migration.
const THIS: usize = 7;

/// The tables whose rows the server numbers.
const NUMBERED: [&str; 4] = ["version", "manifest", "audit", "admin_audit"];

/// The two audit logs.
const LOGS: [&str; 2] = ["audit", "admin_audit"];

/// This version's migration, or a panic that says there is none.
fn text() -> &'static str {
    assert!(
        MIGRATIONS.len() > THIS,
        "there are {} migrations",
        MIGRATIONS.len()
    );
    MIGRATIONS[THIS]
}

// What must work.

#[test]
fn version_8_is_the_eighth_migration_and_numbers_the_rows() {
    assert!(
        text().contains("CREATE FUNCTION vcon.number_row() RETURNS trigger"),
        "the eighth makes the function that numbers a row"
    );
}

#[test]
fn the_binary_s_version_is_at_least_8() {
    assert!(
        usize::try_from(SCHEMA_VERSION).expect("positive") > THIS,
        "this binary's version: {SCHEMA_VERSION}"
    );
}

#[test]
fn every_numbered_table_is_named_once_for_the_numbering_trigger() {
    let named = format!(
        "ARRAY[{}]",
        NUMBERED.map(|table| format!("'{table}'")).join(", ")
    );
    assert_eq!(
        text().matches(&named).count(),
        1,
        "the four tables with a seq, in one list: {named}"
    );
}

#[test]
fn both_logs_are_named_once_for_the_append_only_triggers() {
    let named = format!(
        "ARRAY[{}]",
        LOGS.map(|table| format!("'{table}'")).join(", ")
    );
    assert_eq!(
        text().matches(&named).count(),
        1,
        "the tenant's log and the admin log, in one list: {named}"
    );
}

// What must be refused.

#[test]
fn a_version_7_database_is_refused_with_the_command_that_upgrades_it() {
    let why = version_refusal(Some(7)).expect("refused");
    assert!(
        why.contains(&format!(
            "version 7, older than this binary's {SCHEMA_VERSION}"
        )) && why.contains("vcon-server migrate"),
        "the versions and the command: {why}"
    );
}

#[test]
fn no_role_is_granted_anything_by_version_8() {
    assert!(
        !text()
            .lines()
            .any(|line| line.trim_start().to_uppercase().starts_with("GRANT ")),
        "the triggers need no grant, and the application role gains none"
    );
}

// The limits and odd inputs.

#[test]
fn version_8_changes_no_row_by_a_statement_and_makes_no_table() {
    // The owner role is held to row-level security too, so an UPDATE or
    // DELETE in a migration would raise. A statement begins its line.
    for statement in [
        "UPDATE ",
        "DELETE ",
        "DROP ",
        "TRUNCATE ",
        "CREATE TABLE ",
        "ALTER TABLE ",
    ] {
        assert!(
            !text()
                .lines()
                .any(|line| line.trim_start().to_uppercase().starts_with(statement)),
            "no {statement}in version 8"
        );
    }
}

#[test]
fn each_function_of_version_8_has_a_fixed_search_path() {
    let functions = text().matches("CREATE FUNCTION ").count();
    assert_eq!(functions, 2, "the numbering and the append-only rule");
    assert_eq!(
        text()
            .matches("SET search_path = pg_catalog, pg_temp")
            .count(),
        functions,
        "a function reads no name from the caller's search path"
    );
}

#[test]
fn only_the_numbering_runs_as_its_owner() {
    assert_eq!(
        text().matches("SECURITY DEFINER").count(),
        1,
        "the sequence is the owner's; the append-only rule needs no privilege"
    );
}

#[test]
fn the_functions_are_revoked_from_public() {
    assert!(
        text().contains(
            "REVOKE ALL ON FUNCTION vcon.number_row(), vcon.audit_append_only() FROM PUBLIC;"
        ),
        "no role is given the functions"
    );
}

#[cfg(feature = "backend-tests")]
pub(super) mod on_a_server {
    use tokio_postgres::Client;

    use super::super::super::super::schema::{MIGRATIONS, Migrated, SCHEMA_VERSION};
    use super::super::live::{Db, TENANT_A, TENANT_B, in_scope};
    use super::{LOGS, NUMBERED, THIS};

    /// The server's words when a record would be changed.
    /// Version 9 changed them with the rule.
    pub(in super::super) const NOT_CHANGED: &str =
        "an audit record is not changed, but for a larger count of a counted record";
    /// The server's words when a record would be deleted.
    pub(in super::super) const NOT_DELETED: &str = "an audit record is not deleted";
    /// The server's words when a log would be emptied.
    pub(in super::super) const NOT_EMPTIED: &str = "an audit log is not emptied";

    /// A database at version 7.
    async fn at_version_7() -> Db {
        let db = Db::fresh().await;
        assert_eq!(
            db.migrate_with(&MIGRATIONS[..THIS])
                .await
                .expect("version 7"),
            Migrated { from: 0, to: 7 },
            "the first seven migrations alone"
        );
        db
    }

    /// The tester's connection as the role that owns the schema.
    pub(in super::super) async fn owner(db: &Db) -> Client {
        let client = db.admin().await;
        client
            .batch_execute("SET ROLE vcon_owner")
            .await
            .expect("the owner role");
        client
    }

    /// The scope a transaction on `table` runs in: the registry's for the
    /// admin log, the first tenant's for the others.
    pub(in super::super) fn scope(table: &str) -> &'static str {
        if table == "admin_audit" {
            "registry"
        } else {
            TENANT_A
        }
    }

    /// The first value of the first row `sql` gives `client` in `scope`.
    pub(in super::super) async fn one(client: &Client, scope: &str, sql: &str) -> String {
        in_scope(client, scope, sql)
            .await
            .unwrap_or_else(|e| panic!("{sql}: {e}"))
            .iter()
            .find_map(|m| match m {
                tokio_postgres::SimpleQueryMessage::Row(r) => {
                    Some(r.get(0).unwrap_or("NULL").to_owned())
                }
                _ => None,
            })
            .unwrap_or_else(|| panic!("{sql}: no row"))
    }

    /// The server's message refusing `sql`.
    pub(in super::super) async fn refused(client: &Client, scope: &str, sql: &str) -> String {
        match in_scope(client, scope, sql).await {
            Ok(_) => panic!("not refused: {sql}"),
            Err(e) => e,
        }
    }

    /// An INSERT of one row into `table` for `tenant`, with ` RETURNING
    /// seq`; `seq` is the number the statement asks for, written with
    /// OVERRIDING SYSTEM VALUE, and `counted` marks an audit record.
    fn insert(table: &str, tenant: &str, seq: Option<&str>, counted: bool) -> String {
        let (columns, values) = match table {
            "version" => (
                "tenant, version, uuid_mac, form_sealed, gzip_sealed, size_sealed, stored_at, \
                 tenant_key_id, wrapped_key"
                    .to_owned(),
                format!(
                    "'\\x{tenant}', gen_random_uuid()::text::bytea, '\\x02', '\\x03', '\\x04', \
                     '\\x05', 1, '\\x06', '\\x07'"
                ),
            ),
            "manifest" => (
                "tenant, published_at".to_owned(),
                format!("'\\x{tenant}', 1"),
            ),
            "audit" => (
                "tenant, at, actor, action, status, count, counted".to_owned(),
                format!("'\\x{tenant}', 't', 'a', 'b', 1, 1, {counted}"),
            ),
            "admin_audit" => (
                "at, actor, action, status, count, counted".to_owned(),
                format!("'t', 'a', 'b', 1, 1, {counted}"),
            ),
            other => panic!("no insert for {other}"),
        };
        match seq {
            Some(seq) => format!(
                "INSERT INTO vcon.{table} ({columns}, seq) OVERRIDING SYSTEM VALUE \
                 VALUES ({values}, {seq}) RETURNING seq"
            ),
            None => format!("INSERT INTO vcon.{table} ({columns}) VALUES ({values}) RETURNING seq"),
        }
    }

    /// Stores one row in `table` as the application role and gives its
    /// number.
    async fn store(db: &Db, table: &str, counted: bool) -> i64 {
        one(
            &db.app().await,
            scope(table),
            &insert(table, TENANT_A, None, counted),
        )
        .await
        .parse()
        .expect("a number")
    }

    /// The count of the one record of `table`, read by the owner.
    pub(in super::super) async fn count_of(db: &Db, table: &str) -> String {
        one(
            &owner(db).await,
            scope(table),
            &format!("SELECT count FROM vcon.{table}"),
        )
        .await
    }

    /// How many rows `table` holds, read by the owner in the table's scope.
    pub(in super::super) async fn rows(db: &Db, table: &str) -> String {
        one(
            &owner(db).await,
            scope(table),
            &format!("SELECT count(*) FROM vcon.{table}"),
        )
        .await
    }

    /// How many triggers of this version the database has.
    pub(in super::super) async fn triggers(db: &Db) -> String {
        one(
            &db.admin().await,
            TENANT_A,
            "SELECT count(*) FROM pg_trigger WHERE NOT tgisinternal AND tgname IN \
             ('number_rows', 'append_only', 'not_emptied')",
        )
        .await
    }

    // What must work.

    #[tokio::test]
    async fn a_version_7_database_is_brought_to_version_8_with_its_rows_as_they_were() {
        let db = at_version_7().await;
        // A server is refused at version 7, so the tester writes the rows.
        let admin = db.admin().await;
        let mut before = Vec::new();
        for table in NUMBERED {
            let seq = one(&admin, scope(table), &insert(table, TENANT_A, None, false)).await;
            before.push(seq);
        }
        assert_eq!(
            triggers(&db).await,
            "0",
            "version 7 has none of the triggers"
        );
        assert_eq!(
            db.migrate().await,
            Migrated {
                from: 7,
                to: SCHEMA_VERSION
            },
            "migrate brings it to this binary's version"
        );
        let mut after = Vec::new();
        for table in NUMBERED {
            let sql = format!("SELECT seq FROM vcon.{table}");
            after.push(one(&admin, scope(table), &sql).await);
        }
        assert_eq!(after, before, "each row kept its number");
        assert_eq!(
            triggers(&db).await,
            "8",
            "four number the rows, and each log has one against changes and one against emptying"
        );
    }

    #[tokio::test]
    async fn rows_stored_after_the_migration_take_numbers_after_the_ones_before() {
        let db = at_version_7().await;
        let admin = db.admin().await;
        let mut before = Vec::new();
        for table in NUMBERED {
            let seq = one(&admin, scope(table), &insert(table, TENANT_A, None, false)).await;
            before.push(seq.parse::<i64>().expect("a number"));
        }
        db.migrate().await;
        for (table, before) in NUMBERED.iter().zip(before) {
            let after = store(&db, table, false).await;
            assert!(
                after > before,
                "{table}: {after} is after {before}, the sequence went on"
            );
        }
    }

    #[tokio::test]
    async fn the_server_numbers_each_row_in_the_order_stored() {
        let db = Db::migrated().await;
        for table in NUMBERED {
            let first = store(&db, table, false).await;
            let second = store(&db, table, false).await;
            let third = store(&db, table, false).await;
            assert!(
                first < second && second < third,
                "{table}: {first}, {second}, {third} rise"
            );
        }
    }

    #[tokio::test]
    async fn the_application_role_adds_one_to_a_counted_record() {
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, true).await;
            let app = db.app().await;
            let update = format!("UPDATE vcon.{table} SET count = count + 1");
            in_scope(&app, scope(table), &update)
                .await
                .expect("adds one");
            in_scope(&app, scope(table), &update)
                .await
                .expect("and one more");
            assert_eq!(
                count_of(&db, table).await,
                "3",
                "{table}: one, and one twice"
            );
        }
    }

    #[tokio::test]
    async fn the_owner_adds_one_to_a_counted_record_too() {
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, true).await;
            in_scope(
                &owner(&db).await,
                scope(table),
                &format!("UPDATE vcon.{table} SET count = count + 1"),
            )
            .await
            .expect("the one change a record takes");
            assert_eq!(count_of(&db, table).await, "2", "{table}");
        }
    }

    #[tokio::test]
    async fn an_update_that_writes_the_other_columns_as_they_are_is_one_more_in_the_count() {
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, true).await;
            in_scope(
                &owner(&db).await,
                scope(table),
                &format!(
                    "UPDATE vcon.{table} SET count = count + 1, at = at, version = NULL, \
                     target_mac = NULL, counted = true"
                ),
            )
            .await
            .expect("nothing but the count is different");
            assert_eq!(count_of(&db, table).await, "2", "{table}");
        }
    }

    #[tokio::test]
    async fn the_application_role_reads_the_records_it_wrote_in_their_order() {
        let db = Db::migrated().await;
        for table in LOGS {
            let first = store(&db, table, false).await;
            let second = store(&db, table, true).await;
            let read = one(
                &db.app().await,
                scope(table),
                &format!("SELECT string_agg(seq::text, ',' ORDER BY seq) FROM vcon.{table}"),
            )
            .await;
            assert_eq!(read, format!("{first},{second}"), "{table}");
        }
    }

    #[tokio::test]
    async fn a_new_database_has_the_triggers() {
        let db = Db::migrated().await;
        assert_eq!(
            triggers(&db).await,
            "8",
            "migrate from nothing makes them too"
        );
    }

    #[tokio::test]
    async fn every_column_the_server_numbers_has_the_numbering_trigger() {
        let db = Db::migrated().await;
        let numbered = "FROM pg_attribute a JOIN pg_class c ON c.oid = a.attrelid \
            JOIN pg_namespace n ON n.oid = c.relnamespace \
            WHERE n.nspname = 'vcon' AND c.relkind = 'r' AND a.attnum > 0 AND NOT a.attisdropped \
              AND (a.attidentity <> '' OR EXISTS (SELECT 1 FROM pg_attrdef d \
                   WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum \
                     AND pg_get_expr(d.adbin, d.adrelid) LIKE '%nextval(%'))";
        let admin = db.admin().await;
        assert_eq!(
            one(
                &admin,
                TENANT_A,
                &format!("SELECT string_agg(c.relname || '.' || a.attname, ',' ORDER BY c.relname) {numbered}"),
            )
            .await,
            "admin_audit.seq,audit.seq,manifest.seq,version.seq",
            "the columns a sequence fills: a new one needs the trigger and a place in this test"
        );
        assert_eq!(
            one(
                &admin,
                TENANT_A,
                &format!(
                    "SELECT count(*) {numbered} AND NOT EXISTS (SELECT 1 FROM pg_trigger t \
                     WHERE t.tgrelid = c.oid AND t.tgname = 'number_rows' AND NOT t.tgisinternal \
                       AND t.tgtype = 23)"
                ),
            )
            .await,
            "0",
            "each has the trigger, for each row, before INSERT and UPDATE"
        );
    }

    #[tokio::test]
    async fn the_owner_stores_a_record_as_the_application_role_does() {
        let db = Db::migrated().await;
        for table in LOGS {
            one(
                &owner(&db).await,
                scope(table),
                &insert(table, TENANT_A, None, false),
            )
            .await;
            assert_eq!(rows(&db, table).await, "1", "{table}: appended");
        }
    }

    #[tokio::test]
    async fn an_update_of_a_version_or_a_manifest_keeps_its_number() {
        let db = Db::migrated().await;
        for (table, change) in [
            ("version", "stored_at = 2"),
            ("manifest", "published_at = 2"),
        ] {
            let seq = store(&db, table, false).await;
            let after = one(
                &db.app().await,
                TENANT_A,
                &format!("UPDATE vcon.{table} SET {change} RETURNING seq"),
            )
            .await;
            assert_eq!(
                after,
                seq.to_string(),
                "{table}: the row is changed, not its number"
            );
        }
    }

    #[tokio::test]
    async fn migrating_twice_from_version_7_does_the_work_once() {
        let db = at_version_7().await;
        assert_eq!(
            db.migrate().await,
            Migrated {
                from: 7,
                to: SCHEMA_VERSION
            },
            "the first run"
        );
        assert_eq!(
            db.migrate().await,
            Migrated {
                from: SCHEMA_VERSION,
                to: SCHEMA_VERSION
            },
            "the second finds nothing to do"
        );
        assert_eq!(triggers(&db).await, "8", "and makes no trigger twice");
    }

    // What must be refused.

    #[tokio::test]
    async fn a_number_the_statement_chooses_is_not_kept() {
        let db = Db::migrated().await;
        for table in NUMBERED {
            let before = store(&db, table, false).await;
            for chosen in ["999999", "0", "-5"] {
                let got = one(
                    &db.app().await,
                    scope(table),
                    &insert(table, TENANT_A, Some(chosen), false),
                )
                .await;
                assert!(
                    got != chosen && got.parse::<i64>().expect("a number") > before,
                    "{table}: asked for {chosen}, numbered {got} after {before}"
                );
            }
        }
    }

    #[tokio::test]
    async fn a_number_an_earlier_row_holds_cannot_be_chosen_again() {
        let db = Db::migrated().await;
        for table in NUMBERED {
            let first = store(&db, table, false).await;
            let got = one(
                &db.app().await,
                scope(table),
                &insert(table, TENANT_A, Some(&first.to_string()), false),
            )
            .await;
            assert!(
                got.parse::<i64>().expect("a number") > first,
                "{table}: a second row is after {first}, not at it: {got}"
            );
            assert_eq!(rows(&db, table).await, "2", "{table}: both rows are there");
        }
    }

    #[tokio::test]
    async fn a_version_or_a_manifest_is_not_given_a_new_number() {
        let db = Db::migrated().await;
        for table in ["version", "manifest"] {
            let seq = store(&db, table, false).await;
            store(&db, table, false).await;
            let after = one(
                &db.app().await,
                TENANT_A,
                &format!("UPDATE vcon.{table} SET seq = DEFAULT WHERE seq = {seq} RETURNING seq"),
            )
            .await;
            assert_eq!(
                after,
                seq.to_string(),
                "{table}: the first row stays first, whatever DEFAULT would give"
            );
        }
    }

    #[tokio::test]
    async fn the_application_role_deletes_no_record() {
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, false).await;
            let e = refused(
                &db.app().await,
                scope(table),
                &format!("DELETE FROM vcon.{table}"),
            )
            .await;
            assert!(
                e.contains(&format!("permission denied for table {table}")),
                "{table}: {e}"
            );
            assert_eq!(rows(&db, table).await, "1", "{table}: the record is there");
        }
    }

    #[tokio::test]
    async fn the_owner_deletes_no_record() {
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, false).await;
            let e = refused(
                &owner(&db).await,
                scope(table),
                &format!("DELETE FROM vcon.{table}"),
            )
            .await;
            assert_eq!(e, NOT_DELETED, "{table}: the trigger holds the owner");
            assert_eq!(rows(&db, table).await, "1", "{table}: the record is there");
        }
    }

    #[tokio::test]
    async fn the_application_role_empties_no_log() {
        let db = Db::migrated().await;
        for table in LOGS {
            let e = refused(
                &db.app().await,
                scope(table),
                &format!("TRUNCATE vcon.{table}"),
            )
            .await;
            assert!(
                e.contains(&format!("permission denied for table {table}")),
                "{table}: {e}"
            );
        }
    }

    #[tokio::test]
    async fn the_owner_empties_no_log() {
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, false).await;
            let e = refused(
                &owner(&db).await,
                scope(table),
                &format!("TRUNCATE vcon.{table}"),
            )
            .await;
            assert_eq!(e, NOT_EMPTIED, "{table}: the trigger holds the owner");
            assert_eq!(rows(&db, table).await, "1", "{table}: the record is there");
        }
    }

    #[tokio::test]
    async fn the_owner_changes_no_column_of_a_record() {
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, true).await;
            let client = owner(&db).await;
            for change in [
                "at = 'later'",
                "actor = 'other'",
                "action = 'other'",
                "target_mac = '\\x01'",
                "target_sealed = '\\x01'",
                "version = 'v'",
                "status = 2",
                "status = NULL",
                "counted = false",
                "seq = DEFAULT",
            ] {
                for with_count in ["", ", count = count + 1"] {
                    let sql = format!("UPDATE vcon.{table} SET {change}{with_count}");
                    let e = refused(&client, scope(table), &sql).await;
                    assert_eq!(e, NOT_CHANGED, "{sql}");
                }
            }
            assert_eq!(
                count_of(&db, table).await,
                "1",
                "{table}: the count is as stored"
            );
        }
    }

    #[tokio::test]
    async fn the_owner_moves_no_record_to_another_tenant() {
        let db = Db::migrated().await;
        store(&db, "audit", true).await;
        let e = refused(
            &owner(&db).await,
            TENANT_A,
            &format!("UPDATE vcon.audit SET tenant = '\\x{TENANT_B}', count = count + 1"),
        )
        .await;
        assert_eq!(e, NOT_CHANGED, "the tenant is a column like the others");
    }

    #[tokio::test]
    async fn the_application_role_changes_no_column_but_the_count() {
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, true).await;
            let e = refused(
                &db.app().await,
                scope(table),
                &format!("UPDATE vcon.{table} SET actor = 'other'"),
            )
            .await;
            assert!(
                e.contains(&format!("permission denied for table {table}")),
                "{table}: {e}"
            );
        }
    }

    #[tokio::test]
    async fn the_count_of_a_record_that_is_not_counted_is_not_changed() {
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, false).await;
            let sql = format!("UPDATE vcon.{table} SET count = count + 1");
            for client in [db.app().await, owner(&db).await] {
                let e = refused(&client, scope(table), &sql).await;
                assert_eq!(e, NOT_CHANGED, "{table}");
            }
            assert_eq!(count_of(&db, table).await, "1", "{table}");
        }
    }

    #[tokio::test]
    async fn a_count_is_not_changed_to_itself_or_to_less() {
        // Version 8 took one more and nothing else; since version 9 a
        // count takes any larger number (v9.rs).
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, true).await;
            for to in ["count - 1", "count", "0", "1", "-1000", "count * 2 - 2"] {
                let sql = format!("UPDATE vcon.{table} SET count = {to}");
                for client in [db.app().await, owner(&db).await] {
                    let e = refused(&client, scope(table), &sql).await;
                    assert_eq!(e, NOT_CHANGED, "{sql}");
                }
            }
            assert_eq!(count_of(&db, table).await, "1", "{table}");
        }
    }

    #[tokio::test]
    async fn the_application_role_cannot_use_or_set_a_sequence() {
        let db = Db::migrated().await;
        let app = db.app().await;
        for table in NUMBERED {
            for call in [
                format!("SELECT nextval('vcon.{table}_seq_seq')"),
                format!("SELECT setval('vcon.{table}_seq_seq', 1)"),
                format!("ALTER SEQUENCE vcon.{table}_seq_seq RESTART"),
            ] {
                let e = refused(&app, scope(table), &call).await;
                assert!(
                    e.contains("permission denied for sequence")
                        || e.contains("must be owner of sequence"),
                    "{call}: {e}"
                );
            }
        }
    }

    #[tokio::test]
    async fn the_application_role_cannot_remove_or_stop_a_trigger() {
        let db = Db::migrated().await;
        let app = db.app().await;
        for table in LOGS {
            for statement in [
                format!("DROP TRIGGER append_only ON vcon.{table}"),
                format!("DROP TRIGGER number_rows ON vcon.{table}"),
                format!("ALTER TABLE vcon.{table} DISABLE TRIGGER ALL"),
                format!("ALTER TABLE vcon.{table} DISABLE TRIGGER not_emptied"),
            ] {
                let e = refused(&app, scope(table), &statement).await;
                assert!(
                    e.contains(&format!("must be owner of relation {table}"))
                        || e.contains(&format!("must be owner of table {table}")),
                    "{statement}: {e}"
                );
            }
        }
    }

    #[tokio::test]
    async fn the_application_role_cannot_replace_or_call_a_function() {
        let db = Db::migrated().await;
        let app = db.app().await;
        for function in ["number_row", "audit_append_only"] {
            let e = refused(
                &app,
                TENANT_A,
                &format!(
                    "CREATE OR REPLACE FUNCTION vcon.{function}() RETURNS trigger \
                     LANGUAGE plpgsql AS 'BEGIN RETURN NEW; END'"
                ),
            )
            .await;
            assert!(
                e.contains("permission denied for schema vcon"),
                "{function}: {e}"
            );
            let e = refused(&app, TENANT_A, &format!("SELECT vcon.{function}()")).await;
            assert!(
                e.contains(&format!("permission denied for function {function}")),
                "{function}: {e}"
            );
        }
    }

    #[tokio::test]
    async fn the_application_role_cannot_turn_the_triggers_off_for_its_session() {
        let db = Db::migrated().await;
        let e = refused(
            &db.app().await,
            TENANT_A,
            "SET session_replication_role = replica",
        )
        .await;
        assert!(
            e.contains("permission denied to set parameter \"session_replication_role\""),
            "{e}"
        );
    }

    #[tokio::test]
    async fn a_version_8_that_fails_leaves_version_7_whole() {
        let db = at_version_7().await;
        let broken = format!("{}\nSELECT boom;", MIGRATIONS[THIS]);
        let mut migrations: Vec<&str> = MIGRATIONS[..THIS].to_vec();
        migrations.push(&broken);
        let e = db.migrate_with(&migrations).await.expect_err("fails");
        assert!(e.starts_with("version 8: "), "names the version: {e}");
        assert_eq!(
            triggers(&db).await,
            "0",
            "the triggers were rolled back with it"
        );
        assert_eq!(
            db.migrate().await,
            Migrated {
                from: 7,
                to: SCHEMA_VERSION
            },
            "and the real migration then runs from 7"
        );
    }

    #[tokio::test]
    async fn a_tenant_adds_nothing_to_another_tenant_s_count() {
        let db = Db::migrated().await;
        store(&db, "audit", true).await;
        in_scope(
            &db.app().await,
            TENANT_B,
            "UPDATE vcon.audit SET count = count + 1",
        )
        .await
        .expect("finds no row of its own");
        assert_eq!(
            count_of(&db, "audit").await,
            "1",
            "row-level security still stands before the trigger"
        );
    }

    // The limits and odd inputs.

    #[tokio::test]
    async fn a_refused_change_among_several_rows_changes_none() {
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, true).await;
            store(&db, table, false).await;
            store(&db, table, true).await;
            let e = refused(
                &db.app().await,
                scope(table),
                &format!("UPDATE vcon.{table} SET count = count + 1"),
            )
            .await;
            assert_eq!(e, NOT_CHANGED, "{table}: one of the three is not counted");
            assert_eq!(
                one(
                    &owner(&db).await,
                    scope(table),
                    &format!("SELECT sum(count) FROM vcon.{table}"),
                )
                .await,
                "3",
                "{table}: the statement changed no count"
            );
        }
    }

    #[tokio::test]
    async fn only_the_counted_records_among_several_are_added_to() {
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, true).await;
            store(&db, table, false).await;
            in_scope(
                &db.app().await,
                scope(table),
                &format!("UPDATE vcon.{table} SET count = count + 1 WHERE counted"),
            )
            .await
            .expect("the statement the server sends");
            assert_eq!(
                one(
                    &owner(&db).await,
                    scope(table),
                    &format!("SELECT string_agg(count::text, ',' ORDER BY seq) FROM vcon.{table}"),
                )
                .await,
                "2,1",
                "{table}"
            );
        }
    }

    #[tokio::test]
    async fn a_delete_or_an_update_that_finds_no_record_is_no_error() {
        let db = Db::migrated().await;
        for table in LOGS {
            store(&db, table, false).await;
            let client = owner(&db).await;
            for statement in [
                format!("DELETE FROM vcon.{table} WHERE seq < 0"),
                format!("UPDATE vcon.{table} SET actor = 'other' WHERE seq < 0"),
            ] {
                in_scope(&client, scope(table), &statement)
                    .await
                    .expect("a row trigger runs for a row, and there is none");
            }
            assert_eq!(rows(&db, table).await, "1", "{table}: nothing changed");
        }
    }

    #[tokio::test]
    async fn an_empty_log_is_not_emptied_either() {
        let db = Db::migrated().await;
        for table in LOGS {
            let e = refused(
                &owner(&db).await,
                scope(table),
                &format!("TRUNCATE vcon.{table}"),
            )
            .await;
            assert_eq!(
                e, NOT_EMPTIED,
                "{table}: the statement is refused, not a row"
            );
        }
    }

    #[tokio::test]
    async fn emptying_a_log_with_another_table_is_refused() {
        let db = Db::migrated().await;
        let e = refused(
            &owner(&db).await,
            TENANT_A,
            "TRUNCATE vcon.key_use, vcon.audit, vcon.admin_audit RESTART IDENTITY CASCADE",
        )
        .await;
        assert_eq!(e, NOT_EMPTIED, "whatever else the statement names");
    }

    #[tokio::test]
    async fn a_record_stored_with_a_null_number_is_numbered() {
        let db = Db::migrated().await;
        for table in LOGS {
            let got = one(
                &db.app().await,
                scope(table),
                &insert(table, TENANT_A, Some("NULL"), false),
            )
            .await;
            assert!(
                got.parse::<i64>().is_ok_and(|seq| seq > 0),
                "{table}: {got}"
            );
        }
    }

    #[tokio::test]
    async fn a_write_rolled_back_leaves_a_number_unused_and_none_used_twice() {
        let db = Db::migrated().await;
        for table in LOGS {
            let first = store(&db, table, false).await;
            let app = db.app().await;
            let rolled = format!(
                "{}; SELECT 1 / 0",
                insert(table, TENANT_A, None, false).replace(" RETURNING seq", "")
            );
            let e = refused(&app, scope(table), &rolled).await;
            assert!(e.contains("division by zero"), "{e}");
            let next = store(&db, table, false).await;
            assert!(
                next > first + 1,
                "{table}: {next} skips the number the rolled-back record took after {first}"
            );
            assert_eq!(rows(&db, table).await, "2", "{table}");
        }
    }

    #[tokio::test]
    async fn the_records_of_two_tenants_take_numbers_from_one_sequence() {
        let db = Db::migrated().await;
        let app = db.app().await;
        let mut numbers = Vec::new();
        for tenant in [TENANT_A, TENANT_B, TENANT_A] {
            let seq = one(&app, tenant, &insert("audit", tenant, None, false)).await;
            numbers.push(seq.parse::<i64>().expect("a number"));
        }
        assert!(
            numbers[0] < numbers[1] && numbers[1] < numbers[2],
            "the order is the database's: {numbers:?}"
        );
    }

    #[tokio::test]
    async fn an_insert_that_would_change_a_record_on_conflict_finds_no_conflict() {
        let db = Db::migrated().await;
        let first = store(&db, "audit", false).await;
        let statement = format!(
            "INSERT INTO vcon.audit (tenant, at, actor, action, status, count, seq) \
             OVERRIDING SYSTEM VALUE VALUES ('\\x{TENANT_A}', 't', 'a', 'b', 1, 1, {first}) \
             ON CONFLICT (tenant, seq) DO UPDATE SET count = vcon.audit.count + 1 RETURNING seq"
        );
        let got = one(&db.app().await, TENANT_A, &statement).await;
        assert!(
            got.parse::<i64>().expect("a number") > first,
            "the new record took its own number: {got}"
        );
        assert_eq!(
            one(
                &owner(&db).await,
                TENANT_A,
                "SELECT string_agg(count::text, ',' ORDER BY seq) FROM vcon.audit",
            )
            .await,
            "1,1",
            "and the first record's count is as stored"
        );
    }

    #[tokio::test]
    async fn a_count_at_the_largest_number_is_not_added_to() {
        let db = Db::migrated().await;
        let statement = format!(
            "INSERT INTO vcon.audit (tenant, at, actor, action, status, count, counted) \
             VALUES ('\\x{TENANT_A}', 't', 'a', 'b', 1, 9223372036854775807, true)"
        );
        in_scope(&db.app().await, TENANT_A, &statement)
            .await
            .expect("stores");
        let e = refused(
            &db.app().await,
            TENANT_A,
            "UPDATE vcon.audit SET count = count + 1",
        )
        .await;
        assert!(e.contains("bigint out of range"), "{e}");
    }
}