summaryrefslogtreecommitdiff
path: root/rust/libnewsboat/tests/configpaths_helpers/mod.rs
blob: 13f036e93e3eb241c74dfe2034b5295421244bb7 (plain) (blame)
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
// Each integration test uses only a handful of the functions here, and generates the "unused code"
// warnings for the rest. This is annoying and useless, so we suppress the warning.
#![allow(dead_code)]

// `libc` contains file mode constants, like S_IXUSR, which are useful for `struct Chmod`. Thus it
// makes sense to re-export the crate.
pub use libc;

use libnewsboat::configpaths::ConfigPaths;
use std::io::{Read, Write};
use std::os::unix::fs::PermissionsExt;
use std::{fs, path};
use tempfile::TempDir;

/// Strings that are placed in files before running commands, to see if commands modify those
/// files.
pub struct FileSentries {
    /// Sentry for the config file.
    pub config: String,
    /// Sentry for the urls file.
    pub urls: String,
    /// Sentry for the cache file.
    pub cache: String,
    /// Sentry for the queue file.
    pub queue: String,
    /// Sentry for the search history file.
    pub search: String,
    /// Sentry for the command-line history file.
    pub cmdline: String,
}

impl FileSentries {
    /// Create a new struct with random strings for sentries.
    pub fn new() -> FileSentries {
        FileSentries {
            config: fastrand::u32(..).to_string() + "config",
            urls: fastrand::u32(..).to_string() + "urls",
            cache: fastrand::u32(..).to_string() + "cache",
            queue: fastrand::u32(..).to_string() + "queue",
            search: fastrand::u32(..).to_string() + "search",
            cmdline: fastrand::u32(..).to_string() + "cmdline",
        }
    }
}

fn mock_dotdir(dotdir_path: &path::Path, sentries: &FileSentries) {
    assert!(fs::create_dir(&dotdir_path).is_ok());
    assert!(create_file(&dotdir_path.join("config"), &sentries.config));
    assert!(create_file(&dotdir_path.join("urls"), &sentries.urls));
    assert!(create_file(&dotdir_path.join("cache.db"), &sentries.cache));
    assert!(create_file(&dotdir_path.join("queue"), &sentries.queue));
    assert!(create_file(
        &dotdir_path.join("history.search"),
        &sentries.search
    ));
    assert!(create_file(
        &dotdir_path.join("history.cmdline"),
        &sentries.cmdline
    ));
}

pub fn mock_newsbeuter_dotdir(tmp: &TempDir) -> FileSentries {
    let sentries = FileSentries::new();

    let dotdir_path = tmp.path().join(".newsbeuter");
    mock_dotdir(&dotdir_path, &sentries);

    sentries
}

pub fn mock_newsboat_dotdir(tmp: &TempDir) -> FileSentries {
    let sentries = FileSentries::new();

    let dotdir_path = tmp.path().join(".newsboat");
    mock_dotdir(&dotdir_path, &sentries);

    sentries
}

pub fn mock_xdg_dirs(config_dir: &path::Path, data_dir: &path::Path) -> FileSentries {
    let sentries = FileSentries::new();

    assert!(fs::create_dir_all(&config_dir).is_ok());
    assert!(create_file(&config_dir.join("config"), &sentries.config));
    assert!(create_file(&config_dir.join("urls"), &sentries.urls));

    assert!(fs::create_dir_all(&data_dir).is_ok());
    assert!(create_file(&data_dir.join("cache.db"), &sentries.cache));
    assert!(create_file(&data_dir.join("queue"), &sentries.queue));
    assert!(create_file(
        &data_dir.join("history.search"),
        &sentries.search
    ));
    assert!(create_file(
        &data_dir.join("history.cmdline"),
        &sentries.cmdline
    ));

    sentries
}

pub fn mock_newsbeuter_xdg_dirs(tmp: &TempDir) -> FileSentries {
    let config_dir_path = tmp.path().join(".config").join("newsbeuter");
    let data_dir_path = tmp.path().join(".local").join("share").join("newsbeuter");
    mock_xdg_dirs(&config_dir_path, &data_dir_path)
}

