static void test_crypto_curve25519_persist(void *arg) { curve25519_keypair_t keypair, keypair2; char *fname = tor_strdup(get_fname("curve25519_keypair")); char *tag = NULL; char *content = NULL; const char *cp; struct stat st; size_t taglen; (void)arg; tt_int_op(0,==,curve25519_keypair_generate(&keypair, 0)); tt_int_op(0,==,curve25519_keypair_write_to_file(&keypair, fname, "testing")); tt_int_op(0,==,curve25519_keypair_read_from_file(&keypair2, &tag, fname)); tt_str_op(tag,==,"testing"); tor_free(tag); test_memeq(keypair.pubkey.public_key, keypair2.pubkey.public_key, CURVE25519_PUBKEY_LEN); test_memeq(keypair.seckey.secret_key, keypair2.seckey.secret_key, CURVE25519_SECKEY_LEN); content = read_file_to_str(fname, RFTS_BIN, &st); tt_assert(content); taglen = strlen("== c25519v1: testing =="); tt_int_op(st.st_size, ==, 32+CURVE25519_PUBKEY_LEN+CURVE25519_SECKEY_LEN); tt_assert(fast_memeq(content, "== c25519v1: testing ==", taglen)); tt_assert(tor_mem_is_zero(content+taglen, 32-taglen)); cp = content + 32; test_memeq(keypair.seckey.secret_key, cp, CURVE25519_SECKEY_LEN); cp += CURVE25519_SECKEY_LEN; test_memeq(keypair.pubkey.public_key, cp, CURVE25519_SECKEY_LEN); tor_free(fname); fname = tor_strdup(get_fname("bogus_keypair")); tt_int_op(-1, ==, curve25519_keypair_read_from_file(&keypair2, &tag, fname)); tor_free(tag); content[69] ^= 0xff; tt_int_op(0, ==, write_bytes_to_file(fname, content, (size_t)st.st_size, 1)); tt_int_op(-1, ==, curve25519_keypair_read_from_file(&keypair2, &tag, fname)); done: tor_free(fname); tor_free(content); tor_free(tag); }
int do_for_each_file(char *name, int (*call_back)(char *, int), int param) { char buf[256]; int c; struct ffblk dta; errno = 0; findfirst(name, &dta, FA_RDONLY | FA_ARCH); if (errno!=0) { for (c=0; name[c]; c++) if ((name[c]=='*') || (name[c]=='?')) goto dont_load; errno=0; (*call_back)(name,param); dont_load: return errno; } do { strcpy(buf,name); strcpy(get_fname(buf),dta.ff_name); (*call_back)(buf,param); if (errno!=0) break; } while (findnext(&dta)==0); errno=0; return errno; }
int do_for_each_directory(char *name, int (*call_back)(char *, int), int param) { char buf[256]; struct ffblk dta; errno = 0; findfirst(name, &dta, FA_DIREC); if (errno!=0) return errno; do { if ((dta.ff_attrib & FA_DIREC) && (strcmp(dta.ff_name, ".") != 0)) { strcpy(buf,name); strcpy(get_fname(buf),dta.ff_name); strcat(buf,"\\"); (*call_back)(buf,param); if (errno!=0) break; } } while (findnext(&dta)==0); errno=0; return errno; }
static void test_config_write_to_data_subdir(void *arg) { or_options_t* options = get_options_mutable(); char *datadir; char *cp = NULL; const char* subdir = "test_stats"; const char* fname = "test_file"; const char* str = "Lorem ipsum dolor sit amet, consetetur sadipscing\n" "elitr, sed diam nonumy eirmod\n" "tempor invidunt ut labore et dolore magna aliquyam\n" "erat, sed diam voluptua.\n" "At vero eos et accusam et justo duo dolores et ea\n" "rebum. Stet clita kasd gubergren,\n" "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n" "Lorem ipsum dolor sit amet,\n" "consetetur sadipscing elitr, sed diam nonumy eirmod\n" "tempor invidunt ut labore et dolore\n" "magna aliquyam erat, sed diam voluptua. At vero eos et\n" "accusam et justo duo dolores et\n" "ea rebum. Stet clita kasd gubergren, no sea takimata\n" "sanctus est Lorem ipsum dolor sit amet."; char* filepath = NULL; (void)arg; tor_free(options->DataDirectory); datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1")); filepath = get_datadir_fname2(subdir, fname); #if defined (_WIN32) tt_int_op(mkdir(options->DataDirectory), ==, 0); #else tt_int_op(mkdir(options->DataDirectory, 0700), ==, 0); #endif // Write attempt shoudl fail, if subdirectory doesn't exist. test_assert(write_to_data_subdir(subdir, fname, str, NULL)); test_assert(! check_or_create_data_subdir(subdir)); // Content of file after write attempt should be // equal to the original string. test_assert(!write_to_data_subdir(subdir, fname, str, NULL)); cp = read_file_to_str(filepath, 0, NULL); test_streq(cp, str); tor_free(cp); // A second write operation should overwrite the old content. test_assert(!write_to_data_subdir(subdir, fname, str, NULL)); cp = read_file_to_str(filepath, 0, NULL); test_streq(cp, str); tor_free(cp); done: (void) unlink(filepath); rmdir(options->DataDirectory); tor_free(datadir); tor_free(filepath); tor_free(cp); }
static void do_findtrash (void) { char *from; char *uri_as_string; MateVFSResult result; MateVFSURI *from_uri; MateVFSURI *result_vfs_uri; from = get_fname (); from_uri = mate_vfs_uri_new (from); result = mate_vfs_find_directory (from_uri, MATE_VFS_DIRECTORY_KIND_TRASH, &result_vfs_uri, TRUE, TRUE, 0777); if (result != MATE_VFS_OK) { fprintf (stdout, "couldn't find or create trash there, error code %d", result); } else { uri_as_string = mate_vfs_uri_to_string (result_vfs_uri, MATE_VFS_URI_HIDE_NONE); fprintf (stdout, "trash found or created here: %s", uri_as_string); g_free (uri_as_string); } mate_vfs_uri_unref (from_uri); mate_vfs_uri_unref (result_vfs_uri); }
static void do_dump (void) { char *from; MateVFSHandle *from_handle; MateVFSResult result; guint32 offset; from = get_fname (); result = mate_vfs_open (&from_handle, from, MATE_VFS_OPEN_READ); if (show_if_error (result, "open ", from)) return; for (offset = 0; 1; ) { MateVFSFileSize bytes_read; guint8 data [1024]; result = mate_vfs_read (from_handle, data, 1024, &bytes_read); if (show_if_error (result, "read ", from)) return; if (bytes_read == 0) break; ms_ole_dump (data, bytes_read, offset); offset += bytes_read; } result = mate_vfs_close (from_handle); if (show_if_error (result, "close ", from)) return; }
void test_getfne( void ) { STRPTR name[ NUM_NAME ] = { "c:/hugo/sucks.txt", "//silly/file.tar.gz", "test:dir/no_ext", "test:no_ext", "no_ext", "test:emptyext." }; STRPTR res; int i; for ( i=0; i < NUM_NAME; i++ ) { res = get_fext( name[i] ); printf( "get_fext (\"%-25s\") -> \"%s\"\n", name[i], res ); printf( "get_fname(\"%-25s\") -> \"%s\"\n", name[i], get_fname( name[i] ) ); printf( "\n" ); } }
static void test_md_cache_broken(void *data) { or_options_t *options; char *fn=NULL; microdesc_cache_t *mc = NULL; (void)data; options = get_options_mutable(); tt_assert(options); tor_free(options->CacheDirectory); options->CacheDirectory = tor_strdup(get_fname("md_datadir_test2")); #ifdef _WIN32 tt_int_op(0, OP_EQ, mkdir(options->CacheDirectory)); #else tt_int_op(0, OP_EQ, mkdir(options->CacheDirectory, 0700)); #endif tor_asprintf(&fn, "%s"PATH_SEPARATOR"cached-microdescs", options->CacheDirectory); write_str_to_file(fn, truncated_md, 1); mc = get_microdesc_cache(); tt_assert(mc); done: if (options) tor_free(options->CacheDirectory); tor_free(fn); microdesc_free_all(); }
int mix_sfl::save() { int* d = new int[2 + ts_fl.size() + ra2_fl.size()]; int* w = d; write(w, ts_fl); write(w, ra2_fl); int error = file32_write(get_fname(), d, w - d << 2); delete[] d; return error; }
void check_fval(int fd, mkv_t *kv) { uint16_t crc = calc_crc16(kv->v.data, kv->v.len); if (kv->vcrc != crc) { char fp[256]; get_fname(fd, fp, sizeof(fp)); FATAL("%s corrupt!!", fp); ASSERT(kv->vcrc == crc); } }
static void test_ext_or_init_auth(void *arg) { or_options_t *options = get_options_mutable(); const char *fn; char *cp = NULL; struct stat st; char cookie0[32]; (void)arg; /* Check default filename location */ tor_free(options->DataDirectory); options->DataDirectory = tor_strdup("foo"); cp = get_ext_or_auth_cookie_file_name(); tt_str_op(cp, OP_EQ, "foo"PATH_SEPARATOR"extended_orport_auth_cookie"); tor_free(cp); /* Shouldn't be initialized already, or our tests will be a bit * meaningless */ ext_or_auth_cookie = tor_malloc_zero(32); tt_assert(tor_mem_is_zero((char*)ext_or_auth_cookie, 32)); /* Now make sure we use a temporary file */ fn = get_fname("ext_cookie_file"); options->ExtORPortCookieAuthFile = tor_strdup(fn); cp = get_ext_or_auth_cookie_file_name(); tt_str_op(cp, OP_EQ, fn); tor_free(cp); /* Test the initialization function with a broken write_bytes_to_file(). See if the problem is handled properly. */ MOCK(write_bytes_to_file, write_bytes_to_file_fail); tt_int_op(-1, OP_EQ, init_ext_or_cookie_authentication(1)); tt_int_op(ext_or_auth_cookie_is_set, OP_EQ, 0); UNMOCK(write_bytes_to_file); /* Now do the actual initialization. */ tt_int_op(0, OP_EQ, init_ext_or_cookie_authentication(1)); tt_int_op(ext_or_auth_cookie_is_set, OP_EQ, 1); cp = read_file_to_str(fn, RFTS_BIN, &st); tt_ptr_op(cp, OP_NE, NULL); tt_u64_op((uint64_t)st.st_size, OP_EQ, 64); tt_mem_op(cp,OP_EQ, "! Extended ORPort Auth Cookie !\x0a", 32); tt_mem_op(cp+32,OP_EQ, ext_or_auth_cookie, 32); memcpy(cookie0, ext_or_auth_cookie, 32); tt_assert(!tor_mem_is_zero((char*)ext_or_auth_cookie, 32)); /* Operation should be idempotent. */ tt_int_op(0, OP_EQ, init_ext_or_cookie_authentication(1)); tt_mem_op(cookie0,OP_EQ, ext_or_auth_cookie, 32); done: tor_free(cp); ext_orport_free_all(); }
static void do_mv (void) { char *from, *to; char *msg; MateVFSResult result; from = get_fname (); to = get_fname (); if (!from || !to) { fprintf (vfserr, "mv <from> <to>\n"); return; } result = mate_vfs_move (from, to, FALSE); msg = g_strdup_printf ("%s to %s", from, to); show_if_error (result, "move ", msg); g_free (msg); }
static void do_mkdir (void) { char *fname; MateVFSResult result; fname = get_fname (); result = mate_vfs_make_directory (fname, MATE_VFS_PERM_USER_ALL); if (show_if_error (result, "mkdir ", fname)) return; }
int mix_sfl::load() { Ccc_file f(true); if (f.open(get_fname())) return 1; if (f.get_size() < 8) return 1; const int* r = reinterpret_cast<const int*>(f.data()); read(r, ts_fl); read(r, ra2_fl); return 0; }
static void do_rmdir (void) { char *fname; MateVFSResult result; fname = get_fname (); result = mate_vfs_remove_directory (fname); if (show_if_error (result, "rmdir ", fname)) return; }
static void do_rm (void) { char *fname; MateVFSResult result; fname = get_fname (); result = mate_vfs_unlink (fname); if (show_if_error (result, "unlink ", fname)) return; }
static void test_config_check_or_create_data_subdir(void *arg) { or_options_t *options = get_options_mutable(); char *datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0")); const char *subdir = "test_stats"; char *subpath = get_datadir_fname(subdir); struct stat st; int r; #if !defined (_WIN32) || defined (WINCE) unsigned group_permission; #endif (void)arg; #if defined (_WIN32) && !defined (WINCE) tt_int_op(mkdir(options->DataDirectory), ==, 0); #else tt_int_op(mkdir(options->DataDirectory, 0700), ==, 0); #endif r = stat(subpath, &st); // The subdirectory shouldn't exist yet, // but should be created by the call to check_or_create_data_subdir. test_assert(r && (errno == ENOENT)); test_assert(!check_or_create_data_subdir(subdir)); test_assert(is_private_dir(subpath)); // The check should return 0, if the directory already exists // and is private to the user. test_assert(!check_or_create_data_subdir(subdir)); #if !defined (_WIN32) || defined (WINCE) group_permission = st.st_mode | 0070; r = chmod(subpath, group_permission); if (r) { test_fail_msg("Changing permissions for the subdirectory failed."); } // If the directory exists, but its mode is too permissive // a call to check_or_create_data_subdir should reset the mode. test_assert(!is_private_dir(subpath)); test_assert(!check_or_create_data_subdir(subdir)); test_assert(is_private_dir(subpath)); #endif done: rmdir(subpath); tor_free(datadir); tor_free(subpath); }
void send_content(char *buf, int sock, char** argv) { char file_str[1024*1024]; char file_name[1024] = {0}; char file_path[1024] = {0}; char content_type[1024] = {0}; strcpy(file_path, argv[2]); get_fname(file_name, buf); // add index.html by default if (file_name[1] == 0) { strcpy(file_name, "/index.html"); } printf("%s\n", file_name); strcat(file_path, file_name); // get content type char *type = strrchr(file_name, '.'); if (strcmp(type, ".gif") == 0) { strcpy(content_type, "Content-Type: image/gif"); } else if (strcmp(type, ".html") == 0) { strcpy(content_type, "Content-Type: text/html"); } else if (strcmp(type, ".png") == 0) { strcpy(content_type, "Content-Type: image/png"); } else if (strcmp(type, ".jpg") == 0) { strcpy(content_type, "Content-Type: image/jpg"); } else if (strcmp(type, ".jpeg") == 0) { strcpy(content_type, "Content-Type: image/jpg"); } else if (strcmp(type, ".pdf") == 0) { strcpy(content_type, "Content-Type: application/pdf"); } // send message int file_len = read_file(file_path, file_str); if (file_len == -1) { // 404 error char *message = "HTTP/1.1 404\r\n\r\n<HTML><body>404 not found</body></head></HTML>\r\n\r\n"; send(sock, message, strlen(message), 0); printf("sent 404 error\n"); return; } int header_len = strlen("HTTP/1.1 200 OK\r\n\r\n\r\n") + strlen(content_type); int message_len = file_len + header_len; char message[1024*1024]; sprintf(message, "HTTP/1.1 200 OK\r\n%s\r\n\r\n", content_type); memcpy(message + header_len, file_str, file_len); send(sock, message, message_len, 0); }
static void test_sigsafe_err(void *arg) { const char *fn=get_fname("sigsafe_err_log"); char *content=NULL; log_severity_list_t include_bug; smartlist_t *lines = smartlist_new(); (void)arg; set_log_severity_config(LOG_WARN, LOG_ERR, &include_bug); init_logging(1); mark_logs_temp(); add_file_log(&include_bug, fn, 0); tor_log_update_sigsafe_err_fds(); close_temp_logs(); close(STDERR_FILENO); log_err(LD_BUG, "Say, this isn't too cool."); tor_log_err_sigsafe("Minimal.\n", NULL); set_log_time_granularity(100*1000); tor_log_err_sigsafe("Testing any ", "attempt to manually log ", "from a signal.\n", NULL); mark_logs_temp(); close_temp_logs(); close(STDERR_FILENO); content = read_file_to_str(fn, 0, NULL); tt_assert(content != NULL); tor_split_lines(lines, content, (int)strlen(content)); tt_int_op(smartlist_len(lines), OP_GE, 5); if (strstr(smartlist_get(lines, 0), "opening new log file")) smartlist_del_keeporder(lines, 0); tt_assert(strstr(smartlist_get(lines, 0), "Say, this isn't too cool")); /* Next line is blank. */ tt_assert(!strcmpstart(smartlist_get(lines, 1), "==============")); tt_assert(!strcmpstart(smartlist_get(lines, 2), "Minimal.")); /* Next line is blank. */ tt_assert(!strcmpstart(smartlist_get(lines, 3), "==============")); tt_str_op(smartlist_get(lines, 4), OP_EQ, "Testing any attempt to manually log from a signal."); done: tor_free(content); smartlist_free(lines); }
bool setup_next_file_send(file_transfer *ft) { TCHAR *file; struct _stati64 statbuf; for (;;) { file = ft->pfts.ptszFiles[ft->cf]; if (file == NULL) return false; if (_tstati64(file, &statbuf) == 0 && (statbuf.st_mode & _S_IFDIR) == 0) break; ++ft->cf; } ft->pfts.tszCurrentFile = file; ft->pfts.currentFileSize = statbuf.st_size; ft->pfts.currentFileTime = statbuf.st_mtime; ft->pfts.currentFileProgress = 0; char* fnamea; T2Utf fname(file); if (ft->pfts.totalFiles > 1 && ft->file[0]) { size_t dlen = mir_strlen(ft->file); if (strncmp(fname, ft->file, dlen) == 0 && fname[dlen] == '\\') { fnamea = &fname[dlen + 1]; for (char *p = fnamea; *p; ++p) if (*p == '\\') *p = 1; } else fnamea = get_fname(fname); } else fnamea = get_fname(fname); send_init_oft2(ft, fnamea); return true; }
unsigned long ModQueryModuleHandle(const char *pszModname, unsigned long *phmod) { struct module_rec *prev; char *mname; mname = get_fname(pszModname); if (getrec(mname, &prev)) { LOG("module not found"); *phmod = 0; return 123; /* ERROR_INVALID_NAME */ } *phmod = prev->module_struct; return 0; /* NO_ERROR */
int select_file(char *prompt, char *fname) { int ret; static char p[256] = ""; filebox.h = screen_h - 10; filebox.height = screen_h - 14; filebox.w = MAX(screen_w - 16, 32); filebox.slen = filebox.w - 8; draw_box(prompt, 7, 2, filebox.w, 5, TRUE); draw_listbox(&filebox); hi_vid(); goto1(7,6); pch(LJOIN_CHAR); goto1(filebox.w+6,6); pch(RJOIN_CHAR); if (!fname[0]) { if (p[0]) strcpy(fname, p); else getcwd(fname, 256); cleanup_filename(fname); append_backslash(fname); } fill_filelist(fname); goto1(12, 4); ret = do_input_text(fname, 255, 54, is_filechar, fsel_proc, TRUE, fsel_mouse); if (ret != ESC) { strcpy(p, fname); *get_fname(p) = 0; } n_vid(); empty_filelist(); dirty_everything(); return ret; }
static void do_info (void) { char *from; MateVFSResult result; MateVFSFileInfo *info; from = get_fname (); info = mate_vfs_file_info_new (); result = mate_vfs_get_file_info ( from, info, MATE_VFS_FILE_INFO_GET_MIME_TYPE); if (show_if_error (result, "getting info on: ", from)) return; print_info (info); mate_vfs_file_info_unref (info); }
static void do_open (void) { char *from, *handle; MateVFSHandle *from_handle; MateVFSResult result; handle = get_handle (); from = get_fname (); if (!handle || !from) { fprintf (vfserr, "open <handle> <filename>\n"); return; } result = mate_vfs_open (&from_handle, from, MATE_VFS_OPEN_READ); if (show_if_error (result, "open ", from)) return; register_file (handle, from_handle); }
static void do_cat (void) { char *from; MateVFSHandle *from_handle; MateVFSResult result; from = get_fname (); result = mate_vfs_open (&from_handle, from, MATE_VFS_OPEN_READ); if (show_if_error (result, "open ", from)) return; while (1) { MateVFSFileSize bytes_read; guint8 data [1025]; result = mate_vfs_read (from_handle, data, 1024, &bytes_read); if (show_if_error (result, "read ", from)) return; if (bytes_read == 0) break; if (bytes_read > 0 && bytes_read <= 1024) data [bytes_read] = '\0'; else { data [1024] = '\0'; g_warning ("Wierd error from vfs_read"); } fprintf (stdout, "%s", data); } result = mate_vfs_close (from_handle); if (show_if_error (result, "close ", from)) return; fprintf (stdout, "\n"); }
int search_directory(const char *filepath, const struct stat *info, const int typeflag, struct FTW *pathinfo) { struct tm mtime; time_t mtime_sec; time_t curtime_sec; unsigned long diff_time; char *fname; int flen; fname = (char *)malloc(MAX_FILENAME); localtime_r(&(info->st_mtime), &mtime); mtime_sec = mktime(&mtime); curtime_sec = time(NULL); diff_time = curtime_sec - mtime_sec; if(typeflag == FTW_F) { if(diff_time < MAX_DIFF_TM) { flen = get_fname(filepath,fname); if(!strcmp(gs_watch_fname, fname)) { log_prt(gs_logname, "Error Message: %04d-%02d-%02d %02d:%02d:%02d %s is detected!", mtime.tm_year+1900, mtime.tm_mon+1, mtime.tm_mday, mtime.tm_hour, mtime.tm_min, mtime.tm_sec,filepath); gi_detect_cnt++; } } } else if(typeflag == FTW_D || FTW_DP) { //no action for directory } return 0; }
static void do_create (void) { char *from, *handle; MateVFSHandle *from_handle; MateVFSResult result; handle = get_handle (); from = get_fname (); if (!handle || !from) { fprintf (vfserr, "create <handle> <filename>\n"); return; } result = mate_vfs_create (&from_handle, from, MATE_VFS_OPEN_READ, FALSE, MATE_VFS_PERM_USER_READ | MATE_VFS_PERM_USER_WRITE); if (show_if_error (result, "create ", from)) return; register_file (handle, from_handle); }
static void test_routerkeys_write_fingerprint(void *arg) { crypto_pk_t *key = pk_generate(2); or_options_t *options = get_options_mutable(); const char *ddir = get_fname("write_fingerprint"); char *cp = NULL, *cp2 = NULL; char fp[FINGERPRINT_LEN+1]; (void)arg; tt_assert(key); options->ORPort_set = 1; /* So that we can get the server ID key */ tor_free(options->DataDirectory); options->DataDirectory = tor_strdup(ddir); options->Nickname = tor_strdup("haflinger"); set_server_identity_key(key); set_client_identity_key(crypto_pk_dup_key(key)); tt_int_op(0, OP_EQ, check_private_dir(ddir, CPD_CREATE, NULL)); tt_int_op(crypto_pk_cmp_keys(get_server_identity_key(),key),OP_EQ,0); /* Write fingerprint file */ tt_int_op(0, OP_EQ, router_write_fingerprint(0)); cp = read_file_to_str(get_fname("write_fingerprint/fingerprint"), 0, NULL); crypto_pk_get_fingerprint(key, fp, 0); tor_asprintf(&cp2, "haflinger %s\n", fp); tt_str_op(cp, OP_EQ, cp2); tor_free(cp); tor_free(cp2); /* Write hashed-fingerprint file */ tt_int_op(0, OP_EQ, router_write_fingerprint(1)); cp = read_file_to_str(get_fname("write_fingerprint/hashed-fingerprint"), 0, NULL); crypto_pk_get_hashed_fingerprint(key, fp); tor_asprintf(&cp2, "haflinger %s\n", fp); tt_str_op(cp, OP_EQ, cp2); tor_free(cp); tor_free(cp2); /* Replace outdated file */ write_str_to_file(get_fname("write_fingerprint/hashed-fingerprint"), "junk goes here", 0); tt_int_op(0, OP_EQ, router_write_fingerprint(1)); cp = read_file_to_str(get_fname("write_fingerprint/hashed-fingerprint"), 0, NULL); crypto_pk_get_hashed_fingerprint(key, fp); tor_asprintf(&cp2, "haflinger %s\n", fp); tt_str_op(cp, OP_EQ, cp2); tor_free(cp); tor_free(cp2); done: crypto_pk_free(key); set_client_identity_key(NULL); tor_free(cp); tor_free(cp2); }
/** Run unit tests for our public key crypto functions */ static void test_crypto_pk(void) { crypto_pk_t *pk1 = NULL, *pk2 = NULL; char *encoded = NULL; char data1[1024], data2[1024], data3[1024]; size_t size; int i, j, p, len; /* Public-key ciphers */ pk1 = pk_generate(0); pk2 = crypto_pk_new(); test_assert(pk1 && pk2); test_assert(! crypto_pk_write_public_key_to_string(pk1, &encoded, &size)); test_assert(! crypto_pk_read_public_key_from_string(pk2, encoded, size)); test_eq(0, crypto_pk_cmp_keys(pk1, pk2)); /* comparison between keys and NULL */ tt_int_op(crypto_pk_cmp_keys(NULL, pk1), <, 0); tt_int_op(crypto_pk_cmp_keys(NULL, NULL), ==, 0); tt_int_op(crypto_pk_cmp_keys(pk1, NULL), >, 0); test_eq(128, crypto_pk_keysize(pk1)); test_eq(1024, crypto_pk_num_bits(pk1)); test_eq(128, crypto_pk_keysize(pk2)); test_eq(1024, crypto_pk_num_bits(pk2)); test_eq(128, crypto_pk_public_encrypt(pk2, data1, sizeof(data1), "Hello whirled.", 15, PK_PKCS1_OAEP_PADDING)); test_eq(128, crypto_pk_public_encrypt(pk1, data2, sizeof(data1), "Hello whirled.", 15, PK_PKCS1_OAEP_PADDING)); /* oaep padding should make encryption not match */ test_memneq(data1, data2, 128); test_eq(15, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data1, 128, PK_PKCS1_OAEP_PADDING,1)); test_streq(data3, "Hello whirled."); memset(data3, 0, 1024); test_eq(15, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128, PK_PKCS1_OAEP_PADDING,1)); test_streq(data3, "Hello whirled."); /* Can't decrypt with public key. */ test_eq(-1, crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data2, 128, PK_PKCS1_OAEP_PADDING,1)); /* Try again with bad padding */ memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */ test_eq(-1, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128, PK_PKCS1_OAEP_PADDING,1)); /* File operations: save and load private key */ test_assert(! crypto_pk_write_private_key_to_filename(pk1, get_fname("pkey1"))); /* failing case for read: can't read. */ test_assert(crypto_pk_read_private_key_from_filename(pk2, get_fname("xyzzy")) < 0); write_str_to_file(get_fname("xyzzy"), "foobar", 6); /* Failing case for read: no key. */ test_assert(crypto_pk_read_private_key_from_filename(pk2, get_fname("xyzzy")) < 0); test_assert(! crypto_pk_read_private_key_from_filename(pk2, get_fname("pkey1"))); test_eq(15, crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data1, 128, PK_PKCS1_OAEP_PADDING,1)); /* Now try signing. */ strlcpy(data1, "Ossifrage", 1024); test_eq(128, crypto_pk_private_sign(pk1, data2, sizeof(data2), data1, 10)); test_eq(10, crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128)); test_streq(data3, "Ossifrage"); /* Try signing digests. */ test_eq(128, crypto_pk_private_sign_digest(pk1, data2, sizeof(data2), data1, 10)); test_eq(20, crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128)); test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128)); test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128)); /*XXXX test failed signing*/ /* Try encoding */ crypto_pk_free(pk2); pk2 = NULL; i = crypto_pk_asn1_encode(pk1, data1, 1024); test_assert(i>0); pk2 = crypto_pk_asn1_decode(data1, i); test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0); /* Try with hybrid encryption wrappers. */ crypto_rand(data1, 1024); for (i = 0; i < 2; ++i) { for (j = 85; j < 140; ++j) { memset(data2,0,1024); memset(data3,0,1024); p = (i==0)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING; len = crypto_pk_public_hybrid_encrypt(pk1,data2,sizeof(data2), data1,j,p,0); test_assert(len>=0); len = crypto_pk_private_hybrid_decrypt(pk1,data3,sizeof(data3), data2,len,p,1); test_eq(len,j); test_memeq(data1,data3,j); } } /* Try copy_full */ crypto_pk_free(pk2); pk2 = crypto_pk_copy_full(pk1); test_assert(pk2 != NULL); test_neq_ptr(pk1, pk2); test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0); done: if (pk1) crypto_pk_free(pk1); if (pk2) crypto_pk_free(pk2); tor_free(encoded); }
static void do_cp (void) { char *from = NULL; char *to = NULL; MateVFSHandle *from_handle = NULL; MateVFSHandle *to_handle = NULL; MateVFSResult result; from = get_fname (); if (from) to = get_fname (); else { fprintf (vfserr, "cp <from> <to>\n"); goto out; } result = mate_vfs_open (&from_handle, from, MATE_VFS_OPEN_READ); if (show_if_error (result, "open ", from)) goto out; result = mate_vfs_open (&to_handle, to, MATE_VFS_OPEN_WRITE); if (result == MATE_VFS_ERROR_NOT_FOUND) result = mate_vfs_create (&to_handle, to, MATE_VFS_OPEN_WRITE, FALSE, MATE_VFS_PERM_USER_ALL); if (show_if_error (result, "open ", to)) goto out; while (1) { MateVFSFileSize bytes_read; MateVFSFileSize bytes_written; guint8 data [1024]; result = mate_vfs_read (from_handle, data, 1024, &bytes_read); if (show_if_error (result, "read ", from)) goto out; if (bytes_read == 0) break; result = mate_vfs_write (to_handle, data, bytes_read, &bytes_written); if (show_if_error (result, "write ", to)) goto out; if (bytes_read != bytes_written) fprintf (vfserr, "Didn't write it all"); } out: g_free (from); g_free (to); if (to_handle) { result = mate_vfs_close (to_handle); if (show_if_error (result, "close ", to)) /* Nothing */; } if (from_handle) { result = mate_vfs_close (from_handle); if (show_if_error (result, "close ", from)) /* Nothing */; } }