Skip to content

Commit

Permalink
Implement newer utils APIs and fix more tests. Some still fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
regiMario committed Sep 11, 2024
1 parent bb7732d commit b72bdd9
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 92 deletions.
2 changes: 1 addition & 1 deletion examples/changeappid.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int main ( int argc, char **argv ) {
ret = dvNew(&dc, PROVIDER_URL, oldId, NULL);
if (ret != RUE_OK) break;
// check for easy SSL mode
if (ruStrCmp("1", ruGetenv("EASYSSL")) == 0) {
if (ruStrEquals("1", ruGetenv("EASYSSL"))) {
// disable certificate checks
ret = dvSetProp(dc, DV_SKIP_CERT_CHECK, "1");
if (ret) break;
Expand Down
8 changes: 4 additions & 4 deletions examples/datause.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ int main ( int argc, char **argv ) {
a1 = argv[1];
// -d turns on verbose debug logging
// -v turns on verbose debug logging with curl debug logging
if (ruStrCmp(a1, "-v") == 0 || ruStrCmp(a1, "-d") == 0) {
if (ruStrEquals(a1, "-v") || ruStrEquals(a1, "-d")) {
rsc = ruSinkCtxNew(logfile, NULL, NULL);
dvSetCleanLogger(ruFileLogSink, RU_LOG_VERB, rsc);
ruSetLogger(ruFileLogSink, RU_LOG_VERB, rsc, true, true);
} else {
a1 = NULL;
}
Expand All @@ -90,12 +90,12 @@ int main ( int argc, char **argv ) {
ret = dvNew(&dc, PROVIDER_URL, APPID, kvs);
if (ret) break;
// check for easy SSL mode
if (ruStrCmp("1", ruGetenv("EASYSSL")) == 0) {
if (ruStrEquals("1", ruGetenv("EASYSSL"))) {
// disable certificate checks
ret = dvSetProp(dc, DV_SKIP_CERT_CHECK, "1");
if (ret) break;
}
if (ruStrCmp(a1, "-v") == 0) {
if (ruStrEquals(a1, "-v")) {
// turn on curl debug logging
ret = dvSetProp(dc, DV_CURL_LOGGING, "1");
if (ret) break;
Expand Down
2 changes: 1 addition & 1 deletion examples/publish.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main ( int argc, char **argv ) {
if (ret != RUE_OK) break;

// check for easy SSL mode
if (ruStrCmp("1", ruGetenv("EASYSSL")) == 0) {
if (ruStrEquals("1", ruGetenv("EASYSSL"))) {
// disable certificate checks
ret = dvSetProp(dc, DV_SKIP_CERT_CHECK, "1");
if (ret) break;
Expand Down
29 changes: 0 additions & 29 deletions include/vaccinator.h
Original file line number Diff line number Diff line change
Expand Up @@ -591,20 +591,6 @@ DVAPI int32_t dvSetPostCb(dvCtx dc, dvPostCb callback, void* cbCtx);
*/
DVAPI const char* dvVersion(void);

/**
* \brief Sets the global logging function for this process.
*
* This function is like \ref ruSetLogger with the difference that log data has
* its credentials sanitized out. This is the preferred alternative to
* \ref ruSetLogger when using the \ref dvclient. You can specify
* credentials to be sanitized out by calling \ref dvSetProp with \ref DV_SECRET.
* @param logger Logging function that will be called with messages.
* @param logLevel Loglevel to determine what gets logged.
* @param userData Opaque custom user data that will be passed to the
* \ref ruLogFunc implementation.
*/
DVAPI void dvSetCleanLogger(ruLogFunc logger, uint32_t logLevel, perm_ptr userData);

/**
* \brief Constants used to set \ref dvclient context parameters.
*/
Expand All @@ -630,21 +616,6 @@ enum dvCtxOpt {
DV_APPVERSION,
/** File path to certificate authority file or directory. */
DV_CERT_PATH,
/**
* A secret to replace in the logs when calling \ref ruVerbLogf type functions.
* This happens when the logger is initialized by calling \ref dvSetCleanLogger.
* The secret is replaced with whatever the value of \ref DV_SECRET_PLACE_HOLDER
* is at the time of this call, not at the time the log call.
*/
DV_SECRET,
/**
* The place holder that will replace a given \ref DV_SECRET in a
* log cleaned using \ref ruVerbLogf. When this has not been set or set to
* NULL, it is replaced with \ref dvDefaultSecretPlaceHolder instead. This
* value is not thread safe and it is copied when \ref dvSetProp is called with
* \ref DV_SECRET, so it may be set to various values for given secrets.
*/
DV_SECRET_PLACE_HOLDER,
/**
* This will disable SSL certificate verification when set to non 0.
* Helpful when developing.
Expand Down
4 changes: 2 additions & 2 deletions lib/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ int32_t dvAes256Enc(trans_bytes key, const char* cs, const char* str, char** cip
rusize blen = 0; // payload base64 encoded set at run time

do {
ruVerbLogf("looking to recipe '%s'", str);
ruVerbLogf("looking to encrypt '%s'", str);
// get length estimates
// cipher text
ret = aesEnc(key, str, iv, NULL, &ciphsz);
Expand Down Expand Up @@ -532,7 +532,7 @@ int32_t dvAes256Dec(trans_bytes key, const char* cipherRecipe, char** data, char
do {
// recipe:cs:iv:encoding:payload
// aes-256-cbc:18:835cc...c20:b:YOB4WAENU9TmlIykp1VV0w==
ruVerbLogf("looking at recipe '%s'", cipherRecipe);
ruVerbLogf("looking at cipher recipe '%s'", cipherRecipe);
// sanity check
if (!ruStrStartsWith(cipherRecipe, "aes-256-cbc:", NULL)) {
dvSetError("recipe '%s' is incompatible", cipherRecipe);
Expand Down
30 changes: 12 additions & 18 deletions lib/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ static int32_t dvPost(dvCtx dc, const char* data, char** vid, ruList indexWords,
ruJsonSetKeyInt(jrq, "duration", durationDays);
}
if (indexWords) {
ret = encodeWordList(indexWords, jsn, "words");
ret = encodeWordList(indexWords, jrq, "words");
if (ret != RUE_OK) break;
}
perm_chars str = NULL;
ret = ruJsonWrite(jsn, &str);
ret = ruJsonWrite(jrq, &str);
if (ret != RUE_OK) break;

ret = newKvList(&kvl, JSON_FIELD, str, 0);
Expand Down Expand Up @@ -251,7 +251,7 @@ static int32_t doGet(dvCtx dc, ruList vids, ruMap* data, trans_chars passwd,
ret = encodeWordList(getvids, jrq, "vid");
if (ret != RUE_OK) break;
perm_chars str = NULL;
ret = ruJsonWrite(jsn, &str);
ret = ruJsonWrite(jrq, &str);
if (ret != RUE_OK) break;

ret = newKvList(&kvl, JSON_FIELD, str, 0);
Expand Down Expand Up @@ -330,11 +330,11 @@ static int32_t doUpdate(dvCtx dc, const char* vid, const char* data,
ruJsonSetKeyStr(jrq, "vid", vid);
ruJsonSetKeyStr(jrq, "data", cipher);
if (indexWords) {
ret = encodeWordList(indexWords, jsn, "words");
ret = encodeWordList(indexWords, jrq, "words");
if (ret != RUE_OK) break;
}
perm_chars str = NULL;
ret = ruJsonWrite(jsn, &str);
ret = ruJsonWrite(jrq, &str);
if (ret != RUE_OK) break;

ret = newKvList(&kvl, JSON_FIELD, str, 0);
Expand Down Expand Up @@ -528,10 +528,10 @@ DVAPI int32_t dvSearch(dvCtx dc, ruList searchWords, ruList* vids) {
ruJsonSetKeyStr(jrq, "op", "search");

do {
ret = encodeWordList(searchWords, jsn, "words");
ret = encodeWordList(searchWords, jrq, "words");
if (ret != RUE_OK) break;
perm_chars str = NULL;
ret = ruJsonWrite(jsn, &str);
ret = ruJsonWrite(jrq, &str);
if (ret != RUE_OK) break;

ret = newKvList(&kvl, JSON_FIELD, str, 0);
Expand Down Expand Up @@ -579,10 +579,10 @@ DVAPI int32_t dvDelete(dvCtx dc, ruList vids) {
ruJsonSetKeyStr(jrq, "op", "delete");

do {
ret = encodeWordList(vids, jsn, "vid");
ret = encodeWordList(vids, jrq, "vid");
if (ret != RUE_OK) break;
perm_chars str = NULL;
ret = ruJsonWrite(jsn, &str);
ret = ruJsonWrite(jrq, &str);
if (ret != RUE_OK) break;

ret = newKvList(&kvl, JSON_FIELD, str, 0);
Expand Down Expand Up @@ -627,8 +627,9 @@ DVAPI int32_t dvWipe(dvCtx dc, ruList vids) {
ruCritLogf("failed getting vid list from store. ec: %d", ret);
return ret;
}
} else if (ruListSize(vids, NULL) == -1) {
return RUE_INVALID_PARAMETER;
} else {
ruListSize(vids, &ret);
if (ret != RUE_OK) return ret;
}

ruIterator li = ruListIter(myvids);
Expand Down Expand Up @@ -815,13 +816,6 @@ DVAPI int dvSetProp(dvCtx dc, enum dvCtxOpt opt, const char* value) {
if (value)
ctx->certPath = ruStrDup(value);
break;
case DV_SECRET_PLACE_HOLDER:
ruFree(dvPwReplacement);
dvPwReplacement = ruStrDup(value);
break;
case DV_SECRET:
dvCleanerAdd(value);
break;
case DV_SKIP_CERT_CHECK:
if (!value || ruStrEquals(value, "0")) {
ruVerbLog("Enabling certificate verification");
Expand Down
25 changes: 2 additions & 23 deletions lib/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ DVAPI int32_t dvGetVid(ruMap vidMap, const char* vid, char** pid) {
dvGetRes gr = NULL;
int32_t ret = ruMapGet(vidMap, vid, &gr);
if (ret == RUE_OK && gr) {
if (*pid) *pid = gr->data;
if (pid) *pid = gr->data;
ret = gr->status;
}
return ret;
Expand All @@ -100,31 +100,10 @@ DVAPI int32_t dvGetVid(ruMap vidMap, const char* vid, char** pid) {
/******************************************************************************/
/* CLEAN LOGGER */
/******************************************************************************/
// password cleaner to store credentials in case caller wants to clean the logs.
static ruCleaner pwCleaner_ = NULL;
// The current dvPwReplacement of a secret when calling dvSetProp with DV_SECRET.
char *dvPwReplacement = dvDefaultSecretPlaceHolder;

static ruCleaner dvGetCleaner(void) {
if (!pwCleaner_) {
pwCleaner_ = ruCleanNew(0);
}
return pwCleaner_;
}

void dvCleanerAdd(trans_chars secret) {
if (dvPwReplacement) {
ruCleanAdd(dvGetCleaner(), secret, dvPwReplacement);
} else {
ruCleanAdd(dvGetCleaner(), secret, dvDefaultSecretPlaceHolder);
}
}

DVAPI void dvSetCleanLogger(ruLogFunc logger, uint32_t logLevel, perm_ptr userData) {
if (logger) {
ruSetLogger(logger, logLevel, userData, dvGetCleaner(), true);
} else {
ruSetLogger(NULL, logLevel, NULL, NULL, false);
}
ruCleanAdd(ruGetCleaner(), secret, dvDefaultSecretPlaceHolder);
}

9 changes: 8 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ set(VAULT_PW vaccinator CACHE STRING

list(APPEND CMAKE_PREFIX_PATH ${CHECK_BASE})
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/tests/")
find_package(Check REQUIRED)
if(MSVC)
set(CHECK_LIBRARIES check.lib)
elseif(MAC OR MINGW)
set(CHECK_LIBRARIES check)
else()
set(CHECK_LIBRARIES -lcheck -lm -lrt -lsubunit)
endif()
#find_package(Check REQUIRED)

if(WIN AND NOT MINGW)
add_compile_definitions(_CRT_SECURE_NO_DEPRECATE CURL_STATICLIB=ON)
Expand Down
2 changes: 1 addition & 1 deletion tests/caching.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ START_TEST ( run ) {
ret = dvNew(&dc, PROVIDER_URL, APPID, kvs);
fail_unless(exp == ret, retText, test, exp, ret);

if (ruStrCmp("1", ruGetenv("EASYSSL")) == 0) {
if (ruStrEquals("1", ruGetenv("EASYSSL"))) {
// disable certificate checks
test = "dvSetProp";
ret = dvSetProp(dc, DV_SKIP_CERT_CHECK, "1");
Expand Down
6 changes: 3 additions & 3 deletions tests/change.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ ruList search(dvCtx dc, const char* appId, const char* word, const char* searchv
for (char *vd = ruIterNext(li, char*); li;
vd = ruIterNext(li, char*)) {
ruVerbLogf("searchvids vid: '%s'", vd);
if (ruStrCmp(searchvid, vd) == 0) found = true;
if (ruStrEquals(searchvid, vd)) found = true;
}
fail_unless(true == found, retText, test, true, found);
}
Expand Down Expand Up @@ -183,7 +183,7 @@ void dochange(dvCtx dc, const char* appId, ruList vids, rusize cnt, const char*
vd = ruIterNext(li, char*)) {
ret = dvGetVid(vidMap, vd, &out);
ruVerbLogf("vid: '%s' data: '%s' status: %d", vd, out, ret);
if (ruStrCmp(misvid, vd) == 0) {
if (ruStrEquals(misvid, vd)) {
exp = DVE_INVALID_CREDENTIALS;
fail_unless(exp == ret, retText, test, exp, ret);
fail_unless(NULL == out, retText, test, NULL, out);
Expand Down Expand Up @@ -241,7 +241,7 @@ START_TEST ( change ) {
ret = dvNew(&dc, PROVIDER_URL, oldId, NULL);
fail_unless(exp == ret, retText, test, exp, ret);

if (ruStrCmp("1", ruGetenv("EASYSSL")) == 0) {
if (ruStrEquals("1", ruGetenv("EASYSSL"))) {
// disable certificate checks
test = "dvSetProp";
ret = dvSetProp(dc, DV_SKIP_CERT_CHECK, "1");
Expand Down
8 changes: 3 additions & 5 deletions tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@

int main ( int argc, char **argv ) {
int32_t number_failed;
if (argc > 1 && ruStrCmp(argv[1], "-v") == 0) {
ruSetLogger(ruStdErrLogSink, RU_LOG_DBUG, NULL,
NULL, false);
if (argc > 1 && ruStrEquals(argv[1], "-v")) {
ruSetLogger(ruStdErrLogSink, RU_LOG_DBUG, NULL, false, false);
} else {
ruSetLogger(ruStdErrLogSink, RU_LOG_INFO, NULL,
NULL, false);
ruSetLogger(ruStdErrLogSink, RU_LOG_INFO, NULL, false, false);
}
Suite *suite = suite_create("vaccinator");
suite_add_tcase(suite, cipherTests());
Expand Down
8 changes: 4 additions & 4 deletions tests/vacc.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ START_TEST ( run ) {
ret = dvNew(&dc, PROVIDER_URL, APPID, NULL);
fail_unless(exp == ret, retText, test, exp, ret);

if (ruStrCmp("1", ruGetenv("EASYSSL")) == 0) {
if (ruStrEquals("1", ruGetenv("EASYSSL"))) {
// disable certificate checks
test = "dvSetProp";
ret = dvSetProp(dc, DV_SKIP_CERT_CHECK, "1");
fail_unless(exp == ret, retText, test, exp, ret);
fail_unless(exp == ret, retText, test, exp, ret);
}

test = "dvSetHeaderCb";
Expand Down Expand Up @@ -312,7 +312,7 @@ START_TEST ( run ) {
bool found = false;
for(char* vd = ruIterNext(li, char*); li; vd = ruIterNext(li, char*)) {
ruVerbLogf("fndVids2 vid: '%s'", vd);
if (ruStrCmp(fovid, vd) == 0) found = true;
if (ruStrEquals(fovid, vd)) found = true;
}
fail_unless(true == found, retText, test, true, found);

Expand Down Expand Up @@ -364,7 +364,7 @@ START_TEST ( publish ) {
ret = dvNew(&dc, PROVIDER_URL, APPID, NULL);
fail_unless(exp == ret, retText, test, exp, ret);

if (ruStrCmp("1", ruGetenv("EASYSSL")) == 0) {
if (ruStrEquals("1", ruGetenv("EASYSSL"))) {
// disable certificate checks
test = "dvSetProp";
ret = dvSetProp(dc, DV_SKIP_CERT_CHECK, "1");
Expand Down

0 comments on commit b72bdd9

Please sign in to comment.