]> git.saurik.com Git - apple/hfs.git/blob - livefiles_cs_plugin/livefiles_cs_tester.c
hfs-556.41.1.tar.gz
[apple/hfs.git] / livefiles_cs_plugin / livefiles_cs_tester.c
1 //
2 // Copyright (c) 2019 Apple Inc. All rights reserved.
3 //
4 // @APPLE_LICENSE_HEADER_START@
5 //
6 // This file contains Original Code and/or Modifications of Original Code
7 // as defined in and that are subject to the Apple Public Source License
8 // Version 2.0 (the 'License'). You may not use this file except in
9 // compliance with the License. Please obtain a copy of the License at
10 // http://www.opensource.apple.com/apsl/ and read it before using this
11 // file.
12 //
13 // The Original Code and all software distributed under the License are
14 // distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 // EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 // INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 // Please see the License for the specific language governing rights and
19 // limitations under the License.
20 //
21 // @APPLE_LICENSE_HEADER_END@
22 //
23 // livefiles_cs_tester.c - Implements unit tests for livefiles
24 // Apple_CoreStorage plugin.
25 //
26
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33
34 #include <UserFS/UserVFS.h>
35
36 extern UVFSFSOps cs_fsops;
37
38 //
39 // Enums describing file-system formats.
40 //
41 typedef enum {
42 JHFS = 1,
43 APFS,
44 FAT32,
45 EXFAT,
46 APPLE_CS,
47
48 INVALID_FS_TYPE = INT8_MAX,
49 } lf_cspt_fstype_t;
50
51 //
52 // Array describing file-system name and analogous types.
53 //
54 const struct {
55 const char *const fs_name;
56 lf_cspt_fstype_t fs_type;
57 } lf_cspt_fstype_arr_t[] = {
58
59 {"JHFS", JHFS},
60 {"APFS", APFS},
61 {"EXFAT",EXFAT},
62 {"FAT32",FAT32},
63 {"APPLE_CS", APPLE_CS},
64
65 {NULL, INVALID_FS_TYPE}
66 };
67
68 //
69 // Validate file-system types that is supported by this program.
70 //
71 static bool
72 is_fstype_valid(const char *fs_name, lf_cspt_fstype_t *fs_type)
73 {
74 int idx;
75
76 for (idx = 0; lf_cspt_fstype_arr_t[idx].fs_name != NULL; idx++) {
77 if (strcmp(fs_name, lf_cspt_fstype_arr_t[idx].fs_name) == 0) {
78 *fs_type = lf_cspt_fstype_arr_t[idx].fs_type;
79 return true;
80 }
81 }
82
83 return false;
84 }
85
86 //
87 // Usage string returned to user.
88 //
89 static int
90 usage(const char *prog_name)
91 {
92 fprintf(stderr, "Usage: %s filesystem-format device-path\n",
93 prog_name);
94 return EINVAL;
95 }
96
97 int
98 main(int argc, char *argv[])
99 {
100 int fd, error;
101 lf_cspt_fstype_t fs_type;
102
103 if (argc != 3) {
104 return usage(argv[0]);
105 }
106
107 if (!is_fstype_valid(argv[1], &fs_type)) {
108 fprintf(stderr, "Unknown file-system type %s\n", argv[1]);
109 return EINVAL;
110 }
111
112 fd = open(argv[2], O_RDWR);
113 if (fd < 0) {
114 fprintf(stderr, "Failed to open device [%s]: %d\n",
115 argv[2], errno);
116 return EBADF;
117 }
118
119 error = cs_fsops.fsops_init();
120 printf("Init for fs_type %s returned [%d]\n", argv[1], error);
121 if (error) {
122 goto test_end;
123 }
124
125 error = cs_fsops.fsops_taste(fd);
126 switch(fs_type) {
127 case JHFS:
128 case APFS:
129 case EXFAT:
130 case FAT32:
131
132 //
133 // Taste with these disk-image is expected to retrun
134 // ENOTSUP. Thus supress the error here.
135 //
136 if (error == ENOTSUP) {
137 error = 0;
138 }
139 break;
140
141 case APPLE_CS:
142 break;
143
144 //
145 // Control should not come here.
146 //
147 default:
148 fprintf(stderr, "Bug in test.\n");
149 }
150 printf("Taste for fs_type %s returned [%d]\n", argv[1], error);
151 cs_fsops.fsops_fini();
152
153 test_end:
154 printf("Test result [%d]\n", error);
155 close(fd);
156 return EXIT_SUCCESS;
157 }