Linux Audio

Check our new training course

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <string.h>
#include <zephyr/types.h>
#include <stdbool.h>
#include <ztest.h>
#include <drivers/flash.h>
#include <settings/settings.h>

#include <storage/stream_flash.h>

#define BUF_LEN 512
#define MAX_PAGE_SIZE 0x1000 /* Max supported page size to run test on */
#define MAX_NUM_PAGES 4      /* Max number of pages used in these tests */
#define TESTBUF_SIZE (MAX_PAGE_SIZE * MAX_NUM_PAGES)
#define SOC_NV_FLASH_NODE DT_INST(0, soc_nv_flash)
#define FLASH_SIZE DT_REG_SIZE(SOC_NV_FLASH_NODE)
#define FLASH_NAME DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL

/* so that we don't overwrite the application when running on hw */
#define FLASH_BASE (64*1024)
#define FLASH_AVAILABLE (FLASH_SIZE-FLASH_BASE)

static const struct device *fdev;
static const struct flash_driver_api *api;
static const struct flash_pages_layout *layout;
static size_t layout_size;
static struct stream_flash_ctx ctx;
static int page_size;
static uint8_t *cb_buf;
static size_t cb_len;
static size_t cb_offset;
static int cb_ret;

static const char progress_key[] = "sf-test/progress";

static uint8_t buf[BUF_LEN];
static uint8_t read_buf[TESTBUF_SIZE];
const static uint8_t write_buf[TESTBUF_SIZE] = {[0 ... TESTBUF_SIZE - 1] = 0xaa};
static uint8_t written_pattern[TESTBUF_SIZE] = {[0 ... TESTBUF_SIZE - 1] = 0xaa};
static uint8_t erased_pattern[TESTBUF_SIZE]  = {[0 ... TESTBUF_SIZE - 1] = 0xff};

#define VERIFY_BUF(start, size, buf) \
do { \
	rc = flash_read(fdev, FLASH_BASE + start, read_buf, size); \
	zassert_equal(rc, 0, "should succeed"); \
	zassert_mem_equal(read_buf, buf, size, "should equal %s", #buf);\
} while (0)

#define VERIFY_WRITTEN(start, size) VERIFY_BUF(start, size, written_pattern)
#define VERIFY_ERASED(start, size) VERIFY_BUF(start, size, erased_pattern)

int stream_flash_callback(uint8_t *buf, size_t len, size_t offset)
{
	if (cb_buf) {
		zassert_equal(cb_buf, buf, "incorrect buf");
		zassert_equal(cb_len, len, "incorrect length");
		zassert_equal(cb_offset, offset, "incorrect offset");
	}

	return cb_ret;
}

static void erase_flash(void)
{
	int rc;

	for (int i = 0; i < MAX_NUM_PAGES; i++) {
		rc = flash_erase(fdev,
				 FLASH_BASE + (i * layout->pages_size),
				 layout->pages_size);
		zassert_equal(rc, 0, "should succeed");
	}
}


static void init_target(void)
{
	int rc;

	/* Ensure that target is clean */
	memset(&ctx, 0, sizeof(ctx));
	memset(buf, 0, BUF_LEN);

	/* Disable callback tests */
	cb_len = 0;
	cb_offset = 0;
	cb_buf = NULL;
	cb_ret = 0;

	erase_flash();

	rc = stream_flash_init(&ctx, fdev, buf, BUF_LEN, FLASH_BASE, 0,
			       stream_flash_callback);
	zassert_equal(rc, 0, "expected success");
}

static void test_stream_flash_init(void)
{
	int rc;

	init_target();

	/* End address out of range */
	rc = stream_flash_init(&ctx, fdev, buf, BUF_LEN, FLASH_BASE,
		      FLASH_AVAILABLE + 4, NULL);
	zassert_true(rc < 0, "should fail as size is more than available");

	rc = stream_flash_init(NULL, fdev, buf, BUF_LEN, FLASH_BASE, 0, NULL);
	zassert_true(rc < 0, "should fail as ctx is NULL");

	rc = stream_flash_init(&ctx, NULL, buf, BUF_LEN, FLASH_BASE, 0, NULL);
	zassert_true(rc < 0, "should fail as fdev is NULL");

	rc = stream_flash_init(&ctx, fdev, NULL, BUF_LEN, FLASH_BASE, 0, NULL);
	zassert_true(rc < 0, "should fail as buffer is NULL");

	/* Entering '0' as flash size uses rest of flash. */
	rc = stream_flash_init(&ctx, fdev, buf, BUF_LEN, FLASH_BASE, 0, NULL);
	zassert_equal(rc, 0, "should succeed");
	zassert_equal(FLASH_AVAILABLE, ctx.available, "Wrong size");
}

static void test_stream_flash_buffered_write(void)
{
	int rc;

	init_target();

	/* Don't fill up the buffer */
	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 1, false);
	zassert_equal(rc, 0, "expected success");

	/* Verify that no data has been written */
	VERIFY_ERASED(0, BUF_LEN);

	/* Now, write the missing byte, which should trigger a dump to flash */
	rc = stream_flash_buffered_write(&ctx, write_buf, 1, false);
	zassert_equal(rc, 0, "expected success");

	VERIFY_WRITTEN(0, BUF_LEN);
}

