settings/
storage_migrations.rs

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
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::base::SettingType;
use crate::migration::{MigrationError, MigrationManager, MigrationManagerBuilder};
use fidl_fuchsia_io::DirectoryProxy;
use fidl_fuchsia_stash::StoreProxy;
use std::collections::HashSet;
use v1653667208_light_migration::V1653667208LightMigration;
use v1653667210_light_migration_teardown::V1653667210LightMigrationTeardown;

mod v1653667208_light_migration;
mod v1653667210_light_migration_teardown;

pub(crate) fn register_migrations(
    settings: &HashSet<SettingType>,
    migration_dir: DirectoryProxy,
    store_proxy: StoreProxy,
) -> Result<MigrationManager, MigrationError> {
    let mut builder = MigrationManagerBuilder::new();
    builder.set_migration_dir(migration_dir);
    if settings.contains(&SettingType::Light) {
        builder.register(V1653667208LightMigration(store_proxy.clone()))?;
        builder.register(V1653667210LightMigrationTeardown(store_proxy))?;
    }
    Ok(builder.build())
}

#[cfg(test)]
mod tests {
    use super::*;
    use fidl::endpoints::create_proxy;
    use fidl_fuchsia_io::DirectoryMarker;
    use fidl_fuchsia_stash::StoreMarker;
    use fuchsia_async as fasync;

    // Run migration registration with all settings and policies turned on so we can ensure there's
    // no issues with registering any of the migrations.
    #[fasync::run_until_stalled(test)]
    async fn ensure_unique_ids() {
        let mut settings = HashSet::new();
        let _ = settings.insert(SettingType::Accessibility);
        let _ = settings.insert(SettingType::Audio);
        let _ = settings.insert(SettingType::Display);
        let _ = settings.insert(SettingType::DoNotDisturb);
        let _ = settings.insert(SettingType::FactoryReset);
        let _ = settings.insert(SettingType::Input);
        let _ = settings.insert(SettingType::Intl);
        let _ = settings.insert(SettingType::Keyboard);
        let _ = settings.insert(SettingType::Light);
        let _ = settings.insert(SettingType::NightMode);
        let _ = settings.insert(SettingType::Privacy);
        let _ = settings.insert(SettingType::Setup);
        let (directory_proxy, _) = create_proxy::<DirectoryMarker>();
        let (store_proxy, _) = create_proxy::<StoreMarker>();
        if let Err(e) = register_migrations(&settings, directory_proxy, store_proxy) {
            panic!("Unable to register migrations: Err({e:?})");
        }
    }

    // Opens a FIDL connection to a `TempDir`.
    pub(crate) fn open_tempdir(tempdir: &tempfile::TempDir) -> DirectoryProxy {
        fuchsia_fs::directory::open_in_namespace(
            tempdir.path().to_str().expect("tempdir path is not valid UTF-8"),
            fuchsia_fs::PERM_READABLE | fuchsia_fs::PERM_WRITABLE,
        )
        .expect("failed to open connection to tempdir")
    }
}