summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/tests/fs/invalid_offset.rs
blob: 995e302a1d3256305868006c1b4a1f72a3bf626e (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
//! Tests for extreme `u64` file offsets.
//!
//! POSIX-ish interfaces tend to use signed integers for file offsets, while
//! Rust APIs tend to use `u64`. Test that extreme `u64` values in APIs that
//! take file offsets are properly diagnosed.
//!
//! These tests are disabled on ios/macos since those platforms kill the
//! process with `SIGXFSZ` instead of returning an error.

#![cfg(not(any(target_os = "redox", target_os = "wasi")))]

use rustix::io::SeekFrom;

#[test]
fn invalid_offset_seek() {
    use rustix::fs::{cwd, openat, seek, Mode, OFlags};
    let tmp = tempfile::tempdir().unwrap();
    let dir = openat(cwd(), tmp.path(), OFlags::RDONLY, Mode::empty()).unwrap();
    let file = openat(
        &dir,
        "foo",
        OFlags::WRONLY | OFlags::TRUNC | OFlags::CREATE,
        Mode::RUSR | Mode::WUSR,
    )
    .unwrap();

    seek(&file, SeekFrom::Start(u64::MAX)).unwrap_err();
    seek(&file, SeekFrom::Start(i64::MAX as u64 + 1)).unwrap_err();
    seek(&file, SeekFrom::End(-1)).unwrap_err();
    seek(&file, SeekFrom::End(i64::MIN)).unwrap_err();
    seek(&file, SeekFrom::Current(-1)).unwrap_err();
    seek(&file, SeekFrom::Current(i64::MIN)).unwrap_err();
}

#[cfg(not(any(
    target_os = "dragonfly",
    target_os = "illumos",
    target_os = "netbsd",
    target_os = "openbsd",
    target_os = "redox",
)))]
#[test]
fn invalid_offset_fallocate() {
    use rustix::fs::{cwd, fallocate, openat, FallocateFlags, Mode, OFlags};
    let tmp = tempfile::tempdir().unwrap();
    let dir = openat(cwd(), tmp.path(), OFlags::RDONLY, Mode::empty()).unwrap();
    let file = openat(
        &dir,
        "foo",
        OFlags::WRONLY | OFlags::TRUNC | OFlags::CREATE,
        Mode::RUSR | Mode::WUSR,
    )
    .unwrap();

    fallocate(&file, FallocateFlags::empty(), u64::MAX, 1).unwrap_err();
    fallocate(&file, FallocateFlags::empty(), i64::MAX as u64 + 1, 1).unwrap_err();
    fallocate(&file, FallocateFlags::empty(), 0, u64::MAX).unwrap_err();
    fallocate(&file, FallocateFlags::empty(), 0, i64::MAX as u64 + 1).unwrap_err();
}

#[cfg(not(any(
    target_os = "dragonfly",
    target_os = "illumos",
    target_os = "ios",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "openbsd",
    target_os = "redox",
)))]
#[test]
fn invalid_offset_fadvise() {
    use rustix::fs::{cwd, fadvise, openat, Advice, Mode, OFlags};
    let tmp = tempfile::tempdir().unwrap();
    let dir = openat(cwd(), tmp.path(), OFlags::RDONLY, Mode::empty()).unwrap();
    let file = openat(
        &dir,
        "foo",
        OFlags::WRONLY | OFlags::TRUNC | OFlags::CREATE,
        Mode::RUSR | Mode::WUSR,
    )
    .unwrap();

    // `fadvise` never fails on invalid offsets.
    fadvise(&file, i64::MAX as u64, i64::MAX as u64, Advice::Normal).unwrap();
    fadvise(&file, u64::MAX, 0, Advice::Normal).unwrap();
    fadvise(&file, i64::MAX as u64, 1, Advice::Normal).unwrap();
    fadvise(&file, 1, i64::MAX as u64, Advice::Normal).unwrap();
    fadvise(&file, i64::MAX as u64 + 1, 0, Advice::Normal).unwrap();
    fadvise(&file, u64::MAX, i64::MAX as u64, Advice::Normal).unwrap();

    // `fadvise` fails on invalid lengths.
    fadvise(&file, u64::MAX, u64::MAX, Advice::Normal).unwrap_err();
    fadvise(&file, i64::MAX as u64, u64::MAX, Advice::Normal).unwrap_err();
    fadvise(&file, 0, u64::MAX, Advice::Normal).unwrap_err();
    fadvise(&file, u64::MAX, i64::MAX as u64 + 1, Advice::Normal).unwrap_err();
    fadvise(&file, i64::MAX as u64 + 1, u64::MAX, Advice::Normal).unwrap_err();
    fadvise(&file, i64::MAX as u64, i64::MAX as u64 + 1, Advice::Normal).unwrap_err();
    fadvise(&file, 0, i64::MAX as u64 + 1, Advice::Normal).unwrap_err();
}

