Skip to content

Commit

Permalink
Add cupsGetClock API and unit test program.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Oct 22, 2024
1 parent 3ced7fa commit fdbdb56
Show file tree
Hide file tree
Showing 9 changed files with 380 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changes in libcups
==================

libcups v3.0rc4 (YYYY-MM-DD)
----------------------------

- Added `cupsGetClock` API.


libcups v3.0rc3 (2024-10-22)
----------------------------

Expand Down
35 changes: 25 additions & 10 deletions cups/Dependencies
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
array.o: array.c ../cups/cups.h file.h base.h ipp.h http.h array.h \
language.h transcode.h pwg.h string-private.h ../config.h \
array.o: array.c ../cups/cups.h ../cups/file.h ../cups/base.h \
../cups/ipp.h ../cups/http.h ../cups/array.h ../cups/language.h \
../cups/transcode.h ../cups/pwg.h string-private.h ../config.h base.h \
debug-internal.h debug-private.h
auth.o: auth.c cups-private.h string-private.h ../config.h base.h \
debug-internal.h debug-private.h array.h ipp-private.h cups.h file.h \
ipp.h http.h language.h transcode.h pwg.h http-private.h \
../cups/language.h \
pwg-private.h thread.h
clock.o: clock.c cups-private.h string-private.h ../config.h base.h \
debug-internal.h debug-private.h array.h ipp-private.h cups.h file.h \
ipp.h http.h language.h transcode.h pwg.h http-private.h \
../cups/language.h \
pwg-private.h thread.h
debug.o: debug.c cups-private.h string-private.h ../config.h base.h \
debug-internal.h debug-private.h array.h ipp-private.h cups.h file.h \
ipp.h http.h language.h transcode.h pwg.h http-private.h \
Expand All @@ -32,8 +38,10 @@ dest-options.o: dest-options.c cups-private.h string-private.h \
http-private.h ../cups/language.h \
pwg-private.h thread.h
dir.o: dir.c string-private.h ../config.h base.h debug-internal.h \
debug-private.h cups.h file.h ipp.h http.h array.h language.h \
transcode.h pwg.h dir.h
debug-private.h cups-private.h array.h ipp-private.h cups.h file.h \
ipp.h http.h language.h transcode.h pwg.h http-private.h \
../cups/language.h \
pwg-private.h thread.h dir.h
dnssd.o: dnssd.c cups-private.h string-private.h ../config.h base.h \
debug-internal.h debug-private.h array.h ipp-private.h cups.h file.h \
ipp.h http.h language.h transcode.h pwg.h http-private.h \
Expand Down Expand Up @@ -202,14 +210,18 @@ util.o: util.c cups-private.h string-private.h ../config.h base.h \
fuzzipp.o: fuzzipp.c file.h base.h string-private.h ../config.h \
ipp-private.h cups.h ipp.h http.h array.h language.h transcode.h pwg.h \
test-internal.h
rasterbench.o: rasterbench.c ../config.h ../cups/raster.h cups.h file.h \
base.h ipp.h http.h array.h language.h transcode.h pwg.h
rasterbench.o: rasterbench.c ../config.h ../cups/raster.h ../cups/cups.h \
../cups/file.h ../cups/base.h ../cups/ipp.h ../cups/http.h \
../cups/array.h ../cups/language.h ../cups/transcode.h ../cups/pwg.h
testarray.o: testarray.c string-private.h ../config.h base.h \
debug-private.h cups.h file.h ipp.h http.h array.h language.h \
transcode.h pwg.h dir.h test-internal.h
testclient.o: testclient.c ../config.h ../cups/cups.h file.h base.h ipp.h \
http.h array.h language.h transcode.h pwg.h ../cups/raster.h cups.h \
testclient.o: testclient.c ../config.h ../cups/cups.h ../cups/file.h \
../cups/base.h ../cups/ipp.h ../cups/http.h ../cups/array.h \
../cups/language.h ../cups/transcode.h ../cups/pwg.h ../cups/raster.h \
../cups/string-private.h ../cups/thread.h
testclock.o: testclock.c cups.h file.h base.h ipp.h http.h array.h \
language.h transcode.h pwg.h test-internal.h
testcreds.o: testcreds.c cups-private.h string-private.h ../config.h \
base.h debug-internal.h debug-private.h array.h ipp-private.h cups.h \
file.h ipp.h http.h language.h transcode.h pwg.h http-private.h \
Expand Down Expand Up @@ -247,6 +259,8 @@ testjson.o: testjson.c cups.h file.h base.h ipp.h http.h array.h \
language.h transcode.h pwg.h json.h test-internal.h
testjwt.o: testjwt.c cups.h file.h base.h ipp.h http.h array.h language.h \
transcode.h pwg.h jwt.h json.h test-internal.h
testlang.o: testlang.c ../config.h cups.h file.h base.h ipp.h http.h \
array.h language.h transcode.h pwg.h dir.h test-internal.h
testoauth.o: testoauth.c cups.h file.h base.h ipp.h http.h array.h \
language.h transcode.h pwg.h oauth.h jwt.h json.h test-internal.h
testoptions.o: testoptions.c cups-private.h string-private.h ../config.h \
Expand All @@ -263,8 +277,9 @@ testtestpage.o: testtestpage.c raster-testpage.h raster-private.h \
raster.h cups.h file.h base.h ipp.h http.h array.h language.h \
transcode.h pwg.h debug-private.h string-private.h ../config.h \
test-internal.h
testthreads.o: testthreads.c ../cups/cups.h file.h base.h ipp.h http.h \
array.h language.h transcode.h pwg.h ../cups/thread.h
testthreads.o: testthreads.c ../cups/cups.h ../cups/file.h ../cups/base.h \
../cups/ipp.h ../cups/http.h ../cups/array.h ../cups/language.h \
../cups/transcode.h ../cups/pwg.h ../cups/thread.h
tlscheck.o: tlscheck.c cups-private.h string-private.h ../config.h base.h \
debug-internal.h debug-private.h array.h ipp-private.h cups.h file.h \
ipp.h http.h language.h transcode.h pwg.h http-private.h \
Expand Down
16 changes: 16 additions & 0 deletions cups/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ include ../Makedefs
LIBOBJS = \
array.o \
auth.o \
clock.o \
debug.o \
dest.o \
dest-job.o \
Expand Down Expand Up @@ -62,6 +63,7 @@ TESTOBJS = \
rasterbench.o \
testarray.o \
testclient.o \
testclock.o \
testcreds.o \
testcups.o \
testdest.o \
Expand Down Expand Up @@ -147,6 +149,7 @@ UNITTARGETS = \
rasterbench \
testarray \
testclient \
testclock \
testcreds \
testcups \
testdest \
Expand Down Expand Up @@ -195,6 +198,9 @@ test: $(UNITTARGETS)
# echo Running CUPS API tests...; \
# ./testcups 2>>test.log; \
# fi
echo ""
echo Running clock API tests...
./testclock 2>>test.log
echo ""
echo Running credentials API tests...
./testcreds 2>>test.log
Expand Down Expand Up @@ -442,6 +448,16 @@ testclient: testclient.o $(LIBCUPS_STATIC)
$(CODE_SIGN) $(CSFLAGS) $@


