/* Banco di prova del motore, senza interfaccia. * * Il motore è una libreria con API C: qui si fanno le veci dell'app — si * ricevono le callback da gd_run, si eseguono le prove su una Maildir * sintetica e si stampa OK / NO per ciascuna. Nessun gesto, nessuna Qt. * * Uso: GRUMPYDOVE_MAIL= ./banco * Compila: tests/motore/run.sh */ #include #include #include #include #include #include #include "qt_shim.h" extern int gd_engine_run(void); /* Le callback del motore, conservate all'avvio. */ static gd_folder_fn cb_folder; static gd_open_fn cb_open; static gd_cell_fn cb_cell; static gd_sort_fn cb_sort __attribute__((unused)); static gd_read_fn cb_read; static gd_id_of_fn cb_id_of; static gd_move_fn cb_move; static gd_commit_fn cb_commit; static gd_log_fn cb_log; static gd_text_fn cb_sender; static gd_header_fn cb_header; static gd_compose_fn cb_compose; static gd_place_fn cb_place; static gd_can_hold_fn cb_can_hold; static gd_make_folder_fn cb_make_folder; static gd_make_folder_fn cb_change_folder; static gd_can_hold_fn cb_has_box; static gd_text_fn cb_path; static gd_rescan_fn cb_rescan; static int prove = 0; static int guasti = 0; static const char *radice = ""; /* dove sta la Maildir di prova */ static void esito(const char *nome, int bene, const char *dettaglio) { prove++; if (bene) { printf("OK %s\n", nome); } else { guasti++; printf("NO %s%s%s\n", nome, dettaglio ? " — " : "", dettaglio ? dettaglio : ""); } } static int esiste(const char *percorso) { struct stat s; return stat(percorso, &s) == 0; } /* Quanti file di una directory hanno un pezzo nel nome. */ static int quanti_con(const char *directory, const char *pezzo) { DIR *d = opendir(directory); struct dirent *e; int n = 0; if (!d) return 0; while ((e = readdir(d)) != NULL) { if (e->d_name[0] == '.') continue; if (strstr(e->d_name, pezzo)) n++; } closedir(d); return n; } static int cartella_nome(int indice, char *fuori, int cap, int *profondita) { char buf[512]; int n, i, d = 0; memset(buf, 0, sizeof buf); n = cb_folder(indice, &d, buf, (int)sizeof buf - 1); if (n < 0) { fuori[0] = '\0'; return -1; } if (n > (int)sizeof buf - 1) n = (int)sizeof buf - 1; for (i = 0; i < n && buf[i] && i < cap - 1; i++) fuori[i] = buf[i]; fuori[i] = '\0'; if (profondita) *profondita = d; return i; } static int trova_cartella(const char *nome, int profondita) { char n[512]; int i, d; for (i = 0; i < 500; i++) { if (cartella_nome(i, n, (int)sizeof n, &d) < 0) break; if (strcmp(n, nome) == 0 && (profondita < 0 || d == profondita)) return i; } return -1; } static void prove_cartelle(void) { char nomi[4096]; char n[512]; int i, d, quante = 0; int locale, in, trash; long totale = 0; int righe; nomi[0] = '\0'; for (i = 0; i < 500; i++) { if (cartella_nome(i, n, (int)sizeof n, &d) < 0) break; quante++; if (strlen(nomi) + strlen(n) + 3 < sizeof nomi) { strcat(nomi, n); strcat(nomi, " "); } } printf("cartelle viste: %s\n", nomi); esito("ci sono cartelle", quante > 0, NULL); locale = trova_cartella("Local", 0); esito("c'è Local in cima", locale >= 0, NULL); if (locale < 0) return; in = trova_cartella("In", -1); trash = trova_cartella("Trash", -1); esito("c'è In", in >= 0, NULL); esito("c'è Trash", trash >= 0, NULL); if (in < 0) return; righe = cb_open ? cb_open(in, &totale) : 0; printf("In: %d righe (total_k %ld)\n", righe, totale); esito("In ha i tre messaggi di prova", righe == 3, NULL); /* Le celle della prima riga: colonna 4 è il mittente, 7 l'oggetto. */ { char buf[512]; int n; memset(buf, 0, sizeof buf); n = cb_cell(in, 0, 4, buf, (int)sizeof buf - 1); buf[n > 0 ? (n < (int)sizeof buf - 1 ? n : (int)sizeof buf - 1) : 0] = '\0'; esito("il mittente della prima riga non è vuoto", n > 0, buf); memset(buf, 0, sizeof buf); n = cb_cell(in, 0, 7, buf, (int)sizeof buf - 1); buf[n > 0 ? (n < (int)sizeof buf - 1 ? n : (int)sizeof buf - 1) : 0] = '\0'; esito("l'oggetto della prima riga non è vuoto", n > 0, buf); } /* Creazione, rinomina, eliminazione sotto Local. */ { char percorso[1024]; int r; r = cb_make_folder ? cb_make_folder(locale, "BancoProva", 10, 1) : -9; snprintf(percorso, sizeof percorso, "%s/Local/BancoProva/cur", radice); esito("crea una cartella sotto Local", r == 1 && esiste(percorso), NULL); /* Change_Folder agisce sulla cartella stessa: prima si ritrova * nell'albero, dopo un Rescan che la fa comparire. */ if (cb_rescan) cb_rescan(); { int mia = trova_cartella("BancoProva", -1); esito("la cartella creata compare nell'albero", mia >= 0, NULL); r = (cb_change_folder && mia >= 0) ? cb_change_folder(mia, "BancoProva2", 11, -1) : -9; snprintf(percorso, sizeof percorso, "%s/Local/BancoProva2/cur", radice); esito("rinomina la cartella", r == 1 && esiste(percorso), NULL); if (cb_rescan) cb_rescan(); mia = trova_cartella("BancoProva2", -1); r = (cb_change_folder && mia >= 0) ? cb_change_folder(mia, "", 0, -1) : -9; snprintf(percorso, sizeof percorso, "%s/Local/BancoProva2", radice); esito("elimina la cartella", r == 1 && !esiste(percorso), NULL); } /* Sotto Local si può creare (Can_Hold = 1); un indice che non esiste * no (Can_Hold = 0). */ esito("Can_Hold: Local vale 1", cb_can_hold && cb_can_hold(locale) == 1, NULL); esito("Can_Hold: indice inesistente vale 0", cb_can_hold && cb_can_hold(-1) == 0, NULL); } /* Letto / non letto: il file cambia flag nel nome. */ { int id = cb_id_of ? cb_id_of(in, 0) : -1; char percorso[1024]; int prima, dopo; esito("la prima riga ha un id", id >= 0, NULL); snprintf(percorso, sizeof percorso, "%s/Local/INBOX/cur", radice); prima = quanti_con(percorso, ":2,S"); if (cb_read) { cb_read(in, 0, 1); if (cb_commit) cb_commit(in); } dopo = quanti_con(percorso, ":2,S"); esito("segnare letto mette la flag S", dopo == prima + 1, NULL); if (cb_read) { cb_read(in, 0, 0); if (cb_commit) cb_commit(in); } esito("togliere il letto toglie la flag S", quanti_con(percorso, ":2,S") == prima, NULL); } /* Spostamento: un messaggio va in Trash e In cala di uno. */ if (trash >= 0) { int id = cb_id_of ? cb_id_of(in, 0) : -1; int r = cb_move ? cb_move(in, id, trash) : -9; if (r != 0 && cb_commit) cb_commit(in); righe = cb_open ? cb_open(in, &totale) : 0; esito("sposta un messaggio in Trash", r != 0 && righe == 2, NULL); } } /* Il motore chiama questa: è il posto dell'interfaccia. */ int gd_run(gd_folder_fn folder, gd_open_fn open, gd_cell_fn cell, gd_body_fn body, gd_sort_fn sort, gd_read_fn mark_read, gd_id_of_fn id_of, gd_row_of_fn row_of, gd_move_fn move, gd_commit_fn commit, gd_empty_fn empty, gd_log_fn log, gd_set_fn priority, gd_set_fn label, gd_text_fn decrypt, gd_text_fn sender, gd_text_fn key_of, gd_header_fn header, gd_text_fn plain, gd_text_fn sheet, gd_compose_fn compose, gd_last_id_fn last_id, gd_find_start_fn find_start, gd_find_poll_fn find_poll, gd_find_cancel_fn find_cancel, gd_id_named_fn id_named, gd_claim_fn claim, gd_send_done_fn send_done, gd_send_start_fn send_start, gd_find_poll_fn send_poll, gd_find_start_fn imap_start, gd_find_poll_fn imap_poll, gd_absorb_fn absorb, gd_rescan_fn rescan, gd_shown_at_fn shown_at, gd_can_hold_fn can_hold, gd_make_folder_fn make_folder, gd_can_hold_fn can_change, gd_make_folder_fn change_folder, gd_can_hold_fn forget, gd_can_hold_fn has_box, gd_place_fn place, gd_find_start_fn imap_create, gd_find_start_fn book_start, gd_find_poll_fn book_poll, gd_place_fn book_list, gd_queue_sent_fn queue_sent, gd_text_fn html, gd_text_fn images, gd_text_fn attachments, gd_text_fn path, gd_can_hold_fn unread) { (void)body; (void)sort; (void)row_of; (void)empty; (void)priority; (void)label; (void)decrypt; (void)key_of; (void)plain; (void)sheet; (void)last_id; (void)find_start; (void)find_poll; (void)find_cancel; (void)id_named; (void)claim; (void)send_done; (void)send_start; (void)send_poll; (void)imap_start; (void)imap_poll; (void)absorb; (void)rescan; (void)shown_at; (void)can_change; (void)forget; (void)place; (void)imap_create; (void)book_start; (void)book_poll; (void)book_list; (void)queue_sent; (void)html; (void)images; (void)attachments; (void)unread; cb_folder = folder; cb_open = open; cb_cell = cell; cb_read = mark_read; cb_id_of = id_of; cb_move = move; cb_commit = commit; cb_log = log; cb_sender = sender; cb_header = header; cb_compose = compose; cb_place = place; cb_can_hold = can_hold; cb_make_folder = make_folder; cb_change_folder = change_folder; cb_has_box = has_box; cb_path = path; cb_rescan = rescan; prove_cartelle(); printf("RISULTATO: %d prove, %d guasti\n", prove, guasti); return 0; } int gd_single_instance(void) { return 1; } void gd_already_open(void) { } int main(void) { const char *posta = getenv("GRUMPYDOVE_MAIL"); if (!posta) { fprintf(stderr, "manca GRUMPYDOVE_MAIL\n"); return 2; } radice = posta; /* Il motore chiama gd_run e torna qui quando gd_run esce. */ gd_engine_run(); return guasti == 0 ? 0 : 1; }