vcon-server 0.4.19

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
//! The live-test deadline (tests/support/live_deadline.rs) in the live
//! helpers' settings: `live_settings` without a server, against small
//! servers on the loopback address, and `as_user`, `with_certificate` and
//! the schema tests' `Db` on the servers `scripts/pg-test-server.sh`
//! starts. The server's own default, which those helpers once took, is
//! left as it is.

use std::time::{Duration, Instant};

use super::super::pool::Pool;
use super::{
    closed_port, connect, connector, file, live_settings, no_tls_server, pki, settings,
    silent_server, why,
};
use crate::config::{POSTGRES_CONNECT_TIMEOUT, POSTGRES_LOCK_TIMEOUT, POSTGRES_STATEMENT_TIMEOUT};
use crate::live_deadline::LIVE_DEADLINE;

/// A directory laid out as `pg-test-server.sh` writes it: the CA, and the
/// password `p`.
fn server_dir() -> tempfile::TempDir {
    let dir = tempfile::tempdir().expect("dir");
    file(dir.path(), "ca.pem", pki().ca.as_bytes(), 0o644);
    file(dir.path(), "password", b"p\n", 0o644);
    dir
}

/// One connection with `live_settings` for `port`, given `deadline`, and
/// how long it took to be refused.
async fn refused_within(port: u16, deadline: Duration) -> (String, Duration) {
    let dir = server_dir();
    let (_tmp, mut s) = live_settings(dir.path(), port, "vcon_app", deadline);
    s.hosts = vec!["127.0.0.1".to_owned()];
    let started = Instant::now();
    let message = why(connect(&s).await.expect_err("refused"));
    (message, started.elapsed())
}

mod success {
    use super::*;

    #[test]
    fn the_live_settings_give_each_connection_the_deadline_passed() {
        let dir = server_dir();
        let (_tmp, s) = live_settings(dir.path(), 5432, "vcon_app", Duration::from_secs(7));
        assert_eq!(
            s.connect_timeout,
            Duration::from_secs(7),
            "the deadline passed, not the server's default"
        );
    }

    #[test]
    fn the_live_settings_give_each_connection_the_live_deadline() {
        let dir = server_dir();
        let (_tmp, s) = live_settings(dir.path(), 5432, "vcon_app", LIVE_DEADLINE);
        assert_eq!(s.connect_timeout, LIVE_DEADLINE, "the shared live deadline");
    }

    #[test]
    fn the_live_settings_log_in_to_localhost_at_the_port_as_the_user() {
        let dir = server_dir();
        let (_tmp, s) = live_settings(dir.path(), 55432, "vcon_migrate", LIVE_DEADLINE);
        assert_eq!(s.hosts, vec!["localhost".to_owned()], "the host");
        assert_eq!(s.port, 55432, "the port");
        assert_eq!(s.user, "vcon_migrate", "the role");
        assert_eq!(s.dbname, "vcon", "the server's own database");
    }

    #[test]
    fn the_live_settings_trust_the_servers_ca() {
        let dir = server_dir();
        let (_tmp, s) = live_settings(dir.path(), 5432, "vcon_app", LIVE_DEADLINE);
        assert_eq!(
            s.ca_file,
            Some(dir.path().join("ca.pem")),
            "the servers' CA"
        );
    }

    #[test]
    fn the_live_settings_hold_a_private_copy_of_the_password() {
        use std::os::unix::fs::PermissionsExt;

        let dir = server_dir();
        let (tmp, s) = live_settings(dir.path(), 5432, "vcon_app", LIVE_DEADLINE);
        let copy = s.password_file.clone().expect("a password file");
        assert!(
            copy.starts_with(tmp.path()),
            "in the settings' own directory"
        );
        assert_eq!(std::fs::read(&copy).expect("reads"), b"p\n", "the password");
        let mode = std::fs::metadata(&copy).expect("stat").permissions().mode() & 0o777;
        assert_eq!(
            mode, 0o600,
            "mode 0600, which the server requires: {mode:o}"
        );
    }