#
# testclock (dependency on static libraries is intentional)
#

testclock: testclock.o $(LIBCUPS_STATIC)
echo Linking $@...
$(CC) $(LDFLAGS) $(OPTIM) -o $@ testclock.o $(LIBCUPS_STATIC) $(LIBS)
$(CODE_SIGN) $(CSFLAGS) $@


#
# testcreds (dependency on static CUPS library is intentional)
#
Expand Down
110 changes: 110 additions & 0 deletions cups/clock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// Monotonic clock API for CUPS.
//
// Copyright © 2024 by OpenPrinting.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//

#include "cups-private.h"


//
// Local globals...
//

static bool cups_clock_init = false;// Clock initialized?
static cups_mutex_t cups_clock_mutex = CUPS_MUTEX_INITIALIZER;
// Mutex to control access
#ifdef _WIN32
static ULONGLONG cups_first_tick; // First tick count
#else
# ifdef CLOCK_MONOTONIC
static struct timespec cups_first_clock;// First clock value
# endif // CLOCK_MONOTONIC
static struct timeval cups_first_time; // First time value
#endif // _WIN32


//
// 'cupsGetClock()' - Get a monotonic clock value in seconds.
//
// This function returns a monotonically increasing clock value in seconds. The
// first call will always return 0.0. Subsequent calls will return the number
// of seconds that have elapsed since the first call, regardless of system time
// changes, sleep, etc. The sub-second accuracy varies based on the operating
// system and hardware but is typically 10ms or better.
//

