~ecryptfs/ecryptfs/trunk

« back to all changes in this revision

Viewing changes to tests/kernel/xattr/test.c

  • Committer: Colin King
  • Date: 2013-11-22 17:14:36 UTC
  • Revision ID: colin.king@canonical.com-20131122171436-fu076wx2vy9owiel
* doc/ecryptfs-pkcs11-helper-doc.txt
  - fix spelling mistake, aquired  ==> acquired

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <errno.h>
27
27
#include <sys/xattr.h>
28
28
 
29
 
#define XATTR_METADATA_NAME     "user.ecryptfs"
30
 
 
31
29
static const char *names[]  = {
32
30
        "user.test1",
33
31
        "user.test2",
42
40
        NULL
43
41
};
44
42
 
45
 
static void usage(const char *name)
46
 
{
47
 
        fprintf(stderr, "Usage: %s [-x] file\n", name);
48
 
}
49
 
 
50
43
int main(int argc, char **argv)
51
44
{
52
45
        ssize_t len, names_len = 0;
53
 
        int i, rc, xattr_metadata = 0;
 
46
        int i, rc;
54
47
        char buffer[1024];
55
 
        char *file, *ptr = buffer;
 
48
        char *ptr = buffer;
56
49
 
57
 
        if (argc < 2 || argc > 3) {
58
 
                usage(argv[0]);
 
50
        if (argc != 2) {
 
51
                fprintf(stderr, "Usage: %s file\n", argv[0]);
59
52
                exit(EXIT_FAILURE);
60
53
        }
61
54
 
62
 
        file = argv[1];
63
 
 
64
 
        if (argc == 3) {
65
 
                if (strcmp(argv[1], "-x")) {
66
 
                        usage(argv[0]);
67
 
                        exit(EXIT_FAILURE);
68
 
                }
69
 
                file = argv[2];
70
 
 
71
 
                /*
72
 
                 * The XATTR_METADATA_NAME xattr is set. Account for it in
73
 
                 * future sanity checks.
74
 
                 */
75
 
                xattr_metadata = 1;
76
 
                names_len = 1 + strlen(XATTR_METADATA_NAME);
77
 
        }
78
 
 
79
55
        for (i = 0; names[i]; i++) {
80
 
                if (setxattr(file, names[i], values[i], strlen(values[i]), 0) < 0)
 
56
                if (setxattr(argv[1], names[i], values[i], strlen(values[i]), 0) < 0)
81
57
                        exit(EXIT_FAILURE);
82
58
                names_len += 1 + strlen(names[i]);
83
59
        }
85
61
        /*
86
62
         *  Sanity check that listxattr returns correct length
87
63
         */
88
 
        len = listxattr(file, NULL, 0);
89
 
        if (len != names_len || len > sizeof(buffer))
90
 
                exit(EXIT_FAILURE);
91
 
 
92
 
        memset(buffer, 0, sizeof(buffer));
93
 
 
94
 
        len = listxattr(file, buffer, sizeof(buffer));
 
64
        len = listxattr(argv[1], NULL, 0);
95
65
        if (len != names_len)
96
66
                exit(EXIT_FAILURE);
97
67
 
 
68
        len = listxattr(argv[1], buffer, sizeof(buffer));
 
69
        if (len < 0)
 
70
                exit(EXIT_FAILURE);
 
71
 
98
72
        /*  
99
73
         *  Check listxattr names match what has been just set
100
74
         */
101
 
        for (ptr = buffer; *ptr; ptr += strlen(ptr) + 1) {
102
 
                int matched = 0;
103
 
 
104
 
                if (xattr_metadata && !strcmp(XATTR_METADATA_NAME, ptr))
105
 
                        continue;
106
 
 
107
 
                for (i = 0; names[i]; i++) {
108
 
                        if (strcmp(names[i], ptr))
109
 
                                continue;
110
 
 
111
 
                        matched = 1;
112
 
                        break;
113
 
                }
114
 
 
115
 
                if (!matched)
 
75
        for (i = 0; names[i]; i++) {
 
76
                if (strcmp(names[i], ptr))
116
77
                        exit(EXIT_FAILURE);
 
78
                ptr += strlen(ptr) + 1;
117
79
        }
118
80
 
119
81
        /*
120
82
         *  Check contents of xattr
121
83
         */
122
84
        for (i = 0; names[i]; i++) {
123
 
                len = getxattr(file, names[i], buffer, sizeof(buffer));
 
85
                len = getxattr(argv[1], names[i], buffer, sizeof(buffer));
124
86
                if (len < 0)
125
87
                        exit(EXIT_FAILURE);
126
88
                buffer[len] = '\0';
133
95
         *  Remove xattr
134
96
         */
135
97
        for (i = 0; names[i]; i++) {
136
 
                rc = removexattr(file, names[i]);
 
98
                rc = removexattr(argv[1], names[i]);
137
99
                if (rc < 0)
138
100
                        exit(EXIT_FAILURE);
139
 
                names_len -= 1 + strlen(names[i]);
140
101
        }
141
102
 
142
103
        /*
143
 
         *  ..and the only xattrs that should be left are those that were
144
 
         *  already there when the test started
 
104
         *  ..and there should be no xattrs left
145
105
         */
146
 
        len = listxattr(file, NULL, 0);
147
 
        if (len != names_len)
 
106
        len = listxattr(argv[1], NULL, 0);
 
107
        if (len != 0)
148
108
                exit(EXIT_FAILURE);
149
109
                
150
110
        exit(EXIT_SUCCESS);