static void test_stream_flash_buffered_write_cross_buf_border(void)
{
	int rc;

	init_target();

	/* Test when write crosses border of the buffer */
	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN + 128, false);
	zassert_equal(rc, 0, "expected success");

	/* 1xBuffer should be dumped to flash */
	VERIFY_WRITTEN(0, BUF_LEN);

	/* Fill rest of the buffer */
	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 128, false);
	zassert_equal(rc, 0, "expected success");
	VERIFY_WRITTEN(BUF_LEN, BUF_LEN);

	/* Fill half of the buffer */
	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN/2, false);
	zassert_equal(rc, 0, "expected success");

	/* Flush the buffer */
	rc = stream_flash_buffered_write(&ctx, write_buf, 0, true);
	zassert_equal(rc, 0, "expected success");

	/* Two and a half buffers should be written */
	VERIFY_WRITTEN(0, BUF_LEN * 2 + BUF_LEN / 2);
}

static void test_stream_flash_buffered_write_unaligned(void)
{
	int rc;

	if (flash_get_write_block_size(fdev) == 1) {
		ztest_test_skip();
	}

	init_target();

	/* Test unaligned data size */
	rc = stream_flash_buffered_write(&ctx, write_buf, 1, true);
	zassert_equal(rc, 0, "expected success (%d)", rc);

	/* 1 byte should be dumped to flash */
	VERIFY_WRITTEN(0, 1);

	rc = stream_flash_init(&ctx, fdev, buf, BUF_LEN, FLASH_BASE + BUF_LEN,
			       0, stream_flash_callback);
	zassert_equal(rc, 0, "expected success");

	/* Trigger verification in callback */
	cb_buf = buf;
	cb_len = BUF_LEN - 1;
	cb_offset = FLASH_BASE + BUF_LEN;

	/* Test unaligned data size */
	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 1, true);
	zassert_equal(rc, 0, "expected success");

	/* BUF_LEN-1 bytes should be dumped to flash */
	VERIFY_WRITTEN(BUF_LEN, BUF_LEN - 1);
}

static void test_stream_flash_buffered_write_multi_page(void)
{
	int rc;
	int num_pages = MAX_NUM_PAGES - 1;

	init_target();

	/* Test when write spans multiple pages crosses border of page */
	rc = stream_flash_buffered_write(&ctx, write_buf,
					 (page_size * num_pages) + 128, false);
	zassert_equal(rc, 0, "expected success");

	/* First three pages should be written */
	VERIFY_WRITTEN(0, page_size * num_pages);

	/* Fill rest of the page */
	rc = stream_flash_buffered_write(&ctx, write_buf,
					 page_size - 128, false);
	zassert_equal(rc, 0, "expected success");

	/* First four pages should be written */
	VERIFY_WRITTEN(0, BUF_LEN * (num_pages + 1));
}

