aboutsummaryrefslogtreecommitdiff
path: root/tests/root_validation.rs
blob: 61263f2e7252c48dbd754563b8d852871fb12771 (plain)
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
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use std::path::PathBuf;
use std::time::Duration;
use tempfile::TempDir;

// Root directory validation edge case tests

#[test]
fn test_root_dir_is_file_not_directory() {
    let temp_dir = TempDir::new().unwrap();

    // Create a file instead of a directory
    let file_path = temp_dir.path().join("not_a_directory");
    fs::write(&file_path, "this is a file").unwrap();

    // Try to use the file as root directory - should fail
    let mut cmd = Command::cargo_bin("demon").unwrap();
    cmd.args(&[
        "run",
        "--root-dir",
        file_path.to_str().unwrap(),
        "test",
        "echo",
        "hello",
    ])
    .assert()
    .failure()
    .stderr(predicate::str::contains("not a directory"));
}

#[test]
fn test_root_dir_does_not_exist() {
    let temp_dir = TempDir::new().unwrap();

    // Use a non-existent path
    let nonexistent_path = temp_dir.path().join("does_not_exist");

    // Try to use non-existent path as root directory - should fail
    let mut cmd = Command::cargo_bin("demon").unwrap();
    cmd.args(&[
        "run",
        "--root-dir",
        nonexistent_path.to_str().unwrap(),
        "test",
        "echo",
        "hello",
    ])
    .assert()
    .failure()
    .stderr(predicate::str::contains("does not exist"));
}

#[test]
fn test_git_root_demon_dir_exists_as_file() {
    // Create a temporary git repo
    let temp_dir = TempDir::new().unwrap();
    let git_dir = temp_dir.path().join(".git");
    std::fs::create_dir(&git_dir).unwrap();

    // Create .demon as a FILE instead of directory
    let demon_file = temp_dir.path().join(".demon");
    fs::write(&demon_file, "this should be a directory").unwrap();

    // Change to the temp directory
    let original_dir = std::env::current_dir().unwrap();
    std::env::set_current_dir(temp_dir.path()).unwrap();

    // Restore directory when done
    struct DirGuard(PathBuf);
    impl Drop for DirGuard {
        fn drop(&mut self) {
            let _ = std::env::set_current_dir(&self.0);
        }
    }
    let _guard = DirGuard(original_dir);

    // Run command without --root-dir (should use git root and fail)
    let mut cmd = Command::cargo_bin("demon").unwrap();
    cmd.args(&["run", "test", "echo", "hello"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("exists but is not a directory"));
}

#[test]
fn test_git_root_demon_dir_permission_denied() {
    // This test is tricky to implement portably since it requires creating
    // a directory with restricted permissions. We'll create a more comprehensive
    // test that simulates the condition by creating a read-only parent directory.

    let temp_dir = TempDir::new().unwrap();
    let git_dir = temp_dir.path().join(".git");
    std::fs::create_dir(&git_dir).unwrap();

    // Create a subdirectory and make it read-only
    let subdir = temp_dir.path().join("subdir");
    std::fs::create_dir(&subdir).unwrap();
    let subdir_git = subdir.join(".git");
    std::fs::create_dir(&subdir_git).unwrap();

    // Make the subdirectory read-only (this should prevent .demon creation)
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = std::fs::metadata(&subdir).unwrap().permissions();
        perms.set_mode(0o444); // Read-only
        std::fs::set_permissions(&subdir, perms).unwrap();
    }

    // Change to the subdirectory
    let original_dir = std::env::current_dir().unwrap();

    // Handle the case where changing to the directory fails due to permissions
    if std::env::set_current_dir(&subdir).is_err() {
        // This is actually the expected behavior for a directory with insufficient permissions
        return;
    }

    // Restore directory and permissions when done
    struct TestGuard {
        original_dir: PathBuf,
        #[cfg(unix)]
        restore_path: PathBuf,
    }
    impl Drop for TestGuard {
        fn drop(&mut self) {
            let _ = std::env::set_current_dir(&self.original_dir);
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                if let Ok(mut perms) =
                    std::fs::metadata(&self.restore_path).map(|m| m.permissions())
                {
                    perms.set_mode(0o755);
                    let _ = std::fs::set_permissions(&self.restore_path, perms);
                }
            }
        }
    }
    let _guard = TestGuard {
        original_dir,
        #[cfg(unix)]
        restore_path: subdir.clone(),
    };

    // Run command without --root-dir - should fail due to permission denied
    #[cfg(unix)]
    {
        let mut cmd = Command::cargo_bin("demon").unwrap();
        cmd.args(&["run", "test", "echo", "hello"])
            .assert()
            .failure()
            .stderr(predicate::str::contains(
                "Failed to create daemon directory",
            ));
    }
}

