Skip to content

Commit df5f671

Browse files
netheril96QZHelen
authored andcommitted
Add test for pnv
1 parent 043c5a7 commit df5f671

File tree

10 files changed

+302
-2
lines changed

10 files changed

+302
-2
lines changed

matcher/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
cmake_minimum_required(VERSION 3.30)
2+
project(Matcher)
3+
4+
# Add include directories
5+
include_directories(. cJSON)
6+
7+
# Common library source files
8+
set(COMMON_LIB_SRCS
9+
cJSON/cJSON.c
10+
credentialmanager.c
11+
base64.c
12+
)
13+
14+
set(CMAKE_CXX_STANDARD 17)
15+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
16+
17+
# Target: openid4vp1_0.wasm
18+
add_library(openid4vp1_0 openid4vp1_0.c dcql.c ${COMMON_LIB_SRCS})
19+
set_target_properties(openid4vp1_0 PROPERTIES SUFFIX ".wasm")
20+
21+
# Target: issuance_provision.wasm
22+
add_library(issuance_provision issuance/provision.c dcql.c ${COMMON_LIB_SRCS})
23+
set_target_properties(issuance_provision PROPERTIES SUFFIX ".wasm")
24+
25+
# Target: pnv_openid4vp1_0.wasm
26+
add_library(pnv_openid4vp1_0 pnv/openid4vp1_0.c pnv/dcql.c ${COMMON_LIB_SRCS})
27+
set_target_properties(pnv_openid4vp1_0 PROPERTIES SUFFIX ".wasm")
28+
29+
# Native library for testing
30+
add_library(pnv_openid4vp1_0_native pnv/openid4vp1_0.c pnv/dcql.c ${COMMON_LIB_SRCS})
31+
32+
# Googletest setup
33+
enable_testing()
34+
35+
find_package(doctest REQUIRED)
36+
37+
# Test for pnv/openid4vp1_0
38+
add_executable(pnv_openid4vp1_0_test pnv/test/openid4vp1_0_test.cpp pnv/test/common.cpp)
39+
target_link_libraries(pnv_openid4vp1_0_test PRIVATE doctest::doctest pnv_openid4vp1_0_native)
40+
add_test(NAME pnv_openid4vp1_0_test COMMAND pnv_openid4vp1_0_test)
41+

matcher/base64.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#ifndef BASE64_H
22
#define BASE64_H
33

4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
48
int B64DecodeURL(char* input, char** output);
59

10+
#ifdef __cplusplus
11+
}
612
#endif
13+
14+
#endif

matcher/credentialmanager.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef CREDENTIALMANAGER_H
22
#define CREDENTIALMANAGER_H
33

4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
48
#include <stdint.h>
59
#include <stdlib.h>
610

