summaryrefslogtreecommitdiff
path: root/gbl/libgbl/tests/integration_tests.rs
blob: 082ac44e1ea156eaf0e642e06064a1690123464b (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
// Copyright 2024, 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.

use gbl_storage::BlockIo;
use gbl_storage_testlib::TestBlockIo;
use libgbl::{
    digest::Algorithm, BootImages, Context, Digest, FuchsiaBootImages, GblBuilder, GblOps,
    GblOpsError,
};
use std::{collections::VecDeque, vec::Vec};

extern crate avb_sysdeps;

struct GblTestBlockIo {
    io: TestBlockIo,
    max_gpt_entries: u64,
}

/// `TestGblOps` provides mock implementation of GblOps for integration test.
#[derive(Default)]
struct TestGblOps<'a> {
    block_io: Vec<GblTestBlockIo>,
    console_out: VecDeque<u8>,
    boot_cb: Option<MustUse<&'a mut dyn FnMut(BootImages)>>,
}

impl TestGblOps<'_> {
    /// Adds a new block device.
    pub(crate) fn add_block_device<T: AsRef<[u8]>>(
        &mut self,
        alignment: u64,
        block_size: u64,
        max_gpt_entries: u64,
        data: T,
    ) {
        self.block_io.push(GblTestBlockIo {
            io: TestBlockIo::new(alignment, block_size, data),
            max_gpt_entries: max_gpt_entries,
        })
    }
}

impl GblOps for TestGblOps<'_> {
    type Context = TestDigestContext;

    fn visit_block_devices(
        &mut self,
        f: &mut dyn FnMut(&mut dyn BlockIo, u64, u64),
    ) -> Result<(), GblOpsError> {
        for (idx, ele) in self.block_io.iter_mut().enumerate() {
            f(&mut ele.io, idx.try_into().unwrap(), ele.max_gpt_entries);
        }
        Ok(())
    }

    fn console_put_char(&mut self, ch: u8) -> Result<(), GblOpsError> {
        Ok(self.console_out.push_back(ch))
    }

    fn should_stop_in_fastboot(&mut self) -> Result<bool, GblOpsError> {
        Ok(false)
    }

    fn boot(&mut self, boot_images: BootImages) -> Result<(), GblOpsError> {
        Ok((self.boot_cb.as_mut().unwrap().get())(boot_images))
    }
}

/// Placeholder.
struct DigestBytes(Vec<u8>);

impl AsRef<[u8]> for DigestBytes {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl Digest for DigestBytes {
    fn algorithm(&self) -> &Algorithm {
        unimplemented!();
    }
}

/// Placeholder.
struct TestDigestContext {}

impl Context for TestDigestContext {
    type Digest = DigestBytes;

    fn new(_: Algorithm) -> Self {
        unimplemented!();
    }

    fn update(&mut self, _: &[u8]) {
        unimplemented!();
    }

    fn finish(self) -> Self::Digest {
        unimplemented!();
    }

    fn algorithm(&self) -> &Algorithm {
        unimplemented!();
    }
}

/// `MustUse` wraps an object and checks that it is accessed at least once before it's dropped.
/// In this integration test, it is mainly used to check that test provided ops callbacks are run.
struct MustUse<T: ?Sized> {
    used: bool,
    val: T,
}

impl<T: ?Sized> MustUse<T> {
    /// Create a new instance.
    fn new(val: T) -> Self
    where
        T: Sized,
    {
        Self { used: false, val: val }
    }

    /// Returns a mutable reference to the object.
    fn get(&mut self) -> &mut T {
        self.used = true;
        &mut self.val
    }
}

impl<T: ?Sized> Drop for MustUse<T> {
    fn drop(&mut self) {
        assert!(self.used)
    }
}

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

    #[test]
    fn test_zircon_load_and_boot() {
        // TODO(b/334962583): Invocation test only. Update this test once
        // `Gbl::zircon_load_and_boot()` is implemented.
        let mut boot_cb = |boot_images: BootImages| {
            let BootImages::Fuchsia(FuchsiaBootImages { zbi_kernel, zbi_items }) = boot_images
            else {
                panic!("Wrong image type");
            };
            assert_eq!(zbi_kernel, include_bytes!("../testdata/zircon_a.bin"));
            assert_eq!(zbi_items, []);
        };
        let mut ops: TestGblOps = Default::default();
        ops.add_block_device(512, 512, 128, include_bytes!("../testdata/zircon_gpt.bin"));
        ops.boot_cb = Some(MustUse::new(&mut boot_cb));
        let mut gbl = GblBuilder::new(&mut ops).build();
        let mut load_buffer = vec![0u8; 64 * 1024];
        let _ = gbl.zircon_load_and_boot(&mut load_buffer[..]);
    }
}