pub fn mock_newsboat_xdg_dirs(tmp: &TempDir) -> FileSentries {
    let config_dir_path = tmp.path().join(".config").join("newsboat");
    let data_dir_path = tmp.path().join(".local").join("share").join("newsboat");
    mock_xdg_dirs(&config_dir_path, &data_dir_path)
}

/// Creates a file at given `filepath` and writes `content` into it.
///
/// Returns `true` if successful, `false` otherwise.
pub fn create_file(path: &path::Path, content: &str) -> bool {
    fs::File::create(path)
        .and_then(|mut f| f.write_all(content.as_bytes()))
        .is_ok()
}

pub fn file_contents(path: &path::Path) -> String {
    fs::File::open(path)
        .map(|mut f| {
            let mut buf = String::new();
            let _ = f.read_to_string(&mut buf);
            buf
        })
        // If failed to open/read file, return an empty string
        .unwrap_or_else(|_| String::new())
}

fn is_readable(filepath: &path::Path) -> bool {
    fs::File::open(&filepath).is_ok()
}

pub fn assert_xdg_not_migrated(config_dir: &path::Path, data_dir: &path::Path) {
    let mut paths = ConfigPaths::new();
    assert!(paths.initialized());

    // Shouldn't migrate anything, so should return false.
    assert!(!paths.try_migrate_from_newsbeuter());

    assert!(!is_readable(&config_dir.join("config")));
    assert!(!is_readable(&config_dir.join("urls")));

    assert!(!is_readable(&data_dir.join("cache.db")));
    assert!(!is_readable(&data_dir.join("queue")));
    assert!(!is_readable(&data_dir.join("history.search")));
    assert!(!is_readable(&data_dir.join("history.cmdline")));
}

/// Sets new permissions on a given path, and restores them back when the
/// object is destroyed.
pub struct Chmod<'a> {
    path: &'a path::Path,
    original_mode: u32,
}

impl<'a> Chmod<'a> {
    pub fn new(path: &'a path::Path, new_mode: libc::mode_t) -> Chmod<'a> {
        let original_mode = fs::metadata(path)
            .unwrap_or_else(|_| panic!("Chmod: couldn't obtain metadata for `{}'", path.display()))
            .permissions()
            .mode();

        // `from_mode` takes `u32`, but `libc::mode_t` is either `u16` (macOS, FreeBSD) or `u32`
        // (Linux). Suppress the warning to prevent Clippy on Linux from complaining.
        #[allow(clippy::useless_conversion)]
        fs::set_permissions(path, fs::Permissions::from_mode(new_mode.into()))
            .unwrap_or_else(|_| panic!("Chmod: couldn't change mode for `{}'", path.display()));

        Chmod {
            path,
            original_mode,
        }
    }
}

impl<'a> Drop for Chmod<'a> {
    fn drop(&mut self) {
        fs::set_permissions(self.path, fs::Permissions::from_mode(self.original_mode))
            .unwrap_or_else(|_| {
                panic!(
                    "Chmod: couldn't change back the mode for `{}'",
                    self.path.display()
                )
            });
    }
}

pub fn assert_dotdir_not_migrated(dotdir: &path::Path) {
    let mut paths = ConfigPaths::new();
    assert!(paths.initialized());

    // Shouldn't migrate anything, so should return false.
    assert!(!paths.try_migrate_from_newsbeuter());

    assert!(!is_readable(&dotdir.join("config")));
    assert!(!is_readable(&dotdir.join("urls")));

    assert!(!is_readable(&dotdir.join("cache.db")));
    assert!(!is_readable(&dotdir.join("queue")));
    assert!(!is_readable(&dotdir.join("history.search")));
    assert!(!is_readable(&dotdir.join("history.cmdline")));
}

pub fn assert_create_dirs_returns_false(tmp: &TempDir) {
    let readonly = libc::S_IRUSR | libc::S_IXUSR;
    let _home_chmod = Chmod::new(tmp.path(), readonly);

    let paths = ConfigPaths::new();
    assert!(paths.initialized());
    assert!(!paths.create_dirs());
}