| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 |
|
|---|
| 4 | util_file testing
|
|---|
| 5 |
|
|---|
| 6 | Copyright (C) Jelmer Vernooij 2005
|
|---|
| 7 |
|
|---|
| 8 | This program is free software; you can redistribute it and/or modify
|
|---|
| 9 | it under the terms of the GNU General Public License as published by
|
|---|
| 10 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 11 | (at your option) any later version.
|
|---|
| 12 |
|
|---|
| 13 | This program is distributed in the hope that it will be useful,
|
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 16 | GNU General Public License for more details.
|
|---|
| 17 |
|
|---|
| 18 | You should have received a copy of the GNU General Public License
|
|---|
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include "includes.h"
|
|---|
| 23 | #include "system/filesys.h"
|
|---|
| 24 | #include "torture/torture.h"
|
|---|
| 25 |
|
|---|
| 26 | #define TEST_FILENAME "utilfile.test"
|
|---|
| 27 | #define TEST_LINE1 "This is list line 1..."
|
|---|
| 28 | #define TEST_LINE2 ".. and this is line 2"
|
|---|
| 29 | #define TEST_LINE3 "and end of the file"
|
|---|
| 30 |
|
|---|
| 31 | #define TEST_DATA TEST_LINE1 "\n" TEST_LINE2 "\n" TEST_LINE3
|
|---|
| 32 |
|
|---|
| 33 | static bool test_file_load_save(struct torture_context *tctx)
|
|---|
| 34 | {
|
|---|
| 35 | size_t len;
|
|---|
| 36 | char *data;
|
|---|
| 37 | TALLOC_CTX *mem_ctx = tctx;
|
|---|
| 38 |
|
|---|
| 39 | torture_assert(tctx, file_save(TEST_FILENAME, TEST_DATA, strlen(TEST_DATA)),
|
|---|
| 40 | "saving file");
|
|---|
| 41 |
|
|---|
| 42 | torture_assert_file_contains_text(tctx, TEST_FILENAME, TEST_DATA,
|
|---|
| 43 | "file contents");
|
|---|
| 44 |
|
|---|
| 45 | data = file_load(TEST_FILENAME, &len, 0, mem_ctx);
|
|---|
| 46 | torture_assert(tctx, data, "loading file");
|
|---|
| 47 |
|
|---|
| 48 | torture_assert_int_equal(tctx, len, strlen(TEST_DATA), "Length");
|
|---|
| 49 |
|
|---|
| 50 | torture_assert_mem_equal(tctx, data, TEST_DATA, len, "Contents");
|
|---|
| 51 |
|
|---|
| 52 | data = file_load(TEST_FILENAME, &len, 5, mem_ctx);
|
|---|
| 53 |
|
|---|
| 54 | torture_assert_int_equal(tctx, len, 5, "Length");
|
|---|
| 55 |
|
|---|
| 56 | torture_assert_mem_equal(tctx, data, TEST_DATA, len, "Contents");
|
|---|
| 57 |
|
|---|
| 58 | unlink(TEST_FILENAME);
|
|---|
| 59 | return true;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | static bool test_afdgets(struct torture_context *tctx)
|
|---|
| 64 | {
|
|---|
| 65 | int fd;
|
|---|
| 66 | char *line;
|
|---|
| 67 | TALLOC_CTX *mem_ctx = tctx;
|
|---|
| 68 |
|
|---|
| 69 | torture_assert(tctx, file_save(TEST_FILENAME, (const void *)TEST_DATA,
|
|---|
| 70 | strlen(TEST_DATA)),
|
|---|
| 71 | "saving file");
|
|---|
| 72 |
|
|---|
| 73 | fd = open(TEST_FILENAME, O_RDONLY);
|
|---|
| 74 |
|
|---|
| 75 | torture_assert(tctx, fd != -1, "opening file");
|
|---|
| 76 |
|
|---|
| 77 | line = afdgets(fd, mem_ctx, 8);
|
|---|
| 78 | torture_assert(tctx, strcmp(line, TEST_LINE1) == 0, "line 1 mismatch");
|
|---|
| 79 |
|
|---|
| 80 | line = afdgets(fd, mem_ctx, 8);
|
|---|
| 81 | torture_assert(tctx, strcmp(line, TEST_LINE2) == 0, "line 2 mismatch");
|
|---|
| 82 |
|
|---|
| 83 | line = afdgets(fd, mem_ctx, 8);
|
|---|
| 84 | torture_assert(tctx, strcmp(line, TEST_LINE3) == 0, "line 3 mismatch");
|
|---|
| 85 |
|
|---|
| 86 | close(fd);
|
|---|
| 87 |
|
|---|
| 88 | unlink(TEST_FILENAME);
|
|---|
| 89 | return true;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | struct torture_suite *torture_local_util_file(TALLOC_CTX *mem_ctx)
|
|---|
| 93 | {
|
|---|
| 94 | struct torture_suite *suite = torture_suite_create(mem_ctx, "file");
|
|---|
| 95 |
|
|---|
| 96 | torture_suite_add_simple_test(suite, "file_load_save",
|
|---|
| 97 | test_file_load_save);
|
|---|
| 98 |
|
|---|
| 99 | torture_suite_add_simple_test(suite, "afdgets", test_afdgets);
|
|---|
| 100 |
|
|---|
| 101 | return suite;
|
|---|
| 102 | }
|
|---|