static void test_stream_flash_bytes_written(void)
{
	int rc;
	size_t offset;

	init_target();

	/* Verify that the offset is retained across failed downolads */
	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN + 128, false);
	zassert_equal(rc, 0, "expected success");

	/* First page should be written */
	VERIFY_WRITTEN(0, BUF_LEN);

	/* Fill rest of the page */
	offset = stream_flash_bytes_written(&ctx);
	zassert_equal(offset, BUF_LEN, "offset should match buf size");

	/* Fill up the buffer MINUS 128 to verify that write_buf_pos is kept */
	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 128, false);
	zassert_equal(rc, 0, "expected success");

	/* Second page should be written */
	VERIFY_WRITTEN(BUF_LEN, BUF_LEN);
}

static void test_stream_flash_buf_size_greater_than_page_size(void)
{
	int rc;

	/* To illustrate that other params does not trigger error */
	rc = stream_flash_init(&ctx, fdev, buf, 0x10, 0, 0, NULL);
	zassert_equal(rc, 0, "expected success");

	/* Only change buf_len param */
	rc = stream_flash_init(&ctx, fdev, buf, 0x10000, 0, 0, NULL);
	zassert_true(rc < 0, "expected failure");
}

static int bad_read(const struct device *dev, off_t off, void *data, size_t len)
{
	return -EINVAL;
}

static int fake_write(const struct device *dev, off_t off, const void *data, size_t len)
{
	return 0;
}

static int bad_write(const struct device *dev, off_t off, const void *data, size_t len)
{
	return -EINVAL;
}

static void test_stream_flash_buffered_write_callback(void)
{
	int rc;

	init_target();

	/* Trigger verification in callback */
	cb_buf = buf;
	cb_len = BUF_LEN;
	cb_offset = FLASH_BASE;

	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN + 128, false);
	zassert_equal(rc, 0, "expected success");

	cb_len = BUF_LEN;
	cb_offset = FLASH_BASE + BUF_LEN;

	/* Fill rest of the buffer */
	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 128, false);
	zassert_equal(rc, 0, "expected success");
	VERIFY_WRITTEN(BUF_LEN, BUF_LEN);

	/* Fill half of the buffer and flush it to flash */
	cb_len = BUF_LEN/2;
	cb_offset = FLASH_BASE + (2 * BUF_LEN);

	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN/2, true);
	zassert_equal(rc, 0, "expected success");

	/* Ensure that failing callback trickles up to caller */
	cb_ret = -EFAULT;
	cb_buf = NULL; /* Don't verify other parameters of the callback */
	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN, true);
	zassert_equal(rc, -EFAULT, "expected failure from callback");
	/* Expect that the BUF_LEN of bytes got stuck in buffer as the  verification callback
	 * failed.
	 */
	zassert_equal(ctx.buf_bytes, BUF_LEN, "Expected bytes to be left in buffer");

	struct device fake_dev = *ctx.fdev;
	struct flash_driver_api fake_api = *(struct flash_driver_api *)ctx.fdev->api;
	struct stream_flash_ctx bad_ctx = ctx;
	struct stream_flash_ctx cmp_ctx;

	fake_api.read = bad_read;
	/* Using fake write here because after previous write, with faked callback failure,
	 * the flash is already written and real flash_write would cause failure.
	 */
	fake_api.write = fake_write;
	fake_dev.api = &fake_api;
	bad_ctx.fdev = &fake_dev;
	/* Triger erase attempt */
	cmp_ctx = bad_ctx;
	/* Just flush buffer */
	rc = stream_flash_buffered_write(&bad_ctx, write_buf, 0, true);
	zassert_equal(rc, -EINVAL, "expected failure from flash_sync", rc);
	zassert_equal(ctx.buf_bytes, BUF_LEN, "Expected bytes to be left in buffer");

	/* Pretend flashed context and attempt write write block - 1 bytes to trigger unaligned
	 * write; the write needs to fail so that we could check that context does not get modified.
	 */
	fake_api.write = bad_write;
	bad_ctx.callback = NULL;
	bad_ctx.buf_bytes = 0;
	cmp_ctx = bad_ctx;
	size_t wblock = flash_get_write_block_size(ctx.fdev);
	size_t tow = (wblock == 1) ? 1 : wblock - 1;

	rc = stream_flash_buffered_write(&bad_ctx, write_buf, tow, true);
	zassert_equal(rc, -EINVAL, "expected failure from flash_sync", rc);
	zassert_equal(cmp_ctx.bytes_written, bad_ctx.bytes_written,
		      "Expected bytes_written not modified");
	/* The write failed but bytes have already been added to buffer and buffer offset
	 * increased.
	 */
	zassert_equal(bad_ctx.buf_bytes, cmp_ctx.buf_bytes + tow,
		      "Expected %d bytes added to buffer", tow);
}

