File: fuseInterfaceTest.cpp

package info (click to toggle)
fuse-zip 0.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 9,776 kB
  • sloc: cpp: 5,509; sh: 2,587; makefile: 193
file content (260 lines) | stat: -rw-r--r-- 5,589 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "../config.h"

#include <zip.h>
#include <fuse.h>

#include <sys/stat.h>
#include <sys/sysmacros.h>

#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <memory>

// Public Morozoff design pattern :)
#define private public
#define protected public

#include "fuse-zip.h"
#include "fileNode.h"
#include "fuseZipData.h"
#include "common.h"

using namespace std;

// libzip stubs

struct zip {
};
struct zip_file {
};
struct zip_source {
};

zip *zip_open(const char *, int, int *) {
    return new zip;
}

int zip_close(zip_t *zip) {
    delete zip;
    return 0;
}

int zip_delete(zip_t *, zip_uint64_t) {
    assert(false);
    return 0;
}

int zip_stat_index(struct zip *, zip_uint64_t, zip_flags_t, struct zip_stat *) {
    assert(false);
    return 0;
}

struct zip_file *zip_fopen_index(struct zip *, zip_uint64_t, zip_flags_t) {
    assert(false);
    return NULL;
}

zip_int64_t zip_fread(struct zip_file *, void *, zip_uint64_t) {
    assert(false);
    return 0;
}

int zip_fclose(struct zip_file *) {
    assert(false);
    return 0;
}

zip_int64_t zip_file_add(struct zip *, const char *, struct zip_source *, zip_flags_t) {
    return 0;
}

zip_int64_t zip_dir_add(zip_t *, const char *, zip_flags_t) {
    assert(false);
    return 0;
}

int zip_file_rename(zip_t *, zip_uint64_t, const char *, zip_flags_t) {
    assert(false);
    return 0;
}

int zip_file_replace(struct zip *, zip_uint64_t, struct zip_source *, zip_flags_t) {
    assert(false);
    return 0;
}

struct zip_source *zip_source_function(struct zip *, zip_source_callback, void *) {
    assert(false);
    return NULL;
}

void zip_source_free(struct zip_source *) {
    assert(false);
}

const char *zip_get_name(struct zip *, zip_uint64_t, zip_flags_t) {
    assert(false);
    return NULL;
}

const char *zip_file_strerror(struct zip_file *) {
    assert(false);
    return NULL;
}

const char *zip_strerror(struct zip *) {
    assert(false);
    return NULL;
}

zip_int64_t zip_get_num_entries(zip_t *, zip_flags_t) {
    return 0;
}

void zip_error_init_with_code(zip_error_t *, int) {
    assert(false);
}

const char *zip_error_strerror(zip_error_t *) {
    assert(false);
    return NULL;
}

void zip_error_fini(zip_error_t *) {
    assert(false);
}

const char *zip_get_archive_comment(zip_t *, int *, zip_flags_t) {
    return NULL;
}

int zip_set_archive_comment(zip_t *, const char *, zip_uint16_t) {
    assert(false);
    return 0;
}

const char *zip_file_get_comment(zip_t *, zip_uint64_t, zip_uint32_t *, zip_flags_t) {
    return NULL;
}

int zip_file_set_comment(zip_t *, zip_uint64_t, const char *, zip_uint16_t, zip_flags_t) {
    assert(false);
    return 0;
}

// FUSE stubs

fuse_context context;

struct fuse_context *fuse_get_context(void) {
    return &context;
}

// tests

/**
 * Test fisezip_create()
 */
void test_create () {
    FuseZipData *data = initFuseZip("testprogram", "test.zip", false, false);
    context.private_data = data;
    fuse_conn_info conn;
    void *initResult = fusezip_init(&conn);
    assert(initResult == data);

    fuse_file_info fi;

    // empty path
    assert(fusezip_create("", 0, &fi) == -EACCES);
    // create new
    assert(fusezip_create("/test", 0, &fi) == 0);
    assert(reinterpret_cast<void*>(fi.fh) != NULL);
    assert(std::string("test") == reinterpret_cast<FileNode*>(fi.fh)->name);
    // create twice
    assert(fusezip_create("/test", 0, &fi) == -EEXIST);

    // unlink created file
    assert(fusezip_unlink("/test") == 0);

    fusezip_destroy(data);
}

/**
 * Test fisezip_mknod()
 */
void test_mknod () {
    FuseZipData *data = initFuseZip("testprogram", "test.zip", false, false);
    context.private_data = data;
    fuse_conn_info conn;
    void *initResult = fusezip_init(&conn);
    assert(initResult == data);

    // empty path
    assert(fusezip_mknod("", 0, 0) == -EACCES);
    {
        // character device
        dev_t dev = makedev(4, 63);
        assert(fusezip_mknod("/char", S_IFCHR, dev) == 0);
        FileNode *node = data->find("char");
        assert(node != NULL);
        assert(S_ISCHR(node->mode()));
        assert(node->device() == dev);

        assert(fusezip_unlink("/char") == 0);
    }
    {
        // block device
        dev_t dev = makedev(8, 255);
        assert(fusezip_mknod("/block", S_IFBLK, dev) == 0);
        FileNode *node = data->find("block");
        assert(node != NULL);
        assert(S_ISBLK(node->mode()));
        assert(node->device() == dev);

        assert(fusezip_unlink("/block") == 0);
    }
    {
        // FIFO
        assert(fusezip_mknod("/fifo", S_IFIFO, 0) == 0);
        FileNode *node = data->find("fifo");
        assert(node != NULL);
        assert(S_ISFIFO(node->mode()));
        assert(node->size() == 0);

        assert(fusezip_unlink("/fifo") == 0);
    }
    {
        // socket
        assert(fusezip_mknod("/socket", S_IFSOCK, 0) == 0);
        FileNode *node = data->find("socket");
        assert(node != NULL);
        assert(S_ISSOCK(node->mode()));

        assert(fusezip_unlink("/socket") == 0);
    }

    {
        // create new
        assert(fusezip_mknod("/test", 0, 0) == 0);
        FileNode *node = data->find("test");
        assert(node != NULL);
        assert(std::string("test") == node->name);
        // create twice
        assert(fusezip_mknod("/test", 0, 0) == -EEXIST);

        // unlink created file
        assert(fusezip_unlink("/test") == 0);
    }

    fusezip_destroy(data);
}

int main(int, char **) {
    test_create();
    test_mknod();

    return EXIT_SUCCESS;
}