2 * Copyright (c) 2020 Tilman Sauerbeck (tilman at code-monkey de)
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 use common::storage::{Storage, Error};
26 use common::logger::*;
34 fn new() -> FakeStorage {
36 expected: vec![0xff; MEMORY_SIZE].into_boxed_slice(),
37 actual: vec![0xff; MEMORY_SIZE].into_boxed_slice(),
42 impl Storage for FakeStorage {
43 fn size(&self) -> usize {
47 fn read(&self, address: usize, buffer: &mut [u8]) {
48 for i in 0..buffer.len() {
49 buffer[i] = self.actual[address + i];
53 fn write(&mut self, address: usize, buffer: &[u8; 4096]) -> Result<(), Error> {
54 if (address & 4095) != 0 {
55 return Err(Error::UnalignedAddress);
58 for i in 0..buffer.len() {
59 self.actual[address + i] = buffer[i];
66 self.actual = vec![0xff; MEMORY_SIZE].into_boxed_slice();
70 // Runs a couple of recordings on fully erased flash memory.
72 fn first_recording() {
73 let mut fake_storage = FakeStorage::new();
75 let mut logger = Logger::new(&mut fake_storage);
78 let tap = gps::TimeAndPos {
80 unix_time: 1478026311,
85 let recording_id = logger.start_recording(&tap);
86 assert_eq!(1, recording_id);
88 let sectors_written = logger.stop_recording(&tap);
89 assert_eq!(1, sectors_written);
93 0x01, 0x00, 0x01, 0x00,
94 0x47, 0xe4, 0x18, 0x58,
99 0x9c, 0x8d, 0x99, 0x07,
100 0x9e, 0x8d, 0x99, 0x07,
103 0xff, 0xff, 0xff, 0xff, 0x0f,
110 let end = start + expected.len();
111 fake_storage.expected[start..end].copy_from_slice(&expected);
113 assert_eq!(fake_storage.expected, fake_storage.actual);
117 fn second_recording() {
118 let mut fake_storage = FakeStorage::new();
120 // Mark first sector as in use.
123 0x01, 0x00, 0x01, 0x00,
126 fake_storage.expected[0..recording0.len()].copy_from_slice(&recording0);
127 fake_storage.actual[0..recording0.len()].copy_from_slice(&recording0);
129 let mut logger = Logger::new(&mut fake_storage);
132 let tap = gps::TimeAndPos {
134 unix_time: 1478026312,
139 let recording_id = logger.start_recording(&tap);
140 assert_eq!(2, recording_id);
142 let sectors_written = logger.stop_recording(&tap);
143 assert_eq!(1, sectors_written);
147 0x01, 0x00, 0x02, 0x00,
148 0x48, 0xe4, 0x18, 0x58,
153 0x9c, 0x8d, 0x99, 0x07,
154 0x9e, 0x8d, 0x99, 0x07,
157 0xff, 0xff, 0xff, 0xff, 0x0f,
164 let end = start + expected.len();
165 fake_storage.expected[start..end].copy_from_slice(&expected);
167 assert_eq!(fake_storage.expected, fake_storage.actual);
171 fn multi_sector_recording() {
172 let mut fake_storage = FakeStorage::new();
174 let mut logger = Logger::new(&mut fake_storage);
177 let tap = gps::TimeAndPos {
179 unix_time: 1578425250,
184 let recording_id = logger.start_recording(&tap);
185 assert_eq!(1, recording_id);
187 let mut prev_tap = tap;
190 let tap = gps::TimeAndPos {
192 unix_time: prev_tap.unix_time + 1,
193 latitude: prev_tap.latitude + 1,
194 longitude: prev_tap.longitude + 1,
197 logger.log(&prev_tap, &tap);
202 let sectors_written = logger.stop_recording(&tap);
203 assert_eq!(2, sectors_written);
206 0x01, 0x00, 0x01, 0x00,
210 0x03, 0x00, 0x01, 0x00,
213 assert_eq!(header0, fake_storage.actual[0..(0 + header0.len())]);
214 assert_eq!(header1, fake_storage.actual[4096..(4096 + header1.len())]);