static int test_updating (void) { char * path; tr_session * session; /* init the session */ session = libttest_session_init (NULL); path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL); /* no blocklist to start with... */ check_int_eq (0, tr_blocklistGetRuleCount (session)); /* test that updated source files will get loaded */ create_text_file (path, contents1); tr_sessionReloadBlocklists (session); check_int_eq (4, tr_blocklistGetRuleCount (session)); /* test that updated source files will get loaded */ create_text_file (path, contents2); tr_sessionReloadBlocklists (session); check_int_eq (5, tr_blocklistGetRuleCount (session)); /* test that updated source files will get loaded */ create_text_file (path, contents1); tr_sessionReloadBlocklists (session); check_int_eq (4, tr_blocklistGetRuleCount (session)); /* ensure that new files, if bad, get skipped */ create_text_file (path, "# nothing useful\n"); tr_sessionReloadBlocklists (session); check_int_eq (4, tr_blocklistGetRuleCount (session)); /* cleanup */ libttest_session_close (session); tr_free (path); return 0; }
static int test_parsing (void) { char * path; tr_session * session; /* init the session */ session = libttest_session_init (NULL); check (!tr_blocklistExists (session)); check_int_eq (0, tr_blocklistGetRuleCount (session)); /* init the blocklist */ path = tr_buildPath (tr_sessionGetConfigDir(session), "blocklists", "level1", NULL); create_text_file (path, contents1); tr_free (path); tr_sessionReloadBlocklists (session); check (tr_blocklistExists (session)); check_int_eq (4, tr_blocklistGetRuleCount (session)); /* enable the blocklist */ check (!tr_blocklistIsEnabled (session)); tr_blocklistSetEnabled (session, true); check (tr_blocklistIsEnabled (session)); /* test blocked addresses */ check (!address_is_blocked (session, "216.16.1.143")); check ( address_is_blocked (session, "216.16.1.144")); check ( address_is_blocked (session, "216.16.1.145")); check ( address_is_blocked (session, "216.16.1.146")); check ( address_is_blocked (session, "216.16.1.147")); check ( address_is_blocked (session, "216.16.1.148")); check ( address_is_blocked (session, "216.16.1.149")); check ( address_is_blocked (session, "216.16.1.150")); check ( address_is_blocked (session, "216.16.1.151")); check (!address_is_blocked (session, "216.16.1.152")); check (!address_is_blocked (session, "216.16.1.153")); check (!address_is_blocked (session, "217.0.0.1")); check (!address_is_blocked (session, "255.0.0.1")); /* cleanup */ libttest_session_close (session); return 0; }
main() { FILE *in, *out; char line[80], *tmp, *ptr; /* ** Open the original file for reading and a temporary file for writing */ create_text_file(); in = cant("test.txt", "r"); tmp = tmpnam(NULL); out = cant(tmp, "w"); /* ** Read the first line and copy it */ fgets(line, 80, in); fputs(line, out); /* ** Discard the second line */ fgets(line, 80, in); /* ** Add a new line */ fputs("(Isn't it?)\n", out); /* ** Read the 3rd line, modify it, then write it out */ fgets(line, 80, in); ptr = strrchr(line, 'm'); strcpy(ptr, "edit...\n"); fputs(line, out); /* ** Read the last line and copy it */ fgets(line, 80, in); fputs(line, out); /* ** Close the files, delete the old, rename the temp */ fclose(in); fclose(out); remove("test.txt"); rename(tmp, "test.txt"); /* ** Now let's see the results */ show_text_file("The file as modified is"); return 0; }