static void test_stream_flash_flush(void)
{
	int rc;

	init_target();

	/* Perform flush with NULL data pointer and 0 lentgth */
	rc = stream_flash_buffered_write(&ctx, NULL, 0, true);
	zassert_equal(rc, 0, "expected success");
}

#ifdef CONFIG_STREAM_FLASH_ERASE
static void test_stream_flash_buffered_write_whole_page(void)
{
	int rc;

	init_target();

	/* Write all bytes of a page, verify that next page is not erased */

	/* First fill two pages with data */
	rc = stream_flash_buffered_write(&ctx, write_buf, page_size * 2, true);
	zassert_equal(rc, 0, "expected success");

	VERIFY_WRITTEN(0, page_size);
	VERIFY_WRITTEN(page_size, page_size);

	/* Reset stream_flash context */
	memset(&ctx, 0, sizeof(ctx));
	memset(buf, 0, BUF_LEN);
	rc = stream_flash_init(&ctx, fdev, buf, BUF_LEN, FLASH_BASE, 0,
			       stream_flash_callback);
	zassert_equal(rc, 0, "expected success");

	/* Write all bytes of a page, verify that next page is not erased */
	rc = stream_flash_buffered_write(&ctx, write_buf, page_size, true);
	zassert_equal(rc, 0, "expected success");

	/* Second page should not be erased */
	VERIFY_WRITTEN(page_size, page_size);
}

/* Erase that never completes successfully */
static int bad_erase(const struct device *dev, off_t offset, size_t size)
{
	return -EINVAL;
}

static void test_stream_flash_erase_page(void)
{
	int rc;

	init_target();

	/* Write out one buf */
	rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN, false);
	zassert_equal(rc, 0, "expected success");

	rc = stream_flash_erase_page(&ctx, FLASH_BASE);
	zassert_equal(rc, 0, "expected success");

	VERIFY_ERASED(FLASH_BASE, page_size);

	/*
	 * Test failure in erase does not change context.
	 * The test is done by replacing erase function of device API with fake
	 * one that returns with an error, invoking the erase procedure
	 * and than comparing state of context prior to call to the one after.
	 */
	struct device fake_dev = *ctx.fdev;
	struct flash_driver_api fake_api = *(struct flash_driver_api *)ctx.fdev->api;
	struct stream_flash_ctx bad_ctx = ctx;
	struct stream_flash_ctx cmp_ctx;

	fake_api.erase = bad_erase;
	fake_dev.api = &fake_api;
	bad_ctx.fdev = &fake_dev;
	/* Triger erase attempt */
	bad_ctx.last_erased_page_start_offset = FLASH_BASE - 16;
	cmp_ctx = bad_ctx;

	rc = stream_flash_erase_page(&bad_ctx, FLASH_BASE);
	zassert_equal(memcmp(&bad_ctx, &cmp_ctx, sizeof(bad_ctx)), 0,
		      "Ctx should not get altered");
	zassert_equal(rc, -EINVAL, "Expected failure");
}
#else
static void test_stream_flash_erase_page(void)
{
	ztest_test_skip();
}

