File: input_arg.rs

package info (click to toggle)
thin-provisioning-tools 1.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,300 kB
  • sloc: xml: 544; sh: 521; makefile: 133
file content (366 lines) | stat: -rw-r--r-- 9,065 bytes parent folder | download | duplicates (2)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
use anyhow::Result;
use std::ffi::OsStr;

use thinp::file_utils;

use crate::args;
use crate::common::fixture::*;
use crate::common::process::*;
use crate::common::program::*;
use crate::common::test_dir::*;
use crate::common::thin_xml_generator::{write_xml, FragmentedS};

//------------------------------------------
// wrappers

type ArgsBuilder = fn(&mut TestDir, &OsStr, &dyn Fn(&[&OsStr]) -> Result<()>) -> Result<()>;

fn with_output_md_untouched<'a, P>(
    td: &mut TestDir,
    input: &OsStr,
    thunk: &dyn Fn(&[&OsStr]) -> Result<()>,
) -> Result<()>
where
    P: Program<'a>,
{
    let output = mk_zeroed_md(td)?;
    ensure_untouched(&output, || {
        let mut args = args!["-i", input, "-o", &output].to_vec();
        args.extend(P::required_args().iter().map(OsStr::new));
        thunk(&args)
    })
}

fn with_output_superblock_zeroed<'a, P>(
    td: &mut TestDir,
    input: &OsStr,
    thunk: &dyn Fn(&[&OsStr]) -> Result<()>,
) -> Result<()>
where
    P: Program<'a>,
{
    let output = mk_zeroed_md(td)?;
    ensure_superblock_zeroed(&output, || {
        let mut args = args!["-i", input, "-o", &output].to_vec();
        args.extend(P::required_args().iter().map(OsStr::new));
        thunk(&args)
    })
}

fn with_output_simple<'a, P>(
    td: &mut TestDir,
    input: &OsStr,
    thunk: &dyn Fn(&[&OsStr]) -> Result<()>,
) -> Result<()>
where
    P: Program<'a>,
{
    let output = mk_zeroed_md(td)?;
    let mut args = args!["-i", input, "-o", &output].to_vec();
    args.extend(P::required_args().iter().map(OsStr::new));
    thunk(&args)
}

fn input_arg_only<'a, P>(
    _td: &mut TestDir,
    input: &OsStr,
    thunk: &dyn Fn(&[&OsStr]) -> Result<()>,
) -> Result<()>
where
    P: Program<'a>,
{
    let mut args = args![input].to_vec();
    args.extend(P::required_args().iter().map(OsStr::new));
    thunk(&args)
}

fn build_args_fn<'a, P>() -> Result<ArgsBuilder>
where
    P: Program<'a>,
{
    match P::arg_type() {
        ArgType::InputArg => Ok(input_arg_only::<P>),
        ArgType::IoOptions => Ok(with_output_md_untouched::<P>),
    }
}

//------------------------------------------
// test invalid arguments

pub fn test_missing_input_arg<'a, P>() -> Result<()>
where
    P: InputProgram<'a>,
{
    let stderr = run_fail(P::cmd(P::required_args()))?;
    assert!(stderr.contains(P::missing_input_arg()));
    Ok(())
}

#[macro_export]
macro_rules! test_missing_input_arg {
    ($program: ident) => {
        #[test]
        fn missing_input_arg() -> Result<()> {
            test_missing_input_arg::<$program>()
        }
    };
}

pub fn test_missing_input_option<'a, P>() -> Result<()>
where
    P: InputProgram<'a>,
{
    let mut td = TestDir::new()?;
    let output = mk_zeroed_md(&mut td)?;
    ensure_untouched(&output, || {
        let mut args = args!["-o", &output].to_vec();
        args.extend(P::required_args().iter().map(OsStr::new));
        let stderr = run_fail(P::cmd(&args))?;
        assert!(stderr.contains(P::missing_input_arg()));
        Ok(())
    })
}

#[macro_export]
macro_rules! test_missing_input_option {
    ($program: ident) => {
        #[test]
        fn missing_input_option() -> Result<()> {
            test_missing_input_option::<$program>()
        }
    };
}

pub fn test_input_file_not_found<'a, P>() -> Result<()>
where
    P: InputProgram<'a>,
{
    let mut td = TestDir::new()?;

    let wrapper = build_args_fn::<P>()?;
    wrapper(&mut td, "no-such-file".as_ref(), &|args: &[&OsStr]| {
        let stderr = run_fail(P::cmd(args))?;
        assert!(stderr.contains(P::file_not_found()));
        Ok(())
    })
}

#[macro_export]
macro_rules! test_input_file_not_found {
    ($program: ident) => {
        #[test]
        fn input_file_not_found() -> Result<()> {
            test_input_file_not_found::<$program>()
        }
    };
}

pub fn test_input_cannot_be_a_directory<'a, P>() -> Result<()>
where
    P: InputProgram<'a>,
{
    let mut td = TestDir::new()?;

    let wrapper = build_args_fn::<P>()?;
    wrapper(&mut td, "/tmp".as_ref(), &|args: &[&OsStr]| {
        let stderr = run_fail(P::cmd(args))?;
        assert!(stderr.contains("Not a block device or regular file"));
        Ok(())
    })
}