double // O - Elapsed seconds
cupsGetClock(void)
{
double secs; // Elapsed seconds
#ifdef _WIN32
ULONGLONG curtick; // Current tick count
#else
# ifdef CLOCK_MONOTONIC
struct timespec curclock; // Current clock value
# endif // CLOCK_MONOTONIC
struct timeval curtime; // Current time value
#endif // _WIN32


cupsMutexLock(&cups_clock_mutex);

#ifdef _WIN32
// Get the current tick count in milliseconds...
curtick = GetTickCount64();

if (!cups_clock_init)
{
// First time through initialize the initial tick count...
cups_clock_init = true;
cups_first_tick = curtick;
}

// Convert ticks to seconds...
if (curtick < cups_first_tick)
secs = 0.0;
else
secs = 0.001 * (curtick - cups_first_tick);

#else
# ifdef CLOCK_MONOTONIC
// Get the current tick count in milliseconds...
if (!clock_gettime(CLOCK_MONOTONIC, &curclock))
{
if (!cups_clock_init)
{
// First time through initialize the initial clock value...
cups_clock_init = true;
cups_first_clock = curclock;
}

// Convert clock value to seconds...
if ((secs = curclock.tv_sec - cups_first_clock.tv_sec + 0.000000001 * (curclock.tv_nsec - cups_first_clock.tv_nsec)) < 0.0)
secs = 0.0;
}
else
# endif // CLOCK_MONOTONIC
{
gettimeofday(&curtime, /*tzp*/NULL);

if (!cups_clock_init)
{
// First time through initialize the initial clock value...
cups_clock_init = true;
cups_first_time = curtime;
}

// Convert time value to seconds...
if ((secs = curtime.tv_sec - cups_first_time.tv_sec + 0.000001 * (curtime.tv_usec - cups_first_time.tv_usec)) < 0.0)
secs = 0.0;
}
#endif // _WIN32

cupsMutexUnlock(&cups_clock_mutex);

return (secs);
}
1 change: 1 addition & 0 deletions cups/cups.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ extern void cupsFreeDests(size_t num_dests, cups_dest_t *dests) _CUPS_PUBLIC;
extern void cupsFreeJobs(size_t num_jobs, cups_job_t *jobs) _CUPS_PUBLIC;
extern void cupsFreeOptions(size_t num_options, cups_option_t *options) _CUPS_PUBLIC;

extern double cupsGetClock(void) _CUPS_PUBLIC;
extern time_t cupsGetCredentialsExpiration(const char *credentials) _CUPS_PUBLIC;
extern char *cupsGetCredentialsInfo(const char *credentials, char *buffer, size_t bufsize) _CUPS_PUBLIC;
extern http_trust_t cupsGetCredentialsTrust(const char *path, const char *common_name, const char *credentials, bool require_ca) _CUPS_PUBLIC;
Expand Down
1 change: 1 addition & 0 deletions cups/libcups3.def
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ cupsFreeDestInfo
cupsFreeDests
cupsFreeJobs
cupsFreeOptions
cupsGetClock
cupsGetCredentialsExpiration
cupsGetCredentialsInfo
cupsGetCredentialsTrust
Expand Down
88 changes: 88 additions & 0 deletions cups/testclock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// Monotonic clock test program for CUPS.
//
// Copyright © 2024 by OpenPrinting.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//

#include "cups.h"
#include "test-internal.h"
#include <math.h>


//
// 'main()' - Main entry for clock tests.
//

int // O - Exit status
main(void)
{
double current; // Current time


// Test clock values at 0, 1, 5, 10, and 30 seconds
testBegin("cupsGetClock(initial)");
current = cupsGetClock();
if (current == 0.0)
testEndMessage(true, "%g", current);
else
testEndMessage(false, "got %g, expected 0.0", current);

sleep(1);

testBegin("cupsGetClock(1 second)");
current = cupsGetClock();
if (fabs(current - 1.0) < 0.1)
testEndMessage(true, "%g", current);
else
testEndMessage(false, "got %g, expected 1.0 +/- 0.1", current);

sleep(4);

testBegin("cupsGetClock(5 seconds)");
current = cupsGetClock();
if (fabs(current - 5.0) < 0.1)
testEndMessage(true, "%g", current);
else
testEndMessage(false, "got %g, expected 5.0 +/- 0.1", current);

sleep(5);

testBegin("cupsGetClock(10 seconds)");
current = cupsGetClock();
if (fabs(current - 10.0) < 0.1)
testEndMessage(true, "%g", current);
else
testEndMessage(false, "got %g, expected 10.0 +/- 0.1", current);

sleep(20);

testBegin("cupsGetClock(30 seconds)");
current = cupsGetClock();
if (fabs(current - 30.0) < 0.1)
testEndMessage(true, "%g", current);
else
testEndMessage(false, "got %g, expected 30.0 +/- 0.1", current);

sleep(30);

testBegin("cupsGetClock(60 seconds)");
current = cupsGetClock();
if (fabs(current - 60.0) < 0.1)
testEndMessage(true, "%g", current);
else
testEndMessage(false, "got %g, expected 60.0 +/- 0.1", current);

sleep(60);

testBegin("cupsGetClock(120 seconds)");
current = cupsGetClock();
if (fabs(current - 120.0) < 0.1)
testEndMessage(true, "%g", current);
else
testEndMessage(false, "got %g, expected 120.0 +/- 0.1", current);

return (testsPassed ? 0 : 1);
}
1 change: 1 addition & 0 deletions vcnet/libcups3.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
<ItemGroup>
<ClCompile Include="..\cups\array.c" />
<ClCompile Include="..\cups\auth.c" />
<ClCompile Include="..\cups\clock.c" />
<ClCompile Include="..\cups\debug.c" />
<ClCompile Include="..\cups\dest-job.c" />
<ClCompile Include="..\cups\dest-localization.c" />
Expand Down
Loading

0 comments on commit fdbdb56

Please sign in to comment.