headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / system / libroot / os / fs_attr_test.cpp
blobe94441a5d1b72cc9533ad45f14debd2eae4220d2
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 */
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
11 #include <fs_attr.h>
12 #include <TypeConstants.h>
15 const char* kTestFileName = "/tmp/fs_attr_test";
18 void
19 test_read(int fd, const char* attribute, type_code type, const char* data,
20 size_t length)
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);
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);
35 if (info.type != type) {
36 fprintf(stderr, "Type does not match B_RAW_TYPE!\n");
37 exit(1);
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);
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);
57 int
58 main(int argc, char** argv)
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;
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",
78 22);
80 fs_write_attr(fd, "TESTraw", B_RAW_TYPE, 0, "Haiku", 6);
81 test_read(fd, "TESTraw", B_RAW_TYPE, "Haiku", 6);
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);
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);
94 return 0;