#[macro_export]
macro_rules! test_input_cannot_be_a_directory {
    ($program: ident) => {
        #[test]
        fn input_cannot_be_a_directory() -> Result<()> {
            test_input_cannot_be_a_directory::<$program>()
        }
    };
}

pub fn test_unreadable_input_file<'a, P>() -> Result<()>
where
    P: InputProgram<'a>,
{
    unsafe {
        if libc::getuid() == 0 {
            return Ok(());
        }
    }

    let mut td = TestDir::new()?;

    // input an unreadable file
    let input = P::mk_valid_input(&mut td)?;
    duct::cmd!("chmod", "-r", &input).run()?;

    let wrapper = build_args_fn::<P>()?;
    wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
        let stderr = run_fail(P::cmd(args))?;
        assert!(stderr.contains("Permission denied"));
        Ok(())
    })
}

#[macro_export]
macro_rules! test_unreadable_input_file {
    ($program: ident) => {
        #[test]
        fn unreadable_input_file() -> Result<()> {
            test_unreadable_input_file::<$program>()
        }
    };
}

//------------------------------------------
// test invalid content

pub fn test_tiny_input_file<'a, P>() -> Result<()>
where
    P: MetadataReader<'a>,
{
    let mut td = TestDir::new()?;

    let input = td.mk_path("meta.bin");
    file_utils::create_sized_file(&input, 1024)?;

    let wrapper = build_args_fn::<P>()?;
    wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
        run_fail(P::cmd(args))?;
        Ok(())
    })
}

#[macro_export]
macro_rules! test_tiny_input_file {
    ($program: ident) => {
        #[test]
        fn tiny_input_file() -> Result<()> {
            test_tiny_input_file::<$program>()
        }
    };
}

pub fn test_help_message_for_tiny_input_file<'a, P>() -> Result<()>
where
    P: MetadataReader<'a>,
{
    let mut td = TestDir::new()?;

    let input = td.mk_path("meta.bin");
    file_utils::create_sized_file(&input, 1024)?;

    let wrapper = build_args_fn::<P>()?;
    wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
        let stderr = run_fail(P::cmd(args))?;
        eprintln!("actual: {:?}", stderr);
        assert!(stderr.contains("Metadata device/file too small.  Is this binary metadata?"));
        Ok(())
    })
}

#[macro_export]
macro_rules! test_help_message_for_tiny_input_file {
    ($program: ident) => {
        #[test]
        fn prints_help_message_for_tiny_input_file() -> Result<()> {
            test_help_message_for_tiny_input_file::<$program>()
        }
    };
}

pub fn test_spot_xml_data<'a, P>() -> Result<()>
where
    P: MetadataReader<'a>,
{
    let mut td = TestDir::new()?;

    // input a large xml file
    let input = td.mk_path("meta.xml");
    let mut gen = FragmentedS::new(4, 10240);
    write_xml(&input, &mut gen)?;

    let wrapper = build_args_fn::<P>()?;
    wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
        let stderr = run_fail(P::cmd(args))?;
        let msg =
            "This looks like XML.  This tool only supports the binary metadata format.".to_string();
        assert!(stderr.contains(&msg));
        Ok(())
    })
}

#[macro_export]
macro_rules! test_spot_xml_data {
    ($program: ident) => {
        #[test]
        fn spot_xml_data() -> Result<()> {
            test_spot_xml_data::<$program>()
        }
    };
}

pub fn test_corrupted_input_data<'a, P>() -> Result<()>
where
    P: InputProgram<'a>,
{
    let mut td = TestDir::new()?;
    let input = mk_zeroed_md(&mut td)?;

    let wrapper = match P::arg_type() {
        ArgType::InputArg => input_arg_only::<P>,
        ArgType::IoOptions => with_output_superblock_zeroed::<P>,
    };
    wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
        let stderr = run_fail(P::cmd(args))?;
        assert!(stderr.contains(P::corrupted_input()));
        Ok(())
    })
}

#[macro_export]
macro_rules! test_corrupted_input_data {
    ($program: ident) => {
        #[test]
        fn corrupted_input_data() -> Result<()> {
            test_corrupted_input_data::<$program>()
        }
    };
}

//------------------------------------------
// test special inputs

pub fn test_readonly_input_file<'a, P>() -> Result<()>
where
    P: InputProgram<'a>,
{
    let mut td = TestDir::new()?;

    // input an unreadable file
    let input = P::mk_valid_input(&mut td)?;
    duct::cmd!("chmod", "-w", &input).run()?;

    let wrapper = match P::arg_type() {
        ArgType::InputArg => input_arg_only::<P>,
        ArgType::IoOptions => with_output_simple::<P>,
    };

    wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
        run_ok(P::cmd(args))?;
        Ok(())
    })
}

#[macro_export]
macro_rules! test_readonly_input_file {
    ($program: ident) => {
        #[test]
        fn readonly_input_file() -> Result<()> {
            test_readonly_input_file::<$program>()
        }
    };
}

//------------------------------------------