injectorpp 0.4.0

Injectorpp is a powerful tool designed to facilitate the writing of unit tests without the need to introduce traits solely for testing purposes. It streamlines the testing process by providing a seamless and efficient way to abstract dependencies, ensuring that your code remains clean and maintainable.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# Injectorpp for rust

[![CI](https://github.com/microsoft/injectorppforrust/actions/workflows/ci.yml/badge.svg)](https://github.com/microsoft/injectorppforrust/actions/workflows/ci.yml)
[![Crates.io](https://img.shields.io/crates/v/injectorpp.svg)](https://crates.io/crates/injectorpp)
[![Documentation](https://docs.rs/injectorpp/badge.svg)](https://docs.rs/injectorpp)

Injectorpp for rust is a crate that allows you to change rust function behavior at runtime without adding additional traits or code changes. It introduces an innovative and easy way to write rust unit tests.

# Why injectorpp

When working with a large codebase, a common challenge in writing unit tests is managing dependencies. Elements such as disk I/O, network operations, and even singleton or static functions can cause the code non-unit testable.

Traditionally, writing effective unit tests for such code requires refactoring the production code first. This process may involve introducing additional traits, even when there is only one implementation in production. Consequently, numerous traits are created solely for testing purposes, rather than for actual production use.

For example, to write tests for below code:

```rust
fn try_repair() -> Result<(), String> {
    if let Err(e) = fs::create_dir_all("/tmp/target_files") {
        // Failure business logic here

        return Err(format!("Could not create directory: {}", e));
    }

    // Success business logic here

    Ok(())
}
```

The code itself is clean and readable but it's not unit testable as `fs::create_dir_all` introduces a dependency on disk. Traditionally, it is necessary to refactor the code to enable the passing of a trait into the function, thereby abstracting away the `fs::create_dir_all()` operation. Or you have to setup the environment to make sure `/tmp/target_files` exists to cover the success path.

With injectorpp, you can write tests without needing to modify the production code solely to make it unit testable or setup environment:

```rust
let mut injector = InjectorPP::new();
injector
    .when_called(injectorpp::func!(fn (fs::create_dir_all)(&'static str) -> std::io::Result<()>)
    .will_execute(injectorpp::fake!(
        func_type: fn(path: &str) -> std::io::Result<()>,
        when: path == "/tmp/target_files",
        returns: Ok(()),
        times: 1
    ));

assert!(try_repair().is_ok());
```

Notice that `try_repair()` is not changed whereas `fs::create_dir_all` is successfully abstracted away. No external dependency, all happen in-memory.

The above config make sure when `fs::create_dir_all` is called with `/tmp/target_files` as its `path` parameter, it will always return `Ok(())` and it's expected to be executed only once.

This approach eliminates the need to make a trait solely for testing purposes. It also ensures that previously non-unit testable code is now unit testable.

# Supported Platform
- OS: Linux and Windows
- Arch: arm64 and amd64

# Usage

Add `injectorpp` to the `Cargo.toml`:

```toml
[dev-dependencies]
injectorpp = "0.4.0"
```

Below `profile.test` config is recommended to make sure `injectorpp` working correctly in tests. If you have workspace, make sure add this on the top level of `Cargo.toml`:

```toml
[profile.test]
opt-level = 0
debug = true
lto = false
codegen-units = 1
incremental = false
```

Import injectorpp in the code:

```rust
use injectorpp::interface::injector::*;
```

Below are multiple ways to config the function behavior.

## `will_return_boolean`

If the function only returns boolean and you only want to make it constantly returns a specific boolean value, you can use `will_return_boolean`:

```rust
let mut injector = InjectorPP::new();
injector
    .when_called(injectorpp::func!(fn (Path::exists)(&Path) -> bool))
    .will_return_boolean(true);
```

Above code will make `Path::exists` always return true.

## `will_execute`

For complex scenarios, `will_execute` is the major feature to use.

In `will_execute` there are different options:

```rust
func_type: // Required. The signature of the function to fake.
when: // Optional. A condition check for the parameters of the function to fake.
assign: // Optional. Use to set values to reference variables of the function to fake.
returns: // Required for the function has return. Specify what the return value should be.
times: // Optional. How many times the function should be called. If the value is not satisfied at the end of the test, the test will fail.
```

A simple example:

```rust
#[test]
fn test_will_execute_when_fake_file_dependency_should_success() {
    let mut injector = InjectorPP::new();
    injector
        .when_called(injectorpp::func!(fn (Path::exists)(&Path) -> bool))
        .will_execute(injectorpp::fake!(
            func_type: fn(_path: &Path) -> bool,
            returns: true
        ));

    let test_path = "/path/that/does/not/exist";
    let result = Path::new(test_path).exists();

    assert_eq!(result, true);
}
```

Below is a more complex example. The function has generic type. The fake only takes effect when a given generic type is hit.

```rust
#[test]
fn test_will_execute_when_fake_generic_function_multiple_types_should_success() {
    let mut injector = InjectorPP::new();
    injector
        .when_called(injectorpp::func!(
            fn (complex_generic_multiple_types_func)(&'static str, bool, i32) -> String
        ))
        .will_execute(injectorpp::fake!(
            func_type: fn(a: &str, b: bool, c: i32) -> String,
            when: a == "abc" && b == true && c == 123,
            returns: "Fake value".to_string(),
            times: 1
        ));

    let actual_result = complex_generic_multiple_types_func("abc", true, 123);

    // This call should not be counted as the types are different from the fake_closure.
    complex_generic_multiple_types_func(1, 2, 3);

    assert_eq!(actual_result, "Fake value".to_string());
}
```

Below is an example for assigning the values to the reference parameters:

```rust
#[test]
fn test_will_execute_when_fake_multiple_reference_param_function_should_success() {
    let mut injector = InjectorPP::new();
    injector
        .when_called(injectorpp::func!(
            fn (multiple_reference_params_func)(&mut i32, &mut bool) -> bool
        ))
        .will_execute(injectorpp::fake!(
            func_type: fn(a: &mut i32, b: &mut bool) -> bool,
            assign: { *a = 6; *b = true },
            returns: true,
            times: 1
        ));

    let mut value1 = 0;
    let mut value2 = false;

    let result = multiple_reference_params_func(&mut value1, &mut value2);

    assert_eq!(value1, 6);
    assert_eq!(value2, true);
    assert_eq!(result, true);
}
```

Below is an example for faking a method:

```rust
#[test]
fn test_will_execute_when_fake_method_with_parameter_should_success() {
    let mut injector = InjectorPP::new();
    injector
        .when_called(injectorpp::func!(fn (Foo::add)(&Foo, i32) -> i32))
        .will_execute(injectorpp::fake!(
            func_type: fn(f: &Foo, value: i32) -> i32,
            when: f.value > 0,
            returns: f.value * 2 + value * 2
        ));

    let foo = Foo::new(6);
    let result = foo.add(3);

    assert_eq!(result, 18);
}
```

The fake can be limited to a given scope:

```rust
#[test]
fn test_will_execute_when_fake_generic_function_single_type_can_recover() {
    {
        let mut injector = InjectorPP::new();
        injector
            .when_called(injectorpp::func!(
                fn (complex_generic_single_type_always_fail_func)(&'static str) -> std::io::Result<()>
            ))
            .will_execute(injectorpp::fake!(
                func_type: fn(path: &str) -> std::io::Result<()>,
                when: path == "/not/exist/path",
                returns: Ok(()),
                times: 1
            ));

        let actual_result = complex_generic_single_type_always_fail_func("/not/exist/path");

        assert!(actual_result.is_ok());
    }

    let actual_result = complex_generic_single_type_always_fail_func("/not/exist/path");

    assert!(actual_result.is_err());
}
```

More examples can be found [here](tests/will_execute.rs).

## `will_execute_raw`

`will_execute_raw` allows to fully customize the function behavior. A custom function or closure can be used to replace the original function.

Below is an example for using custom function:

```rust
pub fn fake_path_exists() -> bool {
    println!("fake_path_exists executed.");
    true
}

#[test]
fn test_will_execute_raw_when_fake_file_dependency_should_success() {
    let mut injector = InjectorPP::new();
    injector
        .when_called(injectorpp::func!(fn (Path::exists)(&Path) -> bool))
        .will_execute_raw(injectorpp::func!(fn (fake_path_exists)(&Path) -> bool));

    let test_path = "/path/that/does/not/exist";
    let result = Path::new(test_path).exists();

    assert_eq!(result, true);
}
```

Below is an example of using closure:

```rust
#[test]
fn test_will_execute_raw_when_fake_no_return_function_use_closure_should_success() {
    static CALL_COUNT_CLOSURE: AtomicU32 = AtomicU32::new(0);

    let fake_closure = || {
        CALL_COUNT_CLOSURE.fetch_add(1, Ordering::SeqCst);
    };

    let mut injector = InjectorPP::new();
    injector
        .when_called(injectorpp::func!(fn (func_no_return)()))
        .will_execute_raw(injectorpp::closure!(fake_closure, fn()));

    func_no_return();

    assert_eq!(CALL_COUNT_CLOSURE.load(Ordering::SeqCst), 1);
}
```

## `Fake async functions`

To fake async functions, `when_called_async` and `will_return_async` are needed.

Below is an example to fake simple async functions:

```rust
async fn simple_async_func_u32_add_one(x: u32) -> u32 {
    x + 1
}

async fn simple_async_func_u32_add_two(x: u32) -> u32 {
    x + 2
}

async fn simple_async_func_bool(x: bool) -> bool {
    x
}

#[tokio::test]
async fn test_simple_async_func_should_success() {
    let mut injector = InjectorPP::new();

    injector
        .when_called_async(injectorpp::async_func!(
            simple_async_func_u32_add_one(u32::default()),
            u32
        ))
        .will_return_async(injectorpp::async_return!(123, u32));

    let x = simple_async_func_u32_add_one(1).await;
    assert_eq!(x, 123);

    // simple_async_func_u32_add_two should not be affected
    let x = simple_async_func_u32_add_two(1).await;
    assert_eq!(x, 3);

    injector
        .when_called_async(injectorpp::async_func!(
            simple_async_func_u32_add_two(u32::default()),
            u32
        ))
        .will_return_async(injectorpp::async_return!(678, u32));

    // Now because it's faked the return value should be changed
    let x = simple_async_func_u32_add_two(1).await;
    assert_eq!(x, 678);

    // simple_async_func_bool should not be affected
    let y = simple_async_func_bool(true).await;
    assert_eq!(y, true);

    injector
        .when_called_async(injectorpp::async_func!(
            simple_async_func_bool(bool::default()),
            bool
        ))
        .will_return_async(injectorpp::async_return!(false, bool));

    // Now because it's faked the return value should be false
    let y = simple_async_func_bool(true).await;
    assert_eq!(y, false);
}
```

Below is an example to fake a complex struct method:

```rust
struct HttpClientTest {
    pub url: String,
}

impl HttpClientTest {
    pub async fn get(&self) -> String {
        format!("GET {}", self.url)
    }

    pub async fn post(&self, payload: &str) -> String {
        format!("POST {} to {}", payload, self.url)
    }
}

#[tokio::test]
async fn test_complex_struct_async_func_without_param_should_success() {
    {
        // This is a temporary instance that is needed for async function fake.
        // Parameter does not matter.
        let temp_client = HttpClientTest {
            url: String::default(),
        };

        let mut injector = InjectorPP::new();
        injector
            .when_called_async(injectorpp::async_func!(temp_client.get(), String))
            .will_return_async(injectorpp::async_return!(
                "Fake GET response".to_string(),
                String
            ));

        // Now the real client will be used and its behavior should be faked
        let real_client = HttpClientTest {
            url: "https://test.com".to_string(),
        };

        let result = real_client.get().await;
        assert_eq!(result, "Fake GET response".to_string());
    }

    let real_client = HttpClientTest {
        url: "https://test.com".to_string(),
    };

    // The original function should be called as the injector is out of scope
    let result = real_client.get().await;
    assert_eq!(result, "GET https://test.com".to_string());
}
```

## `Fake system functions`

Traditionally, system functions could cause the code non-unit testable immediately. It's also one of the test challenges in the projects rely on low level system apis. Now with injectorpp, system function can be easily faked. Below is an example:

```rust
use std::ffi::CString;
use std::os::raw::{c_char, c_int, c_uint};

use injectorpp::interface::injector::*;

extern "C" {
    fn shm_open(name: *const c_char, oflag: c_int, mode: c_uint) -> c_int;
}

#[test]
fn test_fake_shm_open_should_return_fixed_fd() {
    // Fake shm_open to always return file descriptor 32
    let mut injector = InjectorPP::new();
    injector
        .when_called(injectorpp::func!(
            unsafe{} extern "C" fn (shm_open)(*const c_char, c_int, c_uint) -> c_int
        ))
        .will_execute(injectorpp::fake!(
            func_type: unsafe extern "C" fn(_name: *const c_char, _oflag: c_int, _mode: c_uint) -> c_int,
            returns: 32
        ));

    let name = CString::new("/myshm").unwrap();
    let fd = unsafe { shm_open(name.as_ptr(), 0, 0o600) };
    assert_eq!(fd, 32);
}
```

## `Fake Azure SDK client library`

Mocking Azure SDK client library related to http or https request was tough. But by using injectorpp it's simple. Below is an example:

```rust
#[tokio::test]
async fn test_azure_http_client_always_return_200() {
    // Create a temporary client + request to capture the method pointer
    let temp_client = new_http_client();
    let mut temp_req = Request::new(Url::parse("https://temp/").unwrap(), Method::Get);

    // Setup the fake
    let mut injector = InjectorPP::new();
    injector
        .when_called_async(injectorpp::async_func!(
            temp_client.execute_request(&mut temp_req),
            std::result::Result<RawResponse, Error>
        ))
        .will_return_async(injectorpp::async_return!(
            // always return an Ok(RawResponse) with status 200
            Ok(RawResponse::from_bytes(StatusCode::Ok, Headers::new(), vec![])),
            std::result::Result<RawResponse, Error>
        ));

    // Run the real code under test
    let client = new_http_client();
    let url = Url::parse("https://nonexistsitetest").unwrap();
    let mut request = Request::new(url, Method::Get);

    let response = client.execute_request(&mut request).await.unwrap();
    assert_eq!(response.status(), 200);
}
```

## `Unsafe API`

`when_called_unchecked` and `will_execute_raw_unchecked` are the unsafe versions of `when_called` and `will_execute_raw`. They allow you to bypass type check but you need to ensure the safety yourself.

```rust
pub fn fake_path_exists(_path: &Path) -> bool {
    println!("fake_path_exists executed.");
    true
}

#[test]
fn test_will_execute_raw_unchecked_when_fake_file_dependency_should_success() {
    let mut injector = InjectorPP::new();

    unsafe {
        injector
            .when_called_unchecked(injectorpp::func_unchecked!(Path::exists))
            .will_execute_raw_unchecked(injectorpp::func_unchecked!(fake_path_exists));
    }

    let test_path = "/path/that/does/not/exist";
    let result = Path::new(test_path).exists();

    assert_eq!(result, true);
}
```

Similarly, `when_called_async_unchecked` and `will_return_async_unchecked` are the unsafe versions for async functions.

```rust
async fn simple_async_func_u32_add_one(x: u32) -> u32 {
    x + 1
}

async fn simple_async_func_u32_add_two(x: u32) -> u32 {
    x + 2
}

async fn simple_async_func_bool(x: bool) -> bool {
    x
}

#[tokio::test]
async fn test_simple_async_func_unchecked_should_success() {
    let mut injector = InjectorPP::new();

    unsafe {
        injector
            .when_called_async_unchecked(injectorpp::async_func_unchecked!(
                simple_async_func_u32_add_one(u32::default())
            ))
            .will_return_async_unchecked(injectorpp::async_return_unchecked!(123, u32));
    }

    let x = simple_async_func_u32_add_one(1).await;
    assert_eq!(x, 123);

    // simple_async_func_u32_add_two should not be affected
    let x = simple_async_func_u32_add_two(1).await;
    assert_eq!(x, 3);

    unsafe {
        injector
            .when_called_async_unchecked(injectorpp::async_func_unchecked!(
                simple_async_func_u32_add_two(u32::default())
            ))
            .will_return_async_unchecked(injectorpp::async_return_unchecked!(678, u32));
    }

    // Now because it's faked the return value should be changed
    let x = simple_async_func_u32_add_two(1).await;
    assert_eq!(x, 678);

    // simple_async_func_bool should not be affected
    let y = simple_async_func_bool(true).await;
    assert_eq!(y, true);

    unsafe {
        injector
            .when_called_async_unchecked(injectorpp::async_func_unchecked!(simple_async_func_bool(
                bool::default()
            )))
            .will_return_async_unchecked(injectorpp::async_return_unchecked!(false, bool));
    }

    // Now because it's faked the return value should be false
    let y = simple_async_func_bool(true).await;
    assert_eq!(y, false);
}
```

# Contributing

This project welcomes contributions and suggestions. Please see the [CONTRIBUTING.md](CONTRIBUTING.md)

# Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft
trademarks or logos is subject to and must follow
[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/legal/intellectualproperty/trademarks/usage/general).
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
Any use of third-party trademarks or logos are subject to those third-party's policies.