1717#include < gtest/gtest.h>
1818#include < photon/photon.h>
1919#include < photon/common/alog-stdstring.h>
20+ #include < random>
21+ #include < functional>
2022#include " erofs_stress_base.h"
2123
2224#define EROFS_STRESS_UNIMPLEMENTED_FUNC (ret_type, func, ret ) \
2325ret_type func override { \
2426 return ret; \
2527}
2628
29+ class StressInterImpl : public StressGenInter {
30+ public:
31+ int max_file_size = SECTOR_SIZE * 128 ;
32+ int min_file_size = SECTOR_SIZE ;
33+ int block_size = 4096 ;
34+ std::hash<std::string> hash_fn;
35+
36+ /* generate file content in build phase */
37+ bool build_gen_content (StressNode *node, StressHostFile *file) override {
38+ std::random_device rd;
39+ std::mt19937 gen (rd ());
40+ std::uniform_int_distribution<> dis (min_file_size, max_file_size);
41+ int size = dis (gen);
42+ int len;
43+ off64_t offset = 0 ;
44+ std::string hash_val;
45+
46+ while (size > 0 ) {
47+ len = std::min (size, block_size);
48+ std::string block_str = get_randomstr (len, false );
49+ if (file->file ->pwrite (block_str.c_str (), len, offset) != len)
50+ LOG_ERROR_RETURN (-1 , -1 , " fail to write to host file `" , file->path );
51+ hash_val = std::to_string (hash_fn (hash_val + block_str));
52+ size -= len;
53+ offset += len;
54+ }
55+ node->content = hash_val;
56+ return true ;
57+ }
58+
59+ /* generate node content in verify phase */
60+ bool verify_gen_content (StressNode *node, photon::fs::IFile *erofs_file) override {
61+ std::string hash_val;
62+ char buf[block_size];
63+ struct stat st;
64+ off_t left, offset = 0 ;
65+ int len;
66+
67+ if (erofs_file->fstat (&st))
68+ LOG_ERROR_RETURN (-1 , -1 , " fail to stat file" );
69+ left = st.st_size ;
70+ while (left > 0 ) {
71+ len = std::min ((off_t )block_size, left);
72+ if (len != erofs_file->pread (buf, len, offset))
73+ LOG_ERROR_RETURN (-1 , -1 , " fail to pread file" );
74+
75+ std::string block_str (buf, len);
76+ hash_val = std::to_string (hash_fn (hash_val + block_str));
77+ left -= len;
78+ offset += len;
79+ }
80+ node->content = hash_val;
81+ return true ;
82+ }
83+ };
84+
2785/*
2886 * TC001
2987 *
@@ -62,6 +120,48 @@ class StressCase001: public StressBase {
62120 }
63121};
64122
123+ /*
124+ * TC002
125+ *
126+ * Create layers, each layer contains 2 dirs,
127+ * each dir contains 10 files.
128+ *
129+ * Testing the integrity of file contents.
130+ */
131+ class StressCase002 : public StressBase , public StressInterImpl {
132+ public:
133+ StressCase002 (std::string path, int layers): StressBase(path, layers) {}
134+
135+ /* leave mod/own/xattr empty */
136+ EROFS_STRESS_UNIMPLEMENTED_FUNC (bool , build_gen_mod(StressNode *node, StressHostFile *file), true )
137+ EROFS_STRESS_UNIMPLEMENTED_FUNC (bool , build_gen_own(StressNode *node, StressHostFile *file), true )
138+ EROFS_STRESS_UNIMPLEMENTED_FUNC (bool , build_gen_xattrs(StressNode *node, StressHostFile *file), true )
139+ bool build_gen_content (StressNode *node, StressHostFile *file) override {
140+ return StressInterImpl::build_gen_content (node, file);
141+ }
142+
143+ /* leave mod/own/xattr empty */
144+ EROFS_STRESS_UNIMPLEMENTED_FUNC (bool , verify_gen_mod(StressNode *node, photon::fs::IFile *erofs_file), true )
145+ EROFS_STRESS_UNIMPLEMENTED_FUNC (bool , verify_gen_own(StressNode *node, photon::fs::IFile *erofs_file), true )
146+ EROFS_STRESS_UNIMPLEMENTED_FUNC (bool , verify_gen_xattrs(StressNode *node, photon::fs::IFile *erofs_file), true )
147+ bool verify_gen_content (StressNode *node, photon::fs::IFile *erofs_file) override {
148+ return StressInterImpl::verify_gen_content (node, erofs_file);
149+ }
150+
151+ /*
152+ * each layer has two dirs:
153+ * dir one contains 10 files,
154+ * dir two contains 10 files
155+ */
156+ std::vector<int > layer_dirs (int idx) {
157+ std::vector<int > ret;
158+
159+ ret.emplace_back (10 );
160+ ret.emplace_back (10 );
161+ return ret;
162+ }
163+ };
164+
65165TEST (ErofsStressTest, TC001 ) {
66166 std::srand (static_cast <unsigned int >(std::time (0 )));
67167 StressCase001 *tc001 = new StressCase001 (" ./erofs_stress_001" , 20 );
@@ -70,6 +170,14 @@ TEST(ErofsStressTest, TC001) {
70170 delete tc001;
71171}
72172
173+ TEST (ErofsStressTest, TC002 ) {
174+ std::srand (static_cast <unsigned int >(std::time (0 )));
175+ StressCase002 *tc002 = new StressCase002 (" ./erofs_stress_002" , 10 );
176+
177+ ASSERT_EQ (tc002->run (), true );
178+ delete tc002;
179+ }
180+
73181int main (int argc, char **argv) {
74182
75183 ::testing::InitGoogleTest (&argc, argv);
0 commit comments