int main(void) { SXE_HASH * hash; unsigned i; unsigned id; unsigned bucket; int counter[MAX_BUCKET_INDEX]; plan_tests(2); sxe_log_set_level(SXE_LOG_LEVEL_DEBUG); memset(counter, 0, MAX_BUCKET_INDEX * sizeof(int)); hash = sxe_hash_new("test-hash", HASH_SIZE); for (i = 0; i < HASH_SIZE; i++) { char in_buf[5]; char out_buf[41]; snprintf(in_buf, 5, "%d", i); sha1sum( in_buf, 4, out_buf); id = sxe_hash_set(hash, out_buf, SXE_HASH_SHA1_AS_HEX_LENGTH, 1U); bucket = sxe_pool_index_to_state(hash, id); SXEA11(bucket < MAX_BUCKET_INDEX, "Bucket index %u is out of range", bucket); counter[bucket]++; if (counter[bucket] > MAX_ALLOWED_PER_BUCKET_INDEX) { diag("Count at bucket index %u is greater than %u", counter[bucket], MAX_ALLOWED_PER_BUCKET_INDEX); break; } } is(i, HASH_SIZE, "%u items SHA1 hashed and no bucket has more that %u entries", i, MAX_ALLOWED_PER_BUCKET_INDEX); memset(counter, 0, MAX_BUCKET_INDEX * sizeof(int)); hash = sxe_hash_new_plus("lookup3", HASH_SIZE, sizeof(SXE_HASH), 0, 8, SXE_HASH_OPTION_LOOKUP3_HASH); for (i = 0; i < HASH_SIZE; i++) { id = sxe_hash_take(hash); snprintf((char *)&hash[id], 9, "%08x", i); sxe_hash_add(hash, id); bucket = sxe_pool_index_to_state(hash, id); SXEA11(bucket < MAX_BUCKET_INDEX, "Bucket index %u is out of range", bucket); counter[bucket]++; if (counter[bucket] > MAX_ALLOWED_PER_BUCKET_INDEX + 1) { diag("Count at bucket index %u is greater than %u", counter[bucket], MAX_ALLOWED_PER_BUCKET_INDEX + 1); break; } } is(i, HASH_SIZE, "%u items lookup3 hashed and no bucket has more that %u entries", i, MAX_ALLOWED_PER_BUCKET_INDEX + 1); return exit_status(); }
static void sxe_dirwatch_event(EV_P_ ev_io * io, int revents) { char buffer[8192]; int length; unsigned offset; struct inotify_event * event; SXE_UNUSED_PARAMETER(revents); SXEE62("(fd=%d, revents=%08x)", io->fd, revents); SXEA11((length = read(io->fd, buffer, sizeof(buffer))) >= 0, "sxe_dirwatch_event: Failed to read events from inotify: %s", strerror(errno)); for (offset = 0; offset < (unsigned)length;) { SXE_LIST_WALKER walker; SXE_DIRWATCH * dirwatch; int flags = 0; SXEA12(length - offset >= sizeof(struct inotify_event), "Odd sized chunk left in buffer %u (expected inotify event of %u bytes)", length - offset, sizeof(struct inotify_event)); event = (struct inotify_event *)&buffer[offset]; offset += sizeof(struct inotify_event); SXEA12(length - offset >= event->len, "Chunk left in buffer %u (expected length %u)", length - offset, event->len); offset += event->len; SXEL63("dirwatch_event: fd=%d flags=%08x file=%s", event->wd, event->mask, event->name); sxe_list_walker_construct(&walker, &sxe_dirwatch_list); while ((dirwatch = (SXE_DIRWATCH *)sxe_list_walker_step(&walker)) != NULL) { if (dirwatch->fd == event->wd) { break; } } SXEA11(dirwatch, "No watched directory found with fd %d", event->wd); flags |= (event->mask & IN_CREATE) ? SXE_DIRWATCH_CREATED : 0; flags |= (event->mask & IN_MOVED_TO) ? SXE_DIRWATCH_CREATED : 0; flags |= (event->mask & IN_MODIFY) ? SXE_DIRWATCH_MODIFIED : 0; flags |= (event->mask & IN_DELETE) ? SXE_DIRWATCH_DELETED : 0; flags |= (event->mask & IN_MOVED_FROM) ? SXE_DIRWATCH_DELETED : 0; dirwatch->notify(EV_A_ event->name, flags, dirwatch->user_data); } SXER60("return"); }
int main(int argc, char **argv) { char line[2056]; char decoded[2056]; int i; unsigned len; if (argc != 2) { printf("usage: %s -use_stdio\n", argv[0]); return 0; } while (fgets(line, sizeof(line), stdin) != NULL) { i = 0; while ((line[i] != ' ') && (line[i] != '\0') && (line[i] != '\n')) { i++; } line[i] = '\0'; //i++; SXEA11(sxe_dns_uri_decode(decoded, sizeof(decoded), &len, line, i) == SXE_RETURN_OK, "decode failed for %s", line); printf("%.*s\n", len, decoded); } return 0; }
const char * sxe_return_to_string(SXE_RETURN ret) { if (ret >= SXE_RETURN_INVALID_VALUE) { return "INVALID_VALUE"; } /* Convert to string. If any enumerand is missing, should get a compiler error (works with gcc). */ switch (ret) { SXE_RETURN_CASE(OK); SXE_RETURN_CASE(UNCATEGORIZED); SXE_RETURN_CASE(WARN_CACHE_DOUBLE_INITIALIZED); SXE_RETURN_CASE(WARN_WOULD_BLOCK); SXE_RETURN_CASE(ERROR_CACHE_UNINITIALIZED); SXE_RETURN_CASE(ERROR_ALLOC); SXE_RETURN_CASE(ERROR_INTERNAL); SXE_RETURN_CASE(ERROR_NO_CONNECTION); SXE_RETURN_CASE(ERROR_ALREADY_CONNECTED); SXE_RETURN_CASE(ERROR_INVALID_URI); SXE_RETURN_CASE(ERROR_ADDRESS_IN_USE); SXE_RETURN_CASE(WARN_ALREADY_CLOSED); SXE_RETURN_CASE(ERROR_INTERRUPTED); SXE_RETURN_CASE(ERROR_COMMAND_NOT_RUN); SXE_RETURN_CASE(INVALID_VALUE); /* COVERAGE EXCLUSION: Cannot hit this */ } /* Can't happen! */ SXEA11(1, "Invalid return value %u", ret); return "INVALID_VALUE"; /* COVERAGE EXCLUSION: Cannot hit this */ }
static SXE_TIME local_get_time(void) { struct timeval tv; SXEA11(gettimeofday(&tv, NULL) >= 0, "gettimeofday failed: %s", strerror(errno)); return SXE_TIME_FROM_TIMEVAL(&tv); }
void sxe_sync_ev_post(void * sync_point) { SXE_SYNC_EV * sync_ev = (SXE_SYNC_EV *)sync_point; SXEE81("sxe_sync_ev_post(sync_point=%p)", sync_point); SXEA11(sendto(sync_ev->sock, (MOCK_SOCKET_VOID *)&sync_point, sizeof(sync_point), 0, (struct sockaddr *)&sxe_sync_ev_addr, sizeof(sxe_sync_ev_addr)) == sizeof(sync_point), "Can't send to sync_point listener port: %s", sxe_socket_get_last_error_as_str()); SXER80("return"); }
static int sxe_sync_ev_socket(void) { int sock; SXEE80("sxe_sync_ev_socket()"); SXEA11((sock = socket(AF_INET, SOCK_DGRAM, 0)) != SXE_SOCKET_INVALID, "Error creating sync socket: %s", sxe_socket_get_last_error_as_str()); SXEA12(sxe_socket_set_nonblock(sock, 1) >= 0, "socket %d: couldn't set non-blocking flag: %s", sock, sxe_socket_get_last_error_as_str()); SXER81("return sock=%d", sock); return sock; }
/** * Initialize the dirwatch package * * @exceptions Aborts if already initialized or on failure to initialize inotify */ void sxe_dirwatch_init(void) { SXEE60("()"); if (sxe_dirwatch_inotify_fd != -1) { SXEL60("sxe_dirwatch_init: Already initialized"); goto SXE_EARLY_OUT; } SXEA11((sxe_dirwatch_inotify_fd = inotify_init()) != -1, "sxe_dirwatch_init: inotify_init() failed with %s", strerror(errno)); SXEL61("inotify_init() returned fd: %d", sxe_dirwatch_inotify_fd); SXE_LIST_CONSTRUCT(&sxe_dirwatch_list, 0, SXE_DIRWATCH, node); ev_io_init(&sxe_dirwatch_watcher, sxe_dirwatch_event, sxe_dirwatch_inotify_fd, EV_READ); SXE_EARLY_OUT: SXER60("return"); }
SXE_RETURN md5_to_hex(SOPHOS_MD5 * md5, char * md5_in_hex, unsigned md5_in_hex_length) { SXE_RETURN result = SXE_RETURN_OK; SXEE86("(md5=%08x%08x%08x%08x,md5_in_hex='%p',md5_in_hex_length='%u'", md5->word[3], md5->word[2], md5->word[1], md5->word[0], md5_in_hex, md5_in_hex_length); SXEA11(md5_in_hex_length == (MD5_IN_HEX_LENGTH + 1), "Incorrect length of char * for md5_to_hex(): '%u'", md5_in_hex_length); snprintf(md5_in_hex , 9, "%08x", htonl(md5->word[0])); snprintf(md5_in_hex + 8, 9, "%08x", htonl(md5->word[1])); snprintf(md5_in_hex + 16, 9, "%08x", htonl(md5->word[2])); snprintf(md5_in_hex + 24, 9, "%08x", htonl(md5->word[3])); SXEL62("md5_in_hex: '%.*s'", MD5_IN_HEX_LENGTH, md5_in_hex); SXER81("return %s", sxe_return_to_string(result)); return result; }
SXE_RETURN sha1_to_hex(SOPHOS_SHA1 * sha1, char * sha1_in_hex, unsigned sha1_in_hex_length) { SXE_RETURN result = SXE_RETURN_OK; SXEE87("sxe_sha1_to_hex(sha1=%08x%08x%08x%08x%08x,sha1_in_hex='%p',sha1_in_hex_length='%u'", sha1->word[4], sha1->word[3], sha1->word[2], sha1->word[1], sha1->word[0], sha1_in_hex, sha1_in_hex_length); SXEA11(sha1_in_hex_length == (SHA1_IN_HEX_LENGTH + 1), "Incorrect length of char * for sha1_to_hex(): '%u'", sha1_in_hex_length); snprintf(sha1_in_hex , 9, "%08x", htonl(sha1->word[0])); snprintf(sha1_in_hex + 8, 9, "%08x", htonl(sha1->word[1])); snprintf(sha1_in_hex + 16, 9, "%08x", htonl(sha1->word[2])); snprintf(sha1_in_hex + 24, 9, "%08x", htonl(sha1->word[3])); snprintf(sha1_in_hex + 32, 9, "%08x", htonl(sha1->word[4])); SXEL62("sha1_in_hex: '%.*s'", 40, sha1_in_hex); SXER81("return %s", sxe_return_to_string(result)); return result; }
static void sxe_sync_ev_read(EV_P_ ev_io * io, int revents) { SXE_SYNC_EV * handle; SXEE82("sxe_sync_ev_read(io=%p,revents=%d)", io, revents); #if EV_MULTIPLICITY SXE_UNUSED_ARGUMENT(loop); #endif SXE_UNUSED_ARGUMENT(io); SXE_UNUSED_ARGUMENT(revents); while(recvfrom(sxe_sync_ev_sock, &handle, sizeof(handle), 0, NULL, NULL) == sizeof(handle)) { (*sxe_sync_generic_event)(handle, handle->user_data); } SXEA11(sxe_socket_get_last_error() == SXE_SOCKET_ERROR(EWOULDBLOCK), "Unexpected error receiving from sync socket: %s", sxe_socket_get_last_error_as_str()); SXER80("return"); }
/** * Construct a pool state walker (AKA iterator) * * @param walker Pointer to the walker * @param array Pointer to the pool array * @param state State to walk * * @exception If the pool is both locked and timed, it cannot be walked safely */ void sxe_pool_walker_construct(SXE_POOL_WALKER * walker, void * array, unsigned state) { SXE_POOL_IMPL * pool = SXE_POOL_ARRAY_TO_IMPL(array); SXEE83("sxe_pool_walker_construct(walker=%p,pool=%s,state=%s)", walker, pool->name, (*pool->state_to_string)(state)); SXEA11(!((pool->options & SXE_POOL_OPTION_LOCKED) && (pool->options & SXE_POOL_OPTION_TIMED)), "sxe_pool_walker_construct: Can't walk thread safe timed pool %s safely", pool->name); sxe_list_walker_construct(&walker->list_walker, &SXE_POOL_QUEUE(pool)[state]); walker->pool = pool; walker->state = state; if (pool->options & SXE_POOL_OPTION_TIMED) { walker->last.time = 0.0; } else { walker->last.count = 0; } SXER80("return"); }
int main(void) { #ifdef WIN32 /* TODO: Implement sxe_dirwatch() on Windows */ #else char tempdir1[] = "tmp-XXXXXX"; char tempdir2[] = "tmp-XXXXXX"; char tempdir3[] = "tmp-XXXXXX"; char fname[PATH_MAX]; tap_ev ev; plan_tests(28); sxe_register(1, 0); sxe_init(); /* We need to make a new temporary directory, because *everything* appears * to change willy-nilly during the build. The current directory has a * file that always changes. The /tmp directory always seems to have * spurious changes in it. */ SXEA11(mkdtemp(tempdir1), "Failed to create tempdir: %s", strerror(errno)); SXEA11(mkdtemp(tempdir2), "Failed to create tempdir: %s", strerror(errno)); SXEA11(mkdtemp(tempdir3), "Failed to create tempdir: %s", strerror(errno)); sxe_dirwatch_init(10); sxe_dirwatch_add(tempdir1, SXE_DIRWATCH_CREATED|SXE_DIRWATCH_MODIFIED|SXE_DIRWATCH_DELETED, test_dirwatch_event, (void *)1); sxe_dirwatch_add(tempdir2, SXE_DIRWATCH_MODIFIED, test_dirwatch_event, (void *)2); sxe_dirwatch_add(tempdir3, SXE_DIRWATCH_DELETED, test_dirwatch_event, (void *)3); sxe_dirwatch_start(); /* tempdir1: create, modify, delete */ { char rname[PATH_MAX]; int fd; snprintf(rname, sizeof rname, "%s/renamed", tempdir1); snprintf(fname, sizeof fname, "%s/created", tempdir1); fd = open(fname, O_CREAT|O_RDWR, S_IRWXU); write(fd, "Hello", 5); rename(fname, rname); unlink(rname); is_eq(test_tap_ev_identifier_wait(TEST_WAIT, &ev), "test_dirwatch_event", "Got a dirwatch event"); is_eq(tap_ev_arg(ev, "chfile"), "created", "Got filename %s", fname); is(tap_ev_arg(ev, "chflags"), SXE_DIRWATCH_CREATED, "Got flags=SXE_DIRWATCH_CREATED"); is(tap_ev_arg(ev, "user_data"), 1, "Got user_data=1 (%s)", tempdir1); is_eq(test_tap_ev_identifier_wait(TEST_WAIT, &ev), "test_dirwatch_event", "Got a dirwatch event"); is_eq(tap_ev_arg(ev, "chfile"), "created", "Got filename %s", fname); is(tap_ev_arg(ev, "chflags"), SXE_DIRWATCH_MODIFIED, "Got flags=SXE_DIRWATCH_MODIFIED"); is(tap_ev_arg(ev, "user_data"), 1, "Got user_data=1 (%s)", tempdir1); is_eq(test_tap_ev_identifier_wait(TEST_WAIT, &ev), "test_dirwatch_event", "Got a dirwatch event"); is_eq(tap_ev_arg(ev, "chfile"), "created", "Got filename %s", fname); is(tap_ev_arg(ev, "chflags"), SXE_DIRWATCH_DELETED, "Got flags=SXE_DIRWATCH_DELETED"); is(tap_ev_arg(ev, "user_data"), 1, "Got user_data=1 (%s)", tempdir1); is_eq(test_tap_ev_identifier_wait(TEST_WAIT, &ev), "test_dirwatch_event", "Got a dirwatch event"); is_eq(tap_ev_arg(ev, "chfile"), "renamed", "Got filename %s", rname); is(tap_ev_arg(ev, "chflags"), SXE_DIRWATCH_CREATED, "Got flags=SXE_DIRWATCH_CREATED"); is(tap_ev_arg(ev, "user_data"), 1, "Got user_data=1 (%s)", tempdir1); is_eq(test_tap_ev_identifier_wait(TEST_WAIT, &ev), "test_dirwatch_event", "Got a dirwatch event"); is_eq(tap_ev_arg(ev, "chfile"), "renamed", "Got filename %s", rname); is(tap_ev_arg(ev, "chflags"), SXE_DIRWATCH_DELETED, "Got flags=SXE_DIRWATCH_DELETED"); is(tap_ev_arg(ev, "user_data"), 1, "Got user_data=1 (%s)", tempdir1); } /* tempdir2: modified */ { const char *rel; int fd; snprintf(fname, sizeof fname, "%s/file.XXXXXX", tempdir2); SXEA11((rel = strchr(fname, '/')), "Didn't find '/' in %s", fname); rel++; fd = mkstemp(fname); write(fd, "Hello", 5); unlink(fname); is_eq(test_tap_ev_identifier_wait(TEST_WAIT, &ev), "test_dirwatch_event", "Got a dirwatch event"); is_eq(tap_ev_arg(ev, "chfile"), rel, "Got filename %s", fname); is(tap_ev_arg(ev, "chflags"), SXE_DIRWATCH_MODIFIED, "Got flags=SXE_DIRWATCH_MODIFIED"); is(tap_ev_arg(ev, "user_data"), 2, "Got user_data=2 (%s)", tempdir2); } /* tempdir3: deleted */ { const char *rel; int fd; snprintf(fname, sizeof fname, "%s/file.XXXXXX", tempdir3); SXEA11((rel = strchr(fname, '/')), "Didn't find '/' in %s", fname); rel++; fd = mkstemp(fname); unlink(fname); is_eq(test_tap_ev_identifier_wait(TEST_WAIT, &ev), "test_dirwatch_event", "Got a dirwatch event"); is_eq(tap_ev_arg(ev, "chfile"), rel, "Got filename %s", fname); is(tap_ev_arg(ev, "chflags"), SXE_DIRWATCH_DELETED, "Got flags=SXE_DIRWATCH_DELETED"); is(tap_ev_arg(ev, "user_data"), 3, "Got user_data=3 (%s)", tempdir3); } rmdir(tempdir1); rmdir(tempdir2); rmdir(tempdir3); sxe_dirwatch_stop(); #endif return exit_status(); }
int main(int argc, char ** argv) { #ifdef WINDOWS_NT SXEL10("WARNING: Need to implement sxe_spawn() on Windows to run this test file!"); #else int fd; double start_time; unsigned count; unsigned id; unsigned * pool; unsigned * shared; size_t size; SXE_MMAP memmap; SXE_RETURN result; SXE_SPAWN spawn[TEST_CLIENT_INSTANCES]; if (argc > 1) { count = atoi(argv[1]); sxe_mmap_open(&memmap, "memmap"); shared = (unsigned *)(unsigned long)SXE_MMAP_ADDR(&memmap); pool = sxe_pool_from_base(shared); SXEL63("Instance %u mapped to shared pool // base=%p, pool=%p", count, shared, pool); do { usleep(10000 * count); id = sxe_pool_set_oldest_element_state_locked(pool, TEST_STATE_FREE, TEST_STATE_CLIENT_TAKE); SXEA10(id != SXE_POOL_LOCK_NEVER_TAKEN, "Got SXE_POOL_LOCK_NEVER_TAKEN");; } while (id == SXE_POOL_NO_INDEX); SXEL62("Instance %u got pool element %u", count, id); pool[id] = count; sxe_pool_set_indexed_element_state_locked(pool, id, TEST_STATE_CLIENT_TAKE, TEST_STATE_CLIENT_DONE); sxe_mmap_close(&memmap); SXEL61("Instance %u exiting", count); return 0; } plan_tests(5); ok((size = sxe_pool_size(TEST_CLIENT_INSTANCES/2, sizeof(*pool), TEST_STATE_NUMBER_OF_STATES)) >= TEST_CLIENT_INSTANCES * sizeof(*pool), "Expect pool size %u to be at least the size of the array %u", size, TEST_CLIENT_INSTANCES * sizeof(*pool)); SXEA11((fd = open("memmap", O_CREAT | O_TRUNC | O_WRONLY, 0666)) >= 0, "Failed to create file 'memmap': %s", strerror(errno)); SXEA12(ftruncate(fd, size) >= 0, "Failed to extend the file to %lu bytes: %s", size, strerror(errno)); close(fd); sxe_mmap_open(&memmap, "memmap"); shared = (unsigned *)(unsigned long)SXE_MMAP_ADDR(&memmap); pool = sxe_pool_construct(shared, "shared-pool", TEST_CLIENT_INSTANCES/2, sizeof(*pool), TEST_STATE_NUMBER_OF_STATES, SXE_POOL_LOCKS_ENABLED); sxe_register(TEST_CLIENT_INSTANCES + 1, 0); SXEA11((result = sxe_init()) == SXE_RETURN_OK, "Failed to initialize the SXE package: %s", sxe_return_to_string(result)); for (count = 1; count <= TEST_CLIENT_INSTANCES; count++) { char buffer[12]; snprintf(buffer, sizeof(buffer), "%u", count); result = sxe_spawn(NULL, &spawn[count - 1], argv[0], buffer, NULL, NULL, NULL, NULL); SXEA13(result == SXE_RETURN_OK, "Failed to spawn '%s %s': %s", argv[0], buffer, sxe_return_to_string(result)); } start_time = sxe_get_time_in_seconds(); for (count = 0; (count < TEST_CLIENT_INSTANCES); ) { SXEA10((TEST_WAIT + start_time ) > sxe_get_time_in_seconds(), "Unexpected timeout... is the hardware too slow?"); usleep(10000); id = sxe_pool_set_oldest_element_state_locked(pool, TEST_STATE_CLIENT_DONE, TEST_STATE_FREE); /* Assert here in the test. The actual service would take specific action here */ SXEA12(id != SXE_POOL_LOCK_NEVER_TAKEN, "Parent: Failed to acqure lock .. yield limit reached. id %u vs %u", id, SXE_POOL_LOCK_NEVER_TAKEN); if (id != SXE_POOL_NO_INDEX) { SXEL62("Looks like instance %u got element %u", pool[id], id); count++; } } ok(count == TEST_CLIENT_INSTANCES, "All clients got an element in the pool"); start_time = sxe_get_time_in_seconds(); for (count = 0; (count < TEST_CLIENT_INSTANCES); count++) { SXEA10((TEST_WAIT + start_time ) > sxe_get_time_in_seconds(), "Unexpected timeout... is the hardware too slow?"); waitpid(spawn[count].pid, NULL, 0); } ok(SXE_POOL_LOCK_NEVER_TAKEN != sxe_pool_lock(pool), "Forced lock to be always locked!"); id = sxe_pool_set_oldest_element_state_locked(pool, TEST_STATE_FREE, TEST_STATE_FREE); ok(id == SXE_POOL_LOCK_NEVER_TAKEN, "sxe_pool_set_oldest_element_state_locked() Failed to acquire lock"); id = sxe_pool_set_indexed_element_state_locked(pool, 0, TEST_STATE_FREE, TEST_STATE_FREE); ok(id == SXE_POOL_LOCK_NEVER_TAKEN, "sxe_pool_set_indexed_element_state_locked() Failed to acquire lock"); sxe_pool_unlock(pool); sxe_pool_override_locked(pool); /* for coverage */ sxe_mmap_close(&memmap); return exit_status(); #endif }