static void test_stream_flash_buffered_write_whole_page(void)
{
	ztest_test_skip();
}
#endif

static size_t write_and_save_progress(size_t bytes, const char *save_key)
{
	int rc;
	size_t bytes_written;

	rc = stream_flash_buffered_write(&ctx, write_buf, bytes, true);
	zassert_equal(rc, 0, "expected success");

	bytes_written = stream_flash_bytes_written(&ctx);
	zassert_true(bytes_written > 0, "expected bytes to be written");

	if (save_key) {
		rc = stream_flash_progress_save(&ctx, save_key);
		zassert_equal(rc, 0, "expected success");
	}

	return bytes_written;
}

static void clear_all_progress(void)
{
	(void) settings_delete(progress_key);
}

static size_t load_progress(const char *load_key)
{
	int rc;

	rc = stream_flash_progress_load(&ctx, progress_key);
	zassert_equal(rc, 0, "expected success");

	return stream_flash_bytes_written(&ctx);
}

static void test_stream_flash_progress_api(void)
{
	int rc;

	clear_all_progress();
	init_target();

	/* Test save parameter validation */
	rc = stream_flash_progress_save(NULL, progress_key);
	zassert_true(rc < 0, "expected error since ctx is NULL");

	rc = stream_flash_progress_save(&ctx, NULL);
	zassert_true(rc < 0, "expected error since key is NULL");

	rc = stream_flash_progress_save(&ctx, progress_key);
	zassert_equal(rc, 0, "expected success");

	(void) write_and_save_progress(BUF_LEN, progress_key);

	/* Test load parameter validation */
	rc = stream_flash_progress_load(NULL, progress_key);
	zassert_true(rc < 0, "expected error since ctx is NULL");

	rc = stream_flash_progress_load(&ctx, NULL);
	zassert_true(rc < 0, "expected error since key is NULL");

	rc = stream_flash_progress_load(&ctx, progress_key);
	zassert_equal(rc, 0, "expected success");

	/* Test clear parameter validation */
	rc = stream_flash_progress_clear(NULL, progress_key);
	zassert_true(rc < 0, "expected error since ctx is NULL");

	rc = stream_flash_progress_clear(&ctx, NULL);
	zassert_true(rc < 0, "expected error since key is NULL");

	rc = stream_flash_progress_clear(&ctx, progress_key);
	zassert_equal(rc, 0, "expected success");
}

