• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Achiefs / fim / 14275166216

04 Apr 2025 10:13PM UTC coverage: 84.711% (-0.4%) from 85.125%
14275166216

push

github

web-flow
Merge pull request #194 from Achiefs/177-db-hash

Added Hash diff scanner

353 of 433 new or added lines in 12 files covered. (81.52%)

4 existing lines in 1 file now uncovered.

1435 of 1694 relevant lines covered (84.71%)

1.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/db/test.rs
1
use super::*;
2
use serial_test::serial;
3
use std::path::Path;
4

5
use crate::utils;
6

7
// ----------------------------------------------------------------------------
8

9
fn remove_db(path: &str) {
1✔
10
    use std::fs::remove_file;
11

12
    if Path::new(path).exists() {
1✔
13
        match remove_file(path){
1✔
14
            Ok(_v) => (),
NEW
15
            Err(e) => println!("Error deleting db, {}", e)
×
16
        };
17
    };
18
}
19

20
// ----------------------------------------------------------------------------
21

22
fn get_dbfile() -> DBFile {
1✔
23
    DBFile{
24
        id: String::from("ID"),
1✔
25
        timestamp: String::from("TIMESTAMP"),
1✔
26
        hash: String::from("HASH"),
1✔
27
        path: String::from("PATH"),
1✔
28
        size: 10,
29
        permissions: 0
30
    }
31
}
32

33
// ----------------------------------------------------------------------------
34

35
#[test]
36
#[serial]
37
/// Check new instance creation, the instance should match the expected DB path.
38
fn test_new() {
39
    let cfg = AppConfig::new(&utils::get_os(), None);
40
    let tdb = DB::new(&cfg.hashscanner_file);
41

42
    assert_eq!(tdb.path, cfg.hashscanner_file);
43
}
44

45
// ------------------------------------------------------------------------
46

47
#[test]
48
#[serial]
49
/// Check open of new DB changes, it should be 0
50
fn test_open() {
51
    let tdb = DB::new("fim.db");
52
    let connection = tdb.open();
53

54
    assert_eq!(connection.changes(), 0);
55
}
56

57
// ------------------------------------------------------------------------
58

59
#[test]
60
#[serial]
61
/// Check close of new DB, it should not panic
62
fn test_close() {
63
    let tdb = DB::new("fim.db");
64
    let connection = tdb.open();
65
    tdb.close(connection);
66
}
67

68
// ------------------------------------------------------------------------
69

70
#[test]
71
#[serial]
72
/// Check DB emptiness, it should be empty on first check and not empty in second
73
fn test_is_empty() {
74
    let db_path = "fim.db";
75
    let tdb = DB::new(db_path);
76
    
77
    remove_db(db_path);
78
    assert_eq!(tdb.is_empty(), true);
79
    tdb.create_table();
80
    tdb.insert_file(get_dbfile());
81
    assert_eq!(tdb.is_empty(), false);
82
}
83

84
// ------------------------------------------------------------------------
85

86
#[test]
87
#[serial]
88
/// Check DB table creation, pragma query should obtain first row of schema (id)
89
fn test_create_table() {
90
    let db_path = "fim.db";
91
    let tdb = DB::new(db_path);
92
    
93
    remove_db(db_path);
94
    tdb.create_table();
95

96
    let connection = tdb.open();
97
    let result = connection.query_row("SELECT * FROM pragma_table_info('files')",
98
        [],
99
        |row| {
100
            let data0: u32 = row.get(0).unwrap();
101
            let data1: String = row.get(1).unwrap();
102
            let data2: String = row.get(2).unwrap();
103
            assert_eq!(data0, 0);
104
            assert_eq!(data1, "id");
105
            assert_eq!(data2, "TEXT");
106
            Ok(())
107
        }
108
    );
109

110
    assert_eq!(result, Ok(()));
111
}
112

113
// ------------------------------------------------------------------------
114

115
#[test]
116
#[serial]
117
/// Check DB insertion, the data in DB should be the same as inserted object
118
fn test_insert_file() {
119
    let db_path = "fim.db";
120
    let tdb = DB::new(db_path);
121
    
122
    remove_db(db_path);
123
    tdb.create_table();
124
    tdb.insert_file(get_dbfile());
125

126
    let connection = tdb.open();
127
    let result = connection.query_row("SELECT * FROM files WHERE id = 'ID'",
128
        [],
129
        |row| {
130
            let data0: String = row.get(0).unwrap();
131
            let data1: String = row.get(1).unwrap();
132
            let data2: String = row.get(2).unwrap();
133
            let data3: String = row.get(3).unwrap();
134
            let data4: u32 = row.get(4).unwrap();
135
            let data5: u32 = row.get(5).unwrap();
136
            assert_eq!(data0, "ID");
137
            assert_eq!(data1, "TIMESTAMP");
138
            assert_eq!(data2, "HASH");
139
            assert_eq!(data3, "PATH");
140
            assert_eq!(data4, 10);
141
            assert_eq!(data5, 0);
142
            Ok(())
143
        }
144
    );
145

146
    assert_eq!(result, Ok(()));
147
}
148

149
// ------------------------------------------------------------------------
150

