Skip to content

Commit

Permalink
Add C++ test client (#3046)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored May 20, 2024
1 parent 1172cf2 commit 8a2db86
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ jobs:
run: cargo clippy -p sample_component_hello_world
- name: Clippy sample_component_json_validator
run: cargo clippy -p sample_component_json_validator
- name: Clippy sample_component_json_validator_client
run: cargo clippy -p sample_component_json_validator_client
- name: Clippy sample_component_json_validator_winrt
run: cargo clippy -p sample_component_json_validator_winrt
- name: Clippy sample_component_json_validator_winrt_client
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ jobs:
run: cargo test -p sample_component_hello_world --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test sample_component_json_validator
run: cargo test -p sample_component_json_validator --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test sample_component_json_validator_client
run: cargo test -p sample_component_json_validator_client --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test sample_component_json_validator_winrt
run: cargo test -p sample_component_json_validator_winrt --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test sample_component_json_validator_winrt_client
Expand Down
16 changes: 16 additions & 0 deletions crates/samples/components/json_validator_client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "sample_component_json_validator_client"
version = "0.0.0"
edition = "2021"
publish = false

[build-dependencies]
cc = "1.0"

[dependencies.windows-targets]
path = "../../../../crates/libs/targets"

# TODO: this causes a warning about lack of linkage target. The point is to ensure that this binary dependency is built first but
# Cargo doesn't respect cdylib targets. https://github.com/rust-lang/cargo/issues/7825
[dependencies.sample_component_json_validator]
path = "../json_validator"
14 changes: 14 additions & 0 deletions crates/samples/components/json_validator_client/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn main() {
if !cfg!(target_env = "msvc") {
return;
}

println!("cargo:rerun-if-changed=src/client.cpp");
println!("cargo:rustc-link-lib=windows.0.52.0");

cc::Build::new()
.cpp(true)
.std("c++20")
.file("src/client.cpp")
.compile("client");
}
44 changes: 44 additions & 0 deletions crates/samples/components/json_validator_client/src/client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <stdint.h>
#include <assert.h>
#include <windows.h>
#include <string_view>

typedef HRESULT (__stdcall *CreateJsonValidator)(char const* schema, size_t schema_len, uintptr_t* handle);

typedef HRESULT (__stdcall *ValidateJson)(uintptr_t handle, char const* value, size_t value_len, char** sanitized_value, size_t* sanitized_value_len);

typedef void (__stdcall *CloseJsonValidator)(uintptr_t handle);

extern "C" {
void __stdcall client() {
auto library = LoadLibraryExW(L"sample_component_json_validator.dll", 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
assert(library != 0);

auto create = reinterpret_cast<CreateJsonValidator>(GetProcAddress(library, "CreateJsonValidator"));
assert(create);

auto validate = reinterpret_cast<ValidateJson>(GetProcAddress(library, "ValidateJson"));
assert(validate);

auto close = reinterpret_cast<CloseJsonValidator>(GetProcAddress(library, "CloseJsonValidator"));
assert(close);

std::string_view schema = "{\"maxLength\": 5}";
std::string_view json = "\"Hello\" "; // trailing space will be removed from sanitized result
std::string_view json_invalid = "\"Hello world\""; // this json is too long

uintptr_t validator = 0;
assert(S_OK == create(schema.data(), schema.size(), &validator));

char* sanitized_value = nullptr;
size_t sanitized_value_len = 0;
assert(S_OK == validate(validator, json.data(), json.size(), &sanitized_value, &sanitized_value_len));
std::string_view sanitized(sanitized_value, sanitized_value_len);
assert(sanitized == "\"Hello\"");
CoTaskMemFree(sanitized_value);

assert(E_INVALIDARG == validate(validator, json_invalid.data(), json_invalid.size(), &sanitized_value, &sanitized_value_len));

close(validator);
}
}
11 changes: 11 additions & 0 deletions crates/samples/components/json_validator_client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![cfg(target_env = "msvc")]

#[test]
fn test() {
extern "system" {
fn client();
}
unsafe {
client();
}
}

0 comments on commit 8a2db86

Please sign in to comment.