1515using Neo . SmartContract . Testing . Coverage . Formats ;
1616using System ;
1717using System . IO ;
18+ using System . IO . Compression ;
1819using System . Numerics ;
1920using System . Reflection ;
2021using System . Text ;
@@ -26,6 +27,7 @@ namespace Neo.SmartContract.Testing.UnitTests.Coverage
2627 public class CoverageDataTests
2728 {
2829 private static readonly Regex WhiteSpaceRegex = new ( "\\ s" ) ;
30+ private const int MaxDebugInfoJsonBytes = 16 * 1024 * 1024 ;
2931
3032 [ TestMethod ]
3133 public void NeoDebugInfoLoadThrowsHelpfulMessageForNonObjectJson ( )
@@ -43,6 +45,54 @@ public void NeoDebugInfoLoadThrowsHelpfulMessageForNonObjectJson()
4345 Assert . AreEqual ( "The debug info root must be a JSON object." , exception . InnerException ! . Message ) ;
4446 }
4547
48+ [ TestMethod ]
49+ public void NeoDebugInfoTryLoadCompressedLoadsSmallDebugInfo ( )
50+ {
51+ const string json = "{\" hash\" :\" 0x0000000000000000000000000000000000000000\" ,\" documents\" :[],\" methods\" :[]}" ;
52+ byte [ ] archive = CreateDebugInfoArchive ( json ) ;
53+
54+ Assert . IsTrue ( TryLoadCompressed ( archive , out var debugInfo ) ) ;
55+ Assert . IsNotNull ( debugInfo ) ;
56+ Assert . AreEqual ( UInt160 . Zero , debugInfo . Hash ) ;
57+ }
58+
59+ [ TestMethod ]
60+ public void NeoDebugInfoTryLoadCompressedRejectsOversizedDebugInfo ( )
61+ {
62+ string json = "{\" hash\" :\" 0x0000000000000000000000000000000000000000\" ,\" documents\" :[\" " +
63+ new string ( 'a' , MaxDebugInfoJsonBytes ) +
64+ "\" ],\" methods\" :[]}" ;
65+ byte [ ] archive = CreateDebugInfoArchive ( json ) ;
66+
67+ Assert . IsFalse ( TryLoadCompressed ( archive , out var debugInfo ) ) ;
68+ Assert . IsNull ( debugInfo ) ;
69+ }
70+
71+ private static byte [ ] CreateDebugInfoArchive ( string json )
72+ {
73+ using var compressedFileStream = new MemoryStream ( ) ;
74+ using ( var zipArchive = new ZipArchive ( compressedFileStream , ZipArchiveMode . Create , false ) )
75+ {
76+ var zipEntry = zipArchive . CreateEntry ( "test.debug.json" ) ;
77+ using var zipEntryStream = zipEntry . Open ( ) ;
78+ byte [ ] content = Encoding . UTF8 . GetBytes ( json ) ;
79+ zipEntryStream . Write ( content , 0 , content . Length ) ;
80+ }
81+ return compressedFileStream . ToArray ( ) ;
82+ }
83+
84+ private static bool TryLoadCompressed ( byte [ ] archive , out NeoDebugInfo ? debugInfo )
85+ {
86+ var tryLoadCompressed = typeof ( NeoDebugInfo ) . GetMethod ( "TryLoadCompressed" , BindingFlags . Static | BindingFlags . NonPublic , [ typeof ( Stream ) , typeof ( NeoDebugInfo ) . MakeByRefType ( ) ] ) ;
87+ Assert . IsNotNull ( tryLoadCompressed ) ;
88+
89+ using var stream = new MemoryStream ( archive ) ;
90+ object ? [ ] args = [ stream , null ] ;
91+ bool loaded = ( bool ) tryLoadCompressed ! . Invoke ( null , args ) ! ;
92+ debugInfo = ( NeoDebugInfo ? ) args [ 1 ] ;
93+ return loaded ;
94+ }
95+
4696 [ TestMethod ]
4797 public void TestDump ( )
4898 {
0 commit comments