@@ -111,4 +115,8 @@ __attribute__((import_module("credman_v4"), import_name("SelfDeclarePackageInfo"
111115
#endif
112116
void SelfDeclarePackageInfo(const char* package_display_name, const char* package_icon, size_t package_icon_len);
113117

114-
#endif
118+
#ifdef __cplusplus
119+
}
120+
#endif
121+
122+
#endif

matcher/dcql.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
#ifndef DCQL_H
22
#define DCQL_H
33

4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
48
#include "cJSON/cJSON.h"
59

610
cJSON* dcql_query(const int request_id, cJSON* query, cJSON* credential_store);
711

8-
#endif
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif

matcher/pnv/openid4vp1_0.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ cJSON *GetCredsJson()
3131
return cJSON_Parse(creds_json);
3232
}
3333

34+
#if defined(__wasm__)
3435
int main()
36+
#else
37+
int openid_main()
38+
#endif
3539
{
3640
uint32_t credentials_size;
3741
GetCredentialsSize(&credentials_size);

matcher/pnv/test/common.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include "credentialmanager.h"
2+
#include "common.hpp"
3+
4+
#include <string.h>
5+
#include <algorithm>
6+
7+
TestCredmanState& TestCredmanState::instance(){
8+
static TestCredmanState state;
9+
return state;
10+
}
11+
12+
extern "C"
13+
{
14+
void GetCredentialsSize(uint32_t *size)
15+
{
16+
*size = (uint32_t)TestCredmanState::instance().credentials_buffer.size();
17+
}
18+
size_t ReadCredentialsBuffer(void *buffer, size_t offset, size_t len)
19+
{
20+
if (offset + len > TestCredmanState::instance().credentials_buffer.size())
21+
{
22+
len = TestCredmanState::instance().credentials_buffer.size() - offset;
23+
}
24+
memcpy(buffer, TestCredmanState::instance().credentials_buffer.data() + offset, len);
25+
return len;
26+
}
27+
void GetWasmVersion(uint32_t *version)
28+
{
29+
*version = -1;
30+
}
31+
void GetRequestSize(uint32_t *size)
32+
{
33+
*size = (uint32_t)TestCredmanState::instance().request_buffer.size();
34+
}
35+
void GetRequestBuffer(void *buffer)
36+
{
37+
memcpy(buffer, TestCredmanState::instance().request_buffer.data(), TestCredmanState::instance().request_buffer.size());
38+
}
39+
40+
void AddStringIdEntry(char *cred_id, char* icon, size_t icon_len, char *title, char *subtitle, char *disclaimer, char *warning) {
41+
if (!cred_id) return;
42+
StringIdEntry entry;
43+
entry.id = cred_id;
44+
if (icon) entry.icon = std::string(icon, icon_len);
45+
if (title) entry.title = title;
46+
if (subtitle) entry.subtitle = subtitle;
47+
if (disclaimer) entry.disclaimer = disclaimer;
48+
if (warning) entry.warning = warning;
49+
TestCredmanState::instance().string_id_entries.push_back(entry);
50+
}
51+
52+
void SetAdditionalDisclaimerAndUrlForVerificationEntry(char *cred_id, char *secondary_disclaimer, char *url_display_text, char *url_value) {
53+
if (!cred_id) return;
54+
auto& entries = TestCredmanState::instance().string_id_entries;
55+
auto it = std::find_if(entries.begin(), entries.end(), [&](const StringIdEntry& entry) {
56+
return entry.id == cred_id;
57+
});
58+
if (it != entries.end()) {
59+
if (secondary_disclaimer) it->secondary_disclaimer = secondary_disclaimer;
60+
if (url_display_text) it->url_display_text = url_display_text;
61+
if (url_value) it->url_value = url_value;
62+
}
63+
}
64+
65+
void AddFieldForStringIdEntry(char *cred_id, char *field_display_name, char *field_display_value) {
66+
if (!cred_id) return;
67+
auto& entries = TestCredmanState::instance().string_id_entries;
68+
auto it = std::find_if(entries.begin(), entries.end(), [&](const StringIdEntry& entry) {
69+
return entry.id == cred_id;
70+
});
71+
if (it != entries.end()) {
72+
it->fields.emplace_back(
73+
field_display_name ? field_display_name : "",
74+
field_display_value ? field_display_value : ""
75+
);
76+
}
77+
}
78+
79+
void AddPaymentEntry(char *cred_id, char *merchant_name, char *payment_method_name, char *payment_method_subtitle, char* payment_method_icon, size_t payment_method_icon_len, char *transaction_amount, char* bank_icon, size_t bank_icon_len, char* payment_provider_icon, size_t payment_provider_icon_len) {
80+
if (!cred_id) return;
81+
PaymentEntry entry;
82+
entry.id = cred_id;
83+
if (merchant_name) entry.merchant_name = merchant_name;
84+
if (payment_method_name) entry.payment_method_name = payment_method_name;
85+
if (payment_method_subtitle) entry.payment_method_subtitle = payment_method_subtitle;
86+
if (payment_method_icon) entry.payment_method_icon = std::string(payment_method_icon, payment_method_icon_len);
87+
if (transaction_amount) entry.transaction_amount = transaction_amount;
88+
if (bank_icon) entry.bank_icon = std::string(bank_icon, bank_icon_len);
89+
if (payment_provider_icon) entry.payment_provider_icon = std::string(payment_provider_icon, payment_provider_icon_len);
90+
TestCredmanState::instance().payment_entries.push_back(entry);
91+
}
92+
}

matcher/pnv/test/common.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
#pragma once
3+
#include <string>
4+
#include <vector>
5+
#include <map>
6+
7+
struct StringIdEntry {
8+
std::string id;
9+
std::string icon;
10+
std::string title;
11+
std::string subtitle;
12+
std::string disclaimer;
13+
std::string warning;
14+
std::string secondary_disclaimer;
15+
std::string url_display_text;
16+
std::string url_value;
17+
std::vector<std::pair<std::string, std::string>> fields;
18+
};
19+
20+
struct PaymentEntry {
21+
std::string id;
22+
std::string merchant_name;
23+
std::string payment_method_name;
24+
std::string payment_method_subtitle;
25+
std::string payment_method_icon;
26+
std::string transaction_amount;
27+
std::string bank_icon;
28+
std::string payment_provider_icon;
29+
};
30+
31+
struct TestCredmanState {
32+
std::string request_buffer, credentials_buffer;
33+
std::vector<StringIdEntry> string_id_entries;
34+
std::vector<PaymentEntry> payment_entries;
35+
36+
static TestCredmanState& instance();
37+
};
38+
39+
extern TestCredmanState testCredmanState;
3.37 KB
Binary file not shown.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"requests": [
3+
{
4+
"protocol": "openid4vp-v1-unsigned",
5+
"data": {
6+
"dcql_query": {
7+
"credentials": [
8+
{
9+
"claims": [
10+
{
11+
"path": [
12+
"subscription_hint"
13+
],
14+
"values": [
15+
2
16+
]
17+
},
18+
{
19+
"path": [
20+
"carrier_hint"
21+
],
22+
"values": [
23+
"310250"
24+
]
25+
},
26+
{
27+
"path": [
28+
"android_carrier_hint"
29+
],
30+
"values": [
31+
3
32+
]
33+
},
34+
{
35+
"path": [
36+
"phone_number_hint"
37+
],
38+
"values": [
39+
"+16502154321"
40+
]
41+
}
42+
],
43+
"format": "dc-authorization+sd-jwt",
44+
"id": "aggregator1",
45+
"meta": {
46+
"credential_authorization_jwt": "eyJhbGciOiJFUzI1NiIsInR5cCI6Im9hdXRoLWF1dGh6LXJlcStqd3QiLCJ4NWMiOlsiTUlJQ3BUQ0NBa3VnQXdJQkFnSVVDOWZOSnBkVU1RWWRCbDFuaDgrUml0UndNRDh3Q2dZSUtvWkl6ajBFQXdJd2VERUxNQWtHQTFVRUJoTUNWVk14RXpBUkJnTlZCQWdNQ2tOaGJHbG1iM0p1YVdFeEZqQVVCZ05WQkFjTURVMXZkVzUwWVdsdUlGWnBaWGN4R3pBWkJnTlZCQW9NRWtWNFlXMXdiR1VnUVdkbmNtVm5ZWFJ2Y2pFZk1CMEdBMVVFQXd3V1pYaGhiWEJzWlMxaFoyZH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25zZV9lbmNfdmFsdWVzX3N1cHBvcnRlZCI6IFsiQTEyOEdDTSJdLCAiandrcyI6IHsia2V5cyI6IFt7Imt0eSI6ICJFQyIsICJ1c2UiOiAiZW5jIiwgImFsZyI6ICJFQ0RILUVTIiwgImtpZCI6ICIxIiwgImNydiI6ICJQLTI1NiIsICJ4IjogIjl5TGgtNkJJQ1pMUWdKcGEzdl9FQS1ZbkIyU2FhV1BLWGZQWGNKa2EwMGciLCAieSI6ICJKNkRFWXV5SW90NDM0WG5WOE5GTWppb1cxLUFtSkVCRHdwTW9wRUt4WUdrIn1dfSwgImNvbnNlbnRfZGF0YSI6ICJleUpqYjI1elpXNTBYM1JsZUhRaU9pQWlVbWxrWlhJZ2NISnZZMlZ6YzJWeklIbHZkWElnY0dWeWMyOXVZV3dnWkdGMFlTQmhZMk52Y21ScGJtY2dkRzhnYjNWeUlIQnlhWFpoWTNrZ2NHOXNhV041SWl3Z0luQnZiR2xqZVY5c2FXNXJJam9nSW1oMGRIQnpPaTh2WkdWMlpXeHZjR1Z5TG1GdVpISnZhV1F1WTI5dEwybGtaVzUwYVhSNUwyUnBaMmwwWVd3dFkzSmxaR1Z1ZEdsaGJITXZZM0psWkdWdWRHbGhiQzEyWlhKcFptbGxjaUlzSUNKd2IyeHBZM2xmZEdWNGRDSTZJQ0pNWldGeWJpQmhZbTkxZENCd2NtbDJZV041SUhCdmJHbGplU0o5IiwgInN0YXRlIjogIm9wdGlvbmFsX3N0YXRlX3ZhbHVlIn0.w7_X5hLwjDxw26GguGjxuJnhxfcmqtbcCPiTobUrGpoFIvYWat9Luqi5r8ZTu_CIfC3rismGsYZH6ozNQwXgnw",
47+
"vct_values": [
48+
"number-verification/device-phone-number/ts43"
49+
]
50+
}
51+
}
52+
]
53+
},
54+
"nonce": "kk43JKaPscajjXp34fR8puHjtPMrcOB32K5wKMD5Cbw",
55+
"response_mode": "dc_api",
56+
"response_type": "vp_token"
57+
}
58+
}
59+
]
60+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
2+
#include <doctest/doctest.h>
3+
4+
#include "common.hpp"
5+
6+
#include <filesystem>
7+
#include <fstream>
8+
#include <string>
9+
#include <sstream> // Required for std::ostringstream
10+
11+
extern "C" int openid_main();
12+
13+
// Helper function to get the path to a test data file relative to the current source file
14+
std::string getTestDataPath(const std::string &relative_path)
15+
{
16+
std::filesystem::path source_path = __FILE__;
17+
std::filesystem::path source_dir = source_path.parent_path();
18+
return (source_dir / relative_path).string();
19+
}
20+
21+
// Helper function to read the entire content of a file into a std::string
22+
std::string readFileToString(const std::string &file_path)
23+
{
24+
std::ifstream input_file(file_path, std::ios::binary);
25+
if (!input_file.is_open())
26+
{
27+
return ""; // Return empty string if file cannot be opened
28+
}
29+
std::ostringstream ss;
30+
ss << input_file.rdbuf();
31+
return ss.str();
32+
}
33+
34+
TEST_CASE("OpenID4VP")
35+
{
36+
TestCredmanState::instance().credentials_buffer = readFileToString(getTestDataPath("data/pnv_registry.bin"));
37+
TestCredmanState::instance().request_buffer = readFileToString(getTestDataPath("data/request1.json"));
38+
REQUIRE_EQ(0, openid_main());
39+
REQUIRE(TestCredmanState::instance().string_id_entries.size() == 1);
40+
}

0 commit comments

Comments
 (0)