Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple C++ test #3046

Merged
merged 9 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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
10 changes: 6 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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 Expand Up @@ -154,10 +156,10 @@ jobs:
run: cargo test -p test_array --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_bcrypt
run: cargo test -p test_bcrypt --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_bstr
run: cargo test -p test_bstr --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Clean
run: cargo clean
- name: Test test_bstr
run: cargo test -p test_bstr --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_calling_convention
run: cargo test -p test_calling_convention --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_cfg_generic
Expand Down Expand Up @@ -256,10 +258,10 @@ jobs:
run: cargo test -p test_sys --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_targets
run: cargo test -p test_targets --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_unions
run: cargo test -p test_unions --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Clean
run: cargo clean
- name: Test test_unions
run: cargo test -p test_unions --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_variant
run: cargo test -p test_variant --target ${{ matrix.target }} ${{ matrix.etc }}
- name: Test test_wdk
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();
}
}