151
#[test]
152
#[serial]
153
/// Check dbfile retrieve from DB, the retrieved file should match fields with original
154
fn test_get_file_by_path() {
155
    let db_path = "fim.db";
156
    let tdb = DB::new(db_path);
157
    let original_dbfile = get_dbfile();
158
    
159
    remove_db(db_path);
160
    tdb.create_table();
161
    tdb.insert_file(original_dbfile.clone());
162

163
    let dbfile = tdb.get_file_by_path(String::from("PATH")).unwrap();
164
    assert_eq!(dbfile.id, original_dbfile.id);
165
    assert_eq!(dbfile.timestamp, original_dbfile.timestamp);
166
    assert_eq!(dbfile.hash, original_dbfile.hash);
167
    assert_eq!(dbfile.path, original_dbfile.path);
168
    assert_eq!(dbfile.size, original_dbfile.size);
169
    assert_eq!(dbfile.permissions, original_dbfile.permissions);
170
}
171

172
// ------------------------------------------------------------------------
173

174
#[test]
175
#[serial]
176
/// Check list retrieve of same main path files.
177
/// It should contains the list of files with matching attributes
178
fn test_get_file_list() {
179
    let db_path = "fim.db";
180
    let tdb = DB::new(db_path);
181
    let dbfile0 = DBFile{
182
        id: String::from("ID0"),
183
        timestamp: String::from("TIMESTAMP0"),
184
        hash: String::from("HASH0"),
185
        path: String::from("CUSTOM_PATH/0"),
186
        size: 100,
187
        permissions: 0
188
    };
189
    let dbfile1 = DBFile{
190
        id: String::from("ID1"),
191
        timestamp: String::from("TIMESTAMP1"),
192
        hash: String::from("HASH1"),
193
        path: String::from("CUSTOM_PATH/1"),
194
        size: 101,
195
        permissions: 1
196
    };
197
    
198
    remove_db(db_path);
199
    tdb.create_table();
200
    tdb.insert_file(dbfile0.clone());
201
    tdb.insert_file(dbfile1.clone());
202

203
    let list = tdb.get_file_list(String::from("CUSTOM_PATH"));
204
    assert_eq!(list.len(), 2);
205
    assert_eq!(list[0].id, dbfile0.id);
206
    assert_eq!(list[0].timestamp, dbfile0.timestamp);
207
    assert_eq!(list[0].hash, dbfile0.hash);
208
    assert_eq!(list[0].path, dbfile0.path);
209
    assert_eq!(list[0].size, dbfile0.size);
210
    assert_eq!(list[0].permissions, dbfile0.permissions);
211

212
    assert_eq!(list[1].id, dbfile1.id);
213
    assert_eq!(list[1].timestamp, dbfile1.timestamp);
214
    assert_eq!(list[1].hash, dbfile1.hash);
215
    assert_eq!(list[1].path, dbfile1.path);
216
    assert_eq!(list[1].size, dbfile1.size);
217
    assert_eq!(list[1].permissions, dbfile1.permissions);
218
}
219

220
// ------------------------------------------------------------------------
221

222
#[test]
223
#[serial]
224
/// Check present DBFile update.
225
/// It should differ in calculated fields (timestamp, hash, size, permissions)
226
fn test_update_file() {
227
    let cfg = AppConfig::new(&utils::get_os(), None);
228
    let tdb = DB::new(&cfg.hashscanner_file);
229
    let original_dbfile = get_dbfile();
230
    let new_dbfile = DBFile {
231
        id: String::from("ID"),
232
        timestamp: String::from("CALCULATED"),
233
        hash: String::from("CALCULATED"),
234
        path: String::from("LICENSE"),
235
        size: 123,
236
        permissions: 321,
237
    };
238

239
    remove_db(&cfg.hashscanner_file);
240
    tdb.create_table();
241
    tdb.insert_file(original_dbfile.clone());
242
    let result = tdb.update_file(cfg, new_dbfile.clone());
243

244
    match result {
245
        Some(dbfile) => {
246
            assert_eq!(dbfile.id, new_dbfile.id);
247
            assert_ne!(dbfile.timestamp, new_dbfile.timestamp);
248
            assert_ne!(dbfile.hash, new_dbfile.hash);
249
            assert_eq!(dbfile.path, new_dbfile.path);
250
            assert_ne!(dbfile.size, new_dbfile.size);
251
            assert_ne!(dbfile.permissions, new_dbfile.permissions);
252
        },
253
        None => assert!(false)
254
    }
255
}
256

257
// ------------------------------------------------------------------------
258

259
#[test]
260
#[serial]
261
/// Check DBFile delete from DB, it should return QueryReturnedNoRows result on query
262
fn test_delete_file() {
263
    let db_path = "fim.db";
264
    let tdb = DB::new(db_path);
265
    let dbfile = get_dbfile();
266

267
    remove_db(db_path);
268
    tdb.create_table();
269
    tdb.insert_file(dbfile.clone());
270
    let delete_result = tdb.delete_file(dbfile.clone());
271
    match delete_result {
272
        Ok(_v) => (),
273
        Err(_e) => assert!(false)
274
    };
275

276
    let connection = tdb.open();
277
    let result = connection.query_row("SELECT * FROM files WHERE id = 'ID'",
278
        [],
279
        |row| {
280
            let data0: String = row.get(0).unwrap();
281
            let data1: String = row.get(1).unwrap();
282
            let data2: String = row.get(2).unwrap();
283
            let data3: String = row.get(3).unwrap();
284
            let data4: u32 = row.get(4).unwrap();
285
            let data5: u32 = row.get(5).unwrap();
286
            assert_eq!(data0, "ID");
287
            assert_eq!(data1, "TIMESTAMP");
288
            assert_eq!(data2, "HASH");
289
            assert_eq!(data3, "PATH");
290
            assert_eq!(data4, 10);
291
            assert_eq!(data5, 0);
292
            Ok(())
293
        }
294
    );
295

296
    assert_eq!(result, Err(QueryReturnedNoRows));
297
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc