From: Stephan Bosch Date: Sun, 9 Dec 2018 20:18:22 +0000 (+0100) Subject: lib-oauth2: Provide a unit test for the JSON response payload parser. X-Git-Tag: 2.3.6~44 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=543bed6e85759fa0461d5b476d70ee08a01054ce;p=thirdparty%2Fdovecot%2Fcore.git lib-oauth2: Provide a unit test for the JSON response payload parser. --- diff --git a/src/lib-oauth2/Makefile.am b/src/lib-oauth2/Makefile.am index 79ade626b3..af09349404 100644 --- a/src/lib-oauth2/Makefile.am +++ b/src/lib-oauth2/Makefile.am @@ -1,5 +1,6 @@ AM_CPPFLAGS = \ -I$(top_srcdir)/src/lib \ + -I$(top_srcdir)/src/lib-test \ -I$(top_srcdir)/src/lib-http \ -I$(top_srcdir)/src/lib-settings @@ -19,6 +20,37 @@ liboauth2_la_SOURCES = \ oauth2-introspect.c \ oauth2-refresh.c -check_programs = \ - oauth2-server \ - test-oauth2 +test_programs = \ + test-oauth2-json + +noinst_PROGRAMS = $(test_programs) + +test_libs = \ + $(noinst_LTLIBRARIES) \ + ../lib-http/libhttp.la \ + ../lib-dns/libdns.la \ + ../lib-ssl-iostream/libssl_iostream.la \ + ../lib-master/libmaster.la \ + ../lib-settings/libsettings.la \ + ../lib-test/libtest.la \ + ../lib/liblib.la \ + $(MODULE_LIBS) +test_deps = \ + $(noinst_LTLIBRARIES) \ + ../lib-http/libhttp.la \ + ../lib-dns/libdns.la \ + ../lib-ssl-iostream/libssl_iostream.la \ + ../lib-master/libmaster.la \ + ../lib-settings/libsettings.la \ + ../lib-test/libtest.la \ + ../lib/liblib.la + +test_oauth2_json_SOURCES = test-oauth2-json.c +test_oauth2_json_LDADD = $(test_libs) +test_oauth2_json_DEPENDENCIES = $(test_deps) + +check-local: + for bin in $(test_programs); do \ + if ! $(RUN_TEST) ./$$bin; then exit 1; fi; \ + done + diff --git a/src/lib-oauth2/test-oauth2-json.c b/src/lib-oauth2/test-oauth2-json.c new file mode 100644 index 0000000000..ac271c1cbb --- /dev/null +++ b/src/lib-oauth2/test-oauth2-json.c @@ -0,0 +1,105 @@ +/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "array.h" +#include "json-parser.h" +#include "oauth2.h" +#include "oauth2-private.h" +#include "test-common.h" + +static void +test_oauth_json_valid_parsed(struct oauth2_request *req ATTR_UNUSED, + bool success, const char *error ATTR_UNUSED) +{ + test_assert(success); +} + +static void test_oauth2_json_valid(void) +{ + static const char *test_input = + "{\"access_token\":\"9a2dea3c-f8be-4271-b9c8-5b37da4f2f7e\"," + "\"grant_type\":\"authorization_code\"," + "\"openid\":\"\"," + "\"scope\":[\"openid\",\"profile\",\"email\"]," + "\"profile\":\"\"," + "\"realm\":\"/employees\"," + "\"token_type\":\"Bearer\"," + "\"expires_in\":2377," + "\"client_id\":\"mosaic\"," + "\"email\":\"\"," + "\"extensions\":" + "{\"algorithm\":\"cuttlefish\"," + "\"tentacles\":8" + "}" + "}"; + static const struct oauth2_field fields[] = { + { .name = "access_token", + .value = "9a2dea3c-f8be-4271-b9c8-5b37da4f2f7e" }, + { .name = "grant_type", + .value = "authorization_code" }, + { .name = "openid", + .value = "" }, + { .name = "profile", + .value = "" }, + { .name = "realm", + .value = "/employees" }, + { .name = "token_type", + .value = "Bearer" }, + { .name = "expires_in", + .value = "2377" }, + { .name = "client_id", + .value = "mosaic" }, + { .name = "email", + .value = "" }, + }; + static const unsigned int fields_count = N_ELEMENTS(fields); + struct oauth2_request *req; + const struct oauth2_field *pfields; + unsigned int count, i; + pool_t pool; + size_t pos; + + test_begin("oauth json skip"); + + /* Create mock request */ + pool = pool_alloconly_create_clean("oauth2 json test", 1024); + req = p_new(pool, struct oauth2_request, 1); + req->pool = pool; + req->valid = TRUE; + p_array_init(&req->fields, req->pool, 1); + req->is = test_istream_create_data(test_input, strlen(test_input)); + req->parser = json_parser_init(req->is); + req->json_parsed_cb = test_oauth_json_valid_parsed; + + /* Parse the JSON response */ + for (pos = 0; pos <= strlen(test_input); pos +=2) { + test_istream_set_size(req->is, pos); + oauth2_parse_json(req); + if (req->is == NULL) + break; + } + + /* Verify the parsed fields */ + pfields = array_get(&req->fields, &count); + test_assert(count == fields_count); + if (count > fields_count) + count = fields_count; + for (i = 0; i < count; i++) { + test_assert(strcmp(pfields[i].name, fields[i].name) == 0); + test_assert(strcmp(pfields[i].value, fields[i].value) == 0); + } + + /* Clean up */ + pool_unref(&req->pool); + + test_end(); +} + +int main(void) +{ + static void (*const test_functions[])(void) = { + test_oauth2_json_valid, + NULL + }; + return test_run(test_functions); +}