aboutsummaryrefslogtreecommitdiff
path: root/rust/tests/test_ops.rs
blob: aa8c579af4386e28640faa450aeed7041cc78c85 (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
// Copyright 2023, The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Provides `avb::Ops` test fixtures.

use avb::{IoError, IoResult, Ops, PublicKeyForPartitionInfo};
use std::{cmp::min, collections::HashMap, ffi::CStr};
#[cfg(feature = "uuid")]
use uuid::Uuid;

/// Represents a single fake partition.
#[derive(Default)]
pub struct FakePartition {
    /// Partition contents.
    pub contents: Vec<u8>,

    /// Whether the partition should report as preloaded or not.
    pub preloaded: bool,

    /// Partition UUID.
    #[cfg(feature = "uuid")]
    pub uuid: Uuid,
}

/// Fake vbmeta key state.
pub struct FakeVbmetaKeyState {
    /// Key trust & rollback index info.
    pub info: PublicKeyForPartitionInfo,

    /// If specified, indicates the specific partition this vbmeta is tied to (for
    /// `validate_public_key_for_partition()`).
    pub for_partition: Option<&'static str>,
}

/// Fake `Ops` test fixture.
///
/// The user is expected to set up the internal values to the desired device state - disk contents,
/// rollback indices, etc. This class then uses this state to implement the avb callback operations.
pub struct TestOps {
    /// Partitions to provide to libavb callbacks.
    pub partitions: HashMap<&'static str, FakePartition>,

    /// Vbmeta public keys as a map of {(key, metadata): state}. Querying unknown keys will
    /// return `IoError::Io`.
    ///
    /// See `add_vbmeta_key*()` functions for simpler wrappers to inject these keys.
    pub vbmeta_keys: HashMap<(Vec<u8>, Option<Vec<u8>>), FakeVbmetaKeyState>,

    /// Rollback indices. Accessing unknown locations will return `IoError::Io`.
    pub rollbacks: HashMap<usize, u64>,

    /// Unlock state. Set an error to simulate IoError during access.
    pub unlock_state: IoResult<bool>,

    /// Persistent named values. Set an error to simulate `IoError` during access. Writing
    /// a non-existent persistent value will create it; to simulate `NoSuchValue` instead,
    /// create an entry with `Err(IoError::NoSuchValue)` as the value.
    pub persistent_values: HashMap<String, IoResult<Vec<u8>>>,
}

impl TestOps {
    /// Adds a partition with the given contents.
    ///
    /// Reduces boilerplate a bit by taking in a raw array and returning a &mut so tests can
    /// do something like this:
    ///
    /// ```
    /// test_ops.add_partition("foo", [1, 2, 3, 4]);
    /// test_ops.add_partition("bar", [0, 0]).preloaded = true;
    /// ```
    pub fn add_partition<T: Into<Vec<u8>>>(
        &mut self,
        name: &'static str,
        contents: T,
    ) -> &mut FakePartition {
        self.partitions.insert(
            name,
            FakePartition {
                contents: contents.into(),
                ..Default::default()
            },
        );
        self.partitions.get_mut(name).unwrap()
    }

    /// Adds a persistent value with the given state.
    ///
    /// Reduces boilerplate by allowing array input:
    ///
    /// ```
    /// test_ops.add_persistent_value("foo", Ok(b"contents"));
    /// test_ops.add_persistent_value("bar", Err(IoError::NoSuchValue));
    /// ```
    pub fn add_persistent_value(&mut self, name: &str, contents: IoResult<&[u8]>) {
        self.persistent_values
            .insert(name.into(), contents.map(|b| b.into()));
    }

    /// Adds a fake vbmeta key not tied to any partition.
    pub fn add_vbmeta_key(&mut self, key: Vec<u8>, metadata: Option<Vec<u8>>, trusted: bool) {
        self.vbmeta_keys.insert(
            (key, metadata),
            FakeVbmetaKeyState {
                // `rollback_index_location` doesn't matter in this case, it will be read from
                // the vbmeta blob.
                info: PublicKeyForPartitionInfo {
                    trusted,
                    rollback_index_location: 0,
                },
                for_partition: None,
            },
        );
    }

    /// Adds a fake vbmeta key tied to the given partition and rollback index location.
    pub fn add_vbmeta_key_for_partition(
        &mut self,
        key: Vec<u8>,
        metadata: Option<Vec<u8>>,
        trusted: bool,
        partition: &'static str,
        rollback_index_location: u32,
    ) {
        self.vbmeta_keys.insert(
            (key, metadata),
            FakeVbmetaKeyState {
                info: PublicKeyForPartitionInfo {
                    trusted,
                    rollback_index_location,
                },
                for_partition: Some(partition),
            },
        );
    }
}

impl Default for TestOps {
    fn default() -> Self {
        Self {
            partitions: HashMap::new(),
            vbmeta_keys: HashMap::new(),
            rollbacks: HashMap::new(),
            unlock_state: Err(IoError::Io),
            persistent_values: HashMap::new(),
        }
    }
}

impl Ops for TestOps {
    fn read_from_partition(
        &mut self,
        partition: &CStr,
        offset: i64,
        buffer: &mut [u8],
    ) -> IoResult<usize> {
        let partition = self
            .partitions
            .get(partition.to_str()?)
            .ok_or(IoError::NoSuchPartition)?;

        // We should never be trying to read a preloaded partition from disk since we already
        // have it available in memory.
        assert!(!partition.preloaded);

        let contents = &partition.contents;

        // Negative offset means count backwards from the end.
        let offset = {
            if offset < 0 {
                offset
                    .checked_add(i64::try_from(contents.len()).unwrap())
                    .unwrap()
            } else {
                offset
            }
        };
        if offset < 0 {
            return Err(IoError::RangeOutsidePartition);
        }
        let offset = usize::try_from(offset).unwrap();

        if offset >= contents.len() {
            return Err(IoError::RangeOutsidePartition);
        }

        // Truncating is allowed for reads past the partition end.
        let end = min(offset.checked_add(buffer.len()).unwrap(), contents.len());
        let bytes_read = end - offset;

        buffer[..bytes_read].copy_from_slice(&contents[offset..end]);
        Ok(bytes_read)
    }

    fn get_preloaded_partition(&mut self, partition: &CStr) -> IoResult<&[u8]> {
        match self.partitions.get(partition.to_str()?) {
            Some(FakePartition {
                contents,
                preloaded: true,
                ..
            }) => Ok(&contents[..]),
            _ => Err(IoError::NotImplemented),
        }
    }

    fn validate_vbmeta_public_key(
        &mut self,
        public_key: &[u8],
        public_key_metadata: Option<&[u8]>,
    ) -> IoResult<bool> {
        self.vbmeta_keys
            // The compiler can't match (&[u8], Option<&[u8]>) to keys of type
            // (Vec<u8>, Option<Vec<u8>>) so we turn the &[u8] into vectors here. This is a bit
            // inefficient, but it's simple which is more important for tests than efficiency.
            .get(&(public_key.to_vec(), public_key_metadata.map(|m| m.to_vec())))
            .ok_or(IoError::Io)
            .map(|k| k.info.trusted)
    }

    fn read_rollback_index(&mut self, location: usize) -> IoResult<u64> {
        self.rollbacks.get(&location).ok_or(IoError::Io).copied()
    }

    fn write_rollback_index(&mut self, location: usize, index: u64) -> IoResult<()> {
        *(self.rollbacks.get_mut(&location).ok_or(IoError::Io)?) = index;
        Ok(())
    }

    fn read_is_device_unlocked(&mut self) -> IoResult<bool> {
        self.unlock_state.clone()
    }

    #[cfg(feature = "uuid")]
    fn get_unique_guid_for_partition(&mut self, partition: &CStr) -> IoResult<Uuid> {
        self.partitions
            .get(partition.to_str()?)
            .map(|p| p.uuid)
            .ok_or(IoError::NoSuchPartition)
    }

    fn get_size_of_partition(&mut self, partition: &CStr) -> IoResult<u64> {
        self.partitions
            .get(partition.to_str()?)
            .map(|p| u64::try_from(p.contents.len()).unwrap())
            .ok_or(IoError::NoSuchPartition)
    }

    fn read_persistent_value(&mut self, name: &CStr, value: &mut [u8]) -> IoResult<usize> {
        match self
            .persistent_values
            .get(name.to_str()?)
            .ok_or(IoError::NoSuchValue)?
        {
            // If we were given enough space, write the value contents.
            Ok(contents) if contents.len() <= value.len() => {
                value[..contents.len()].clone_from_slice(contents);
                Ok(contents.len())
            }
            // Not enough space, tell the caller how much we need.
            Ok(contents) => Err(IoError::InsufficientSpace(contents.len())),
            // Simulated error, return it.
            Err(e) => Err(e.clone()),
        }
    }

    fn write_persistent_value(&mut self, name: &CStr, value: &[u8]) -> IoResult<()> {
        let name = name.to_str()?;

        // If the test requested a simulated error on this value, return it.
        if let Some(Err(e)) = self.persistent_values.get(name) {
            return Err(e.clone());
        }

        self.persistent_values
            .insert(name.to_string(), Ok(value.to_vec()));
        Ok(())
    }

    fn erase_persistent_value(&mut self, name: &CStr) -> IoResult<()> {
        let name = name.to_str()?;

        // If the test requested a simulated error on this value, return it.
        if let Some(Err(e)) = self.persistent_values.get(name) {
            return Err(e.clone());
        }

        self.persistent_values.remove(name);
        Ok(())
    }

    fn validate_public_key_for_partition(
        &mut self,
        partition: &CStr,
        public_key: &[u8],
        public_key_metadata: Option<&[u8]>,
    ) -> IoResult<PublicKeyForPartitionInfo> {
        let key = self
            .vbmeta_keys
            .get(&(public_key.to_vec(), public_key_metadata.map(|m| m.to_vec())))
            .ok_or(IoError::Io)?;

        if let Some(for_partition) = key.for_partition {
            if (for_partition == partition.to_str()?) {
                // The key is registered for this partition; return its info.
                return Ok(key.info);
            }
        }

        // No match.
        Err(IoError::Io)
    }
}