#[test]
fn invalid_offset_pread() {
    use rustix::fs::{cwd, openat, Mode, OFlags};
    use rustix::io::pread;
    let tmp = tempfile::tempdir().unwrap();
    let dir = openat(cwd(), tmp.path(), OFlags::RDONLY, Mode::empty()).unwrap();
    let file = openat(
        &dir,
        "foo",
        OFlags::RDWR | OFlags::TRUNC | OFlags::CREATE,
        Mode::RUSR | Mode::WUSR,
    )
    .unwrap();

    let mut buf = [0_u8; 1];
    pread(&file, &mut buf, u64::MAX).unwrap_err();
    pread(&file, &mut buf, i64::MAX as u64 + 1).unwrap_err();
}

#[cfg(not(any(target_os = "ios", target_os = "macos")))]
#[test]
fn invalid_offset_pwrite() {
    use rustix::fs::{cwd, openat, Mode, OFlags};
    use rustix::io::pwrite;
    let tmp = tempfile::tempdir().unwrap();
    let dir = openat(cwd(), tmp.path(), OFlags::RDONLY, Mode::empty()).unwrap();
    let file = openat(
        &dir,
        "foo",
        OFlags::WRONLY | OFlags::TRUNC | OFlags::CREATE,
        Mode::RUSR | Mode::WUSR,
    )
    .unwrap();

    let buf = [0_u8; 1];
    pwrite(&file, &buf, u64::MAX).unwrap_err();
    pwrite(&file, &buf, i64::MAX as u64 + 1).unwrap_err();
}

#[cfg(any(target_os = "android", target_os = "linux"))]
#[test]
fn invalid_offset_copy_file_range() {
    use rustix::fs::{copy_file_range, cwd, openat, Mode, OFlags};
    use rustix::io::write;
    let tmp = tempfile::tempdir().unwrap();
    let dir = openat(cwd(), tmp.path(), OFlags::RDONLY, Mode::empty()).unwrap();
    let foo = openat(
        &dir,
        "foo",
        OFlags::RDWR | OFlags::TRUNC | OFlags::CREATE,
        Mode::RUSR | Mode::WUSR,
    )
    .unwrap();
    let bar = openat(
        &dir,
        "bar",
        OFlags::WRONLY | OFlags::TRUNC | OFlags::CREATE,
        Mode::RUSR | Mode::WUSR,
    )
    .unwrap();
    write(&foo, b"a").unwrap();

    let mut off_in = u64::MAX;
    let mut off_out = 0;
    copy_file_range(&foo, Some(&mut off_in), &bar, Some(&mut off_out), 1).unwrap_err();

    let mut off_in = i64::MAX as u64 + 1;
    let mut off_out = 0;
    copy_file_range(&foo, Some(&mut off_in), &bar, Some(&mut off_out), 1).unwrap_err();

    let mut off_in = 0;
    let mut off_out = u64::MAX;
    copy_file_range(&foo, Some(&mut off_in), &bar, Some(&mut off_out), 1).unwrap_err();

    let mut off_in = 0;
    let mut off_out = i64::MAX as u64;
    copy_file_range(&foo, Some(&mut off_in), &bar, Some(&mut off_out), 1).unwrap_err();

    let mut off_in = 0;
    let mut off_out = i64::MAX as u64 + 1;
    copy_file_range(&foo, Some(&mut off_in), &bar, Some(&mut off_out), 1).unwrap_err();
}