140 lines
3.4 KiB
C#
140 lines
3.4 KiB
C#
// -----------------------------------------------------------------------------
|
|
// InternalCallGraphTests.cs
|
|
// Sprint: SPRINT_3700_0003_0001_trigger_extraction
|
|
// Description: Unit tests for InternalCallGraph.
|
|
// -----------------------------------------------------------------------------
|
|
|
|
using StellaOps.Scanner.VulnSurfaces.CallGraph;
|
|
using StellaOps.Scanner.VulnSurfaces.Models;
|
|
using Xunit;
|
|
|
|
using StellaOps.TestKit;
|
|
namespace StellaOps.Scanner.VulnSurfaces.Tests;
|
|
|
|
public class InternalCallGraphTests
|
|
{
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void AddMethod_StoresMethod()
|
|
{
|
|
// Arrange
|
|
var graph = new InternalCallGraph
|
|
{
|
|
PackageId = "TestPackage",
|
|
Version = "1.0.0"
|
|
};
|
|
|
|
var method = new InternalMethodRef
|
|
{
|
|
MethodKey = "Namespace.Class::Method()",
|
|
Name = "Method",
|
|
DeclaringType = "Namespace.Class",
|
|
IsPublic = true
|
|
};
|
|
|
|
// Act
|
|
graph.AddMethod(method);
|
|
|
|
// Assert
|
|
Assert.True(graph.ContainsMethod("Namespace.Class::Method()"));
|
|
Assert.Equal(1, graph.MethodCount);
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void AddEdge_CreatesForwardAndReverseMapping()
|
|
{
|
|
// Arrange
|
|
var graph = new InternalCallGraph
|
|
{
|
|
PackageId = "TestPackage",
|
|
Version = "1.0.0"
|
|
};
|
|
|
|
var edge = new InternalCallEdge
|
|
{
|
|
Caller = "A::M1()",
|
|
Callee = "A::M2()"
|
|
};
|
|
|
|
// Act
|
|
graph.AddEdge(edge);
|
|
|
|
// Assert
|
|
Assert.Contains("A::M2()", graph.GetCallees("A::M1()"));
|
|
Assert.Contains("A::M1()", graph.GetCallers("A::M2()"));
|
|
Assert.Equal(1, graph.EdgeCount);
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void GetPublicMethods_ReturnsOnlyPublic()
|
|
{
|
|
// Arrange
|
|
var graph = new InternalCallGraph
|
|
{
|
|
PackageId = "TestPackage",
|
|
Version = "1.0.0"
|
|
};
|
|
|
|
graph.AddMethod(new InternalMethodRef
|
|
{
|
|
MethodKey = "A::Public()",
|
|
Name = "Public",
|
|
DeclaringType = "A",
|
|
IsPublic = true
|
|
});
|
|
|
|
graph.AddMethod(new InternalMethodRef
|
|
{
|
|
MethodKey = "A::Private()",
|
|
Name = "Private",
|
|
DeclaringType = "A",
|
|
IsPublic = false
|
|
});
|
|
|
|
// Act
|
|
var publicMethods = graph.GetPublicMethods().ToList();
|
|
|
|
// Assert
|
|
Assert.Single(publicMethods);
|
|
Assert.Equal("A::Public()", publicMethods[0].MethodKey);
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void GetCallees_EmptyForUnknownMethod()
|
|
{
|
|
// Arrange
|
|
var graph = new InternalCallGraph
|
|
{
|
|
PackageId = "TestPackage",
|
|
Version = "1.0.0"
|
|
};
|
|
|
|
// Act
|
|
var callees = graph.GetCallees("Unknown::Method()");
|
|
|
|
// Assert
|
|
Assert.Empty(callees);
|
|
}
|
|
|
|
[Trait("Category", TestCategories.Unit)]
|
|
[Fact]
|
|
public void GetMethod_ReturnsNullForUnknown()
|
|
{
|
|
// Arrange
|
|
var graph = new InternalCallGraph
|
|
{
|
|
PackageId = "TestPackage",
|
|
Version = "1.0.0"
|
|
};
|
|
|
|
// Act
|
|
var method = graph.GetMethod("Unknown::Method()");
|
|
|
|
// Assert
|
|
Assert.Null(method);
|
|
}
|
|
}
|