Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-git committed Dec 26, 2024
1 parent bfdf832 commit b2453a7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
6 changes: 3 additions & 3 deletions tracer/src/Datadog.Trace/AppSec/Waf/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ internal class Context : IContext

private readonly List<IEncodeResult> _encodeResults;
private readonly Stopwatch _stopwatch;
private readonly WafLibraryInvoker _wafLibraryInvoker;
private readonly IWafLibraryInvoker _wafLibraryInvoker;
private readonly IEncoder _encoder;
private readonly UserEventsState _userEventsState = new();
private bool _disposed;
private ulong _totalRuntimeOverRuns;

// Beware this class is created on a thread but can be disposed on another so don't trust the lock is not going to be held
private Context(IntPtr contextHandle, IWaf waf, WafLibraryInvoker wafLibraryInvoker, IEncoder encoder)
private Context(IntPtr contextHandle, IWaf waf, IWafLibraryInvoker wafLibraryInvoker, IEncoder encoder)
{
_contextHandle = contextHandle;
_waf = waf;
Expand All @@ -45,7 +45,7 @@ private Context(IntPtr contextHandle, IWaf waf, WafLibraryInvoker wafLibraryInvo

~Context() => Dispose(false);

public static IContext? GetContext(IntPtr contextHandle, IWaf waf, WafLibraryInvoker wafLibraryInvoker, IEncoder encoder)
public static IContext? GetContext(IntPtr contextHandle, IWaf waf, IWafLibraryInvoker wafLibraryInvoker, IEncoder encoder)
{
// in high concurrency, the waf passed as argument here could have been disposed just above in between creation / waf update so last test here
if (waf.Disposed)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// <copyright file="IWafLibraryInvoker.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

#nullable enable
using System;

namespace Datadog.Trace.AppSec.Waf.NativeBindings;

internal interface IWafLibraryInvoker
{
void ContextDestroy(IntPtr handle);

void ResultFree(ref DdwafResultStruct output);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
using System.Runtime.InteropServices;
using Datadog.Trace.AppSec.Waf.Initialization;
using Datadog.Trace.Logging;

#pragma warning disable SA1401

namespace Datadog.Trace.AppSec.Waf.NativeBindings
{
internal class WafLibraryInvoker
internal class WafLibraryInvoker : IWafLibraryInvoker
{
#if NETFRAMEWORK
private const string DllName = "ddwaf.dll";
Expand Down Expand Up @@ -338,7 +339,7 @@ internal unsafe WafReturnCode Run(IntPtr context, DdwafObjectStruct* rawPersiste

internal void Destroy(IntPtr wafHandle) => _destroyField(wafHandle);

internal void ContextDestroy(IntPtr handle) => _contextDestroyField(handle);
public void ContextDestroy(IntPtr handle) => _contextDestroyField(handle);

internal IntPtr ObjectArrayGetIndex(ref DdwafObjectStruct array, long index) => _objectArrayGetIndex(ref array, index);

Expand Down Expand Up @@ -412,7 +413,7 @@ internal DdwafObjectStruct ObjectMap()

internal void ObjectFree(ref DdwafObjectStruct input) => _freeObjectField(ref input);

internal void ResultFree(ref DdwafResultStruct output) => _freeResultField(ref output);
public void ResultFree(ref DdwafResultStruct output) => _freeResultField(ref output);

private void LoggingCallback(
DDWAF_LOG_LEVEL level,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ public void SdkOverrideTest()
{
var iWaf = new Mock<IWaf>().Object;
var encoder = new Mock<Encoder>().Object;
var context = Context.GetContext(IntPtr.Zero, iWaf, new Mock<WafLibraryInvoker>().Object, encoder);
var context = Context.GetContext(IntPtr.Zero, iWaf, new Mock<IWafLibraryInvoker>().Object, encoder);
var security = new Mock<IDatadogSecurity>();
security.Setup(s => s.AddressEnabled("test")).Returns(true);
security.Setup(s => s.AddressEnabled(AddressesConstants.UserId)).Returns(true);
var userId = "toto";
var addresses = context!.ShouldRunWith(security.Object, userId: userId);
addresses.Should().HaveCount(1);
addresses.Should().Contain(new KeyValuePair<string, string>(AddressesConstants.UserId, userId));
}
}

0 comments on commit b2453a7

Please sign in to comment.