Bug 1402151, part 8 - Add an argument to ParseFTPList to return the current time. r=michal
If the year is omitted from a listing, then the parsing code assumes
that the date occurred within the last year. To compute what the year
should be, the code thus needs to know what the current year, month
and day are. This patch parameterizes ParseFTPList over a function
that returns a PRTime. This will allow the gtest to pass in a fixed
time. Firefox itself will continue to use PR_Now to get the current
time.
MozReview-Commit-ID: 8IbVWbUBHYs
--- a/netwerk/streamconv/converters/ParseFTPList.cpp
+++ b/netwerk/streamconv/converters/ParseFTPList.cpp
@@ -35,17 +35,18 @@ FixupYear(PRExplodedTime* aTime)
if (aTime->tm_year < 80) {
aTime->tm_year += 2000;
} else if (aTime->tm_year < 100) {
aTime->tm_year += 1900;
}
}
int ParseFTPList(const char *line, struct list_state *state,
- struct list_result *result, PRTimeParamFn timeParam)
+ struct list_result *result, PRTimeParamFn timeParam,
+ NowTimeFn nowTimeFn)
{
unsigned int carry_buf_len; /* copy of state->carry_buf_len */
unsigned int pos;
const char *p;
if (!line || !state || !result)
return 0;
@@ -1133,17 +1134,17 @@ int ParseFTPList(const char *line, struc
{
result->fe_time.tm_hour = pos;
result->fe_time.tm_min = atoi(p+3);
if (p[5] == ':')
result->fe_time.tm_sec = atoi(p+6);
if (!state->now_time)
{
- state->now_time = PR_Now();
+ state->now_time = nowTimeFn();
PR_ExplodeTime((state->now_time), timeParam, &(state->now_tm) );
}
result->fe_time.tm_year = state->now_tm.tm_year;
if ( (( state->now_tm.tm_month << 5) + state->now_tm.tm_mday) <
((result->fe_time.tm_month << 5) + result->fe_time.tm_mday) )
result->fe_time.tm_year--;
@@ -1611,17 +1612,17 @@ int ParseFTPList(const char *line, struc
else
{
if (p[1] == ':')
p--;
result->fe_time.tm_hour = pos;
result->fe_time.tm_min = atoi(p+3);
if (!state->now_time)
{
- state->now_time = PR_Now();
+ state->now_time = nowTimeFn();
PR_ExplodeTime((state->now_time), timeParam, &(state->now_tm) );
}
result->fe_time.tm_year = state->now_tm.tm_year;
if ( (( state->now_tm.tm_month << 4) + state->now_tm.tm_mday) <
((result->fe_time.tm_month << 4) + result->fe_time.tm_mday) )
result->fe_time.tm_year--;
} /* got year or time */
} /* got month/mday */
--- a/netwerk/streamconv/converters/ParseFTPList.h
+++ b/netwerk/streamconv/converters/ParseFTPList.h
@@ -92,14 +92,17 @@ struct list_result
const char * fe_lname; /* pointer to symlink name */
uint32_t fe_lnlen; /* length of symlink name */
char fe_size[40]; /* size of file in bytes (<= (2^128 - 1)) */
PRExplodedTime fe_time; /* last-modified time */
int32_t fe_cinfs; /* file system is definitely case insensitive */
/* (converting all-upcase names may be desirable) */
};
+typedef PRTime (*NowTimeFn)();
+
int ParseFTPList(const char *line,
struct list_state *state,
struct list_result *result,
- PRTimeParamFn timeParam = PR_LocalTimeParameters);
+ PRTimeParamFn timeParam = PR_LocalTimeParameters,
+ NowTimeFn nowTimeFn = PR_Now);
#endif /* !ParseRTPList_h___ */