    #[test]
    fn a_connector_is_made_from_the_live_settings() {
        let dir = server_dir();
        let (_tmp, s) = live_settings(dir.path(), 5432, "vcon_app", LIVE_DEADLINE);
        let _made = connector(&s);
    }
}

mod failure {
    use super::*;

    #[tokio::test]
    async fn a_server_that_never_answers_is_given_up_at_a_one_second_deadline_by_name() {
        let addr = silent_server().await;
        let (message, _took) = refused_within(addr.port(), Duration::from_secs(1)).await;
        assert!(
            message.contains("catalog.postgres.connect_timeout (1 s)"),
            "names the one-second deadline: {message}"
        );
    }

    #[tokio::test]
    async fn a_server_that_never_answers_is_given_up_at_a_two_second_deadline_by_name() {
        let addr = silent_server().await;
        let (message, took) = refused_within(addr.port(), Duration::from_secs(2)).await;
        assert!(
            message.contains("catalog.postgres.connect_timeout (2 s)"),
            "names the two-second deadline: {message}"
        );
        assert!(
            took >= Duration::from_secs(2) && took < Duration::from_secs(6),
            "given up at the deadline passed: {took:?}"
        );
    }

    #[tokio::test]
    async fn a_one_second_deadline_is_not_given_up_before_it_passes() {
        let addr = silent_server().await;
        let (_message, took) = refused_within(addr.port(), Duration::from_secs(1)).await;
        assert!(
            took >= Duration::from_secs(1),
            "waited the whole deadline: {took:?}"
        );
    }

    #[tokio::test]
    async fn a_closed_port_is_refused_long_before_the_live_deadline() {
        let port = closed_port("127.0.0.1").await;
        let (message, took) = refused_within(port, LIVE_DEADLINE).await;
        assert!(
            !message.contains("connect_timeout"),
            "refused, not timed out: {message}"
        );
        assert!(took < Duration::from_secs(5), "at once: {took:?}");
    }

    #[tokio::test]
    async fn a_server_without_tls_is_refused_long_before_the_live_deadline() {
        let addr = no_tls_server().await;
        let (message, took) = refused_within(addr.port(), LIVE_DEADLINE).await;
        assert!(
            message.contains("server does not support TLS"),
            "refused for its TLS: {message}"
        );
        assert!(took < Duration::from_secs(5), "at once: {took:?}");
    }

    #[tokio::test]
    async fn a_pool_from_the_live_settings_waits_the_deadline_for_a_server_that_never_answers() {
        let addr = silent_server().await;
        let dir = server_dir();
        let (_tmp, mut s) =
            live_settings(dir.path(), addr.port(), "vcon_app", Duration::from_secs(1));
        s.hosts = vec!["127.0.0.1".to_owned()];
        let pool = Pool::new(connector(&s), 1, s.connect_timeout);
        let started = Instant::now();
        let refused = pool.get().await.expect_err("no answer");
        let took = started.elapsed();
        assert!(
            refused.to_string().contains("(1 s)"),
            "names the one-second deadline: {refused}"
        );
        assert!(
            took < Duration::from_secs(5),
            "given up at the deadline: {took:?}"
        );
    }

    #[test]
    #[should_panic(expected = "the test password")]
    fn a_server_directory_without_the_password_is_named() {
        let dir = tempfile::tempdir().expect("dir");
        let _settings = live_settings(dir.path(), 5432, "vcon_app", LIVE_DEADLINE);
    }
}

mod edge {
    use super::*;

    #[test]
    fn the_servers_connect_timeout_default_is_unchanged() {
        assert_eq!(
            POSTGRES_CONNECT_TIMEOUT,
            Duration::from_secs(5),
            "production fails fast at 5 s"
        );
    }

    #[test]
    fn the_settings_for_a_fake_server_keep_the_servers_default() {
        // The tests against small servers on the loopback address time
        // the server's own default; only the live helpers take the live
        // deadline.
        let dir = server_dir();
        let s = settings(
            "127.0.0.1",
            5432,
            "vcon_app",
            &dir.path().join("ca.pem"),
            None,
        );
        assert_eq!(s.connect_timeout, POSTGRES_CONNECT_TIMEOUT, "the default");
    }

