2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3 * This file may be used under the terms of the MIT License.
12 #include <TypeConstants.h>
15 const char* kTestFileName
= "/tmp/fs_attr_test";
19 test_read(int fd
, const char* attribute
, type_code type
, const char* data
,
23 if (fs_stat_attr(fd
, attribute
, &info
) != 0) {
24 fprintf(stderr
, "Could not stat attribute \"%s\": %s\n", attribute
,
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
);
35 if (info
.type
!= type
) {
36 fprintf(stderr
, "Type does not match B_RAW_TYPE!\n");
41 ssize_t bytesRead
= fs_read_attr(fd
, attribute
, B_RAW_TYPE
, 0, buffer
,
43 if (bytesRead
!= (ssize_t
)length
) {
44 fprintf(stderr
, "Bytes read does not match: should be %ld, is %ld\n",
49 if (memcmp(data
, buffer
, length
)) {
50 fprintf(stderr
, "Bytes do not match: should be \"%s\", is \"%s\"\n",
58 main(int argc
, char** argv
)
60 int fd
= open(kTestFileName
, O_CREAT
| O_TRUNC
| O_WRONLY
);
62 fprintf(stderr
, "Creating test file \"%s\" failed: %s\n", kTestFileName
,
67 // Test the old BeOS API
69 fs_write_attr(fd
, "TEST", B_STRING_TYPE
, 0, "Hello BeOS", 11);
70 test_read(fd
, "TEST", B_STRING_TYPE
, "Hello BeOS", 11);
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);
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",
80 fs_write_attr(fd
, "TESTraw", B_RAW_TYPE
, 0, "Haiku", 6);
81 test_read(fd
, "TESTraw", B_RAW_TYPE
, "Haiku", 6);
84 memset(buffer
, 0, sizeof(buffer
));
85 strcpy(buffer
, "Hello");
86 fs_write_attr(fd
, "TESTswitch", B_RAW_TYPE
, 0, buffer
,
88 test_read(fd
, "TESTswitch", B_RAW_TYPE
, buffer
, strlen(buffer
) + 1);
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);