Skip to content

Commit 4d72919

Browse files
committed
chore: optimize security tests to avoid CI timeout
1 parent e0d6fa5 commit 4d72919

File tree

3 files changed

+139
-6
lines changed

3 files changed

+139
-6
lines changed

azure-pipelines.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ jobs:
2424
parameters:
2525
parts: 3
2626
n: 2
27-
codecoverage: true
27+
codecoverage: false
2828
- template: templates/build-template-linux.yml
2929
parameters:
3030
parts: 3
3131
n: 3
32-
codecoverage: true
32+
codecoverage: false
3333
- template: templates/build-template-macos.yml
3434
parameters:
3535
parts: 3
@@ -39,10 +39,10 @@ jobs:
3939
parameters:
4040
parts: 3
4141
n: 2
42-
codecoverage: false
42+
codecoverage: true
4343
- template: templates/build-template-macos.yml
4444
parameters:
4545
parts: 3
4646
n: 3
47-
codecoverage: false
47+
codecoverage: true
4848

test/AElf.Contracts.TestContract.Tests/AElf.Contracts.TestContract.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<OutputItemType>Contract</OutputItemType>
9898
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
9999
</ProjectReference>
100+
<ProjectReference Include="..\AElf.TestBase\AElf.TestBase.csproj" />
100101
</ItemGroup>
101102

102103
<ItemGroup>

test/AElf.Contracts.TestContract.Tests/PatchedContractSecurityTests.cs

Lines changed: 134 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
using System.Text;
22
using System.Threading.Tasks;
33
using AElf.Contracts.TestContract.BasicSecurity;
4+
using AElf.Kernel;
5+
using AElf.Kernel.Blockchain.Application;
6+
using AElf.Kernel.SmartContract;
7+
using AElf.Kernel.SmartContract.Application;
48
using AElf.Sdk.CSharp;
9+
using AElf.TestBase;
510
using AElf.Types;
611
using Google.Protobuf;
712
using Google.Protobuf.WellKnownTypes;
13+
using Microsoft.Extensions.DependencyInjection;
814
using Shouldly;
915
using Xunit;
1016
using SmartContractConstants = AElf.Kernel.SmartContract.SmartContractConstants;
@@ -385,7 +391,7 @@ await TestBasicSecurityContractStub.TestMapped2State.SendAsync(new ProtobufInput
385391
}
386392
}
387393

388-
[Fact]
394+
[IgnoreOnCIFact]
389395
public async Task TestBranchCount()
390396
{
391397
{
@@ -434,7 +440,7 @@ await TestBasicSecurityContractStub.TestForeachInfiniteLoop.SendWithExceptionAsy
434440
}
435441
}
436442

