opendal_testkit/read.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use bytes::Bytes;
19use opendal_core::*;
20use rand::Rng;
21use rand::rng;
22
23use crate::utils::sha256_digest;
24
25/// ReadAction represents a read action.
26#[derive(Debug, Clone, Copy, Eq, PartialEq)]
27pub enum ReadAction {
28 /// Read represents a read action with given input buf size.
29 ///
30 /// # NOTE
31 ///
32 /// The size is the input buf size, it's possible that the actual read size is smaller.
33 Read(usize, usize),
34}
35
36/// ReadChecker is used to check the correctness of the read process.
37pub struct ReadChecker {
38 /// Raw Data is the data we write to the storage.
39 raw_data: Bytes,
40}
41
42impl ReadChecker {
43 /// Create a new read checker by given size and range.
44 ///
45 /// It's by design that we use a random generator to generate the raw data. The content of data
46 /// is not important, we only care about the correctness of the read process.
47 pub fn new(size: usize) -> Self {
48 let mut rng = rng();
49 let mut data = vec![0; size];
50 rng.fill_bytes(&mut data);
51
52 let raw_data = Bytes::from(data);
53
54 Self { raw_data }
55 }
56
57 /// Return the raw data of this read checker.
58 pub fn data(&self) -> Bytes {
59 self.raw_data.clone()
60 }
61
62 /// check_read checks the correctness of the read process after a read action.
63 ///
64 /// - buf_size is the read action's buf size.
65 /// - output is the output of this read action.
66 fn check_read(&self, offset: usize, size: usize, output: &[u8]) {
67 if size == 0 {
68 assert_eq!(
69 output.len(),
70 0,
71 "check read failed: output must be empty if buf_size is 0"
72 );
73 return;
74 }
75
76 if size > 0 && output.is_empty() {
77 assert!(
78 offset >= self.raw_data.len(),
79 "check read failed: no data read means cur must outsides of ranged_data",
80 );
81 return;
82 }
83
84 assert!(
85 offset + output.len() <= self.raw_data.len(),
86 "check read failed: cur + output length must be less than ranged_data length, offset: {}, output: {}, ranged_data: {}",
87 offset,
88 output.len(),
89 self.raw_data.len(),
90 );
91
92 let expected = &self.raw_data[offset..offset + output.len()];
93
94 if output != expected {
95 assert_eq!(
96 sha256_digest(output),
97 sha256_digest(expected),
98 "check read failed: output bs is different with expected bs",
99 );
100 }
101 }
102
103 /// Check will check the correctness of the read process via given actions.
104 ///
105 /// Check will panic if any check failed.
106 pub async fn check(&mut self, r: Reader, actions: &[ReadAction]) {
107 for action in actions {
108 match *action {
109 ReadAction::Read(offset, size) => {
110 let bs = r
111 .read(offset as u64..(offset + size) as u64)
112 .await
113 .expect("read must success");
114 self.check_read(offset, size, bs.to_bytes().as_ref());
115 }
116 }
117 }
118 }
119}