8
8
* This test is designed to be run using flash-simulator which provide
9
9
* functionality for flash property customization and emulating errors in
10
10
* flash operation in parallel to regular flash API.
11
- * Test should be run on qemu_x86 target.
11
+ * Test should be run on qemu_x86 or native_posix target.
12
12
*/
13
13
14
- #ifndef CONFIG_BOARD_QEMU_X86
15
- #error "Run on qemu_x86 only"
14
+ #if !defined( CONFIG_BOARD_QEMU_X86 ) && !defined( CONFIG_BOARD_NATIVE_POSIX )
15
+ #error "Run on qemu_x86 or native_posix only"
16
16
#endif
17
17
18
18
#include <stdio.h>
@@ -41,7 +41,7 @@ void setup(void)
41
41
sim_thresholds = stats_group_find ("flash_sim_thresholds" );
42
42
43
43
/* Verify if NVS is initialized. */
44
- if (fs .sector_count != 0 ) {
44
+ if (fs .ready ) {
45
45
int err ;
46
46
47
47
err = nvs_clear (& fs );
@@ -700,6 +700,146 @@ void test_nvs_gc_corrupt_ate(void)
700
700
zassert_true (err == 0 , "nvs_mount call failure: %d" , err );
701
701
}
702
702
703
+ #ifdef CONFIG_NVS_LOOKUP_CACHE
704
+ static size_t num_matching_cache_entries (uint32_t addr , bool compare_sector_only )
705
+ {
706
+ size_t i , num = 0 ;
707
+ uint32_t mask = compare_sector_only ? ADDR_SECT_MASK : UINT32_MAX ;
708
+
709
+ for (i = 0 ; i < CONFIG_NVS_LOOKUP_CACHE_SIZE ; i ++ ) {
710
+ if ((fs .lookup_cache [i ] & mask ) == addr ) {
711
+ num ++ ;
712
+ }
713
+ }
714
+
715
+ return num ;
716
+ }
717
+ #endif
718
+
719
+ /*
720
+ * Test that NVS lookup cache is properly rebuilt on nvs_mount(), or initialized
721
+ * to NVS_LOOKUP_CACHE_NO_ADDR if the store is empty.
722
+ */
723
+ void test_nvs_cache_init (void )
724
+ {
725
+ #ifdef CONFIG_NVS_LOOKUP_CACHE
726
+ int err ;
727
+ size_t num ;
728
+ uint32_t ate_addr ;
729
+ uint8_t data = 0 ;
730
+
731
+ /* Test cache initialization when the store is empty */
732
+
733
+ fs .sector_count = 3 ;
734
+ err = nvs_mount (& fs );
735
+ zassert_true (err == 0 , "nvs_init call failure: %d" , err );
736
+
737
+ num = num_matching_cache_entries (NVS_LOOKUP_CACHE_NO_ADDR , false);
738
+ zassert_equal (num , CONFIG_NVS_LOOKUP_CACHE_SIZE , "uninitialized cache" );
739
+
740
+ /* Test cache update after nvs_write() */
741
+
742
+ ate_addr = fs .ate_wra ;
743
+ err = nvs_write (& fs , 1 , & data , sizeof (data ));
744
+ zassert_equal (err , sizeof (data ), "nvs_write call failure: %d" , err );
745
+
746
+ num = num_matching_cache_entries (NVS_LOOKUP_CACHE_NO_ADDR , false);
747
+ zassert_equal (num , CONFIG_NVS_LOOKUP_CACHE_SIZE - 1 , "cache not updated after write" );
748
+
749
+ num = num_matching_cache_entries (ate_addr , false);
750
+ zassert_equal (num , 1 , "invalid cache entry after write" );
751
+
752
+ /* Test cache initialization when the store is non-empty */
753
+
754
+ memset (fs .lookup_cache , 0xAA , sizeof (fs .lookup_cache ));
755
+ err = nvs_mount (& fs );
756
+ zassert_true (err == 0 , "nvs_init call failure: %d" , err );
757
+
758
+ num = num_matching_cache_entries (NVS_LOOKUP_CACHE_NO_ADDR , false);
759
+ zassert_equal (num , CONFIG_NVS_LOOKUP_CACHE_SIZE - 1 , "uninitialized cache after restart" );
760
+
761
+ num = num_matching_cache_entries (ate_addr , false);
762
+ zassert_equal (num , 1 , "invalid cache entry after restart" );
763
+ #endif
764
+ }
765
+
766
+ /*
767
+ * Test that even after writing more NVS IDs than the number of NVS lookup cache
768
+ * entries they all can be read correctly.
769
+ */
770
+ void test_nvs_cache_collission (void )
771
+ {
772
+ #ifdef CONFIG_NVS_LOOKUP_CACHE
773
+ int err ;
774
+ uint16_t id ;
775
+ uint16_t data ;
776
+
777
+ fs .sector_count = 3 ;
778
+ err = nvs_mount (& fs );
779
+ zassert_true (err == 0 , "nvs_init call failure: %d" , err );
780
+
781
+ for (id = 0 ; id < CONFIG_NVS_LOOKUP_CACHE_SIZE + 1 ; id ++ ) {
782
+ data = id ;
783
+ err = nvs_write (& fs , id , & data , sizeof (data ));
784
+ zassert_equal (err , sizeof (data ), "nvs_write call failure: %d" , err );
785
+ }
786
+
787
+ for (id = 0 ; id < CONFIG_NVS_LOOKUP_CACHE_SIZE + 1 ; id ++ ) {
788
+ err = nvs_read (& fs , id , & data , sizeof (data ));
789
+ zassert_equal (err , sizeof (data ), "nvs_read call failure: %d" , err );
790
+ zassert_equal (data , id , "incorrect data read" );
791
+ }
792
+ #endif
793
+ }
794
+
795
+ /*
796
+ * Test that NVS lookup cache does not contain any address from gc-ed sector
797
+ */
798
+ void test_nvs_cache_gc (void )
799
+ {
800
+ #ifdef CONFIG_NVS_LOOKUP_CACHE
801
+ int err ;
802
+ size_t num ;
803
+ uint16_t data = 0 ;
804
+
805
+ fs .sector_count = 3 ;
806
+ err = nvs_mount (& fs );
807
+ zassert_true (err == 0 , "nvs_init call failure: %d" , err );
808
+
809
+ /* Fill the first sector with writes of ID 1 */
810
+
811
+ while (fs .data_wra + sizeof (data ) <= fs .ate_wra ) {
812
+ ++ data ;
813
+ err = nvs_write (& fs , 1 , & data , sizeof (data ));
814
+ zassert_equal (err , sizeof (data ), "nvs_write call failure: %d" , err );
815
+ }
816
+
817
+ /* Verify that cache contains a single entry for sector 0 */
818
+
819
+ num = num_matching_cache_entries (0 << ADDR_SECT_SHIFT , true);
820
+ zassert_equal (num , 1 , "invalid cache content after filling sector 0" );
821
+
822
+ /* Fill the second sector with writes of ID 2 */
823
+
824
+ while ((fs .ate_wra >> ADDR_SECT_SHIFT ) != 2 ) {
825
+ ++ data ;
826
+ err = nvs_write (& fs , 2 , & data , sizeof (data ));
827
+ zassert_equal (err , sizeof (data ), "nvs_write call failure: %d" , err );
828
+ }
829
+
830
+ /*
831
+ * At this point sector 0 should have been gc-ed. Verify that action is
832
+ * reflected by the cache content.
833
+ */
834
+
835
+ num = num_matching_cache_entries (0 << ADDR_SECT_SHIFT , true);
836
+ zassert_equal (num , 0 , "not invalidated cache entries aftetr gc" );
837
+
838
+ num = num_matching_cache_entries (2 << ADDR_SECT_SHIFT , true);
839
+ zassert_equal (num , 2 , "invalid cache content after gc" );
840
+ #endif
841
+ }
842
+
703
843
void test_main (void )
704
844
{
705
845
__ASSERT_NO_MSG (device_is_ready (flash_dev ));
@@ -725,7 +865,13 @@ void test_main(void)
725
865
ztest_unit_test_setup_teardown (
726
866
test_nvs_gc_corrupt_close_ate , setup , teardown ),
727
867
ztest_unit_test_setup_teardown (
728
- test_nvs_gc_corrupt_ate , setup , teardown )
868
+ test_nvs_gc_corrupt_ate , setup , teardown ),
869
+ ztest_unit_test_setup_teardown (
870
+ test_nvs_cache_init , setup , teardown ),
871
+ ztest_unit_test_setup_teardown (
872
+ test_nvs_cache_collission , setup , teardown ),
873
+ ztest_unit_test_setup_teardown (
874
+ test_nvs_cache_gc , setup , teardown )
729
875
);
730
876
731
877
ztest_run_test_suite (test_nvs );
0 commit comments