    #[test]
    fn the_live_deadline_is_longer_than_the_servers_default() {
        assert!(
            LIVE_DEADLINE > POSTGRES_CONNECT_TIMEOUT,
            "a loaded host gets more than {POSTGRES_CONNECT_TIMEOUT:?}: {LIVE_DEADLINE:?}"
        );
    }

    #[test]
    fn only_the_connect_timeout_differs_from_the_servers_defaults() {
        let dir = server_dir();
        let (_tmp, s) = live_settings(dir.path(), 5432, "vcon_app", LIVE_DEADLINE);
        assert_eq!(
            s.statement_timeout, POSTGRES_STATEMENT_TIMEOUT,
            "statements"
        );
        assert_eq!(s.lock_timeout, POSTGRES_LOCK_TIMEOUT, "locks");
        assert_eq!(s.pool, crate::config::POSTGRES_POOL, "the pool");
    }

    #[test]
    fn a_sub_second_deadline_is_kept_as_given() {
        let dir = server_dir();
        let (_tmp, s) = live_settings(dir.path(), 5432, "vcon_app", Duration::from_millis(1500));
        assert_eq!(
            s.connect_timeout,
            Duration::from_millis(1500),
            "not rounded: the settings take any duration"
        );
    }

    #[test]
    fn two_live_settings_have_directories_of_their_own() {
        let dir = server_dir();
        let (a, sa) = live_settings(dir.path(), 5432, "vcon_app", LIVE_DEADLINE);
        let (b, sb) = live_settings(dir.path(), 5432, "vcon_app", LIVE_DEADLINE);
        assert_ne!(a.path(), b.path(), "two directories");
        assert_ne!(sa.password_file, sb.password_file, "two copies");
    }

    #[tokio::test]
    async fn the_one_millisecond_deadline_is_missed_at_once() {
        let addr = silent_server().await;
        let (message, took) = refused_within(addr.port(), Duration::from_millis(1)).await;
        assert!(
            message.contains("catalog.postgres.connect_timeout"),
            "timed out by the deadline: {message}"
        );
        assert!(took < Duration::from_secs(2), "at once: {took:?}");
    }
}

/// On the servers `scripts/pg-test-server.sh` starts.
#[cfg(feature = "backend-tests")]
mod on_a_server {
    use super::super::super::connect::Purpose;
    use super::super::live::{Login, as_user, with_certificate};
    use super::super::pool;
    use super::super::schema_tests::live::Db;
    use super::*;

    #[test]
    fn the_application_role_is_given_the_live_deadline() {
        let (_tmp, s) = as_user("vcon_app");
        assert_eq!(
            s.connect_timeout, LIVE_DEADLINE,
            "as_user reads the shared value"
        );
    }

    #[test]
    fn the_tester_is_given_the_live_deadline() {
        let (_tmp, s) = as_user("vcon_tester");
        assert_eq!(
            s.connect_timeout, LIVE_DEADLINE,
            "as_user reads the shared value"
        );
    }

    #[test]
    fn a_certificate_login_is_given_the_live_deadline() {
        let (_tmp, s) = with_certificate("vcon_cert", Login::CertificateAlone);
        assert_eq!(
            s.connect_timeout, LIVE_DEADLINE,
            "with_certificate reads the shared value"
        );
    }

    #[test]
    fn a_certificate_and_password_login_is_given_the_live_deadline() {
        let (_tmp, s) = with_certificate("vcon_cert", Login::CertificateAndSecret);
        assert_eq!(
            s.connect_timeout, LIVE_DEADLINE,
            "with_certificate reads the shared value"
        );
    }

    #[tokio::test]
    async fn a_test_database_is_reached_with_the_live_deadline() {
        let db = Db::fresh().await;
        let (_tmp, s) = db.settings("vcon_app");
        assert_eq!(
            s.connect_timeout, LIVE_DEADLINE,
            "Db::settings reads the shared value"
        );
        let connected = db
            .connect("vcon_tester", Purpose::Connect)
            .await
            .expect("the tester connects");
        drop(connected);
    }

