LexLeo 0.0.0-dev+f8e5087-dirty
Technical documentation
Loading...
Searching...
No Matches
unit_test_osal_file.c
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-3.0-or-later
2 * Copyright (C) 2026 Sylvain Labopin
3 */
4
23#include "osal/file/osal_file_ops.h"
24
25#include "osal/mem/osal_mem.h"
27
28#include "lexleo_cmocka.h"
29
39static void test_osal_file_default_ops(void **state) {
40 (void)state;
41
43
44 assert_non_null(ret);
45 assert_non_null(ret->open);
46 assert_non_null(ret->read);
47 assert_non_null(ret->write);
48 assert_non_null(ret->flush);
49 assert_non_null(ret->close);
50}
51
134
151typedef enum {
152 OUT_CHECK_NONE,
153 OUT_EXPECT_NON_NULL,
154 OUT_EXPECT_UNCHANGED
155} out_expect_t;
156
167typedef struct {
168 const char *name;
169
170 /* arrange */
172 size_t fail_call_idx; /* 0 = no OOM, otherwise 1-based */
173
174 /* assert */
175 osal_file_status_t expected_ret;
176 out_expect_t out_expect;
177} test_osal_file_open_case_t;
178
190typedef struct {
191 /* output state */
192 OSAL_FILE *out;
193 OSAL_FILE *snapshot;
194
195 /* DI */
196 const osal_mem_ops_t *mem_ops;
197
198 const char *pathname;
199 const char *mode;
200
201 const osal_file_ops_t *file_ops;
202
203 /* active parametric test case */
204 const test_osal_file_open_case_t *tc;
205} test_osal_file_open_fixture_t;
206
207//-----------------------------------------------------------------------------
208// FIXTURES
209//-----------------------------------------------------------------------------
210
214static int setup_osal_file_open(void **state)
215{
216 const test_osal_file_open_case_t *tc =
217 (const test_osal_file_open_case_t *)(*state);
218
219 test_osal_file_open_fixture_t *fx =
220 (test_osal_file_open_fixture_t *)osal_malloc(sizeof(*fx));
221 if (!fx) return -1;
222
223 osal_memset(fx, 0, sizeof(*fx));
224 fx->tc = tc;
225
226 fake_memory_reset();
227 if (tc->scenario == OSAL_FILE_OPEN_SCENARIO_OOM && tc->fail_call_idx > 0) {
228 fake_memory_fail_only_on_call(tc->fail_call_idx);
229 }
230
231 // DI
232 fx->mem_ops = osal_mem_test_fake_ops();
233
234 fx->file_ops = osal_file_default_ops();
235
236 fx->pathname = "unit_test_osal_file.tmp";
237 fx->mode = "wb";
238
239 *state = fx;
240 return 0;
241}
242
246static int teardown_osal_file_open(void **state)
247{
248 test_osal_file_open_fixture_t *fx =
249 (test_osal_file_open_fixture_t *)(*state);
250
251 if (fx->out && fx->out != fx->snapshot) {
252 assert_true(
253 fx->file_ops->close(fx->out) == OSAL_FILE_STATUS_OK
254 );
255 fx->out = NULL;
256 }
257
258 assert_true(fake_memory_no_leak());
259 assert_true(fake_memory_no_invalid_free());
260 assert_true(fake_memory_no_double_free());
261
262 osal_free(fx);
263 *state = NULL;
264 return 0;
265}
266
267//-----------------------------------------------------------------------------
268// TEST
269//-----------------------------------------------------------------------------
270
274static void test_osal_file_open(void **state)
275{
276 test_osal_file_open_fixture_t *fx =
277 (test_osal_file_open_fixture_t *)(*state);
278 const test_osal_file_open_case_t *tc =
279 (const test_osal_file_open_case_t *)fx->tc;
280
281 // ARRANGE
282 osal_file_status_t ret = (osal_file_status_t)-1; // poison
283 fx->out = (OSAL_FILE *)(uintptr_t)0xDEADC0DEu;
284 OSAL_FILE **out_arg = &fx->out;
285 const char *pathname_arg = fx->pathname;
286 const char *mode_arg = fx->mode;
287 const osal_mem_ops_t *mem_ops_arg = fx->mem_ops;
288
289 // invalid args
290 if (tc->scenario == OSAL_FILE_OPEN_SCENARIO_OUT_NULL) {
291 out_arg = NULL;
292 }
293 if (tc->scenario == OSAL_FILE_OPEN_SCENARIO_PATHNAME_NULL) {
294 pathname_arg = NULL;
295 }
296 if (tc->scenario == OSAL_FILE_OPEN_SCENARIO_PATHNAME_EMPTY) {
297 pathname_arg = "";
298 }
299 if (tc->scenario == OSAL_FILE_OPEN_SCENARIO_MODE_NULL) {
300 mode_arg = NULL;
301 }
302 if (tc->scenario == OSAL_FILE_OPEN_SCENARIO_MODE_UNSUPPORTED) {
303 mode_arg = "w+";
304 }
305 if (tc->scenario == OSAL_FILE_OPEN_SCENARIO_MEM_OPS_NULL) {
306 mem_ops_arg = NULL;
307 }
308
309 fx->snapshot = fx->out;
310
311 // ACT
312 ret = fx->file_ops->open(out_arg, pathname_arg, mode_arg, mem_ops_arg);
313
314 // ASSERT
315 assert_int_equal(ret, tc->expected_ret);
316
317 switch (tc->out_expect) {
318 case OUT_CHECK_NONE:
319 break;
320 case OUT_EXPECT_NON_NULL:
321 assert_non_null(fx->out);
322 assert_ptr_not_equal(fx->out, fx->snapshot);
323 break;
324 case OUT_EXPECT_UNCHANGED:
325 assert_ptr_equal(fx->out, fx->snapshot);
326 break;
327 default:
328 fail();
329 }
330}
331
332//-----------------------------------------------------------------------------
333// CASES
334//-----------------------------------------------------------------------------
335
336static const test_osal_file_open_case_t CASE_OSAL_FILE_OPEN_OUT_NULL = {
337 .name = "osal_file_open_out_null",
339 .fail_call_idx = 0,
340
341 .expected_ret = OSAL_FILE_STATUS_INVALID,
342 .out_expect = OUT_CHECK_NONE
343};
344
345static const test_osal_file_open_case_t CASE_OSAL_FILE_OPEN_PATHNAME_NULL = {
346 .name = "osal_file_open_pathname_null",
348 .fail_call_idx = 0,
349
350 .expected_ret = OSAL_FILE_STATUS_INVALID,
351 .out_expect = OUT_EXPECT_UNCHANGED
352};
353
354static const test_osal_file_open_case_t CASE_OSAL_FILE_OPEN_PATHNAME_EMPTY = {
355 .name = "osal_file_open_pathname_empty",
357 .fail_call_idx = 0,
358
359 .expected_ret = OSAL_FILE_STATUS_INVALID,
360 .out_expect = OUT_EXPECT_UNCHANGED
361};
362
363static const test_osal_file_open_case_t CASE_OSAL_FILE_OPEN_MODE_NULL = {
364 .name = "osal_file_open_mode_null",
366 .fail_call_idx = 0,
367
368 .expected_ret = OSAL_FILE_STATUS_INVALID,
369 .out_expect = OUT_EXPECT_UNCHANGED
370};
371
372static const test_osal_file_open_case_t CASE_OSAL_FILE_OPEN_MODE_UNSUPPORTED = {
373 .name = "osal_file_open_mode_unsupported",
375 .fail_call_idx = 0,
376
377 .expected_ret = OSAL_FILE_STATUS_INVALID,
378 .out_expect = OUT_EXPECT_UNCHANGED
379};
380
381static const test_osal_file_open_case_t CASE_OSAL_FILE_OPEN_MEM_OPS_NULL = {
382 .name = "osal_file_open_mem_ops_null",
384 .fail_call_idx = 0,
385
386 .expected_ret = OSAL_FILE_STATUS_INVALID,
387 .out_expect = OUT_EXPECT_UNCHANGED
388};
389
390static const test_osal_file_open_case_t CASE_OSAL_FILE_OPEN_OOM_1 = {
391 .name = "osal_file_open_oom_1",
392 .scenario = OSAL_FILE_OPEN_SCENARIO_OOM,
393 .fail_call_idx = 1,
394
395 .expected_ret = OSAL_FILE_STATUS_OOM,
396 .out_expect = OUT_EXPECT_UNCHANGED
397};
398
399static const test_osal_file_open_case_t CASE_OSAL_FILE_OPEN_OK = {
400 .name = "osal_file_open_ok",
401 .scenario = OSAL_FILE_OPEN_SCENARIO_OK,
402 .fail_call_idx = 0,
403
404 .expected_ret = OSAL_FILE_STATUS_OK,
405 .out_expect = OUT_EXPECT_NON_NULL
406};
407
408//-----------------------------------------------------------------------------
409// CASES REGISTRY
410//-----------------------------------------------------------------------------
411
412#define OSAL_FILE_OPEN_CASES(X) \
413X(CASE_OSAL_FILE_OPEN_OUT_NULL) \
414X(CASE_OSAL_FILE_OPEN_PATHNAME_NULL) \
415X(CASE_OSAL_FILE_OPEN_PATHNAME_EMPTY) \
416X(CASE_OSAL_FILE_OPEN_MODE_NULL) \
417X(CASE_OSAL_FILE_OPEN_MODE_UNSUPPORTED) \
418X(CASE_OSAL_FILE_OPEN_MEM_OPS_NULL) \
419X(CASE_OSAL_FILE_OPEN_OOM_1) \
420X(CASE_OSAL_FILE_OPEN_OK)
421
422#define OSAL_FILE_MAKE_OPEN_TEST(case_sym) \
423LEXLEO_MAKE_TEST(osal_file_open, case_sym)
424
425static const struct CMUnitTest osal_file_open_tests[] = {
426 OSAL_FILE_OPEN_CASES(OSAL_FILE_MAKE_OPEN_TEST)
427};
428
429#undef OSAL_FILE_OPEN_CASES
430#undef OSAL_FILE_MAKE_OPEN_TEST
431
584
594typedef struct {
595 const char *name;
596
597 // arrange
599 uint8_t file_content[16];
600 size_t file_content_len;
601 size_t first_size;
602 size_t first_nmemb;
603 bool do_second_read;
604 size_t second_size;
605 size_t second_nmemb;
606
607 // assert
608 size_t first_expected_ret;
609 osal_file_status_t first_expected_st;
610 size_t second_expected_ret;
611 osal_file_status_t second_expected_st;
612} test_osal_file_read_case_t;
613
626typedef struct {
627 // runtime resources
628 OSAL_FILE *readable_osal_file;
629 OSAL_FILE *writable_osal_file;
630
631 osal_file_status_t first_st_storage;
632 osal_file_status_t second_st_storage;
633 uint8_t first_read_content[16];
634 uint8_t second_read_content[16];
635
636 // injection
637 const osal_mem_ops_t *mem_ops;
638
639 // exercised public API
640 const osal_file_ops_t *file_ops;
641
642 const test_osal_file_read_case_t *tc;
643} test_osal_file_read_fixture_t;
644
645//-----------------------------------------------------------------------------
646// FIXTURES
647//-----------------------------------------------------------------------------
648
652static int setup_osal_file_read(void **state)
653{
654 const test_osal_file_read_case_t *tc =
655 (const test_osal_file_read_case_t *)(*state);
656
657 test_osal_file_read_fixture_t *fx =
658 (test_osal_file_read_fixture_t *)osal_malloc(sizeof(*fx));
659 if (!fx) return -1;
660
661 osal_memset(fx, 0, sizeof(*fx));
662 fx->tc = tc;
663
664 // DI
665 fake_memory_reset();
666 fx->mem_ops = osal_mem_test_fake_ops();
667
668 // prepare tmp file
669 fx->file_ops = osal_file_default_ops();
670 assert_int_equal(
671 fx->file_ops->open(
672 &fx->writable_osal_file,
673 "osal_file_read_tmp_file.txt",
674 "wb",
675 fx->mem_ops
676 ),
678 );
680 assert_int_equal(
681 fx->file_ops->write(
682 tc->file_content,
683 1,
684 tc->file_content_len,
685 fx->writable_osal_file,
686 &st
687 ),
688 (size_t)tc->file_content_len
689 );
690 assert_int_equal(st, OSAL_FILE_STATUS_OK);
691 assert_int_equal(
692 fx->file_ops->close(
693 fx->writable_osal_file
694 ),
696 );
697 fx->writable_osal_file = NULL;
698 assert_int_equal(
699 fx->file_ops->open(
700 &fx->readable_osal_file,
701 "osal_file_read_tmp_file.txt",
702 "rb",
703 fx->mem_ops
704 ),
706 );
707
708 *state = fx;
709 return 0;
710}
711
715static int teardown_osal_file_read(void **state)
716{
717 test_osal_file_read_fixture_t *fx =
718 (test_osal_file_read_fixture_t *)(*state);
719
720 if (fx->readable_osal_file) {
721 assert_true(
722 fx->file_ops->close(fx->readable_osal_file) == OSAL_FILE_STATUS_OK
723 );
724 fx->readable_osal_file = NULL;
725 }
726
727 assert_true(fake_memory_no_leak());
728 assert_true(fake_memory_no_invalid_free());
729 assert_true(fake_memory_no_double_free());
730
731 osal_free(fx);
732 *state = NULL;
733 return 0;
734}
735
736//-----------------------------------------------------------------------------
737// TEST
738//-----------------------------------------------------------------------------
739
743static void test_osal_file_read(void **state)
744{
745 test_osal_file_read_fixture_t *fx =
746 (test_osal_file_read_fixture_t *)(*state);
747 const test_osal_file_read_case_t *tc =
748 (const test_osal_file_read_case_t *)fx->tc;
749
750 // ARRANGE (first read call)
751 size_t ret = (size_t)-1; // poison
752 void *ptr_arg = fx->first_read_content;
753 size_t size_arg = tc->first_size;
754 size_t nmemb_arg = tc->first_nmemb;
755 OSAL_FILE *stream_arg = fx->readable_osal_file;
756 osal_file_status_t *st_arg = &fx->first_st_storage;
757
758 // invalid args
759 if (tc->scenario == OSAL_FILE_READ_SCENARIO_PTR_NULL) {
760 ptr_arg = NULL;
761 }
762 if (tc->scenario == OSAL_FILE_READ_SCENARIO_ST_NULL) {
763 st_arg = NULL;
764 }
765 if (tc->scenario == OSAL_FILE_READ_SCENARIO_STREAM_NULL) {
766 stream_arg = NULL;
767 }
768
769 // ACT (first read call)
770 ret = fx->file_ops->read(ptr_arg, size_arg, nmemb_arg, stream_arg, st_arg);
771
772 // ASSERT (first read call)
773 assert_int_equal(ret, tc->first_expected_ret);
774 if (st_arg) {
775 assert_int_equal(*st_arg, tc->first_expected_st);
776 }
777 if (tc->first_expected_ret > 0) {
778 assert_memory_equal(
779 fx->first_read_content,
780 tc->file_content,
781 (tc->first_expected_ret * tc->first_size <= tc->file_content_len) ?
782 tc->first_expected_ret * tc->first_size
783 :
784 tc->file_content_len
785 );
786 }
787
788 if (!tc->do_second_read) {
789 return;
790 }
791
792 // ARRANGE (second read call)
793 ret = (size_t)-1; // poison
794 ptr_arg = fx->second_read_content;
795 size_arg = tc->second_size;
796 nmemb_arg = tc->second_nmemb;
797 stream_arg = fx->readable_osal_file;
798 st_arg = &fx->second_st_storage;
799
800 // ACT (second read call)
801 ret = fx->file_ops->read(ptr_arg, size_arg, nmemb_arg, stream_arg, st_arg);
802
803 // ASSERT (second read call)
804 assert_int_equal(ret, tc->second_expected_ret);
805 if (st_arg) {
806 assert_int_equal(*st_arg, tc->second_expected_st);
807 }
808 if (tc->second_expected_ret > 0) {
809 assert_true(
810 tc->file_content_len >= tc->first_expected_ret * tc->first_size
811 );
812 assert_memory_equal(
813 fx->second_read_content,
814 tc->file_content + tc->first_expected_ret * tc->first_size,
815 (tc->second_expected_ret * tc->second_size + tc->first_expected_ret * tc->first_size <= tc->file_content_len) ?
816 tc->second_expected_ret * tc->second_size : tc->file_content_len - tc->first_expected_ret * tc->first_size
817 );
818 }
819}
820
821//-----------------------------------------------------------------------------
822// CASES
823//-----------------------------------------------------------------------------
824
825static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_PTR_NULL = {
826 .name = "osal_file_read_ptr_null",
828
829 /* arrange */
830 .file_content = "dummy content", /* '\0' is not considered */
831 .file_content_len = 14,
832 .first_size = 1,
833 .first_nmemb = 3,
834 .do_second_read = false,
835
836 /* assert */
837 .first_expected_ret = 0,
838 .first_expected_st = OSAL_FILE_STATUS_INVALID,
839};
840
841static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_ST_NULL = {
842 .name = "osal_file_read_st_null",
844
845 /* arrange */
846 .file_content = "dummy content",
847 .file_content_len = 14,
848 .first_size = 1,
849 .first_nmemb = 3,
850 .do_second_read = false,
851
852 /* assert */
853 .first_expected_ret = 0,
854};
855
856static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_STREAM_NULL = {
857 .name = "osal_file_read_stream_null",
859
860 /* arrange */
861 .file_content = "dummy content",
862 .file_content_len = 14,
863 .first_size = 1,
864 .first_nmemb = 3,
865 .do_second_read = false,
866
867 /* assert */
868 .first_expected_ret = 0,
869 .first_expected_st = OSAL_FILE_STATUS_INVALID,
870};
871
872static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_LESS_THAN_AVAILABLE = {
873 .name = "osal_file_read_less_than_available",
875
876 /* arrange */
877 .file_content = "dummy content",
878 .file_content_len = 14,
879 .first_size = 1,
880 .first_nmemb = 10,
881 .do_second_read = false,
882
883 /* assert */
884 .first_expected_ret = 10,
885 .first_expected_st = OSAL_FILE_STATUS_OK,
886};
887
888static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_READ_EXACT_AVAILABLE = {
889 .name = "osal_file_read_exact_available",
891
892 /* arrange */
893 .file_content = "dummy content",
894 .file_content_len = 14,
895 .first_size = 1,
896 .first_nmemb = 14,
897 .do_second_read = false,
898
899 /* assert */
900 .first_expected_ret = 14,
901 .first_expected_st = OSAL_FILE_STATUS_OK,
902};
903
904static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_READ_MORE_THAN_AVAILABLE = {
905 .name = "osal_file_read_more_than_available",
907
908 /* arrange */
909 .file_content = "dummy content",
910 .file_content_len = 14,
911 .first_size = 1,
912 .first_nmemb = 17,
913 .do_second_read = false,
914
915 /* assert */
916 .first_expected_ret = 14,
917 .first_expected_st = OSAL_FILE_STATUS_OK,
918};
919
920static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_EMPTY_FILE = {
921 .name = "osal_file_read_empty_file",
923
924 /* arrange */
925 .file_content = "",
926 .file_content_len = 0,
927 .first_size = 1,
928 .first_nmemb = 3,
929 .do_second_read = false,
930
931 /* assert */
932 .first_expected_ret = 0,
933 .first_expected_st = OSAL_FILE_STATUS_OK,
934};
935
936static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_SEQUENTIAL_READS_PARTIAL_THEN_PARTIAL = {
937 .name = "osal_file_read_sequential_reads_partial_then_partial",
939
940 /* arrange */
941 .file_content = "dummy content",
942 .file_content_len = 14,
943 .first_size = 1,
944 .first_nmemb = 6,
945 .do_second_read = true,
946 .second_size = 1,
947 .second_nmemb = 4,
948
949 /* assert */
950 .first_expected_ret = 6,
951 .first_expected_st = OSAL_FILE_STATUS_OK,
952 .second_expected_ret = 4,
953 .second_expected_st = OSAL_FILE_STATUS_OK,
954};
955
956static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_AFTER_EOF = {
957 .name = "osal_file_read_after_eof",
959
960 /* arrange */
961 .file_content = "dummy content",
962 .file_content_len = 14,
963 .first_size = 1,
964 .first_nmemb = 14,
965 .do_second_read = true,
966 .second_size = 1,
967 .second_nmemb = 1,
968
969 /* assert */
970 .first_expected_ret = 14,
971 .first_expected_st = OSAL_FILE_STATUS_OK,
972 .second_expected_ret = 0,
973 .second_expected_st = OSAL_FILE_STATUS_OK,
974};
975
976static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_READ_ZERO_SIZE = {
977 .name = "osal_file_read_zero_size",
979
980 /* arrange */
981 .file_content = "dummy content",
982 .file_content_len = 14,
983 .first_size = 0,
984 .first_nmemb = 14,
985 .do_second_read = false,
986
987 /* assert */
988 .first_expected_ret = 0,
989 .first_expected_st = OSAL_FILE_STATUS_OK,
990};
991
992static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_READ_ZERO_NMEMB = {
993 .name = "osal_file_read_zero_nmemb",
995
996 /* arrange */
997 .file_content = "dummy content",
998 .file_content_len = 14,
999 .first_size = 1,
1000 .first_nmemb = 0,
1001 .do_second_read = false,
1002
1003 /* assert */
1004 .first_expected_ret = 0,
1005 .first_expected_st = OSAL_FILE_STATUS_OK,
1006};
1007
1008static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_READ_LESS_THAN_AVAILABLE_SIZE_GT_1 = {
1009 .name = "osal_file_read_read_less_than_available_size_gt_1",
1011
1012 /* arrange */
1013 .file_content = "dummy content",
1014 .file_content_len = 14,
1015 .first_size = 3,
1016 .first_nmemb = 4,
1017 .do_second_read = false,
1018
1019 /* assert */
1020 .first_expected_ret = 4,
1021 .first_expected_st = OSAL_FILE_STATUS_OK,
1022};
1023
1024static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_READ_EXACT_AVAILABLE_SIZE_GT_1 = {
1025 .name = "osal_file_read_read_exact_available_size_gt_1",
1027
1028 /* arrange */
1029 .file_content = "dummy content",
1030 .file_content_len = 14,
1031 .first_size = 2,
1032 .first_nmemb = 7,
1033 .do_second_read = false,
1034
1035 /* assert */
1036 .first_expected_ret = 7,
1037 .first_expected_st = OSAL_FILE_STATUS_OK,
1038};
1039
1040static const test_osal_file_read_case_t CASE_OSAL_FILE_READ_READ_MORE_THAN_AVAILABLE_SIZE_GT_1 = {
1041 .name = "osal_file_read_more_than_available_size_gt_1",
1043
1044 /* arrange */
1045 .file_content = "dummy content",
1046 .file_content_len = 14,
1047 .first_size = 3,
1048 .first_nmemb = 5,
1049 .do_second_read = false,
1050
1051 /* assert */
1052 .first_expected_ret = 4,
1053 .first_expected_st = OSAL_FILE_STATUS_OK,
1054};
1055
1056//-----------------------------------------------------------------------------
1057// CASES REGISTRY
1058//-----------------------------------------------------------------------------
1059
1060#define OSAL_FILE_READ_CASES(X) \
1061X(CASE_OSAL_FILE_READ_PTR_NULL) \
1062X(CASE_OSAL_FILE_READ_ST_NULL) \
1063X(CASE_OSAL_FILE_READ_STREAM_NULL) \
1064X(CASE_OSAL_FILE_READ_LESS_THAN_AVAILABLE) \
1065X(CASE_OSAL_FILE_READ_READ_EXACT_AVAILABLE) \
1066X(CASE_OSAL_FILE_READ_READ_MORE_THAN_AVAILABLE) \
1067X(CASE_OSAL_FILE_READ_EMPTY_FILE) \
1068X(CASE_OSAL_FILE_READ_SEQUENTIAL_READS_PARTIAL_THEN_PARTIAL) \
1069X(CASE_OSAL_FILE_READ_AFTER_EOF) \
1070X(CASE_OSAL_FILE_READ_READ_ZERO_SIZE) \
1071X(CASE_OSAL_FILE_READ_READ_ZERO_NMEMB) \
1072X(CASE_OSAL_FILE_READ_READ_LESS_THAN_AVAILABLE_SIZE_GT_1) \
1073X(CASE_OSAL_FILE_READ_READ_EXACT_AVAILABLE_SIZE_GT_1) \
1074X(CASE_OSAL_FILE_READ_READ_MORE_THAN_AVAILABLE_SIZE_GT_1)
1075
1076#define OSAL_FILE_MAKE_READ_TEST(case_sym) \
1077LEXLEO_MAKE_TEST(osal_file_read, case_sym)
1078
1079static const struct CMUnitTest osal_file_read_tests[] = {
1080 OSAL_FILE_READ_CASES(OSAL_FILE_MAKE_READ_TEST)
1081};
1082
1083#undef OSAL_FILE_READ_CASES
1084#undef OSAL_FILE_MAKE_READ_TEST
1085
1179
1191typedef struct {
1192 const char *name;
1193
1194 // arrange
1196 uint8_t initial_file_content[16];
1197 size_t initial_file_content_len;
1198 const uint8_t first_write_content[16];
1199 size_t first_size;
1200 size_t first_nmemb;
1201 bool do_second_write;
1202 const uint8_t second_write_content[16];
1203 size_t second_size;
1204 size_t second_nmemb;
1205
1206 // assert
1207 size_t first_expected_ret;
1208 osal_file_status_t first_expected_st;
1209 size_t second_expected_ret;
1210 osal_file_status_t second_expected_st;
1211 uint8_t expected_file_content[16];
1212 size_t expected_file_content_len;
1213} test_osal_file_write_case_t;
1214
1226typedef struct {
1227 // runtime resources
1228 OSAL_FILE *writable_osal_file;
1229 OSAL_FILE *verification_osal_file;
1230
1231 osal_file_status_t first_st_storage;
1232 osal_file_status_t second_st_storage;
1233
1234 // injection
1235 const osal_mem_ops_t *mem_ops;
1236
1237 // exercised public API
1238 const osal_file_ops_t *file_ops;
1239
1240 const test_osal_file_write_case_t *tc;
1241} test_osal_file_write_fixture_t;
1242
1243//-----------------------------------------------------------------------------
1244// FIXTURES
1245//-----------------------------------------------------------------------------
1246
1250static int setup_osal_file_write(void **state)
1251{
1252 const test_osal_file_write_case_t *tc =
1253 (const test_osal_file_write_case_t *)(*state);
1254
1255 test_osal_file_write_fixture_t *fx =
1256 (test_osal_file_write_fixture_t *)osal_malloc(sizeof(*fx));
1257 if (!fx) return -1;
1258
1259 osal_memset(fx, 0, sizeof(*fx));
1260 fx->tc = tc;
1261
1262 // DI
1263 fake_memory_reset();
1264 fx->mem_ops = osal_mem_test_fake_ops();
1265
1266 // prepare tmp file
1267 fx->file_ops = osal_file_default_ops();
1268 assert_int_equal(
1269 fx->file_ops->open(
1270 &fx->writable_osal_file,
1271 "osal_file_write_tmp_file.txt",
1272 "wb",
1273 fx->mem_ops
1274 ),
1276 );
1278 assert_int_equal(
1279 fx->file_ops->write(
1280 tc->initial_file_content,
1281 1,
1282 tc->initial_file_content_len,
1283 fx->writable_osal_file,
1284 &st
1285 ),
1286 (size_t)tc->initial_file_content_len
1287 );
1288 assert_int_equal(st, OSAL_FILE_STATUS_OK);
1289
1290 *state = fx;
1291 return 0;
1292}
1293
1297static int teardown_osal_file_write(void **state)
1298{
1299 test_osal_file_write_fixture_t *fx =
1300 (test_osal_file_write_fixture_t *)(*state);
1301
1302 if (fx->verification_osal_file) {
1303 assert_true(
1304 fx->file_ops->close(fx->verification_osal_file) == OSAL_FILE_STATUS_OK
1305 );
1306 fx->verification_osal_file = NULL;
1307 }
1308
1309 if (fx->writable_osal_file) {
1310 assert_true(
1311 fx->file_ops->close(fx->writable_osal_file) == OSAL_FILE_STATUS_OK
1312 );
1313 fx->writable_osal_file = NULL;
1314 }
1315
1316 assert_true(fake_memory_no_leak());
1317 assert_true(fake_memory_no_invalid_free());
1318 assert_true(fake_memory_no_double_free());
1319
1320 osal_free(fx);
1321 *state = NULL;
1322 return 0;
1323}
1324
1325//-----------------------------------------------------------------------------
1326// TEST
1327//-----------------------------------------------------------------------------
1328
1332static void test_osal_file_write(void **state)
1333{
1334 test_osal_file_write_fixture_t *fx =
1335 (test_osal_file_write_fixture_t *)(*state);
1336 const test_osal_file_write_case_t *tc =
1337 (const test_osal_file_write_case_t *)fx->tc;
1338
1339 // ARRANGE (first write call)
1340 size_t first_write_ret = (size_t)-1; // poison
1341 fx->first_st_storage = (osal_file_status_t)-1;
1342 const void *ptr_arg = tc->first_write_content;
1343 size_t size_arg = tc->first_size;
1344 size_t nmemb_arg = tc->first_nmemb;
1345 OSAL_FILE *stream_arg = fx->writable_osal_file;
1346 osal_file_status_t *first_write_st_arg = &fx->first_st_storage;
1347
1348 // invalid args
1349 if (tc->scenario == OSAL_FILE_WRITE_SCENARIO_PTR_NULL) {
1350 ptr_arg = NULL;
1351 }
1352 if (tc->scenario == OSAL_FILE_WRITE_SCENARIO_STREAM_NULL) {
1353 stream_arg = NULL;
1354 }
1355 if (tc->scenario == OSAL_FILE_WRITE_SCENARIO_ST_NULL) {
1356 first_write_st_arg = NULL;
1357 }
1358
1359 // ACT (first write call)
1360 first_write_ret = fx->file_ops->write(ptr_arg, size_arg, nmemb_arg, stream_arg, first_write_st_arg);
1361
1362 // ARRANGE (second write call)
1363 osal_file_status_t *second_write_st_arg = NULL;
1364 size_t second_write_ret = (size_t)-1; // poison
1365 if (tc->do_second_write) {
1366 fx->second_st_storage = (osal_file_status_t)-1;
1367 ptr_arg = tc->second_write_content;
1368 size_arg = tc->second_size;
1369 nmemb_arg = tc->second_nmemb;
1370 stream_arg = fx->writable_osal_file;
1371 second_write_st_arg = &fx->second_st_storage;
1372
1373 // ACT (second write call)
1374 second_write_ret = fx->file_ops->write(ptr_arg, size_arg, nmemb_arg, stream_arg, second_write_st_arg);
1375 }
1376
1377 // ASSERT
1378 assert_true(first_write_ret == tc->first_expected_ret);
1379 if (first_write_st_arg) {
1380 assert_int_equal(*first_write_st_arg, tc->first_expected_st);
1381 }
1382 if (tc->do_second_write) {
1383 assert_true(second_write_ret == tc->second_expected_ret);
1384 if (second_write_st_arg) {
1385 assert_int_equal(*second_write_st_arg, tc->second_expected_st);
1386 }
1387 }
1388
1389 size_t total_len =
1390 tc->initial_file_content_len
1391 +
1392 tc->first_expected_ret * tc->first_size
1393 +
1394 ((tc->do_second_write) ? tc->second_expected_ret * tc->second_size : 0);
1395 assert_true(
1396 tc->expected_file_content_len
1397 ==
1398 total_len
1399 );
1400 assert_true(
1401 fx->file_ops->close(fx->writable_osal_file)
1402 ==
1404 );
1405
1406 fx->writable_osal_file = NULL;
1407
1408 if (tc->expected_file_content_len > 0) {
1409
1410 assert_int_equal(
1411 fx->file_ops->open(
1412 &fx->verification_osal_file,
1413 "osal_file_write_tmp_file.txt",
1414 "rb",
1415 fx->mem_ops
1416 ),
1418 );
1419 uint8_t read_buf[16] = { 0 };
1421 assert_true(
1422 fx->file_ops->read(read_buf, 1, tc->expected_file_content_len, fx->verification_osal_file, &st)
1423 ==
1424 tc->expected_file_content_len
1425 );
1426 assert_int_equal(st, OSAL_FILE_STATUS_OK);
1427 assert_memory_equal(
1428 read_buf,
1429 tc->expected_file_content,
1430 tc->expected_file_content_len
1431 );
1432 assert_int_equal(
1433 fx->file_ops->close(
1434 fx->verification_osal_file
1435 ),
1437 );
1438 fx->verification_osal_file = NULL;
1439 }
1440}
1441
1442//-----------------------------------------------------------------------------
1443// CASES
1444//-----------------------------------------------------------------------------
1445
1446static const test_osal_file_write_case_t CASE_OSAL_FILE_WRITE_PTR_NULL = {
1447 .name = "osal_file_write_ptr_null",
1449
1450 .initial_file_content = "abc",
1451 .initial_file_content_len = 3,
1452 .first_size = 1,
1453 .first_nmemb = 5,
1454 .do_second_write = false,
1455
1456 .first_expected_ret = 0,
1457 .first_expected_st = OSAL_FILE_STATUS_INVALID,
1458 .expected_file_content = "abc",
1459 .expected_file_content_len = 3
1460};
1461
1462static const test_osal_file_write_case_t CASE_OSAL_FILE_WRITE_STREAM_NULL = {
1463 .name = "osal_file_write_stream_null",
1465
1466 .initial_file_content = "abc",
1467 .initial_file_content_len = 3,
1468 .first_write_content = "first",
1469 .first_size = 1,
1470 .first_nmemb = 5,
1471 .do_second_write = false,
1472
1473 .first_expected_ret = 0,
1474 .first_expected_st = OSAL_FILE_STATUS_INVALID,
1475 .expected_file_content = "abc",
1476 .expected_file_content_len = 3
1477};
1478
1479static const test_osal_file_write_case_t CASE_OSAL_FILE_WRITE_ST_NULL = {
1480 .name = "osal_file_write_st_null",
1482
1483 .initial_file_content = "abc",
1484 .initial_file_content_len = 3,
1485 .first_write_content = "first",
1486 .first_size = 1,
1487 .first_nmemb = 5,
1488 .do_second_write = false,
1489
1490 .first_expected_ret = 0,
1491 .expected_file_content = "abc",
1492 .expected_file_content_len = 3
1493};
1494
1495static const test_osal_file_write_case_t CASE_OSAL_FILE_WRITE_ZERO_SIZE = {
1496 .name = "osal_file_write_zero_size",
1498
1499 .initial_file_content = "abc",
1500 .initial_file_content_len = 3,
1501 .first_write_content = "first",
1502 .first_size = 0,
1503 .first_nmemb = 5,
1504 .do_second_write = false,
1505
1506 .first_expected_ret = 0,
1507 .first_expected_st = OSAL_FILE_STATUS_OK,
1508 .expected_file_content = "abc",
1509 .expected_file_content_len = 3
1510};
1511
1512static const test_osal_file_write_case_t CASE_OSAL_FILE_WRITE_ZERO_NMEMB = {
1513 .name = "osal_file_write_zero_nmemb",
1515
1516 .initial_file_content = "abc",
1517 .initial_file_content_len = 3,
1518 .first_write_content = "first",
1519 .first_size = 1,
1520 .first_nmemb = 0,
1521 .do_second_write = false,
1522
1523 .first_expected_ret = 0,
1524 .first_expected_st = OSAL_FILE_STATUS_OK,
1525 .expected_file_content = "abc",
1526 .expected_file_content_len = 3
1527};
1528
1529static const test_osal_file_write_case_t CASE_OSAL_FILE_WRITE_ONE_ELEMENT_OK = {
1530 .name = "osal_file_write_one_element_ok",
1532
1533 .initial_file_content = "abc",
1534 .initial_file_content_len = 3,
1535 .first_write_content = "first",
1536 .first_size = 2,
1537 .first_nmemb = 1,
1538 .do_second_write = false,
1539
1540 .first_expected_ret = 1,
1541 .first_expected_st = OSAL_FILE_STATUS_OK,
1542 .expected_file_content = "abcfi",
1543 .expected_file_content_len = 5
1544};
1545
1546static const test_osal_file_write_case_t CASE_OSAL_FILE_WRITE_MULTIPLE_ELEMENTS_SIZE_1_OK = {
1547 .name = "osal_file_write_multiple_elements_size_1_ok",
1549
1550 .initial_file_content = "abc",
1551 .initial_file_content_len = 3,
1552 .first_write_content = "first",
1553 .first_size = 1,
1554 .first_nmemb = 4,
1555 .do_second_write = false,
1556
1557 .first_expected_ret = 4,
1558 .first_expected_st = OSAL_FILE_STATUS_OK,
1559 .expected_file_content = "abcfirs",
1560 .expected_file_content_len = 7
1561};
1562
1563static const test_osal_file_write_case_t CASE_OSAL_FILE_WRITE_MULTIPLE_ELEMENTS_SIZE_GT_1_OK = {
1564 .name = "osal_file_write_multiple_elements_size_gt_1_ok",
1566
1567 .initial_file_content = "abc",
1568 .initial_file_content_len = 3,
1569 .first_write_content = "first",
1570 .first_size = 2,
1571 .first_nmemb = 2,
1572 .do_second_write = false,
1573
1574 .first_expected_ret = 2,
1575 .first_expected_st = OSAL_FILE_STATUS_OK,
1576 .expected_file_content = "abcfirs",
1577 .expected_file_content_len = 7
1578};
1579
1580static const test_osal_file_write_case_t CASE_OSAL_FILE_WRITE_SEQUENTIAL_WRITES_OK = {
1581 .name = "osal_file_write_sequential_writes_ok",
1583
1584 .initial_file_content = "abc",
1585 .initial_file_content_len = 3,
1586 .first_write_content = "first",
1587 .first_size = 1,
1588 .first_nmemb = 5,
1589 .do_second_write = true,
1590 .second_write_content = "second",
1591 .second_size = 2,
1592 .second_nmemb = 3,
1593
1594 .first_expected_ret = 5,
1595 .first_expected_st = OSAL_FILE_STATUS_OK,
1596 .second_expected_ret = 3,
1597 .second_expected_st = OSAL_FILE_STATUS_OK,
1598 .expected_file_content = "abcfirstsecond",
1599 .expected_file_content_len = 14
1600};
1601
1602//-----------------------------------------------------------------------------
1603// CASES REGISTRY
1604//-----------------------------------------------------------------------------
1605
1606#define OSAL_FILE_WRITE_CASES(X) \
1607X(CASE_OSAL_FILE_WRITE_PTR_NULL) \
1608X(CASE_OSAL_FILE_WRITE_STREAM_NULL) \
1609X(CASE_OSAL_FILE_WRITE_ST_NULL) \
1610X(CASE_OSAL_FILE_WRITE_ZERO_SIZE) \
1611X(CASE_OSAL_FILE_WRITE_ZERO_NMEMB) \
1612X(CASE_OSAL_FILE_WRITE_ONE_ELEMENT_OK) \
1613X(CASE_OSAL_FILE_WRITE_MULTIPLE_ELEMENTS_SIZE_1_OK) \
1614X(CASE_OSAL_FILE_WRITE_MULTIPLE_ELEMENTS_SIZE_GT_1_OK) \
1615X(CASE_OSAL_FILE_WRITE_SEQUENTIAL_WRITES_OK)
1616
1617#define OSAL_FILE_MAKE_WRITE_TEST(case_sym) \
1618LEXLEO_MAKE_TEST(osal_file_write, case_sym)
1619
1620static const struct CMUnitTest osal_file_write_tests[] = {
1621 OSAL_FILE_WRITE_CASES(OSAL_FILE_MAKE_WRITE_TEST)
1622};
1623
1624#undef OSAL_FILE_WRITE_CASES
1625#undef OSAL_FILE_MAKE_WRITE_TEST
1626
1652
1662typedef struct {
1663 const char *name;
1664
1665 // arrange
1667 uint8_t file_content[16];
1668 size_t file_content_len;
1669
1670 // assert
1671 osal_file_status_t expected_ret;
1672} test_osal_file_flush_case_t;
1673
1683typedef struct {
1684 // runtime resources
1685 OSAL_FILE *writable_osal_file;
1686
1687 // injection
1688 const osal_mem_ops_t *mem_ops;
1689
1690 // exercised public operations table
1691 const osal_file_ops_t *file_ops;
1692
1693 const test_osal_file_flush_case_t *tc;
1694} test_osal_file_flush_fixture_t;
1695
1696//-----------------------------------------------------------------------------
1697// FIXTURES
1698//-----------------------------------------------------------------------------
1699
1703static int setup_osal_file_flush(void **state)
1704{
1705 const test_osal_file_flush_case_t *tc =
1706 (const test_osal_file_flush_case_t *)(*state);
1707
1708 test_osal_file_flush_fixture_t *fx =
1709 (test_osal_file_flush_fixture_t *)osal_malloc(sizeof(*fx));
1710 if (!fx) return -1;
1711
1712 osal_memset(fx, 0, sizeof(*fx));
1713 fx->tc = tc;
1714
1715 // DI
1716 fake_memory_reset();
1717 fx->mem_ops = osal_mem_test_fake_ops();
1718
1719 // prepare tmp file
1720 fx->file_ops = osal_file_default_ops();
1721 assert_int_equal(
1722 fx->file_ops->open(
1723 &fx->writable_osal_file,
1724 "osal_file_flush_tmp_file.txt",
1725 "wb",
1726 fx->mem_ops
1727 ),
1729 );
1731 assert_true(
1732 fx->file_ops->write(
1733 tc->file_content,
1734 1,
1735 tc->file_content_len,
1736 fx->writable_osal_file,
1737 &st)
1738 ==
1739 tc->file_content_len
1740 );
1741 assert_int_equal(st, OSAL_FILE_STATUS_OK);
1742
1743 *state = fx;
1744 return 0;
1745}
1746
1750static int teardown_osal_file_flush(void **state)
1751{
1752 test_osal_file_flush_fixture_t *fx =
1753 (test_osal_file_flush_fixture_t *)(*state);
1754
1755 if (fx->writable_osal_file) {
1756 assert_true(
1757 fx->file_ops->close(fx->writable_osal_file) == OSAL_FILE_STATUS_OK
1758 );
1759 fx->writable_osal_file = NULL;
1760 }
1761
1762 assert_true(fake_memory_no_leak());
1763 assert_true(fake_memory_no_invalid_free());
1764 assert_true(fake_memory_no_double_free());
1765
1766 osal_free(fx);
1767 *state = NULL;
1768 return 0;
1769}
1770
1771//-----------------------------------------------------------------------------
1772// TEST
1773//-----------------------------------------------------------------------------
1774
1778static void test_osal_file_flush(void **state)
1779{
1780 test_osal_file_flush_fixture_t *fx =
1781 (test_osal_file_flush_fixture_t *)(*state);
1782 const test_osal_file_flush_case_t *tc =
1783 (const test_osal_file_flush_case_t *)fx->tc;
1784
1785 // ARRANGE
1786 OSAL_FILE *stream_arg = fx->writable_osal_file;
1787
1788 // invalid args
1789 if (tc->scenario == OSAL_FILE_FLUSH_SCENARIO_STREAM_NULL) {
1790 stream_arg = NULL;
1791 }
1792
1793 // ACT
1794 osal_file_status_t ret = fx->file_ops->flush(stream_arg);
1795
1796
1797 // ASSERT
1798 assert_int_equal(ret, tc->expected_ret);
1799}
1800
1801//-----------------------------------------------------------------------------
1802// CASES
1803//-----------------------------------------------------------------------------
1804
1805static const test_osal_file_flush_case_t CASE_OSAL_FILE_FLUSH_STREAM_NULL = {
1806 .name = "osal_file_flush_stream_null",
1808
1809 .file_content = "abc",
1810 .file_content_len = 3,
1811
1812 .expected_ret = OSAL_FILE_STATUS_INVALID
1813};
1814
1815static const test_osal_file_flush_case_t CASE_OSAL_FILE_FLUSH_OK = {
1816 .name = "osal_file_flush_ok",
1817 .scenario = OSAL_FILE_FLUSH_SCENARIO_OK,
1818
1819 .file_content = "abc",
1820 .file_content_len = 3,
1821
1822 .expected_ret = OSAL_FILE_STATUS_OK
1823};
1824
1825//-----------------------------------------------------------------------------
1826// CASES REGISTRY
1827//-----------------------------------------------------------------------------
1828
1829#define OSAL_FILE_FLUSH_CASES(X) \
1830X(CASE_OSAL_FILE_FLUSH_STREAM_NULL) \
1831X(CASE_OSAL_FILE_FLUSH_OK)
1832
1833#define OSAL_FILE_MAKE_FLUSH_TEST(case_sym) \
1834LEXLEO_MAKE_TEST(osal_file_flush, case_sym)
1835
1836static const struct CMUnitTest osal_file_flush_tests[] = {
1837 OSAL_FILE_FLUSH_CASES(OSAL_FILE_MAKE_FLUSH_TEST)
1838};
1839
1840#undef OSAL_FILE_FLUSH_CASES
1841#undef OSAL_FILE_MAKE_FLUSH_TEST
1842
1868
1874typedef struct {
1875 const char *name;
1876
1877 // arrange
1879
1880 // assert
1881 osal_file_status_t expected_ret;
1882} test_osal_file_close_case_t;
1883
1893typedef struct {
1894 // runtime resources
1895 OSAL_FILE *osal_file;
1896
1897 // injection
1898 const osal_mem_ops_t *mem_ops;
1899
1900 // exercised public operations table
1901 const osal_file_ops_t *file_ops;
1902
1903 const test_osal_file_close_case_t *tc;
1904} test_osal_file_close_fixture_t;
1905
1906//-----------------------------------------------------------------------------
1907// FIXTURES
1908//-----------------------------------------------------------------------------
1909
1913static int setup_osal_file_close(void **state)
1914{
1915 const test_osal_file_close_case_t *tc =
1916 (const test_osal_file_close_case_t *)(*state);
1917
1918 test_osal_file_close_fixture_t *fx =
1919 (test_osal_file_close_fixture_t *)osal_malloc(sizeof(*fx));
1920 if (!fx) return -1;
1921
1922 osal_memset(fx, 0, sizeof(*fx));
1923 fx->tc = tc;
1924
1925 // DI
1926 fake_memory_reset();
1927 fx->mem_ops = osal_mem_test_fake_ops();
1928
1929 // prepare tmp file
1930 fx->file_ops = osal_file_default_ops();
1931 assert_int_equal(
1932 fx->file_ops->open(
1933 &fx->osal_file,
1934 "osal_file_close_tmp_file.txt",
1935 "wb",
1936 fx->mem_ops
1937 ),
1939 );
1940
1941 *state = fx;
1942 return 0;
1943}
1944
1948static int teardown_osal_file_close(void **state)
1949{
1950 test_osal_file_close_fixture_t *fx =
1951 (test_osal_file_close_fixture_t *)(*state);
1952
1953 if (fx->osal_file) {
1954 assert_int_equal(
1955 fx->file_ops->close(fx->osal_file),
1957 );
1958 fx->osal_file = NULL;
1959 }
1960
1961 assert_true(fake_memory_no_leak());
1962 assert_true(fake_memory_no_invalid_free());
1963 assert_true(fake_memory_no_double_free());
1964
1965 osal_free(fx);
1966 *state = NULL;
1967 return 0;
1968}
1969
1970//-----------------------------------------------------------------------------
1971// TEST
1972//-----------------------------------------------------------------------------
1973
1977static void test_osal_file_close(void **state)
1978{
1979 test_osal_file_close_fixture_t *fx =
1980 (test_osal_file_close_fixture_t *)(*state);
1981 const test_osal_file_close_case_t *tc =
1982 (const test_osal_file_close_case_t *)fx->tc;
1983
1984 // ARRANGE
1985 OSAL_FILE *stream_arg = fx->osal_file;
1986
1987 // invalid args
1988 if (tc->scenario == OSAL_FILE_CLOSE_SCENARIO_STREAM_NULL) {
1989 stream_arg = NULL;
1990 }
1991
1992 // ACT
1993 osal_file_status_t ret = fx->file_ops->close(stream_arg);
1994 if (ret == OSAL_FILE_STATUS_OK && stream_arg == fx->osal_file) {
1995 fx->osal_file = NULL;
1996 }
1997
1998 // ASSERT
1999 assert_int_equal(ret, tc->expected_ret);
2000}
2001
2002//-----------------------------------------------------------------------------
2003// CASES
2004//-----------------------------------------------------------------------------
2005
2006static const test_osal_file_close_case_t CASE_OSAL_FILE_CLOSE_STREAM_NULL = {
2007 .name = "osal_file_close_stream_null",
2009
2010 .expected_ret = OSAL_FILE_STATUS_INVALID
2011};
2012
2013static const test_osal_file_close_case_t CASE_OSAL_FILE_CLOSE_OK = {
2014 .name = "osal_file_close_ok",
2015 .scenario = OSAL_FILE_CLOSE_SCENARIO_OK,
2016
2017 .expected_ret = OSAL_FILE_STATUS_OK
2018};
2019
2020//-----------------------------------------------------------------------------
2021// CASES REGISTRY
2022//-----------------------------------------------------------------------------
2023
2024#define OSAL_FILE_CLOSE_CASES(X) \
2025X(CASE_OSAL_FILE_CLOSE_STREAM_NULL) \
2026X(CASE_OSAL_FILE_CLOSE_OK)
2027
2028#define OSAL_FILE_MAKE_CLOSE_TEST(case_sym) \
2029LEXLEO_MAKE_TEST(osal_file_close, case_sym)
2030
2031static const struct CMUnitTest osal_file_close_tests[] = {
2032 OSAL_FILE_CLOSE_CASES(OSAL_FILE_MAKE_CLOSE_TEST)
2033};
2034
2035#undef OSAL_FILE_CLOSE_CASES
2036#undef OSAL_FILE_MAKE_CLOSE_TEST
2037
2042//-----------------------------------------------------------------------------
2043// MAIN
2044//-----------------------------------------------------------------------------
2045
2046int main(void) {
2047 static const struct CMUnitTest osal_file_non_parametric_tests[] = {
2048 cmocka_unit_test(test_osal_file_default_ops)
2049 };
2050
2051 int failed = 0;
2052 failed += cmocka_run_group_tests(osal_file_non_parametric_tests, NULL, NULL);
2053 failed += cmocka_run_group_tests(osal_file_open_tests, NULL, NULL);
2054 failed += cmocka_run_group_tests(osal_file_read_tests, NULL, NULL);
2055 failed += cmocka_run_group_tests(osal_file_write_tests, NULL, NULL);
2056 failed += cmocka_run_group_tests(osal_file_flush_tests, NULL, NULL);
2057 failed += cmocka_run_group_tests(osal_file_close_tests, NULL, NULL);
2058
2059 return failed;
2060}
2061
const osal_file_ops_t * osal_file_default_ops(void)
Return the default OSAL file operations for the active platform.
@ OSAL_FILE_STATUS_OOM
@ OSAL_FILE_STATUS_OK
@ OSAL_FILE_STATUS_INVALID
osal_file_status_t
int main()
Definition main.c:5
void * osal_memset(void *s, int c, size_t n)
Definition osal_mem.c:30
void * osal_malloc(size_t size)
Definition osal_mem.c:8
void osal_free(void *ptr)
Definition osal_mem.c:11
const osal_mem_ops_t * osal_mem_test_fake_ops(void)
Private representation of an acquired OSAL file handle.
size_t(* write)(osal_file_t *f, const void *buf, size_t n, osal_file_status_t *status)
osal_file_status_t(* close)(osal_file_t *f)
osal_file_status_t(* flush)(osal_file_t *f)
size_t(* read)(osal_file_t *f, void *buf, size_t n, osal_file_status_t *status)
osal_file_t *(* open)(const char *path_utf8, uint32_t flags, osal_file_status_t *status, const osal_file_env_t *env)
osal_file_write_scenario_t
Scenarios for osal_file_ops_t::write().
@ OSAL_FILE_WRITE_SCENARIO_WRITE_MULTIPLE_ELEMENTS_SIZE_GT_1_OK
@ OSAL_FILE_WRITE_SCENARIO_ST_NULL
@ OSAL_FILE_WRITE_SCENARIO_ZERO_SIZE
@ OSAL_FILE_WRITE_SCENARIO_WRITE_MULTIPLE_ELEMENTS_SIZE_1_OK
@ OSAL_FILE_WRITE_SCENARIO_ZERO_NMEMB
@ OSAL_FILE_WRITE_SCENARIO_STREAM_NULL
@ OSAL_FILE_WRITE_SCENARIO_PTR_NULL
@ OSAL_FILE_WRITE_SCENARIO_WRITE_ONE_ELEMENT_OK
@ OSAL_FILE_WRITE_SCENARIO_SEQUENTIAL_WRITES_OK
osal_file_close_scenario_t
Scenarios for osal_file_ops_t::close().
@ OSAL_FILE_CLOSE_SCENARIO_OK
@ OSAL_FILE_CLOSE_SCENARIO_STREAM_NULL
static void test_osal_file_default_ops(void **state)
Test osal_file_default_ops().
osal_file_flush_scenario_t
Scenarios for osal_file_ops_t::flush().
@ OSAL_FILE_FLUSH_SCENARIO_OK
@ OSAL_FILE_FLUSH_SCENARIO_STREAM_NULL
osal_file_read_scenario_t
Scenarios for osal_file_ops_t::read().
@ OSAL_FILE_READ_SCENARIO_READ_AFTER_EOF
@ OSAL_FILE_READ_SCENARIO_PTR_NULL
@ OSAL_FILE_READ_SCENARIO_SEQUENTIAL_READS_PARTIAL_THEN_PARTIAL
@ OSAL_FILE_READ_SCENARIO_ZERO_NMEMB
@ OSAL_FILE_READ_SCENARIO_READ_EXACT_AVAILABLE_SIZE_GT_1
@ OSAL_FILE_READ_SCENARIO_READ_LESS_THAN_AVAILABLE_SIZE_GT_1
@ OSAL_FILE_READ_SCENARIO_READ_EXACT_AVAILABLE
@ OSAL_FILE_READ_SCENARIO_ZERO_SIZE
@ OSAL_FILE_READ_SCENARIO_READ_MORE_THAN_AVAILABLE_SIZE_GT_1
@ OSAL_FILE_READ_SCENARIO_STREAM_NULL
@ OSAL_FILE_READ_SCENARIO_READ_MORE_THAN_AVAILABLE
@ OSAL_FILE_READ_SCENARIO_READ_LESS_THAN_AVAILABLE
@ OSAL_FILE_READ_SCENARIO_EMPTY_FILE
@ OSAL_FILE_READ_SCENARIO_ST_NULL
osal_file_open_scenario_t
Scenarios for osal_file_ops_t::open().
@ OSAL_FILE_OPEN_SCENARIO_MODE_NULL
@ OSAL_FILE_OPEN_SCENARIO_PATHNAME_EMPTY
@ OSAL_FILE_OPEN_SCENARIO_PATHNAME_NULL
@ OSAL_FILE_OPEN_SCENARIO_OOM
@ OSAL_FILE_OPEN_SCENARIO_MODE_UNSUPPORTED
@ OSAL_FILE_OPEN_SCENARIO_OK
@ OSAL_FILE_OPEN_SCENARIO_MEM_OPS_NULL
@ OSAL_FILE_OPEN_SCENARIO_OUT_NULL