1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 #include <zephyr/types.h>
9 #include <stdbool.h>
10 #include <zephyr/ztest.h>
11 #include <zephyr/drivers/flash.h>
12 #include <zephyr/settings/settings.h>
13 
14 #include <zephyr/storage/stream_flash.h>
15 
16 #define BUF_LEN 512
17 #define MAX_PAGE_SIZE 0x1000 /* Max supported page size to run test on */
18 #define MAX_NUM_PAGES 4      /* Max number of pages used in these tests */
19 #define TESTBUF_SIZE (MAX_PAGE_SIZE * MAX_NUM_PAGES)
20 #define SOC_NV_FLASH_NODE DT_INST(0, soc_nv_flash)
21 #define FLASH_SIZE DT_REG_SIZE(SOC_NV_FLASH_NODE)
22 
23 /* so that we don't overwrite the application when running on hw */
24 #define FLASH_BASE (128*1024)
25 #define FLASH_AVAILABLE (FLASH_SIZE-FLASH_BASE)
26 
27 static const struct device *const fdev = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
28 static const struct flash_driver_api *api;
29 static const struct flash_pages_layout *layout;
30 static size_t layout_size;
31 static struct stream_flash_ctx ctx;
32 static int page_size;
33 static uint8_t *cb_buf;
34 static size_t cb_len;
35 static size_t cb_offset;
36 static int cb_ret;
37 
38 static const char progress_key[] = "sf-test/progress";
39 
40 static uint8_t generic_buf[BUF_LEN];
41 static uint8_t read_buf[TESTBUF_SIZE];
42 const static uint8_t write_buf[TESTBUF_SIZE] = {[0 ... TESTBUF_SIZE - 1] = 0xaa};
43 static uint8_t written_pattern[TESTBUF_SIZE] = {[0 ... TESTBUF_SIZE - 1] = 0xaa};
44 #if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
45 static uint8_t erased_pattern[TESTBUF_SIZE]  = {[0 ... TESTBUF_SIZE - 1] = 0xff};
46 #endif
47 
48 #define VERIFY_BUF(start, size, buf) \
49 do { \
50 	rc = flash_read(fdev, FLASH_BASE + start, read_buf, size); \
51 	zassert_equal(rc, 0, "should succeed"); \
52 	zassert_mem_equal(read_buf, buf, size, "should equal %s", #buf);\
53 } while (0)
54 
55 #define VERIFY_WRITTEN(start, size) VERIFY_BUF(start, size, written_pattern)
56 #if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
57 #define VERIFY_ERASED(start, size) VERIFY_BUF(start, size, erased_pattern)
58 #else
59 #define VERIFY_ERASED(start, size)
60 #endif
61 
stream_flash_callback(uint8_t * buf,size_t len,size_t offset)62 int stream_flash_callback(uint8_t *buf, size_t len, size_t offset)
63 {
64 	if (cb_buf) {
65 		zassert_equal(cb_buf, buf, "incorrect buf");
66 		zassert_equal(cb_len, len, "incorrect length");
67 		zassert_equal(cb_offset, offset, "incorrect offset");
68 	}
69 
70 	return cb_ret;
71 }
72 
erase_flash(void)73 static void erase_flash(void)
74 {
75 #if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
76 	int rc;
77 #if defined(CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE)
78 	const struct flash_parameters *fparam = flash_get_parameters(fdev);
79 
80 	if (!(flash_params_get_erase_cap(fparam) & FLASH_ERASE_C_EXPLICIT)) {
81 		return;
82 	}
83 #endif
84 
85 	for (int i = 0; i < MAX_NUM_PAGES; i++) {
86 		rc = flash_erase(fdev,
87 				 FLASH_BASE + (i * layout->pages_size),
88 				 layout->pages_size);
89 		zassert_equal(rc, 0, "should succeed");
90 	}
91 #endif
92 }
93 
94 
init_target(void)95 static void init_target(void)
96 {
97 	int rc;
98 
99 	/* Ensure that target is clean */
100 	memset(&ctx, 0, sizeof(ctx));
101 	memset(generic_buf, 0, BUF_LEN);
102 
103 	/* Disable callback tests */
104 	cb_len = 0;
105 	cb_offset = 0;
106 	cb_buf = NULL;
107 	cb_ret = 0;
108 
109 	erase_flash();
110 
111 	rc = stream_flash_init(&ctx, fdev, generic_buf, BUF_LEN, FLASH_BASE, FLASH_AVAILABLE,
112 			       stream_flash_callback);
113 	zassert_equal(rc, 0, "expected success");
114 }
115 
ZTEST(lib_stream_flash,test_stream_flash_init)116 ZTEST(lib_stream_flash, test_stream_flash_init)
117 {
118 	int rc;
119 
120 	init_target();
121 
122 	/* End address out of range */
123 	rc = stream_flash_init(&ctx, fdev, generic_buf, BUF_LEN, FLASH_BASE,
124 		      FLASH_AVAILABLE + 4, NULL);
125 	zassert_true(rc < 0, "should fail as size is more than available");
126 
127 	rc = stream_flash_init(NULL, fdev, generic_buf, BUF_LEN, FLASH_BASE, 0, NULL);
128 	zassert_true(rc < 0, "should fail as ctx is NULL");
129 
130 	rc = stream_flash_init(&ctx, NULL, generic_buf, BUF_LEN, FLASH_BASE, 0, NULL);
131 	zassert_true(rc < 0, "should fail as fdev is NULL");
132 
133 	rc = stream_flash_init(&ctx, fdev, NULL, BUF_LEN, FLASH_BASE, 0, NULL);
134 	zassert_true(rc < 0, "should fail as buffer is NULL");
135 
136 	rc = stream_flash_init(&ctx, fdev, generic_buf, BUF_LEN, FLASH_BASE, 0, NULL);
137 	zassert_equal(rc, -EFAULT, "should fail as size 0");
138 
139 	rc = stream_flash_init(&ctx, fdev, generic_buf, BUF_LEN,
140 			       flash_get_write_block_size(ctx.fdev) - 1, 0, NULL);
141 	zassert_equal(rc, -EFAULT, "should fail as size is not aligned to write block size ");
142 	flash_get_write_block_size(ctx.fdev);
143 }
144 
ZTEST(lib_stream_flash,test_stream_flash_buffered_write)145 ZTEST(lib_stream_flash, test_stream_flash_buffered_write)
146 {
147 	int rc;
148 
149 	init_target();
150 
151 	/* Don't fill up the buffer */
152 	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 1, false);
153 	zassert_equal(rc, 0, "expected success");
154 
155 	/* Verify that no data has been written */
156 	VERIFY_ERASED(0, BUF_LEN);
157 
158 	/* Now, write the missing byte, which should trigger a dump to flash */
159 	rc = stream_flash_buffered_write(&ctx, write_buf, 1, false);
160 	zassert_equal(rc, 0, "expected success");
161 
162 	VERIFY_WRITTEN(0, BUF_LEN);
163 }
164 
ZTEST(lib_stream_flash,test_stream_flash_buffered_write_cross_buf_border)165 ZTEST(lib_stream_flash, test_stream_flash_buffered_write_cross_buf_border)
166 {
167 	int rc;
168 
169 	init_target();
170 
171 	/* Test when write crosses border of the buffer */
172 	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN + 128, false);
173 	zassert_equal(rc, 0, "expected success");
174 
175 	/* 1xBuffer should be dumped to flash */
176 	VERIFY_WRITTEN(0, BUF_LEN);
177 
178 	/* Fill rest of the buffer */
179 	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 128, false);
180 	zassert_equal(rc, 0, "expected success");
181 	VERIFY_WRITTEN(BUF_LEN, BUF_LEN);
182 
183 	/* Fill half of the buffer */
184 	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN/2, false);
185 	zassert_equal(rc, 0, "expected success");
186 
187 	/* Flush the buffer */
188 	rc = stream_flash_buffered_write(&ctx, write_buf, 0, true);
189 	zassert_equal(rc, 0, "expected success");
190 
191 	/* Two and a half buffers should be written */
192 	VERIFY_WRITTEN(0, BUF_LEN * 2 + BUF_LEN / 2);
193 }
194 
ZTEST(lib_stream_flash,test_stream_flash_buffered_write_unaligned)195 ZTEST(lib_stream_flash, test_stream_flash_buffered_write_unaligned)
196 {
197 	int rc;
198 
199 	if (flash_get_write_block_size(fdev) == 1) {
200 		ztest_test_skip();
201 	}
202 
203 	init_target();
204 
205 	/* Test unaligned data size */
206 	rc = stream_flash_buffered_write(&ctx, write_buf, 1, true);
207 	zassert_equal(rc, 0, "expected success (%d)", rc);
208 
209 	/* 1 byte should be dumped to flash */
210 	VERIFY_WRITTEN(0, 1);
211 
212 	rc = stream_flash_init(&ctx, fdev, generic_buf, BUF_LEN, FLASH_BASE + BUF_LEN,
213 			       FLASH_AVAILABLE - BUF_LEN, stream_flash_callback);
214 	zassert_equal(rc, 0, "expected success");
215 
216 	/* Trigger verification in callback */
217 	cb_buf = generic_buf;
218 	cb_len = BUF_LEN - 1;
219 	cb_offset = FLASH_BASE + BUF_LEN;
220 
221 	/* Test unaligned data size */
222 	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 1, true);
223 	zassert_equal(rc, 0, "expected success");
224 
225 	/* BUF_LEN-1 bytes should be dumped to flash */
226 	VERIFY_WRITTEN(BUF_LEN, BUF_LEN - 1);
227 }
228 
ZTEST(lib_stream_flash,test_stream_flash_buffered_write_multi_page)229 ZTEST(lib_stream_flash, test_stream_flash_buffered_write_multi_page)
230 {
231 	int rc;
232 	int num_pages = MAX_NUM_PAGES - 1;
233 
234 	init_target();
235 
236 	/* Test when write spans multiple pages crosses border of page */
237 	rc = stream_flash_buffered_write(&ctx, write_buf,
238 					 (page_size * num_pages) + 128, false);
239 	zassert_equal(rc, 0, "expected success");
240 
241 	/* First three pages should be written */
242 	VERIFY_WRITTEN(0, page_size * num_pages);
243 
244 	/* Fill rest of the page */
245 	rc = stream_flash_buffered_write(&ctx, write_buf,
246 					 page_size - 128, false);
247 	zassert_equal(rc, 0, "expected success");
248 
249 	/* First four pages should be written */
250 	VERIFY_WRITTEN(0, BUF_LEN * (num_pages + 1));
251 }
252 
ZTEST(lib_stream_flash,test_stream_flash_bytes_written)253 ZTEST(lib_stream_flash, test_stream_flash_bytes_written)
254 {
255 	int rc;
256 	size_t offset;
257 
258 	init_target();
259 
260 	/* Verify that the offset is retained across failed downloads */
261 	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN + 128, false);
262 	zassert_equal(rc, 0, "expected success");
263 
264 	/* First page should be written */
265 	VERIFY_WRITTEN(0, BUF_LEN);
266 
267 	/* Fill rest of the page */
268 	offset = stream_flash_bytes_written(&ctx);
269 	zassert_equal(offset, BUF_LEN, "offset should match buf size");
270 
271 	/* Fill up the buffer MINUS 128 to verify that write_buf_pos is kept */
272 	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 128, false);
273 	zassert_equal(rc, 0, "expected success");
274 
275 	/* Second page should be written */
276 	VERIFY_WRITTEN(BUF_LEN, BUF_LEN);
277 }
278 
ZTEST(lib_stream_flash,test_stream_flash_buf_size_greater_than_page_size)279 ZTEST(lib_stream_flash, test_stream_flash_buf_size_greater_than_page_size)
280 {
281 	int rc;
282 
283 	/* To illustrate that other params does not trigger error */
284 	rc = stream_flash_init(&ctx, fdev, generic_buf, 0x10, 0, FLASH_AVAILABLE, NULL);
285 	zassert_equal(rc, 0, "expected success");
286 
287 	/* Only change buf_len param */
288 	rc = stream_flash_init(&ctx, fdev, generic_buf, 0x10000, 0, FLASH_AVAILABLE, NULL);
289 	zassert_true(rc < 0, "expected failure");
290 }
291 
bad_read(const struct device * dev,off_t off,void * data,size_t len)292 static int bad_read(const struct device *dev, off_t off, void *data, size_t len)
293 {
294 	return -EINVAL;
295 }
296 
fake_write(const struct device * dev,off_t off,const void * data,size_t len)297 static int fake_write(const struct device *dev, off_t off, const void *data, size_t len)
298 {
299 	return 0;
300 }
301 
bad_write(const struct device * dev,off_t off,const void * data,size_t len)302 static int bad_write(const struct device *dev, off_t off, const void *data, size_t len)
303 {
304 	return -EINVAL;
305 }
306 
ZTEST(lib_stream_flash,test_stream_flash_buffered_write_callback)307 ZTEST(lib_stream_flash, test_stream_flash_buffered_write_callback)
308 {
309 	int rc;
310 
311 	init_target();
312 
313 	/* Trigger verification in callback */
314 	cb_buf = generic_buf;
315 	cb_len = BUF_LEN;
316 	cb_offset = FLASH_BASE;
317 
318 	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN + 128, false);
319 	zassert_equal(rc, 0, "expected success");
320 
321 	cb_len = BUF_LEN;
322 	cb_offset = FLASH_BASE + BUF_LEN;
323 
324 	/* Fill rest of the buffer */
325 	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 128, false);
326 	zassert_equal(rc, 0, "expected success");
327 	VERIFY_WRITTEN(BUF_LEN, BUF_LEN);
328 
329 	/* Fill half of the buffer and flush it to flash */
330 	cb_len = BUF_LEN/2;
331 	cb_offset = FLASH_BASE + (2 * BUF_LEN);
332 
333 	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN/2, true);
334 	zassert_equal(rc, 0, "expected success");
335 
336 	/* Ensure that failing callback trickles up to caller */
337 	cb_ret = -EFAULT;
338 	cb_buf = NULL; /* Don't verify other parameters of the callback */
339 	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN, true);
340 	zassert_equal(rc, -EFAULT, "expected failure from callback");
341 	/* Expect that the BUF_LEN of bytes got stuck in buffer as the  verification callback
342 	 * failed.
343 	 */
344 	zassert_equal(ctx.buf_bytes, BUF_LEN, "Expected bytes to be left in buffer");
345 
346 	struct device fake_dev = *ctx.fdev;
347 	struct flash_driver_api fake_api = *(struct flash_driver_api *)ctx.fdev->api;
348 	struct stream_flash_ctx bad_ctx = ctx;
349 	struct stream_flash_ctx cmp_ctx;
350 
351 	fake_api.read = bad_read;
352 	/* Using fake write here because after previous write, with faked callback failure,
353 	 * the flash is already written and real flash_write would cause failure.
354 	 */
355 	fake_api.write = fake_write;
356 	fake_dev.api = &fake_api;
357 	bad_ctx.fdev = &fake_dev;
358 	/* Trigger erase attempt */
359 	cmp_ctx = bad_ctx;
360 	/* Just flush buffer */
361 	rc = stream_flash_buffered_write(&bad_ctx, write_buf, 0, true);
362 	zassert_equal(rc, -EINVAL, "expected failure from flash_sync", rc);
363 	zassert_equal(ctx.buf_bytes, BUF_LEN, "Expected bytes to be left in buffer");
364 
365 	/* Pretend flashed context and attempt write write block - 1 bytes to trigger unaligned
366 	 * write; the write needs to fail so that we could check that context does not get modified.
367 	 */
368 	fake_api.write = bad_write;
369 	bad_ctx.callback = NULL;
370 	bad_ctx.buf_bytes = 0;
371 	cmp_ctx = bad_ctx;
372 	size_t wblock = flash_get_write_block_size(ctx.fdev);
373 	size_t tow = (wblock == 1) ? 1 : wblock - 1;
374 
375 	rc = stream_flash_buffered_write(&bad_ctx, write_buf, tow, true);
376 	zassert_equal(rc, -EINVAL, "expected failure from flash_sync", rc);
377 	zassert_equal(cmp_ctx.bytes_written, bad_ctx.bytes_written,
378 		      "Expected bytes_written not modified");
379 	/* The write failed but bytes have already been added to buffer and buffer offset
380 	 * increased.
381 	 */
382 	zassert_equal(bad_ctx.buf_bytes, cmp_ctx.buf_bytes + tow,
383 		      "Expected %d bytes added to buffer", tow);
384 }
385 
ZTEST(lib_stream_flash,test_stream_flash_flush)386 ZTEST(lib_stream_flash, test_stream_flash_flush)
387 {
388 	int rc;
389 
390 	init_target();
391 
392 	/* Perform flush with NULL data pointer and 0 length */
393 	rc = stream_flash_buffered_write(&ctx, NULL, 0, true);
394 	zassert_equal(rc, 0, "expected success");
395 }
396 
397 #ifdef CONFIG_STREAM_FLASH_ERASE
ZTEST(lib_stream_flash,test_stream_flash_buffered_write_whole_page)398 ZTEST(lib_stream_flash, test_stream_flash_buffered_write_whole_page)
399 {
400 	int rc;
401 
402 	init_target();
403 
404 	/* Write all bytes of a page, verify that next page is not erased */
405 
406 	/* First fill two pages with data */
407 	rc = stream_flash_buffered_write(&ctx, write_buf, page_size * 2, true);
408 	zassert_equal(rc, 0, "expected success");
409 
410 	VERIFY_WRITTEN(0, page_size);
411 	VERIFY_WRITTEN(page_size, page_size);
412 
413 	/* Reset stream_flash context */
414 	memset(&ctx, 0, sizeof(ctx));
415 	memset(generic_buf, 0, BUF_LEN);
416 	rc = stream_flash_init(&ctx, fdev, generic_buf, BUF_LEN, FLASH_BASE, FLASH_AVAILABLE,
417 			       stream_flash_callback);
418 	zassert_equal(rc, 0, "expected success");
419 
420 	/* Write all bytes of a page, verify that next page is not erased */
421 	rc = stream_flash_buffered_write(&ctx, write_buf, page_size, true);
422 	zassert_equal(rc, 0, "expected success");
423 
424 	/* Second page should not be erased */
425 	VERIFY_WRITTEN(page_size, page_size);
426 }
427 #endif
428 
write_and_save_progress(size_t bytes,const char * save_key)429 static size_t write_and_save_progress(size_t bytes, const char *save_key)
430 {
431 	int rc;
432 	size_t bytes_written;
433 
434 	rc = stream_flash_buffered_write(&ctx, write_buf, bytes, true);
435 	zassert_equal(rc, 0, "expected success");
436 
437 	bytes_written = stream_flash_bytes_written(&ctx);
438 	zassert_true(bytes_written > 0, "expected bytes to be written");
439 
440 	if (save_key) {
441 		rc = stream_flash_progress_save(&ctx, save_key);
442 		zassert_equal(rc, 0, "expected success");
443 	}
444 
445 	return bytes_written;
446 }
447 
clear_all_progress(void)448 static void clear_all_progress(void)
449 {
450 	(void) settings_delete(progress_key);
451 }
452 
load_progress(const char * load_key)453 static size_t load_progress(const char *load_key)
454 {
455 	int rc;
456 
457 	rc = stream_flash_progress_load(&ctx, progress_key);
458 	zassert_equal(rc, 0, "expected success");
459 
460 	return stream_flash_bytes_written(&ctx);
461 }
462 
ZTEST(lib_stream_flash,test_stream_flash_progress_api)463 ZTEST(lib_stream_flash, test_stream_flash_progress_api)
464 {
465 	int rc;
466 
467 	clear_all_progress();
468 	init_target();
469 
470 	/* Test save parameter validation */
471 	rc = stream_flash_progress_save(NULL, progress_key);
472 	zassert_true(rc < 0, "expected error since ctx is NULL");
473 
474 	rc = stream_flash_progress_save(&ctx, NULL);
475 	zassert_true(rc < 0, "expected error since key is NULL");
476 
477 	rc = stream_flash_progress_save(&ctx, progress_key);
478 	zassert_equal(rc, 0, "expected success");
479 
480 	(void) write_and_save_progress(BUF_LEN, progress_key);
481 
482 	/* Test load parameter validation */
483 	rc = stream_flash_progress_load(NULL, progress_key);
484 	zassert_true(rc < 0, "expected error since ctx is NULL");
485 
486 	rc = stream_flash_progress_load(&ctx, NULL);
487 	zassert_true(rc < 0, "expected error since key is NULL");
488 
489 	rc = stream_flash_progress_load(&ctx, progress_key);
490 	zassert_equal(rc, 0, "expected success");
491 
492 	/* Test clear parameter validation */
493 	rc = stream_flash_progress_clear(NULL, progress_key);
494 	zassert_true(rc < 0, "expected error since ctx is NULL");
495 
496 	rc = stream_flash_progress_clear(&ctx, NULL);
497 	zassert_true(rc < 0, "expected error since key is NULL");
498 
499 	rc = stream_flash_progress_clear(&ctx, progress_key);
500 	zassert_equal(rc, 0, "expected success");
501 }
502 
ZTEST(lib_stream_flash,test_stream_flash_progress_resume)503 ZTEST(lib_stream_flash, test_stream_flash_progress_resume)
504 {
505 	int rc;
506 	size_t bytes_written_old;
507 	size_t bytes_written;
508 #ifdef CONFIG_STREAM_FLASH_ERASE
509 	size_t erased_up_to_old;
510 #endif
511 
512 	clear_all_progress();
513 	init_target();
514 
515 	bytes_written_old = stream_flash_bytes_written(&ctx);
516 #ifdef CONFIG_STREAM_FLASH_ERASE
517 	erased_up_to_old = ctx.erased_up_to;
518 #endif
519 
520 	/* Test load with zero bytes_written */
521 	rc = stream_flash_progress_save(&ctx, progress_key);
522 	zassert_equal(rc, 0, "expected success");
523 
524 	rc = stream_flash_progress_load(&ctx, progress_key);
525 	zassert_equal(rc, 0, "expected success");
526 
527 	bytes_written = stream_flash_bytes_written(&ctx);
528 	zassert_equal(bytes_written, bytes_written_old,
529 		      "expected bytes_written to be unchanged");
530 #ifdef CONFIG_STREAM_FLASH_ERASE
531 	zassert_equal(ctx.erased_up_to, erased_up_to_old,
532 		      "expected erase offset to be unchanged");
533 #endif
534 
535 	clear_all_progress();
536 	init_target();
537 
538 	/* Write some data and save the progress */
539 	bytes_written_old = write_and_save_progress(page_size * 2,
540 						    progress_key);
541 #ifdef CONFIG_STREAM_FLASH_ERASE
542 	zassert_false(ctx.erased_up_to == 0, "expected pages to be erased");
543 	erased_up_to_old = ctx.erased_up_to;
544 #endif
545 
546 	init_target();
547 
548 	/* Load the previous progress */
549 	bytes_written = load_progress(progress_key);
550 	zassert_equal(bytes_written, bytes_written_old,
551 		      "expected bytes_written to be loaded");
552 #if defined(CONFIG_STREAM_FLASH_ERASE)
553 	zassert_equal(erased_up_to_old, ctx.erased_up_to,
554 		      "expected last erased page offset to be loaded");
555 #endif
556 
557 	/* Check that outdated progress does not overwrite current progress */
558 	init_target();
559 
560 	(void) write_and_save_progress(BUF_LEN, progress_key);
561 	bytes_written_old = write_and_save_progress(BUF_LEN, NULL);
562 	bytes_written = load_progress(progress_key);
563 	zassert_equal(bytes_written, bytes_written_old,
564 		      "expected bytes_written to not be overwritten");
565 }
566 
ZTEST(lib_stream_flash,test_stream_flash_progress_clear)567 ZTEST(lib_stream_flash, test_stream_flash_progress_clear)
568 {
569 	int rc;
570 	size_t bytes_written_old;
571 	size_t bytes_written;
572 #ifdef CONFIG_STREAM_FLASH_ERASE
573 	off_t erase_offset_old;
574 #endif
575 
576 	clear_all_progress();
577 	init_target();
578 
579 	/* Test that progress is cleared. */
580 	(void) write_and_save_progress(BUF_LEN, progress_key);
581 
582 	rc = stream_flash_progress_clear(&ctx, progress_key);
583 	zassert_equal(rc, 0, "expected success");
584 
585 	init_target();
586 
587 	bytes_written_old = stream_flash_bytes_written(&ctx);
588 #ifdef CONFIG_STREAM_FLASH_ERASE
589 	erase_offset_old = ctx.erased_up_to;
590 #endif
591 
592 	rc = stream_flash_progress_load(&ctx, progress_key);
593 	zassert_equal(rc, 0, "expected success");
594 
595 	bytes_written = stream_flash_bytes_written(&ctx);
596 	zassert_equal(bytes_written, bytes_written_old,
597 		      "expected bytes_written to be unchanged");
598 
599 #ifdef CONFIG_STREAM_FLASH_ERASE
600 	zassert_equal(ctx.erased_up_to, erase_offset_old,
601 		      "expected erase offset to be unchanged");
602 #endif
603 }
604 
lib_stream_flash_before(void * data)605 void lib_stream_flash_before(void *data)
606 {
607 	zassume_true(device_is_ready(fdev), "Device is not ready");
608 
609 	api = fdev->api;
610 	api->page_layout(fdev, &layout, &layout_size);
611 
612 	page_size = layout->pages_size;
613 	zassume_true((page_size > BUF_LEN), "page size is not enough");
614 }
615 
616 ZTEST_SUITE(lib_stream_flash, NULL, NULL, lib_stream_flash_before, NULL, NULL);
617