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
|
#include "../config.h"
#include <fuse.h>
#include <zip.h>
#include <assert.h>
#include <stdlib.h>
#include <string>
#include <stdexcept>
// Public Morozoff design pattern :)
#define private public
#include "fuseZipData.h"
#include "common.h"
// FUSE stub functions
struct fuse_context *fuse_get_context(void) {
return NULL;
}
// libzip stub structures
struct zip {};
struct zip_file {};
struct zip_source {};
// libzip stub functions
// only stubs
struct zip *zip_open(const char *, int, int *) {
assert(false);
return NULL;
}
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_name(struct zip *, zip_uint64_t, zip_flags_t) {
assert(false);
return NULL;
}
zip_int64_t zip_file_add(struct zip *, const char *, struct zip_source *, zip_flags_t) {
assert(false);
return 0;
}
zip_int64_t zip_dir_add(struct zip *, const char *, zip_flags_t) {
assert(false);
return 0;
}
int zip_close(struct zip *) {
assert(false);
return 0;
}
int zip_delete(struct zip *, zip_uint64_t) {
assert(false);
return 0;
}
int zip_fclose(struct zip_file *) {
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;
}
zip_int64_t zip_get_num_entries(struct zip *, zip_flags_t) {
assert(false);
return 0;
}
int zip_file_rename(struct zip *, 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;
}
void zip_source_free(struct zip_source *) {
assert(false);
}
struct zip_source *zip_source_function(struct zip *, zip_source_callback, void *) {
assert(false);
return NULL;
}
int zip_stat_index(struct zip *, zip_uint64_t, zip_flags_t, struct zip_stat *) {
assert(false);
return 0;
}
const char *zip_strerror(struct zip *) {
assert(false);
return NULL;
}
const char *zip_file_strerror(struct zip_file *) {
assert(false);
return NULL;
}
const char *zip_get_archive_comment(zip_t *, int *, zip_flags_t) {
assert(false);
return NULL;
}
int zip_set_archive_comment(zip_t *, const char *, zip_uint16_t) {
return 0;
}
const char *zip_file_get_comment(zip_t *, zip_uint64_t, zip_uint32_t *, zip_flags_t) {
assert(false);
return NULL;
}
int zip_file_set_comment(zip_t *, zip_uint64_t, const char *, zip_uint16_t, zip_flags_t) {
return 0;
}
void checkValidationException(const char *fname, const char *prefix) {
bool thrown = false;
try {
FuseZipData::validateFileName(fname);
}
catch (const std::runtime_error &e) {
thrown = true;
assert(strncmp(e.what(), prefix, strlen(prefix)) == 0);
}
assert(thrown);
}
void checkConvertException(const char *fname, const char *prefix, bool readonly, bool needPrefix) {
bool thrown = false;
try {
std::string converted;
FuseZipData::convertFileName(fname, readonly, needPrefix, converted);
}
catch (const std::runtime_error &e) {
thrown = true;
assert(strncmp(e.what(), prefix, strlen(prefix)) == 0);
}
assert(thrown);
}
void checkConversion(const char *fname, bool readonly, bool needPrefix, const char *expected) {
std::string res;
FuseZipData::convertFileName(fname, readonly, needPrefix, res);
assert(res == expected);
}
int main(int, char **) {
initTest();
// validator
FuseZipData::validateFileName("normal.name");
FuseZipData::validateFileName("path/to/normal.name");
FuseZipData::validateFileName(".hidden");
FuseZipData::validateFileName("path/to/.hidden");
FuseZipData::validateFileName("path/to/.hidden/dir");
FuseZipData::validateFileName("..superhidden");
FuseZipData::validateFileName("path/to/..superhidden");
FuseZipData::validateFileName("path/to/..superhidden/dir");
checkValidationException("", "empty file name");
checkValidationException("moo//moo", "bad file name (two slashes): ");
// converter
checkConversion("normal.name", true, false, "normal.name");
checkConversion("normal.name", true, true, "CUR/normal.name");
checkConversion("path/to/normal.name", true, false, "path/to/normal.name");
checkConversion("path/to/normal.name", true, true, "CUR/path/to/normal.name");
checkConvertException(".", "bad file name: ", true, false);
checkConvertException("./", "bad file name: ", true, false);
checkConvertException("abc/./cde", "bad file name: ", true, false);
checkConvertException("abc/.", "bad file name: ", true, false);
checkConversion(".hidden", false, false, ".hidden");
checkConversion("path/to/.hidden", false, false, "path/to/.hidden");
checkConversion("path/to/.hidden/dir", false, false, "path/to/.hidden/dir");
checkConvertException(".", "bad file name: .", false, true);
checkConvertException(".", "bad file name: .", true, true);
checkConvertException("/.", "bad file name: /.", true, true);
checkConvertException("./", "bad file name: ./", false, false);
checkConvertException("./", "bad file name: ./", true, false);
checkConvertException("..", "bad file name: ..", false, true);
checkConvertException("../", "paths relative to parent directory are not supported", false, true);
checkConversion("../", true, true, "UP/");
checkConversion("../../../", true, true, "UPUPUP/");
checkConvertException("/..", "bad file name: /..", true, true);
checkConvertException("/../blah", "bad file name: /../blah", true, true);
checkConversion("../abc", true, true, "UP/abc");
checkConversion("../../../abc", true, true, "UPUPUP/abc");
checkConvertException("abc/../cde", "bad file name: ", false, false);
checkConvertException("abc/../cde", "bad file name: ", true, true);
checkConvertException("abc/..", "bad file name: ", false, false);
checkConvertException("abc/..", "bad file name: ", true, true);
checkConvertException("../abc/..", "bad file name: ", true, true);
checkConvertException("/", "absolute paths are not supported in read-write mode", false, false);
checkConvertException("/rootname", "absolute paths are not supported in read-write mode", false, false);
checkConversion("/", true, true, "ROOT/");
checkConversion("/rootname", true, true, "ROOT/rootname");
checkConversion("/path/name", true, true, "ROOT/path/name");
return EXIT_SUCCESS;
}
|