2020-03-02 01:53:41 -05:00
|
|
|
// This file is part of Moonfire NVR, a security camera network video recorder.
|
2021-02-17 16:28:48 -05:00
|
|
|
// Copyright (C) 2020 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
|
|
|
|
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
|
2016-01-09 00:44:19 -05:00
|
|
|
|
2021-04-10 20:34:52 -04:00
|
|
|
//! Utilities for automated testing involving Moonfire NVR's persistence library.
|
|
|
|
//! Used for tests of both the `moonfire_db` crate itself and the `moonfire_nvr` crate.
|
|
|
|
|
2018-12-28 13:21:49 -05:00
|
|
|
use crate::db;
|
|
|
|
use crate::dir;
|
2021-02-17 01:15:54 -05:00
|
|
|
use crate::writer;
|
|
|
|
use base::clock::Clocks;
|
2018-02-12 01:45:51 -05:00
|
|
|
use fnv::FnvHashMap;
|
2016-11-30 14:17:46 -05:00
|
|
|
use std::env;
|
2019-07-25 00:52:55 -04:00
|
|
|
use std::sync::Arc;
|
2016-12-02 23:40:55 -05:00
|
|
|
use std::thread;
|
2021-05-17 16:08:01 -04:00
|
|
|
use tempfile::TempDir;
|
2016-12-02 23:40:55 -05:00
|
|
|
use uuid::Uuid;
|
2016-01-09 00:44:19 -05:00
|
|
|
|
2022-09-29 01:19:35 -04:00
|
|
|
static INIT: std::sync::Once = std::sync::Once::new();
|
2016-01-09 00:44:19 -05:00
|
|
|
|
2016-12-02 23:40:55 -05:00
|
|
|
/// id of the camera created by `TestDb::new` below.
|
|
|
|
pub const TEST_CAMERA_ID: i32 = 1;
|
2018-01-23 14:05:07 -05:00
|
|
|
pub const TEST_STREAM_ID: i32 = 1;
|
2016-12-02 23:40:55 -05:00
|
|
|
|
2019-07-22 01:40:01 -04:00
|
|
|
pub const TEST_VIDEO_SAMPLE_ENTRY_DATA: &[u8] =
|
|
|
|
b"\x00\x00\x00\x7D\x61\x76\x63\x31\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\
|
|
|
|
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07\x80\x04\x38\x00\x48\x00\x00\x00\x48\x00\x00\x00\x00\
|
|
|
|
\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
|
|
|
|
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18\xFF\xFF\x00\x00\x00\x27\x61\x76\
|
|
|
|
\x63\x43\x01\x4D\x00\x2A\xFF\xE1\x00\x10\x67\x4D\x00\x2A\x95\xA8\x1E\x00\x89\xF9\x66\xE0\x20\
|
|
|
|
\x20\x20\x40\x01\x00\x04\x68\xEE\x3C\x80";
|
|
|
|
|
2016-11-30 14:17:46 -05:00
|
|
|
/// Performs global initialization for tests.
|
|
|
|
/// * set up logging. (Note the output can be confusing unless `RUST_TEST_THREADS=1` is set in
|
|
|
|
/// the program's environment prior to running.)
|
|
|
|
/// * set `TZ=America/Los_Angeles` so that tests that care about calendar time get the expected
|
|
|
|
/// results regardless of machine setup.)
|
2018-11-26 00:31:50 -05:00
|
|
|
/// * use a fast but insecure password hashing format.
|
2016-11-30 14:17:46 -05:00
|
|
|
pub fn init() {
|
|
|
|
INIT.call_once(|| {
|
2023-08-08 21:24:50 -04:00
|
|
|
base::tracing_setup::install_for_tests();
|
2016-11-30 14:17:46 -05:00
|
|
|
env::set_var("TZ", "America/Los_Angeles");
|
|
|
|
time::tzset();
|
2018-12-28 13:21:49 -05:00
|
|
|
crate::auth::set_test_config();
|
2016-11-25 17:34:00 -05:00
|
|
|
});
|
|
|
|
}
|
2016-12-02 23:40:55 -05:00
|
|
|
|
2018-03-23 16:31:23 -04:00
|
|
|
pub struct TestDb<C: Clocks + Clone> {
|
|
|
|
pub db: Arc<db::Database<C>>,
|
2018-02-12 01:45:51 -05:00
|
|
|
pub dirs_by_stream_id: Arc<FnvHashMap<i32, Arc<dir::SampleFileDir>>>,
|
2021-09-23 18:55:53 -04:00
|
|
|
pub shutdown_tx: base::shutdown::Sender,
|
|
|
|
pub shutdown_rx: base::shutdown::Receiver,
|
2018-03-04 15:24:24 -05:00
|
|
|
pub syncer_channel: writer::SyncerChannel<::std::fs::File>,
|
2016-12-02 23:40:55 -05:00
|
|
|
pub syncer_join: thread::JoinHandle<()>,
|
2018-03-04 15:24:24 -05:00
|
|
|
pub tmpdir: TempDir,
|
2018-02-04 00:56:04 -05:00
|
|
|
pub test_camera_uuid: Uuid,
|
2016-12-02 23:40:55 -05:00
|
|
|
}
|
|
|
|
|
2018-03-23 16:31:23 -04:00
|
|
|
impl<C: Clocks + Clone> TestDb<C> {
|
2016-12-02 23:40:55 -05:00
|
|
|
/// Creates a test database with one camera.
|
2018-03-23 16:31:23 -04:00
|
|
|
pub fn new(clocks: C) -> Self {
|
2019-01-04 19:11:58 -05:00
|
|
|
Self::new_with_flush_if_sec(clocks, 0)
|
|
|
|
}
|
|
|
|
|
2021-09-10 19:31:03 -04:00
|
|
|
pub(crate) fn new_with_flush_if_sec(clocks: C, flush_if_sec: u32) -> Self {
|
2021-05-17 16:08:01 -04:00
|
|
|
let tmpdir = tempfile::Builder::new()
|
|
|
|
.prefix("moonfire-nvr-test")
|
|
|
|
.tempdir()
|
|
|
|
.unwrap();
|
2016-12-02 23:40:55 -05:00
|
|
|
|
2018-02-15 02:10:10 -05:00
|
|
|
let mut conn = rusqlite::Connection::open_in_memory().unwrap();
|
2018-03-23 16:31:23 -04:00
|
|
|
db::init(&mut conn).unwrap();
|
2018-03-09 20:41:53 -05:00
|
|
|
let db = Arc::new(db::Database::new(clocks, conn, true).unwrap());
|
2018-02-12 01:45:51 -05:00
|
|
|
let (test_camera_uuid, sample_file_dir_id);
|
2021-10-26 14:47:13 -04:00
|
|
|
let path = tmpdir.path().to_owned();
|
2018-02-12 01:45:51 -05:00
|
|
|
let dir;
|
2018-02-04 00:56:04 -05:00
|
|
|
{
|
|
|
|
let mut l = db.lock();
|
2021-05-17 17:31:50 -04:00
|
|
|
sample_file_dir_id = l.add_sample_file_dir(path).unwrap();
|
2021-02-17 01:15:54 -05:00
|
|
|
assert_eq!(
|
|
|
|
TEST_CAMERA_ID,
|
|
|
|
l.add_camera(db::CameraChange {
|
|
|
|
short_name: "test camera".to_owned(),
|
2021-09-10 19:31:03 -04:00
|
|
|
config: crate::json::CameraConfig::default(),
|
|
|
|
//description: "".to_owned(),
|
|
|
|
//onvif_host: "test-camera".to_owned(),
|
|
|
|
//username: Some("foo".to_owned()),
|
|
|
|
//password: Some("bar".to_owned()),
|
2021-02-17 01:15:54 -05:00
|
|
|
streams: [
|
|
|
|
db::StreamChange {
|
|
|
|
sample_file_dir_id: Some(sample_file_dir_id),
|
2021-09-10 19:31:03 -04:00
|
|
|
config: crate::json::StreamConfig {
|
|
|
|
url: Some(url::Url::parse("rtsp://test-camera/main").unwrap()),
|
|
|
|
mode: crate::json::STREAM_MODE_RECORD.to_owned(),
|
|
|
|
flush_if_sec,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2021-02-17 01:15:54 -05:00
|
|
|
},
|
|
|
|
Default::default(),
|
2021-09-10 19:31:03 -04:00
|
|
|
Default::default(),
|
2021-02-17 01:15:54 -05:00
|
|
|
],
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
);
|
2018-02-22 19:35:34 -05:00
|
|
|
test_camera_uuid = l.cameras_by_id().get(&TEST_CAMERA_ID).unwrap().uuid;
|
|
|
|
l.update_retention(&[db::RetentionChange {
|
|
|
|
stream_id: TEST_STREAM_ID,
|
|
|
|
new_record: true,
|
|
|
|
new_limit: 1048576,
|
2021-02-17 01:15:54 -05:00
|
|
|
}])
|
|
|
|
.unwrap();
|
|
|
|
dir = l
|
|
|
|
.sample_file_dirs_by_id()
|
|
|
|
.get(&sample_file_dir_id)
|
|
|
|
.unwrap()
|
|
|
|
.get()
|
|
|
|
.unwrap();
|
2018-02-04 00:56:04 -05:00
|
|
|
}
|
2018-02-12 01:45:51 -05:00
|
|
|
let mut dirs_by_stream_id = FnvHashMap::default();
|
2021-05-17 17:31:50 -04:00
|
|
|
dirs_by_stream_id.insert(TEST_STREAM_ID, dir);
|
2021-09-23 18:55:53 -04:00
|
|
|
let (shutdown_tx, shutdown_rx) = base::shutdown::channel();
|
2018-02-20 13:11:10 -05:00
|
|
|
let (syncer_channel, syncer_join) =
|
2021-09-23 18:55:53 -04:00
|
|
|
writer::start_syncer(db.clone(), shutdown_rx.clone(), sample_file_dir_id).unwrap();
|
2018-02-04 00:56:04 -05:00
|
|
|
TestDb {
|
|
|
|
db,
|
2018-02-12 01:45:51 -05:00
|
|
|
dirs_by_stream_id: Arc::new(dirs_by_stream_id),
|
2021-09-23 18:55:53 -04:00
|
|
|
shutdown_tx,
|
|
|
|
shutdown_rx,
|
2018-02-04 00:56:04 -05:00
|
|
|
syncer_channel,
|
|
|
|
syncer_join,
|
|
|
|
tmpdir,
|
|
|
|
test_camera_uuid,
|
2016-12-02 23:40:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 18:40:32 -05:00
|
|
|
/// Creates a recording with a fresh `RecordingToInsert` row which has been touched only by
|
|
|
|
/// a `SampleIndexEncoder`. Fills in a video sample entry id and such to make it valid.
|
|
|
|
/// There will no backing sample file, so it won't be possible to generate a full `.mp4`.
|
2021-02-17 01:15:54 -05:00
|
|
|
pub fn insert_recording_from_encoder(&self, r: db::RecordingToInsert) -> db::ListRecordingsRow {
|
2018-12-28 13:21:49 -05:00
|
|
|
use crate::recording::{self, TIME_UNITS_PER_SEC};
|
2016-12-02 23:40:55 -05:00
|
|
|
let mut db = self.db.lock();
|
2021-02-17 01:15:54 -05:00
|
|
|
let video_sample_entry_id = db
|
|
|
|
.insert_video_sample_entry(db::VideoSampleEntryToInsert {
|
|
|
|
width: 1920,
|
|
|
|
height: 1080,
|
|
|
|
pasp_h_spacing: 1,
|
|
|
|
pasp_v_spacing: 1,
|
|
|
|
data: [0u8; 100].to_vec(),
|
|
|
|
rfc6381_codec: "avc1.000000".to_owned(),
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
let (id, _) = db
|
|
|
|
.add_recording(
|
|
|
|
TEST_STREAM_ID,
|
|
|
|
db::RecordingToInsert {
|
|
|
|
start: recording::Time(1430006400i64 * TIME_UNITS_PER_SEC),
|
|
|
|
video_sample_entry_id,
|
|
|
|
wall_duration_90k: r.media_duration_90k,
|
|
|
|
..r
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-03-01 16:50:59 -05:00
|
|
|
db.mark_synced(id).unwrap();
|
2018-02-22 19:35:34 -05:00
|
|
|
db.flush("create_recording_from_encoder").unwrap();
|
2016-12-02 23:40:55 -05:00
|
|
|
let mut row = None;
|
2021-02-17 01:15:54 -05:00
|
|
|
db.list_recordings_by_id(
|
|
|
|
TEST_STREAM_ID,
|
|
|
|
id.recording()..id.recording() + 1,
|
|
|
|
&mut |r| {
|
|
|
|
row = Some(r);
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2016-12-02 23:40:55 -05:00
|
|
|
row.unwrap()
|
|
|
|
}
|
|
|
|
}
|
2017-02-12 23:37:03 -05:00
|
|
|
|
|
|
|
// For benchmarking
|
2021-02-17 01:15:54 -05:00
|
|
|
#[cfg(feature = "nightly")]
|
2017-02-12 23:37:03 -05:00
|
|
|
pub fn add_dummy_recordings_to_db(db: &db::Database, num: usize) {
|
2018-12-28 13:21:49 -05:00
|
|
|
use crate::recording::{self, TIME_UNITS_PER_SEC};
|
2017-02-12 23:37:03 -05:00
|
|
|
let mut data = Vec::new();
|
|
|
|
data.extend_from_slice(include_bytes!("testdata/video_sample_index.bin"));
|
|
|
|
let mut db = db.lock();
|
2021-02-17 01:15:54 -05:00
|
|
|
let video_sample_entry_id = db
|
|
|
|
.insert_video_sample_entry(db::VideoSampleEntryToInsert {
|
|
|
|
width: 1920,
|
|
|
|
height: 1080,
|
|
|
|
pasp_h_spacing: 1,
|
|
|
|
pasp_v_spacing: 1,
|
|
|
|
data: [0u8; 100].to_vec(),
|
|
|
|
rfc6381_codec: "avc1.000000".to_owned(),
|
|
|
|
})
|
|
|
|
.unwrap();
|
2018-02-20 13:11:10 -05:00
|
|
|
let mut recording = db::RecordingToInsert {
|
2017-02-12 23:37:03 -05:00
|
|
|
sample_file_bytes: 30104460,
|
2018-03-02 18:40:32 -05:00
|
|
|
start: recording::Time(1430006400i64 * TIME_UNITS_PER_SEC),
|
2020-08-07 18:30:22 -04:00
|
|
|
media_duration_90k: 5399985,
|
|
|
|
wall_duration_90k: 5399985,
|
2017-02-12 23:37:03 -05:00
|
|
|
video_samples: 1800,
|
|
|
|
video_sync_samples: 60,
|
|
|
|
video_sample_entry_id: video_sample_entry_id,
|
|
|
|
video_index: data,
|
|
|
|
run_offset: 0,
|
2018-03-02 18:40:32 -05:00
|
|
|
..Default::default()
|
2017-02-12 23:37:03 -05:00
|
|
|
};
|
2018-02-23 00:57:43 -05:00
|
|
|
for _ in 0..num {
|
2018-03-02 18:40:32 -05:00
|
|
|
let (id, _) = db.add_recording(TEST_STREAM_ID, recording.clone()).unwrap();
|
2020-08-07 18:30:22 -04:00
|
|
|
recording.start += recording::Duration(recording.wall_duration_90k as i64);
|
2017-02-12 23:37:03 -05:00
|
|
|
recording.run_offset += 1;
|
2018-03-01 16:50:59 -05:00
|
|
|
db.mark_synced(id).unwrap();
|
2017-02-12 23:37:03 -05:00
|
|
|
}
|
2018-02-23 00:57:43 -05:00
|
|
|
db.flush("add_dummy_recordings_to_db").unwrap();
|
2017-02-12 23:37:03 -05:00
|
|
|
}
|