437-
[Fact]
443+
[IgnoreOnCIFact]
438444
public async Task TestMethodCallCount()
439445
{
440446
{
@@ -453,4 +459,130 @@ await TestBasicSecurityContractStub.TestInfiniteRecursiveCallInSeparateClass.Sen
453459
txResult.TransactionResult.Error.ShouldContain(nameof(RuntimeCallThresholdExceededException));
454460
}
455461
}
462+
463+
[Fact]
464+
public async Task TestBranchCountWithReducedThreshold()
465+
{
466+
// Get required services
467+
var blockchainService = Application.ServiceProvider.GetRequiredService<IBlockchainService>();
468+
var executionObserverThresholdProvider =
469+
Application.ServiceProvider.GetRequiredService<IExecutionObserverThresholdProvider>();
470+
471+
// Get current best chain block
472+
var bestChainBlock = await blockchainService.GetBestChainLastBlockHeaderAsync();
473+
var blockIndex = new BlockIndex
474+
{
475+
BlockHash = bestChainBlock.GetHash(),
476+
BlockHeight = bestChainBlock.Height
477+
};
478+
479+
// Set reduced threshold to 5000
480+
const int reducedThreshold = 5000;
481+
var newThreshold = new ExecutionObserverThreshold
482+
{
483+
ExecutionBranchThreshold = reducedThreshold,
484+
ExecutionCallThreshold = reducedThreshold
485+
};
486+
await executionObserverThresholdProvider.SetExecutionObserverThresholdAsync(blockIndex, newThreshold);
487+
488+
// Verify threshold was set correctly
489+
var currentThreshold = executionObserverThresholdProvider.GetExecutionObserverThreshold(blockIndex);
490+
currentThreshold.ExecutionBranchThreshold.ShouldBe(reducedThreshold);
491+
currentThreshold.ExecutionCallThreshold.ShouldBe(reducedThreshold);
492+
493+
{
494+
await TestBasicSecurityContractStub.TestWhileInfiniteLoop.SendAsync(new Int32Input
495+
{ Int32Value = reducedThreshold -1 });
496+
var txResult = await TestBasicSecurityContractStub.TestWhileInfiniteLoop.SendWithExceptionAsync(
497+
new Int32Input
498+
{ Int32Value = reducedThreshold });
499+
txResult.TransactionResult.Error.ShouldContain(nameof(RuntimeBranchThresholdExceededException));
500+
}
501+
502+
{
503+
await TestBasicSecurityContractStub.TestForInfiniteLoop.SendAsync(new Int32Input { Int32Value = reducedThreshold - 1 });
504+
var txResult = await TestBasicSecurityContractStub.TestForInfiniteLoop.SendWithExceptionAsync(
505+
new Int32Input
506+
{ Int32Value = reducedThreshold });
507+
txResult.TransactionResult.Error.ShouldContain(nameof(RuntimeBranchThresholdExceededException));
508+
}
509+
510+
{
511+
await TestBasicSecurityContractStub.TestForInfiniteLoopInSeparateClass.SendAsync(new Int32Input
512+
{ Int32Value = reducedThreshold - 1 });
513+
var txResult = await TestBasicSecurityContractStub.TestForInfiniteLoop.SendWithExceptionAsync(
514+
new Int32Input
515+
{ Int32Value = reducedThreshold });
516+
txResult.TransactionResult.Error.ShouldContain(nameof(RuntimeBranchThresholdExceededException));
517+
}
518+
519+
{
520+
await TestBasicSecurityContractStub.TestWhileInfiniteLoopWithState.SendAsync(new Int32Input
521+
{ Int32Value = reducedThreshold - 1 });
522+
var txResult =
523+
await TestBasicSecurityContractStub.TestWhileInfiniteLoopWithState.SendWithExceptionAsync(
524+
new Int32Input
525+
{ Int32Value = reducedThreshold });
526+
txResult.TransactionResult.Error.ShouldContain(nameof(RuntimeBranchThresholdExceededException));
527+
}
528+
529+
{
530+
await TestBasicSecurityContractStub.TestForeachInfiniteLoop.SendAsync(new ListInput
531+
{ List = { new int[reducedThreshold - 1] } });
532+
var txResult =
533+
await TestBasicSecurityContractStub.TestForeachInfiniteLoop.SendWithExceptionAsync(
534+
new ListInput { List = { new int[reducedThreshold] } });
535+
txResult.TransactionResult.Error.ShouldContain(nameof(RuntimeBranchThresholdExceededException));
536+
}
537+
}
538+
539+
[Fact]
540+
public async Task TestMethodCallCountWithReducedThreshold()
541+
{
542+
// Get required services
543+
var blockchainService = Application.ServiceProvider.GetRequiredService<IBlockchainService>();
544+
var executionObserverThresholdProvider =
545+
Application.ServiceProvider.GetRequiredService<IExecutionObserverThresholdProvider>();
546+
547+
// Get current best chain block
548+
var bestChainBlock = await blockchainService.GetBestChainLastBlockHeaderAsync();
549+
var blockIndex = new BlockIndex
550+
{
551+
BlockHash = bestChainBlock.GetHash(),
552+
BlockHeight = bestChainBlock.Height
553+
};
554+
555+
// Set reduced threshold to 5000
556+
const int reducedThreshold = 5000;
557+
var newThreshold = new ExecutionObserverThreshold
558+
{
559+
ExecutionBranchThreshold = reducedThreshold,
560+
ExecutionCallThreshold = reducedThreshold
561+
};
562+
await executionObserverThresholdProvider.SetExecutionObserverThresholdAsync(blockIndex, newThreshold);
563+
564+
// Verify threshold was set correctly
565+
var currentThreshold = executionObserverThresholdProvider.GetExecutionObserverThreshold(blockIndex);
566+
currentThreshold.ExecutionBranchThreshold.ShouldBe(reducedThreshold);
567+
currentThreshold.ExecutionCallThreshold.ShouldBe(reducedThreshold);
568+
569+
// Test recursive call with reduced threshold
570+
{
571+
await TestBasicSecurityContractStub.TestInfiniteRecursiveCall.SendAsync(new Int32Input
572+
{ Int32Value = reducedThreshold - 100 });
573+
var txResult = await TestBasicSecurityContractStub.TestInfiniteRecursiveCall.SendWithExceptionAsync(
574+
new Int32Input { Int32Value = reducedThreshold });
575+
txResult.TransactionResult.Error.ShouldContain(nameof(RuntimeCallThresholdExceededException));
576+
}
577+
578+
// Test recursive call in separate class with reduced threshold
579+
{
580+
await TestBasicSecurityContractStub.TestInfiniteRecursiveCallInSeparateClass.SendAsync(new Int32Input
581+
{ Int32Value = reducedThreshold - 100 });
582+
var txResult =
583+
await TestBasicSecurityContractStub.TestInfiniteRecursiveCallInSeparateClass.SendWithExceptionAsync(
584+
new Int32Input { Int32Value = reducedThreshold });
585+
txResult.TransactionResult.Error.ShouldContain(nameof(RuntimeCallThresholdExceededException));
586+
}
587+
}
456588
}

0 commit comments

Comments
 (0)