    #[tokio::test]
    async fn the_primary_is_reached_within_the_live_deadline() {
        let (_tmp, s) = as_user("vcon_app");
        let started = Instant::now();
        let connected = connect(&s).await.expect("connects");
        assert!(
            started.elapsed() < LIVE_DEADLINE,
            "within the deadline: {:?}",
            started.elapsed()
        );
        drop(connected);
    }

    #[tokio::test]
    async fn a_pool_of_the_live_settings_waits_the_live_deadline_for_a_free_connection() {
        let (_tmp, s) = as_user("vcon_app");
        let pool = pool(&s);
        let lease = pool.get().await.expect("a connection");
        drop(lease);
        assert_eq!(
            s.connect_timeout, LIVE_DEADLINE,
            "the pool's wait is the deadline"
        );
    }
}

/// The waits for a state: `within_the_live_deadline`, here, and
/// `live::within_of` and `within_the_live_deadline_of` on the servers.
mod waits {
    use std::cell::Cell;

    use super::super::within_the_live_deadline;
    use super::*;

    #[tokio::test(start_paused = true)]
    async fn a_state_never_reached_is_waited_for_the_live_deadline() {
        let started = tokio::time::Instant::now();
        let held = within_the_live_deadline(|| false).await;
        assert!(!held, "it never held");
        assert_eq!(
            started.elapsed(),
            LIVE_DEADLINE,
            "the live deadline on the runtime's clock, not five seconds"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn a_state_reached_at_once_is_asked_once() {
        let asked = Cell::new(0);
        let held = within_the_live_deadline(|| {
            asked.set(asked.get() + 1);
            true
        })
        .await;
        assert!(held, "it held");
        assert_eq!(asked.get(), 1, "asked once");
    }

    #[tokio::test(start_paused = true)]
    async fn a_state_reached_later_ends_the_wait_there() {
        let started = tokio::time::Instant::now();
        let asked = Cell::new(0);
        let held = within_the_live_deadline(|| {
            asked.set(asked.get() + 1);
            asked.get() == 5
        })
        .await;
        assert!(held, "it held");
        assert!(
            started.elapsed() < Duration::from_secs(1),
            "four pauses of a tenth of a second: {:?}",
            started.elapsed()
        );
    }

    #[tokio::test(start_paused = true)]
    async fn the_wait_is_longer_than_the_old_five_seconds() {
        let started = tokio::time::Instant::now();
        let _held = within_the_live_deadline(|| false).await;
        assert!(
            started.elapsed() > Duration::from_secs(5),
            "a loaded host gets more than the old fixed 5 s: {:?}",
            started.elapsed()
        );
    }

    /// On the servers `scripts/pg-test-server.sh` starts.
    #[cfg(feature = "backend-tests")]
    mod on_a_server {
        use super::super::super::live::within_of;
        use super::super::super::schema_tests::live::Db;
        use super::*;

        #[tokio::test]
        async fn a_value_already_there_is_seen_at_once() {
            let db = Db::fresh().await;
            let admin = db.admin().await;
            let started = Instant::now();
            assert!(
                within_of(&admin, "SELECT 1", "1", LIVE_DEADLINE).await,
                "the value is there"
            );
            assert!(
                started.elapsed() < Duration::from_secs(5),
                "at once: {:?}",
                started.elapsed()
            );
        }

        #[tokio::test]
        async fn a_value_never_there_is_given_up_at_the_deadline_passed() {
            let db = Db::fresh().await;
            let admin = db.admin().await;
            let started = Instant::now();
            let seen = within_of(&admin, "SELECT 1", "2", Duration::from_secs(1)).await;
            let took = started.elapsed();
            assert!(!seen, "the value never comes");
            assert!(
                took >= Duration::from_secs(1) && took < Duration::from_secs(4),
                "given up at the one-second deadline passed, not a fixed count: {took:?}"
            );
        }

        #[tokio::test]
        async fn a_value_there_is_seen_within_a_short_deadline() {
            let db = Db::fresh().await;
            let admin = db.admin().await;
            assert!(
                within_of(&admin, "SELECT 1", "1", Duration::from_millis(200)).await,
                "seen at the first ask"
            );
        }
    }
}