static void test_stream_flash_progress_resume(void)
{
	int rc;
	size_t bytes_written_old;
	size_t bytes_written;
#ifdef CONFIG_STREAM_FLASH_ERASE
	off_t erase_offset_old;
	off_t erase_offset;
#endif

	clear_all_progress();
	init_target();

	bytes_written_old = stream_flash_bytes_written(&ctx);
#ifdef CONFIG_STREAM_FLASH_ERASE
	erase_offset_old = ctx.last_erased_page_start_offset;
#endif

	/* Test load with zero bytes_written */
	rc = stream_flash_progress_save(&ctx, progress_key);
	zassert_equal(rc, 0, "expected success");

	rc = stream_flash_progress_load(&ctx, progress_key);
	zassert_equal(rc, 0, "expected success");

	bytes_written = stream_flash_bytes_written(&ctx);
	zassert_equal(bytes_written, bytes_written_old,
		      "expected bytes_written to be unchanged");
#ifdef CONFIG_STREAM_FLASH_ERASE
	erase_offset = ctx.last_erased_page_start_offset;
	zassert_equal(erase_offset, erase_offset_old,
		      "expected erase offset to be unchanged");
#endif

	clear_all_progress();
	init_target();

	/* Write some data and save the progress */
	bytes_written_old = write_and_save_progress(page_size * 2,
						    progress_key);
#ifdef CONFIG_STREAM_FLASH_ERASE
	erase_offset_old = ctx.last_erased_page_start_offset;
	zassert_true(erase_offset_old != 0, "expected pages to be erased");
#endif

	init_target();

	/* Load the previous progress */
	bytes_written = load_progress(progress_key);
	zassert_equal(bytes_written, bytes_written_old,
		      "expected bytes_written to be loaded");
#ifdef CONFIG_STREAM_FLASH_ERASE
	zassert_equal(erase_offset_old, ctx.last_erased_page_start_offset,
		      "expected last erased page offset to be loaded");
#endif

	/* Check that outdated progress does not overwrite current progress */
	init_target();

	(void) write_and_save_progress(BUF_LEN, progress_key);
	bytes_written_old = write_and_save_progress(BUF_LEN, NULL);
	bytes_written = load_progress(progress_key);
	zassert_equal(bytes_written, bytes_written_old,
		      "expected bytes_written to not be overwritten");
}

static void test_stream_flash_progress_clear(void)
{
	int rc;
	size_t bytes_written_old;
	size_t bytes_written;
#ifdef CONFIG_STREAM_FLASH_ERASE
	off_t erase_offset_old;
	off_t erase_offset;
#endif

	clear_all_progress();
	init_target();

	/* Test that progress is cleared. */
	(void) write_and_save_progress(BUF_LEN, progress_key);

	rc = stream_flash_progress_clear(&ctx, progress_key);
	zassert_equal(rc, 0, "expected success");

	init_target();

	bytes_written_old = stream_flash_bytes_written(&ctx);
#ifdef CONFIG_STREAM_FLASH_ERASE
	erase_offset_old = ctx.last_erased_page_start_offset;
#endif

	rc = stream_flash_progress_load(&ctx, progress_key);
	zassert_equal(rc, 0, "expected success");

	bytes_written = stream_flash_bytes_written(&ctx);
	zassert_equal(bytes_written, bytes_written_old,
		      "expected bytes_written to be unchanged");

#ifdef CONFIG_STREAM_FLASH_ERASE
	erase_offset = ctx.last_erased_page_start_offset;
	zassert_equal(erase_offset, erase_offset_old,
		      "expected erase offset to be unchanged");
#endif
}

void test_main(void)
{
	fdev = device_get_binding(FLASH_NAME);
	api = fdev->api;
	api->page_layout(fdev, &layout, &layout_size);

	page_size = layout->pages_size;
	__ASSERT_NO_MSG(page_size > BUF_LEN);

	ztest_test_suite(lib_stream_flash_test,
	     ztest_unit_test(test_stream_flash_init),
	     ztest_unit_test(test_stream_flash_buffered_write),
	     ztest_unit_test(test_stream_flash_buffered_write_cross_buf_border),
	     ztest_unit_test(test_stream_flash_buffered_write_unaligned),
	     ztest_unit_test(test_stream_flash_buffered_write_multi_page),
	     ztest_unit_test(test_stream_flash_buf_size_greater_than_page_size),
	     ztest_unit_test(test_stream_flash_buffered_write_callback),
	     ztest_unit_test(test_stream_flash_flush),
	     ztest_unit_test(test_stream_flash_buffered_write_whole_page),
	     ztest_unit_test(test_stream_flash_erase_page),
	     ztest_unit_test(test_stream_flash_bytes_written),
	     ztest_unit_test(test_stream_flash_progress_api),
	     ztest_unit_test(test_stream_flash_progress_resume),
	     ztest_unit_test(test_stream_flash_progress_clear)
	 );

	ztest_run_test_suite(lib_stream_flash_test);
}