void test_single(int udp_sock) { udp_data senddata; senddata.request_id = htons(45); senddata.seq_num = htons(0); senddata.packets = htons(1); senddata.reserved = htons(0); strcpy(senddata.body, "get foo\r\n"); int len = 8 + strlen(senddata.body); ok_test(send(udp_sock, &senddata, len, 0) == len, "sent request", "failed to send request"); fd_set rfds; struct timeval tv; /* Watch stdin (fd 0) to see when it has input. */ FD_ZERO(&rfds); FD_SET(udp_sock, &rfds); /* Wait up to two seconds. */ tv.tv_sec = 2; tv.tv_usec = 0; ok_test(select(udp_sock + 1, &rfds, NULL, NULL, &tv) > 0, "got readability", "did not get readability"); udp_data recvdata; int recvlen = recv(udp_sock, &recvdata, sizeof(recvdata), 0); recvdata.request_id = ntohs(recvdata.request_id); recvdata.seq_num = ntohs(recvdata.seq_num); recvdata.packets = ntohs(recvdata.packets); recvdata.reserved = ntohs(recvdata.reserved); ok_test((recvdata.request_id == 45) && (recvdata.seq_num == 0) && (recvdata.packets == 1) && (recvdata.reserved == 0), "header is correct" ,"header is not correct"); ok_test(recvlen == 36, "received length is 36", "received length is not 36"); ok_test(!memcmp(recvdata.body, "VALUE foo 0 6\r\nfooval\r\nEND\r\n", 28), "payload is as expected", "payload is not as expected"); }
/* Test the btowc() function for a few locales with known character sets. */ int main (int argc, char** argv) { int result = 0; current_locale = setlocale (LC_ALL, "en_US.ANSI_X3.4-1968"); if (current_locale == NULL) { puts ("cannot set locale \"en_US.ANSI_X3.4-1968\""); result = 1; } else { int c; for (c = 0; c < 128; ++c) result |= ok_test (c, c); for (c = 128; c < 256; ++c) result |= fail_test (c); result |= eof_test (); } current_locale = setlocale (LC_ALL, "de_DE.ISO-8859-1"); if (current_locale == NULL) { puts ("cannot set locale \"de_DE.ISO-8859-1\""); result = 1; } else { int c; for (c = 0; c < 256; ++c) result |= ok_test (c, c); result |= eof_test (); } current_locale = setlocale (LC_ALL, "de_DE.UTF-8"); if (current_locale == NULL) { puts ("cannot set locale \"de_DE.UTF-8\""); result = 1; } else { int c; for (c = 0; c < 128; ++c) result |= ok_test (c, c); for (c = 128; c < 256; ++c) result |= fail_test (c); result |= eof_test (); } current_locale = setlocale (LC_ALL, "hr_HR.ISO-8859-2"); if (current_locale == NULL) { puts ("cannot set locale \"hr_HR.ISO-8859-2\""); result = 1; } else { static const wint_t upper_half[] = { 0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7, 0x00A8, 0x0160, 0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B, 0x00B0, 0x0105, 0x02DB, 0x0142, 0x00B4, 0x013E, 0x015B, 0x02C7, 0x00B8, 0x0161, 0x015F, 0x0165, 0x017A, 0x02DD, 0x017E, 0x017C, 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7, 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E, 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F, 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9 }; int c; for (c = 0; c < 161; ++c) result |= ok_test (c, c); for (c = 161; c < 256; ++c) result |= ok_test (c, upper_half[c - 161]); result |= eof_test (); } if (result == 0) puts ("all OK"); return result; }
int main(int argc, char **argv) { test_init(argc, argv); // start the server struct memcached_process_handle* mchandle = new_memcached(0, ""); if (!mchandle) { fprintf(stderr,"Could not start memcached process\n\n"); exit(0); } struct Memcache* memcache = libmemc_create(Automatic); if (libmemc_add_server(memcache, "127.0.0.1", mchandle->port) == -1) { fprintf(stderr,"Could not add server\n\n"); exit(0); } struct Item item = {0}; setItem(&item, 0, "num", 3, 0, "1", 1, 0); ok_test(!libmemc_set(memcache, &item), "stored num", "failed to store num"); mem_get_is(memcache, &item, "num == '1'", "num != '1'"); ok_test(!libmemc_incr(memcache, &item, 1) && !memcmp(item.data, "2", item.size), "+ 1 = 2", "+ 1 != 2"); mem_get_is(memcache, &item, "num == '2'", "num != '2'"); ok_test(!libmemc_incr(memcache, &item, 8) && !memcmp(item.data, "10", item.size), "+ 8 = 10", "+ 8 != 10"); mem_get_is(memcache, &item, "num == '10'", "num != '10'"); ok_test(!libmemc_decr(memcache, &item, 1) && !memcmp(item.data, "9", item.size), "- 1 = 9", "- 1 != 9"); ok_test(!libmemc_decr(memcache, &item, 9) && !memcmp(item.data, "0", item.size), "- 9 = 0", "- 9 != 0"); ok_test(!libmemc_decr(memcache, &item, 5) && !memcmp(item.data, "0", item.size), "- 5 = 0", "- 5 != 0"); setItem(&item, 0, "num", 3, 0, "4294967296", 10, 0); ok_test(!libmemc_set(memcache, &item), "stored 2**32", "failed to store 2**32"); ok_test(!libmemc_incr(memcache, &item, 1) && !memcmp(item.data, "4294967297", item.size), "4294967296 + 1 = 4294967297", "4294967296 + 1 != 4294967297"); setItem(&item, 0, "num", 3, 0, "18446744073709551615", 20, 0); ok_test(!libmemc_set(memcache, &item), "stored 2**64-1", "failed to store 2**64-1"); ok_test(!libmemc_incr(memcache, &item, 1) && !memcmp(item.data, "0", item.size), "(2**64 - 1) + 1 = 0", "(2**64 - 1) + 1 != 0"); if (libmemc_get_protocol(memcache) == Textual) { setItem(&item, 0, "bogus1", 6, 0, "0", 1, 0); ok_test((libmemc_decr(memcache, &item, 5) == -1), "can't decr bogus key", "can decr bogus key"); setItem(&item, 0, "bogus2", 6, 0, "0", 1, 0); ok_test((libmemc_incr(memcache, &item, 5) == -1), "can't incr bogus key", "can incr bogus key"); } else { setItem(&item, 0, "bogus1", 6, 0, "4", 1, 0xffffffff); ok_test((libmemc_decr(memcache, &item, 5) == -1), "can't decr bogus key when expiry == 0xffffffff", "can decr bogus key when expiry == 0xffffffff"); setItem(&item, 0, "bogus2", 6, 0, "4", 1, 0xffffffff); ok_test((libmemc_incr(memcache, &item, 5) == -1), "can't incr bogus key when expiry == 0xffffffff", "can incr bogus key when expiry == 0xffffffff"); setItem(&item, 0, "bogus1", 6, 0, "4", 1, 0); ok_test(!libmemc_decr(memcache, &item, 5), "can decr bogus key when expiry != 0xffffffff", "can't decr bogus key when expiry != 0xffffffff"); setItem(&item, 0, "bogus2", 6, 0, "4", 1, 0); ok_test(!libmemc_incr(memcache, &item, 5), "can incr bogus key when expiry != 0xffffffff", "can't incr bogus key when expiry != 0xffffffff"); } setItem(&item, 0, "text", 4, 0, "hi", 2, 0); ok_test(!libmemc_set(memcache, &item), "stored text", "failed to store text"); ok_test(!libmemc_incr(memcache, &item, 1) && !memcmp(item.data, "1", item.size), "hi + 1 = 1", "hi + 1 != 1"); setItem(&item, 0, "text", 4, 0, "hi", 2, 0); ok_test(!libmemc_set(memcache, &item), "stored text", "failed to store text"); ok_test(!libmemc_decr(memcache, &item, 1) && !memcmp(item.data, "0", item.size), "hi - 1 = 0", "hi - 1 != 0"); libmemc_destroy(memcache); test_report(); }
int main(int argc, char **argv) { test_init(argc, argv); struct memcached_process_handle* mchandle = new_memcached(0, ""); // start the server if (!mchandle) { fprintf(stderr,"Could not start memcached process\n\n"); exit(0); } struct Memcache* memcache = libmemc_create(Automatic); if (libmemc_add_server(memcache, "127.0.0.1", mchandle->port) == -1) { fprintf(stderr,"Could not add server\n\n"); exit(0); } int usock = new_udp_sock(mchandle); char ok_str[50]; char notok_str[50]; if (libmemc_get_protocol(memcache) == Textual) { // set foo struct Item item = {0}; setItem(&item, 0, "foo", 3, 0, "fooval", 6, 0); ok_test(!libmemc_set(memcache, &item), "stored foo", "failed to store foo"); mem_get_is(memcache, &item, "foo == fooval", "foo != fooval"); test_single(usock); int offset[3] = {1, 1, 2}; for (int i=0; i<3; i++) { udp_data *res = send_udp_request(usock, 160 + offset[i], "get foo\r\n"); ok_test(res != NULL, "got result", "did not get result"); ok_test(res[0].packets == 1, "one packet", "not one packet"); ok_test(res[0].seq_num == 0, "only got seq number 0", "did not only get seq number 0"); ok_test(!memcmp(res[0].body, "VALUE foo 0 6\r\nfooval\r\nEND\r\n", 28), "payload is as expected", "payload is not as expected"); sprintf(ok_str, "request id in response %d is correct", res[0].request_id); sprintf(notok_str, "request id in response %d is not correct", res[0].request_id); ok_test(res[0].request_id == 160 + offset[i], ok_str, notok_str); if (res) free(res); } // testing non-existent stuff udp_data *res = send_udp_request(usock, 404, "get notexist\r\n"); ok_test(res != NULL, "got result", "did not get result"); ok_test(res[0].packets == 1, "one packet", "not one packet"); ok_test(res[0].seq_num == 0, "only got seq number 0", "did not only get seq number 0"); sprintf(ok_str, "request id %d correct", res[0].request_id); sprintf(notok_str, "request id %d not correct", res[0].request_id); ok_test(res[0].request_id == 404, ok_str, notok_str); ok_test(!memcmp(res[0].body, "END\r\n", 5), "payload is as expected", "payload is not as expected"); if (res) free(res); int size = 4096; // 256 kB char *big = malloc(size); for (int i=0; i<(size/4); i++) { memcpy(big + i*4, "abcd", 4); } setItem(&item, 0, "big", 3, 0, big, size, 0); ok_test(!libmemc_set(memcache, &item), "stored big", "failed to store big"); mem_get_is(memcache, &item, "big value matches", "big value does not match"); res = send_udp_request(usock, 999, "get big\r\n"); ok_test(res[0].packets == 3, "three packet response", "not three packet response"); ok_test(!memcmp(res[0].body, "VALUE big 0 4096\r\n", 18), "first packet has value line", "first packet is missing value line"); ok_test(strstr(res[2].body, "END\r\n") != NULL, "last packet has end", "last packet is missing end"); ok_test(res[1].request_id == 999, "request id of middle packet is correct", "request id of middle packet is incorrect"); if (res) free(res); } else { fprintf(stdout, "Tests not implemented for binary protocol\n"); } libmemc_destroy(memcache); test_report(); }
int main(int argc, char **argv) { test_init(argc, argv); // start the server struct memcached_process_handle* mchandle = new_memcached(0, ""); if (!mchandle) { fprintf(stderr,"Could not start memcached process\n\n"); exit(0); } struct Memcache* memcache = libmemc_create(Automatic); if (libmemc_add_server(memcache, "127.0.0.1", mchandle->port) == -1) { fprintf(stderr,"Could not add server\n\n"); exit(0); } // wait for early second struct timeval tv; while (1) { gettimeofday(&tv, NULL); if (tv.tv_usec < 500000) break; usleep(100000); // 1/10th of a second sleeps until time changes. } struct Item item = {0}; // We choose expiry time = 2 secs instead of 1 sec. // 1 sec is too small for these tests since memcached updates current_time once a second // which means that an item will frequently expire in much less than a second if exptime=1 setItem(&item, 0, "foo", 3, 0, "fooval", 6, 2); ok_test(!libmemc_set(memcache, &item), "stored foo", "failed to store foo"); mem_get_is(memcache, &item, "foo == 'fooval'", "foo != 'fooval'"); usleep(2500000); // sleep 2.5 secs struct Item item2 = {0}; setItem(&item2, 0, "foo", 3, 0, NULL, 0, 0); mem_get_is(memcache, &item2, "foo == <undef>", "foo != <undef>"); gettimeofday(&tv, NULL); item.exptime = tv.tv_sec - 1; ok_test(!libmemc_set(memcache, &item), "stored foo", "failed to store foo"); mem_get_is(memcache, &item2, "already expired", "not expired"); struct Item item3 = {0}; setItem(&item3, 0, "foo", 3, 0, "foov+1", 6, 2); ok_test(!libmemc_set(memcache, &item3), "stored foo", "failed to store foo"); mem_get_is(memcache, &item3, "foo == 'foov+1'", "foo != 'foov+1'"); usleep(2500000); // sleep 2.5 secs struct Item item4 = {0}; setItem(&item4, 0, "foo", 3, 0, NULL, 0, 0); mem_get_is(memcache, &item4, "now expired", "not expired"); gettimeofday(&tv, NULL); struct Item item5 = {0}; setItem(&item5, 0, "boo", 3, 0, "booval", 6, tv.tv_sec - 20); ok_test(!libmemc_set(memcache, &item3), "stored boo", "failed to store boo"); struct Item item6 = {0}; setItem(&item6, 0, "boo", 3, 0, NULL, 0, 0); mem_get_is(memcache, &item6, "now expired", "not expired"); libmemc_destroy(memcache); test_report(); }