void test_classify(void) { icalcomponent *c, *match; int i = 0; int error_count = 0; /* Open up the two storage files, one for the incomming components, one for the calendar */ icalfileset_options options = { O_RDONLY, 0644, 0, NULL }; icalset *incoming = icalset_new(ICAL_FILE_SET, TEST_DATADIR "/incoming.ics", &options); icalset *cal = icalset_new(ICAL_FILE_SET, TEST_DATADIR "/calendar.ics", &options); icalset *f = icalset_new(ICAL_FILE_SET, TEST_DATADIR "/classify.ics", &options); ok("opening file classify.ics", (f != 0)); ok("opening file calendar.ics", (cal != 0)); ok("opening file incoming.ics", (incoming != 0)); /* some basic tests.. */ if (f) { c = icalset_get_first_component(f); match = icalset_get_next_component(f); ok("test two vcalendars for SEQUENCE with icalclassify()", (icalclassify(c, match, "*****@*****.**") == ICAL_XLICCLASS_REQUESTRESCHEDULE)); icalset_free(f); } assert(incoming != 0); assert(cal != 0); /* Iterate through all of the incoming components */ for (c = icalset_get_first_component(incoming); c != 0; c = icalset_get_next_component(incoming)) { icalproperty_xlicclass class; icalcomponent *match = 0; const char *this_uid; const char *this_note = get_note(c); const char *expected_result = get_expect(c); const char *actual_result; char msg[128]; i++; /* Check this component against the restrictions imposed by iTIP. An errors will be inserted as X-LIC-ERROR properties in the component. The Parser will also insert errors if it cannot parse the component */ icalcomponent_check_restrictions(c); /* If there are any errors, print out the component */ error_count = icalcomponent_count_errors(c); snprintf(msg, sizeof(msg), "%s - parsing", this_note); int_is(msg, error_count, 0); if (error_count != 0) { if (VERBOSE) { printf("----- Component has errors ------- \n%s-----------------\n", icalcomponent_as_ical_string(c)); } } /* Use one of the icalcomponent convenience routines to get the UID. This routine will save you from having to use icalcomponent_get_inner(), icalcomponent_get_first_property(), checking the return value, and then calling icalproperty_get_uid. There are several other convenience routines for DTSTART, DTEND, DURATION, SUMMARY, METHOD, and COMMENT */ this_uid = icalcomponent_get_uid(c); if (this_uid != 0) { /* Look in the calendar for a component with the same UID as the incomming component. We should reall also be checking the RECURRENCE-ID. Another way to do this operation is to us icalset_find_match(), which does use the RECURRENCE-ID. */ match = icalset_fetch(cal, this_uid); } /* Classify the incoming component. The third argument is the calid of the user who owns the calendar. In a real program, you would probably switch() on the class. */ class = icalclassify(c, match, "*****@*****.**"); /** eventually test this too.. **/ (void)get_note(match); actual_result = icalproperty_enum_to_string(class); snprintf(msg, sizeof(msg), "expecting %s", expected_result); str_is(msg, expected_result, actual_result); if (VERBOSE) { printf("Test %d\n" "Incoming: %s\n" "Matched: %s\n" "Classification: %s\n\n", i, this_note, get_note(match), icalproperty_enum_to_string(class)); } } icalset_free(incoming); icalset_free(cal); }
void test_recur_file() { icalset *cin = 0; struct icaltimetype next; icalcomponent *itr; icalproperty *desc, *dtstart, *rrule; struct icalrecurrencetype recur; icalrecur_iterator* ritr; time_t tt; char* file; int num_recurs_found = 0; icalfileset_options options = {O_RDONLY, 0644, 0}; icalerror_set_error_state(ICAL_PARSE_ERROR, ICAL_ERROR_NONFATAL); #ifndef WIN32 signal(SIGALRM,sig_alrm); #endif file = getenv("ICAL_RECUR_FILE"); if (!file) file = TEST_DATADIR "/recur.txt"; #ifndef WIN32 alarm(15); /* to get file lock */ #endif cin = icalset_new(ICAL_FILE_SET, file, &options); #ifndef WIN32 alarm(0); #endif ok("opening file with recurring events", (cin!=NULL)); assert(cin!=NULL); for (itr = icalfileset_get_first_component(cin); itr != 0; itr = icalfileset_get_next_component(cin)){ int badcomp = 0; int expected_events = 0; char msg[128]; struct icaltimetype start = icaltime_null_time(); struct icaltimetype startmin = icaltime_from_timet(1,0); struct icaltimetype endmax = icaltime_null_time(); const char *desc_str = "malformed component"; desc = icalcomponent_get_first_property(itr,ICAL_DESCRIPTION_PROPERTY); dtstart = icalcomponent_get_first_property(itr,ICAL_DTSTART_PROPERTY); rrule = icalcomponent_get_first_property(itr,ICAL_RRULE_PROPERTY); if (desc) { desc_str = icalproperty_get_description(desc); } ok((char*)desc_str, !(desc == 0 || dtstart == 0 || rrule == 0)); if (desc == 0 || dtstart == 0 || rrule == 0) { badcomp = 1; if (VERBOSE) { printf("\n******** Error in input component ********\n"); printf("The following component is malformed:\n %s\n", desc_str); } continue; } if (VERBOSE) { printf("\n\n#### %s\n",desc_str); printf("#### %s\n",icalvalue_as_ical_string(icalproperty_get_value(rrule))); } recur = icalproperty_get_rrule(rrule); start = icalproperty_get_dtstart(dtstart); ritr = icalrecur_iterator_new(recur,start); tt = icaltime_as_timet(start); if (VERBOSE) printf("#### %s\n",ctime(&tt )); icalrecur_iterator_free(ritr); for(ritr = icalrecur_iterator_new(recur,start), next = icalrecur_iterator_next(ritr); !icaltime_is_null_time(next); next = icalrecur_iterator_next(ritr)){ tt = icaltime_as_timet(next); if (VERBOSE) printf(" %s",ctime(&tt )); } icalrecur_iterator_free(ritr); num_recurs_found = 0; expected_events = get_expected_numevents(itr); icalcomponent_foreach_recurrence(itr, startmin, endmax, recur_callback, &num_recurs_found); sprintf(msg," expecting total of %d events", expected_events); int_is(msg, num_recurs_found, expected_events); } icalset_free(cin); }
/** Test icalcomponent_get_span() * */ void test_icalcomponent_get_span() { time_t tm1 = 973378800; /*Sat Nov 4 23:00:00 UTC 2000, Sat Nov 4 15:00:00 PST 2000 */ time_t tm2 = 973382400; /*Sat Nov 5 00:00:00 UTC 2000 Sat Nov 4 16:00:00 PST 2000 */ struct icaldurationtype dur; struct icaltime_span span; icalcomponent *c; icaltimezone *azone, *bzone; int tnum = 0; /** test 0 * Direct assigning time_t means they will be interpreted as UTC */ span.start = tm1; span.end = tm2; if (VERBOSE) print_span(tnum++,span); /** test 1 * We specify times in a timezone, the returned span is in UTC */ azone = icaltimezone_get_builtin_timezone("America/Los_Angeles"); c = icalcomponent_vanew( ICAL_VEVENT_COMPONENT, icalproperty_vanew_dtstart( icaltime_from_timet_with_zone(tm1,0,azone), icalparameter_new_tzid("America/Los_Angeles"),0), icalproperty_vanew_dtend( icaltime_from_timet_with_zone(tm2,0,azone), icalparameter_new_tzid("America/Los_Angeles"),0), 0 ); span = icalcomponent_get_span(c); if (VERBOSE) print_span(tnum++,span); int_is("America/Los_Angeles", span.start, 973407600); icalcomponent_free(c); /** test 2 * We specify times as floating, the returned span is in UTC * with no conversion applied - so result should be as test 0 */ c = icalcomponent_vanew( ICAL_VEVENT_COMPONENT, icalproperty_vanew_dtstart(icaltime_from_timet(tm1,0),0), icalproperty_vanew_dtend(icaltime_from_timet(tm2,0),0), 0 ); span = icalcomponent_get_span(c); if (VERBOSE) print_span(tnum++,span); int_is("floating time", span.start, tm1); icalcomponent_free(c); /** test 3 * We specify times in a timezone, the returned span is in UTC */ azone = icaltimezone_get_builtin_timezone("America/New_York"); c = icalcomponent_vanew( ICAL_VEVENT_COMPONENT, icalproperty_vanew_dtstart( icaltime_from_timet_with_zone(tm1,0,azone), icalparameter_new_tzid("America/New_York"),0), icalproperty_vanew_dtend( icaltime_from_timet_with_zone(tm2,0,azone), icalparameter_new_tzid("America/New_York"),0), 0 ); span = icalcomponent_get_span(c); if (VERBOSE) print_span(tnum++,span); int_is("America/New_York", span.start, 973396800); icalcomponent_free(c); /** test 4 * We specify times in two different timezones, the returned span * is in UTC */ azone = icaltimezone_get_builtin_timezone("America/New_York"); bzone = icaltimezone_get_builtin_timezone("America/Los_Angeles"); c = icalcomponent_vanew( ICAL_VEVENT_COMPONENT, icalproperty_vanew_dtstart( icaltime_from_timet_with_zone(tm1,0,azone), icalparameter_new_tzid("America/New_York"),0), icalproperty_vanew_dtend( icaltime_from_timet_with_zone(tm2,0,bzone), icalparameter_new_tzid("America/Los_Angeles"),0), 0 ); span = icalcomponent_get_span(c); if (VERBOSE) print_span(tnum++,span); int_is("America/New_York", span.start, 973396800); icalcomponent_free(c); /** test 5 * We specify start time in a timezone and a duration, the returned span * is in UTC */ azone = icaltimezone_get_builtin_timezone("America/Los_Angeles"); memset(&dur,0,sizeof(dur)); dur.minutes = 30; c = icalcomponent_vanew( ICAL_VEVENT_COMPONENT, icalproperty_vanew_dtstart( icaltime_from_timet_with_zone(tm1,0,azone), icalparameter_new_tzid("America/Los_Angeles"),0), icalproperty_new_duration(dur), 0 ); span = icalcomponent_get_span(c); if (VERBOSE) print_span(tnum++,span); int_is("America/Los_Angeles w/ duration", span.end, 973409400); icalcomponent_free(c); icalerror_errors_are_fatal = 0; /** test 6 * We specify only start time, should return a null span with no error */ c = icalcomponent_vanew( ICAL_VEVENT_COMPONENT, icalproperty_new_dtstart(icaltime_from_timet(tm1,0)), 0 ); span = icalcomponent_get_span(c); if (VERBOSE) print_span(tnum++,span); int_is("null span", span.start, 0); icalcomponent_free(c); /** test 7 * We specify start and end date */ c = icalcomponent_vanew( ICAL_VEVENT_COMPONENT, icalproperty_new_dtstart(icaltime_from_timet(tm1,1)), icalproperty_new_dtend(icaltime_from_timet(tm1,1)), 0 ); span = icalcomponent_get_span(c); if (VERBOSE) print_span(tnum++,span); int_is("UTC", span.start, 973296000); icalcomponent_free(c); /** test 8 * We specify start and end date */ c = icalcomponent_vanew( ICAL_VEVENT_COMPONENT, icalproperty_new_dtstart(icaltime_from_timet(tm1,1)), icalproperty_new_dtend(icaltime_from_timet(tm2,1)), 0 ); span = icalcomponent_get_span(c); int_is("UTC #2", span.start, 973296000); if (VERBOSE) print_span(tnum++,span); icalcomponent_free(c); /** test 9 * We specify start date */ c = icalcomponent_vanew( ICAL_VEVENT_COMPONENT, icalproperty_new_dtstart(icaltime_from_timet(tm1,1)), 0 ); span = icalcomponent_get_span(c); if (VERBOSE) print_span(tnum++,span); int_is("start date only", span.end, 973382399); icalcomponent_free(c); /* assert(icalerrno == ICAL_MALFORMEDDATA_ERROR); */ icalerror_errors_are_fatal = 1; }