#[test]
fn test_no_git_root_and_no_root_dir() {
    // Create a temporary directory that's NOT a git repository
    let temp_dir = TempDir::new().unwrap();

    // Change to the temp directory
    let original_dir = std::env::current_dir().unwrap();
    std::env::set_current_dir(temp_dir.path()).unwrap();

    // Restore directory when done
    struct DirGuard(PathBuf);
    impl Drop for DirGuard {
        fn drop(&mut self) {
            let _ = std::env::set_current_dir(&self.0);
        }
    }
    let _guard = DirGuard(original_dir);

    // Run command without --root-dir and outside git repo - should fail
    let mut cmd = Command::cargo_bin("demon").unwrap();
    cmd.args(&["run", "test", "echo", "hello"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("No git repository found"));
}

#[test]
fn test_invalid_utf8_path_handling() {
    // This test checks handling of paths with invalid UTF-8 characters
    // This is primarily relevant on Unix systems where paths can contain arbitrary bytes

    // Try to use a path with null bytes (should be invalid on most systems)
    // We expect this to fail at the OS level before reaching our validation code
    let result = std::panic::catch_unwind(|| {
        let mut cmd = Command::cargo_bin("demon").unwrap();
        cmd.args(&[
            "run",
            "--root-dir",
            "path\0with\0nulls",
            "test",
            "echo",
            "hello",
        ])
        .assert()
        .failure();
    });
    // Either the command fails (good) or it panics due to null bytes (also expected)
    // This documents that our validation doesn't need to handle null bytes since the OS catches them
    if result.is_err() {
        // The test environment caught the null byte issue, which is expected behavior
        return;
    }
}

#[test]
fn test_deeply_nested_nonexistent_path() {
    let temp_dir = TempDir::new().unwrap();

    // Create a path with many levels that don't exist
    let deep_path = temp_dir
        .path()
        .join("does")
        .join("not")
        .join("exist")
        .join("at")
        .join("all")
        .join("very")
        .join("deep")
        .join("path");

    let mut cmd = Command::cargo_bin("demon").unwrap();
    cmd.args(&[
        "run",
        "--root-dir",
        deep_path.to_str().unwrap(),
        "test",
        "echo",
        "hello",
    ])
    .assert()
    .failure()
    .stderr(predicate::str::contains("does not exist"));
}

#[test]
fn test_root_dir_is_symlink_to_directory() {
    let temp_dir = TempDir::new().unwrap();

    // Create a real directory
    let real_dir = temp_dir.path().join("real_directory");
    std::fs::create_dir(&real_dir).unwrap();

    // Create a symlink to it (on systems that support it)
    let symlink_path = temp_dir.path().join("symlink_to_dir");

    #[cfg(unix)]
    {
        std::os::unix::fs::symlink(&real_dir, &symlink_path).unwrap();

        // Using symlink as root dir should work (following the symlink)
        let mut cmd = Command::cargo_bin("demon").unwrap();
        cmd.args(&[
            "run",
            "--root-dir",
            symlink_path.to_str().unwrap(),
            "test",
            "echo",
            "hello",
        ])
        .assert()
        .success();

        // Verify files were created in the real directory (following symlink)
        std::thread::sleep(Duration::from_millis(100));
        assert!(real_dir.join("test.pid").exists());
        assert!(real_dir.join("test.stdout").exists());
        assert!(real_dir.join("test.stderr").exists());
    }
}

#[test]
fn test_root_dir_is_symlink_to_file() {
    let temp_dir = TempDir::new().unwrap();

    // Create a regular file
    let regular_file = temp_dir.path().join("regular_file");
    fs::write(&regular_file, "content").unwrap();

    // Create a symlink to the file
    let symlink_path = temp_dir.path().join("symlink_to_file");

    #[cfg(unix)]
    {
        std::os::unix::fs::symlink(&regular_file, &symlink_path).unwrap();

        // Using symlink to file as root dir should fail
        let mut cmd = Command::cargo_bin("demon").unwrap();
        cmd.args(&[
            "run",
            "--root-dir",
            symlink_path.to_str().unwrap(),
            "test",
            "echo",
            "hello",
        ])
        .assert()
        .failure()
        .stderr(predicate::str::contains("not a directory"));
    }
}

#[test]
fn test_root_dir_is_broken_symlink() {
    let temp_dir = TempDir::new().unwrap();

    // Create a symlink to a non-existent target
    let broken_symlink = temp_dir.path().join("broken_symlink");

    #[cfg(unix)]
    {
        std::os::unix::fs::symlink("nonexistent_target", &broken_symlink).unwrap();

        // Using broken symlink as root dir should fail
        let mut cmd = Command::cargo_bin("demon").unwrap();
        cmd.args(&[
            "run",
            "--root-dir",
            broken_symlink.to_str().unwrap(),
            "test",
            "echo",
            "hello",
        ])
        .assert()
        .failure()
        .stderr(predicate::str::contains("does not exist"));
    }
}