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
// Copyright 2019 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 anyhow::{format_err, Error};
use base64::engine::{general_purpose::STANDARD as BASE64_STANDARD, Engine as _};
use serde_json::{to_value, Value};
use std::{fs, io, path::Path};

use super::types::*;

/// Facade providing access to session testing interfaces.
#[derive(Debug)]
pub struct FileFacade;

impl FileFacade {
    pub fn new() -> Self {
        Self
    }

    /// Deletes the given path, which must be a file. Returns OK(NotFound) if the file does not
    /// exist.
    pub async fn delete_file(&self, args: Value) -> Result<DeleteFileResult, Error> {
        let path = args.get("path").ok_or(format_err!("DeleteFile failed, no path"))?;
        let path = path.as_str().ok_or(format_err!("DeleteFile failed, path not string"))?;

        match fs::remove_file(path) {
            Ok(()) => Ok(DeleteFileResult::Success),
            Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(DeleteFileResult::NotFound),
            Err(e) => Err(e.into()),
        }
    }

    /// Creates a new directory. Returns OK(AlreadyExists) if the directory already exists.
    pub async fn make_dir(&self, args: Value) -> Result<MakeDirResult, Error> {
        let path = args.get("path").ok_or(format_err!("MakeDir failed, no path"))?;
        let path = path.as_str().ok_or(format_err!("MakeDir failed, path not string"))?;
        let path = Path::new(path);

        let recurse = args["recurse"].as_bool().unwrap_or(false);

        if path.is_dir() {
            return Ok(MakeDirResult::AlreadyExists);
        }

        if recurse {
            fs::create_dir_all(path)?;
        } else {
            fs::create_dir(path)?;
        }
        Ok(MakeDirResult::Success)
    }

    /// Given a source file, fetches its contents.
    pub async fn read_file(&self, args: Value) -> Result<Value, Error> {
        let path = args.get("path").ok_or(format_err!("ReadFile failed, no path"))?;
        let path = path.as_str().ok_or(format_err!("ReadFile failed, path not string"))?;

        let contents = fs::read(path)?;
        let encoded_contents = BASE64_STANDARD.encode(&contents);

        Ok(to_value(encoded_contents)?)
    }

    /// Given data and the destination, it creates a new file and
    /// puts it in the corresponding path (given by the destination).
    pub async fn write_file(&self, args: Value) -> Result<WriteFileResult, Error> {
        let data = args.get("data").ok_or(format_err!("WriteFile failed, no data"))?;
        let data = data.as_str().ok_or(format_err!("WriteFile failed, data not string"))?;

        let contents = BASE64_STANDARD.decode(data)?;

        let destination =
            args.get("dst").ok_or(format_err!("WriteFile failed, no destination path given"))?;
        let destination =
            destination.as_str().ok_or(format_err!("WriteFile failed, destination not string"))?;

        fs::write(destination, &contents)?;
        Ok(WriteFileResult::Success)
    }

    /// Returns metadata for the given path. Returns Ok(NotFound) if the path does not exist.
    pub async fn stat(&self, args: Value) -> Result<StatResult, Error> {
        let path = args.get("path").ok_or(format_err!("Stat failed, no path"))?;
        let path = path.as_str().ok_or(format_err!("Stat failed, path not string"))?;

        let metadata = match fs::metadata(path) {
            Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(StatResult::NotFound),
            res => res,
        }?;

        Ok(StatResult::Success(Metadata {
            kind: if metadata.is_dir() {
                NodeKind::Directory
            } else if metadata.is_file() {
                NodeKind::File
            } else {
                NodeKind::Other
            },
            size: metadata.len(),
        }))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use assert_matches::assert_matches;
    use serde_json::json;

    #[fuchsia_async::run_singlethreaded(test)]
    async fn delete_file_ok() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("test.txt");
        fs::write(&path, "hello world!".as_bytes()).unwrap();
        assert!(path.exists());

        assert_matches!(
            FileFacade.delete_file(json!({ "path": path })).await,
            Ok(DeleteFileResult::Success)
        );
        assert!(!path.exists());

        assert_matches!(
            FileFacade.delete_file(json!({ "path": path })).await,
            Ok(DeleteFileResult::NotFound)
        );
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn make_dir_ok() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("a");
        assert!(!path.exists());

        assert_matches!(
            FileFacade.make_dir(json!({ "path": path })).await,
            Ok(MakeDirResult::Success)
        );
        assert!(path.is_dir());

        assert_matches!(
            FileFacade.make_dir(json!({ "path": path })).await,
            Ok(MakeDirResult::AlreadyExists)
        );
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn make_dir_recurse_ok() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("a/b/c");
        assert!(!path.exists());

        assert_matches!(FileFacade.make_dir(json!({ "path": path })).await, Err(_));
        assert!(!path.exists());

        assert_matches!(
            FileFacade.make_dir(json!({ "path": path, "recurse": true })).await,
            Ok(MakeDirResult::Success)
        );
        assert!(path.is_dir());
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn read_file_ok() {
        const FILE_CONTENTS: &str = "hello world!";
        const FILE_CONTENTS_AS_BASE64: &str = "aGVsbG8gd29ybGQh";

        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("test.txt");
        fs::write(&path, FILE_CONTENTS.as_bytes()).unwrap();

        assert_matches!(
            FileFacade.read_file(json!({ "path": path })).await,
            Ok(value) if value == json!(FILE_CONTENTS_AS_BASE64)
        );
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn write_file_ok() {
        const FILE_CONTENTS: &str = "hello world!";
        const FILE_CONTENTS_AS_BASE64: &str = "aGVsbG8gd29ybGQh";

        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("test.txt");

        assert_matches!(
            FileFacade.write_file(json!({ "data": FILE_CONTENTS_AS_BASE64, "dst": path })).await,
            Ok(WriteFileResult::Success)
        );

        assert_eq!(fs::read_to_string(&path).unwrap(), FILE_CONTENTS);
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn write_file_unwritable_path() {
        const FILE_CONTENTS_AS_BASE64: &str = "aGVsbG8gd29ybGQh";

        assert_matches!(
            FileFacade
                .write_file(json!({ "data": FILE_CONTENTS_AS_BASE64, "dst": "/pkg/is/readonly" }))
                .await,
            Err(_)
        );
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn stat_file() {
        let temp = tempfile::tempdir().unwrap();
        let path = temp.path().join("test.txt");
        fs::write(&path, "hello world!".as_bytes()).unwrap();

        assert_matches!(
            FileFacade.stat(json!({ "path": path })).await,
            Ok(StatResult::Success(Metadata { kind: NodeKind::File, size: 12 }))
        );
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn stat_dir() {
        assert_matches!(
            FileFacade.stat(json!({ "path": "/pkg" })).await,
            Ok(StatResult::Success(Metadata { kind: NodeKind::Directory, size: 0 }))
        );
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn stat_not_found() {
        assert_matches!(
            FileFacade.stat(json!({ "path": "/the/ultimate/question" })).await,
            Ok(StatResult::NotFound)
        );
    }
}