Skip to content

Commit 746a79c

Browse files
authored
Merge pull request #23 from bbockelm/fix_gtest
Fix gtest APIs and test cases
2 parents aa1fe99 + 163c729 commit 746a79c

File tree

7 files changed

+50
-8
lines changed

7 files changed

+50
-8
lines changed

.github/workflows/ccpp.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
# Note the current convention is to use the -S and -B options here to specify source
3939
# and build directories, but this is only available with CMake 3.13 and higher.
4040
# The CMake binaries on the Github Actions machines are (as of this writing) 3.12
41-
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE
41+
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DBUILD_UNITTESTS=yes
4242

4343
- name: Build
4444
working-directory: ${{runner.workspace}}/build
@@ -51,6 +51,6 @@ jobs:
5151
shell: bash
5252
# Execute tests defined by the CMake configuration.
5353
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
54-
run: ctest -C $BUILD_TYPE
54+
run: ctest -C $BUILD_TYPE --verbose
5555

5656

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,9 @@ if( BUILD_UNITTESTS )
7979
include(ExternalProject)
8080
ExternalProject_Add(gtest
8181
PREFIX external/gtest
82-
SOURCE_DIR ${PROJECT_SOURCE_DIR}/vendor/gtest
83-
BUILD_BYPRODUCTS ${CMAKE_BINARY_DIR}/external/gtest/src/gtest-build/lib/libgtest.a
82+
URL file://${PROJECT_SOURCE_DIR}/vendor/gtest
8483
INSTALL_COMMAND :
8584
)
86-
8785
enable_testing()
8886
add_subdirectory(test)
8987
endif()

src/scitokens.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ int scitoken_set_claim_string(SciToken token, const char *key, const char *value
6767
}
6868

6969

70+
void scitoken_set_serialize_profile(SciToken token, SciTokenProfile profile) {
71+
scitoken_set_serialize_mode(token, profile);
72+
}
73+
74+
7075
void scitoken_set_serialize_mode(SciToken token, SciTokenProfile profile) {
7176
scitokens::SciToken *real_token = reinterpret_cast<scitokens::SciToken*>(token);
7277
if (real_token == nullptr) {return;}
@@ -75,6 +80,14 @@ void scitoken_set_serialize_mode(SciToken token, SciTokenProfile profile) {
7580
}
7681

7782

83+
void scitoken_set_deserialize_profile(SciToken token, SciTokenProfile profile) {
84+
scitokens::SciToken *real_token = reinterpret_cast<scitokens::SciToken*>(token);
85+
if (real_token == nullptr) {return;}
86+
87+
real_token->set_deserialize_mode(static_cast<scitokens::SciToken::Profile>(profile));
88+
}
89+
90+
7891
int scitoken_get_claim_string(const SciToken token, const char *key, char **value, char **err_msg) {
7992
scitokens::SciToken *real_token = reinterpret_cast<scitokens::SciToken*>(token);
8093
std::string claim_str;
@@ -150,6 +163,18 @@ int scitoken_deserialize(const char *value, SciToken *token, char const* const*
150163
scitokens::SciTokenKey key;
151164
scitokens::SciToken *real_token = new scitokens::SciToken(key);
152165

166+
int retval = scitoken_deserialize_v2(value, reinterpret_cast<SciToken>(real_token), allowed_issuers, err_msg);
167+
if (retval) {
168+
delete real_token;
169+
} else {
170+
*token = real_token;
171+
}
172+
return retval;
173+
}
174+
175+
int scitoken_deserialize_v2(const char *value, SciToken token, char const* const* allowed_issuers, char **err_msg) {
176+
scitokens::SciToken *real_token = reinterpret_cast<scitokens::SciToken*>(token);
177+
153178
std::vector<std::string> allowed_issuers_vec;
154179
if (allowed_issuers != nullptr) {
155180
for (int idx=0; allowed_issuers[idx]; idx++) {
@@ -165,7 +190,6 @@ int scitoken_deserialize(const char *value, SciToken *token, char const* const*
165190
}
166191
return -1;
167192
}
168-
*token = real_token;
169193
return 0;
170194
}
171195

src/scitokens.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,16 @@ int scitoken_serialize(const SciToken token, char **value, char **err_msg);
5757
* Set the profile used for serialization; if COMPAT mode is used, then
5858
* the library default is utilized (currently, scitokens 1.0).
5959
*/
60+
void scitoken_set_serialize_profile(SciToken token, SciTokenProfile profile);
61+
6062
void scitoken_set_serialize_mode(SciToken token, SciTokenProfile profile);
6163

64+
void scitoken_set_deserialize_profile(SciToken token, SciTokenProfile profile);
65+
6266
int scitoken_deserialize(const char *value, SciToken *token, char const* const* allowed_issuers, char **err_msg);
6367

68+
int scitoken_deserialize_v2(const char *value, SciToken token, char const* const* allowed_issuers, char **err_msg);
69+
6470
int scitoken_store_public_ec_key(const char *issuer, const char *keyid, const char *value, char **err_msg);
6571

6672
Validator validator_create();

src/scitokens_internal.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ SciToken::deserialize(const std::string &data, const std::vector<std::string> al
341341
scitokens::Validator val;
342342
val.add_allowed_issuers(allowed_issuers);
343343
val.set_validate_all_claims_scitokens_1(false);
344+
val.set_validate_profile(m_deserialize_profile);
344345
val.verify(*m_decoded);
345346

346347
// Set all the claims

src/scitokens_internal.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ friend class scitokens::Validator;
134134
m_serialize_profile = profile;
135135
}
136136

137+
void
138+
set_deserialize_mode(Profile profile) {
139+
m_deserialize_profile = profile;
140+
}
141+
137142
const jwt::claim
138143
get_claim(const std::string &key) {
139144
return m_claims[key];
@@ -182,7 +187,7 @@ friend class scitokens::Validator;
182187
m_claims["aud"] = std::string("ANY");
183188
}
184189
} else if (m_serialize_profile == Profile::WLCG_1_0) {
185-
m_claims["wlcg_ver"] = std::string("1.0");
190+
m_claims["wlcg.ver"] = std::string("1.0");
186191
auto iter = m_claims.find("aud");
187192
if (iter == m_claims.end()) {
188193
m_claims["aud"] = std::string("https://wlcg.cern.ch/jwt/v1/any");
@@ -205,6 +210,7 @@ friend class scitokens::Validator;
205210
int m_lifetime{600};
206211
Profile m_profile{Profile::SCITOKENS_1_0};
207212
Profile m_serialize_profile{Profile::COMPAT};
213+
Profile m_deserialize_profile{Profile::COMPAT};
208214
std::unordered_map<std::string, jwt::claim> m_claims;
209215
std::unique_ptr<jwt::decoded_jwt> m_decoded;
210216
SciTokenKey &m_key;

test/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,11 @@ add_executable(scitokens-gtest main.cpp)
33
add_dependencies(scitokens-gtest gtest)
44
include_directories("${PROJECT_SOURCE_DIR}/vendor/gtest/googletest/include")
55

6-
target_link_libraries(scitokens-gtest SciTokens "${CMAKE_BINARY_DIR}/external/gtest/src/gtest-build/lib/libgtest.a")
6+
target_link_libraries(scitokens-gtest SciTokens "${CMAKE_BINARY_DIR}/external/gtest/src/gtest-build/lib/libgtest.a" -lpthread)
7+
8+
add_test(
9+
NAME
10+
unit
11+
COMMAND
12+
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/test/scitokens-gtest
13+
)

0 commit comments

Comments
 (0)