X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Ftest_support.c;h=5c566dcb25db164e5fc9a1d42e550df017e6d6fe;hb=605df71262af15c229fa3b5f0f6f7611abc011d1;hp=2789920d6193a61e793679c8758489fdf5bc9505;hpb=39c6e5ead74b63de79cdee7833b301351c8598f4;p=wimlib diff --git a/src/test_support.c b/src/test_support.c index 2789920d..5c566dcb 100644 --- a/src/test_support.c +++ b/src/test_support.c @@ -3,7 +3,7 @@ */ /* - * Copyright (C) 2015-2016 Eric Biggers + * Copyright (C) 2015-2018 Eric Biggers * * This file is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free @@ -51,6 +51,7 @@ #include "wimlib/security_descriptor.h" #include "wimlib/test_support.h" #include "wimlib/unix_data.h" +#include "wimlib/xattr.h" /*----------------------------------------------------------------------------* * File tree generation * @@ -466,6 +467,87 @@ set_random_unix_metadata(struct wim_inode *inode) return 0; } +static noinline_for_stack int +set_random_xattrs(struct wim_inode *inode) +{ + int num_xattrs = 1 + rand32() % 16; + char entries[8192]; + struct wim_xattr_entry *entry = (void *)entries; + size_t entries_size; + struct wimlib_unix_data unix_data; +#ifdef __WIN32__ + const char *prefix = ""; +#else + const char *prefix = "user."; +#endif + static const char capability_name[] = "security.capability"; + bool generated_capability_xattr = false; + + /* + * On Linux, xattrs in the "user" namespace are only permitted on + * regular files and directories. For other types of files we can use + * the "trusted" namespace, but this requires root. + */ + if (inode_is_symlink(inode) || + (inode_get_unix_data(inode, &unix_data) && + !S_ISREG(unix_data.mode) && !S_ISDIR(unix_data.mode))) + { + if (!am_root()) + return 0; + prefix = "trusted."; + } + + for (int i = 0; i < num_xattrs; i++) { + int value_len = rand32() % 64; + u8 *p; + + #ifdef __WIN32__ + if (value_len == 0) + value_len++; + #endif + + entry->value_len = cpu_to_le16(value_len); + entry->flags = 0; + + if (rand32() % 16 == 0 && am_root() && + !generated_capability_xattr) { + int name_len = sizeof(capability_name) - 1; + entry->name_len = name_len; + p = mempcpy(entry->name, capability_name, name_len + 1); + generated_capability_xattr = true; + } else { + int name_len = 1 + rand32() % 64; + + entry->name_len = strlen(prefix) + name_len; + p = mempcpy(entry->name, prefix, strlen(prefix)); + *p++ = 'A' + i; + for (int j = 1; j < name_len; j++) { + do { + #ifdef __WIN32__ + *p = 'A' + rand8() % 26; + #else + *p = rand8(); + #endif + } while (*p == '\0'); + p++; + } + *p++ = '\0'; + } + for (int j = 0; j < value_len; j++) + *p++ = rand8(); + + entry = (void *)p; + } + + entries_size = (char *)entry - entries; + wimlib_assert(entries_size > 0 && entries_size <= sizeof(entries)); + + if (!inode_set_xattrs(inode, entries, entries_size)) + return WIMLIB_ERR_NOMEM; + + return 0; +} + static int set_random_metadata(struct wim_inode *inode, struct generation_context *ctx) { @@ -517,6 +599,13 @@ set_random_metadata(struct wim_inode *inode, struct generation_context *ctx) return ret; } + /* Extended attributes */ + if (rand32() % 32 == 0) { + int ret = set_random_xattrs(inode); + if (ret) + return ret; + } + return 0; } @@ -603,36 +692,6 @@ generate_data(u8 *buffer, size_t size, struct generation_context *ctx) } } -static int -add_stream(struct wim_inode *inode, struct generation_context *ctx, - int stream_type, const utf16lechar *stream_name, - void *buffer, size_t size) -{ - struct blob_descriptor *blob = NULL; - struct wim_inode_stream *strm; - - if (size) { - blob = new_blob_descriptor(); - if (!blob) - goto err_nomem; - blob->attached_buffer = buffer; - blob->blob_location = BLOB_IN_ATTACHED_BUFFER; - blob->size = size; - } - - strm = inode_add_stream(inode, stream_type, stream_name, blob); - if (unlikely(!strm)) - goto err_nomem; - - prepare_unhashed_blob(blob, inode, strm->stream_id, - ctx->params->unhashed_blobs); - return 0; - -err_nomem: - free_blob_descriptor(blob); - return WIMLIB_ERR_NOMEM; -} - static noinline_for_stack int set_random_reparse_point(struct wim_inode *inode, struct generation_context *ctx) { @@ -694,6 +753,7 @@ add_random_data_stream(struct wim_inode *inode, struct generation_context *ctx, { void *buffer = NULL; size_t size; + int ret; size = select_stream_size(ctx); if (size) { @@ -703,8 +763,12 @@ add_random_data_stream(struct wim_inode *inode, struct generation_context *ctx, generate_data(buffer, size, ctx); } - return add_stream(inode, ctx, STREAM_TYPE_DATA, stream_name, - buffer, size); + ret = 0; + if (!inode_add_stream_with_data(inode, STREAM_TYPE_DATA, stream_name, + buffer, size, ctx->params->blob_table)) + ret = WIMLIB_ERR_NOMEM; + FREE(buffer); + return ret; } static int @@ -1311,6 +1375,157 @@ cmp_unix_metadata(const struct wim_inode *inode1, return 0; } +static int +cmp_xattr_names(const void *p1, const void *p2) +{ + const struct wim_xattr_entry *entry1 = *(const struct wim_xattr_entry **)p1; + const struct wim_xattr_entry *entry2 = *(const struct wim_xattr_entry **)p2; + int res; + + res = entry1->name_len - entry2->name_len; + if (res) + return res; + + return memcmp(entry1->name, entry2->name, entry1->name_len); +} + +/* Validate and sort by name a list of extended attributes */ +static int +parse_xattrs(const void *xattrs, u32 len, + const struct wim_xattr_entry *entries[], + u32 *num_entries_p) +{ + u32 limit = *num_entries_p; + u32 num_entries = 0; + const struct wim_xattr_entry *entry = xattrs; + + while ((void *)entry < xattrs + len) { + if (!valid_xattr_entry(entry, xattrs + len - (void *)entry)) { + ERROR("Invalid xattr entry"); + return WIMLIB_ERR_INVALID_XATTR; + } + if (num_entries >= limit) { + ERROR("Too many xattr entries"); + return WIMLIB_ERR_INVALID_XATTR; + } + entries[num_entries++] = entry; + entry = xattr_entry_next(entry); + } + + if (num_entries == 0) { + ERROR("No xattr entries"); + return WIMLIB_ERR_INVALID_XATTR; + } + + qsort(entries, num_entries, sizeof(entries[0]), cmp_xattr_names); + + for (u32 i = 1; i < num_entries; i++) { + if (cmp_xattr_names(&entries[i - 1], &entries[i]) == 0) { + ERROR("Duplicate xattr names"); + return WIMLIB_ERR_INVALID_XATTR; + } + } + + *num_entries_p = num_entries; + return 0; +} + +static int +cmp_xattrs(const struct wim_inode *inode1, const struct wim_inode *inode2, + int cmp_flags) +{ + const void *xattrs1, *xattrs2; + u32 len1, len2; + + xattrs1 = inode_get_xattrs(inode1, &len1); + xattrs2 = inode_get_xattrs(inode2, &len2); + + if (!xattrs1 && !xattrs2) { + return 0; + } else if (xattrs1 && !xattrs2) { + if (cmp_flags & WIMLIB_CMP_FLAG_NTFS_3G_MODE) + return 0; + ERROR("%"TS" unexpectedly lost its xattrs", + inode_any_full_path(inode1)); + return WIMLIB_ERR_IMAGES_ARE_DIFFERENT; + } else if (!xattrs1 && xattrs2) { + ERROR("%"TS" unexpectedly gained xattrs", + inode_any_full_path(inode1)); + return WIMLIB_ERR_IMAGES_ARE_DIFFERENT; + } else { + const int max_entries = 64; + const struct wim_xattr_entry *entries1[max_entries]; + const struct wim_xattr_entry *entries2[max_entries]; + u32 xattr_count1 = max_entries; + u32 xattr_count2 = max_entries; + int ret; + + ret = parse_xattrs(xattrs1, len1, entries1, &xattr_count1); + if (ret) { + ERROR("%"TS": invalid xattrs", + inode_any_full_path(inode1)); + return ret; + } + ret = parse_xattrs(xattrs2, len2, entries2, &xattr_count2); + if (ret) { + ERROR("%"TS": invalid xattrs", + inode_any_full_path(inode2)); + return ret; + } + if (xattr_count1 != xattr_count2) { + ERROR("%"TS": number of xattrs changed. had %u " + "before, now has %u", inode_any_full_path(inode1), + xattr_count1, xattr_count2); + } + for (u32 i = 0; i < xattr_count1; i++) { + const struct wim_xattr_entry *entry1 = entries1[i]; + const struct wim_xattr_entry *entry2 = entries2[i]; + + if (entry1->value_len != entry2->value_len || + entry1->name_len != entry2->name_len || + entry1->flags != entry2->flags || + memcmp(entry1->name, entry2->name, + entry1->name_len) || + memcmp(entry1->name + entry1->name_len + 1, + entry2->name + entry2->name_len + 1, + le16_to_cpu(entry1->value_len))) + { + ERROR("xattr %.*s of %"TS" differs", + entry1->name_len, entry1->name, + inode_any_full_path(inode1)); + return WIMLIB_ERR_IMAGES_ARE_DIFFERENT; + } + } + return 0; + } +} + +static int +cmp_timestamps(const struct wim_inode *inode1, const struct wim_inode *inode2, + int cmp_flags) +{ + if (inode1->i_creation_time != inode2->i_creation_time && + !(cmp_flags & WIMLIB_CMP_FLAG_UNIX_MODE)) { + ERROR("Creation time of %"TS" differs", + inode_any_full_path(inode1)); + return WIMLIB_ERR_IMAGES_ARE_DIFFERENT; + } + + if (inode1->i_last_write_time != inode2->i_last_write_time) { + ERROR("Last write time of %"TS" differs", + inode_any_full_path(inode1)); + return WIMLIB_ERR_IMAGES_ARE_DIFFERENT; + } + + if (inode1->i_last_access_time != inode2->i_last_access_time) { + ERROR("Last access time of %"TS" differs", + inode_any_full_path(inode1)); + return WIMLIB_ERR_IMAGES_ARE_DIFFERENT; + } + + return 0; +} + static int cmp_inodes(const struct wim_inode *inode1, const struct wim_inode *inode2, const struct wim_image_metadata *imd1, @@ -1391,11 +1606,21 @@ cmp_inodes(const struct wim_inode *inode1, const struct wim_inode *inode2, if (ret) return ret; + /* Compare timestamps */ + ret = cmp_timestamps(inode1, inode2, cmp_flags); + if (ret) + return ret; + /* Compare standard UNIX metadata */ ret = cmp_unix_metadata(inode1, inode2, cmp_flags); if (ret) return ret; + /* Compare extended attributes */ + ret = cmp_xattrs(inode1, inode2, cmp_flags); + if (ret) + return ret; + return 0; }