1 /*
2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3 * This file may be used under the terms of the MIT License.
4 */
5
6
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include <fs_attr.h>
12 #include <TypeConstants.h>
13
14
15 const char* kTestFileName = "/tmp/fs_attr_test";
16
17
18 void
test_read(int fd,const char * attribute,type_code type,const char * data,size_t length)19 test_read(int fd, const char* attribute, type_code type, const char* data,
20 size_t length)
21 {
22 attr_info info;
23 if (fs_stat_attr(fd, attribute, &info) != 0) {
24 fprintf(stderr, "Could not stat attribute \"%s\": %s\n", attribute,
25 strerror(errno));
26 exit(1);
27 }
28
29 if (info.size != length) {
30 fprintf(stderr, "Length does not match for \"%s\": should be %ld, is "
31 "%lld\n", data, length, info.size);
32 exit(1);
33 }
34
35 if (info.type != type) {
36 fprintf(stderr, "Type does not match B_RAW_TYPE!\n");
37 exit(1);
38 }
39
40 char buffer[4096];
41 ssize_t bytesRead = fs_read_attr(fd, attribute, B_RAW_TYPE, 0, buffer,
42 length);
43 if (bytesRead != (ssize_t)length) {
44 fprintf(stderr, "Bytes read does not match: should be %ld, is %ld\n",
45 length, bytesRead);
46 exit(1);
47 }
48
49 if (memcmp(data, buffer, length)) {
50 fprintf(stderr, "Bytes do not match: should be \"%s\", is \"%s\"\n",
51 data, buffer);
52 exit(1);
53 }
54 }
55
56
57 int
main(int argc,char ** argv)58 main(int argc, char** argv)
59 {
60 int fd = open(kTestFileName, O_CREAT | O_TRUNC | O_WRONLY);
61 if (fd < 0) {
62 fprintf(stderr, "Creating test file \"%s\" failed: %s\n", kTestFileName,
63 strerror(errno));
64 return 1;
65 }
66
67 // Test the old BeOS API
68
69 fs_write_attr(fd, "TEST", B_STRING_TYPE, 0, "Hello BeOS", 11);
70 test_read(fd, "TEST", B_STRING_TYPE, "Hello BeOS", 11);
71
72 // TODO: this shows a bug in BFS; the index is not updated correctly
73 fs_write_attr(fd, "TEST", B_STRING_TYPE, 6, "Haiku", 6);
74 test_read(fd, "TEST", B_STRING_TYPE, "Hello Haiku", 12);
75
76 fs_write_attr(fd, "TESTraw", B_RAW_TYPE, 16, "Haiku", 6);
77 test_read(fd, "TESTraw", B_RAW_TYPE, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0Haiku",
78 22);
79
80 fs_write_attr(fd, "TESTraw", B_RAW_TYPE, 0, "Haiku", 6);
81 test_read(fd, "TESTraw", B_RAW_TYPE, "Haiku", 6);
82
83 char buffer[4096];
84 memset(buffer, 0, sizeof(buffer));
85 strcpy(buffer, "Hello");
86 fs_write_attr(fd, "TESTswitch", B_RAW_TYPE, 0, buffer,
87 strlen(buffer) + 1);
88 test_read(fd, "TESTswitch", B_RAW_TYPE, buffer, strlen(buffer) + 1);
89
90 strcpy(buffer + 4000, "Haiku");
91 fs_write_attr(fd, "TESTswitch", B_RAW_TYPE, 0, buffer, 4006);
92 test_read(fd, "TESTswitch", B_RAW_TYPE, buffer, 4006);
93
94 return 0;
95 }
96
97