@@ -66,23 +66,34 @@ public void ApplyMirroredNetworking_ReplacesSettingAndIsIdempotent()
6666 }
6767
6868 [ Theory ]
69- [ InlineData ( false , "\r \n " ) ]
70- [ InlineData ( true , "\n " ) ]
69+ [ InlineData ( false , "\n " , false ) ]
70+ [ InlineData ( false , "\n " , true ) ]
71+ [ InlineData ( false , "\r \n " , false ) ]
72+ [ InlineData ( false , "\r \n " , true ) ]
73+ [ InlineData ( true , "\n " , false ) ]
74+ [ InlineData ( true , "\n " , true ) ]
75+ [ InlineData ( true , "\r \n " , false ) ]
76+ [ InlineData ( true , "\r \n " , true ) ]
7177 public void ApplyMirroredNetworking_PreservesUtf16EncodingAndRollback (
7278 bool bigEndian ,
73- string newLine )
79+ string newLine ,
80+ bool includeFinalNewLine )
7481 {
7582 Directory . CreateDirectory ( _root ) ;
7683 var configPath = Path . Combine ( _root , "wslconfig" ) ;
7784 var encoding = new UnicodeEncoding ( bigEndian , byteOrderMark : true , throwOnInvalidBytes : true ) ;
78- var original = Encode ( $ "[wsl2]{ newLine } memory=8GB{ newLine } ", encoding ) ;
85+ var originalText = $ "[wsl2]{ newLine } memory=8GB" +
86+ ( includeFinalNewLine ? newLine : string . Empty ) ;
87+ var original = Encode ( originalText , encoding ) ;
7988 File . WriteAllBytes ( configPath , original ) ;
8089 var manager = new WslGlobalConfigManager ( configPath , Path . Combine ( _root , "backup" ) ) ;
8190
8291 Assert . True ( manager . ApplyMirroredNetworking ( ) . Changed ) ;
8392 var updated = File . ReadAllBytes ( configPath ) ;
8493 Assert . True ( updated . AsSpan ( ) . StartsWith ( encoding . Preamble ) ) ;
85- Assert . Contains ( $ "memory=8GB{ newLine } networkingMode=mirrored", encoding . GetString ( updated [ encoding . Preamble . Length .. ] ) ) ;
94+ var updatedText = encoding . GetString ( updated [ encoding . Preamble . Length .. ] ) ;
95+ Assert . Contains ( $ "memory=8GB{ newLine } networkingMode=mirrored", updatedText ) ;
96+ Assert . Equal ( includeFinalNewLine , updatedText . EndsWith ( newLine , StringComparison . Ordinal ) ) ;
8697 Assert . Equal ( WslGlobalConfigRestoreResult . Restored , manager . RestoreIfUnchanged ( ) ) ;
8798 Assert . Equal ( original , File . ReadAllBytes ( configPath ) ) ;
8899 }
@@ -103,6 +114,121 @@ public void ApplyMirroredNetworking_InvalidEncodingDoesNotModifyFiles()
103114 Assert . False ( Directory . Exists ( backupPath ) ) ;
104115 }
105116
117+ [ Theory ]
118+ [ InlineData ( false ) ]
119+ [ InlineData ( true ) ]
120+ public void ApplyMirroredNetworking_UnsupportedUtf32DoesNotModifyFiles ( bool bigEndian )
121+ {
122+ Directory . CreateDirectory ( _root ) ;
123+ var configPath = Path . Combine ( _root , "wslconfig" ) ;
124+ var backupPath = Path . Combine ( _root , "backup" ) ;
125+ var encoding = new UTF32Encoding ( bigEndian , byteOrderMark : true , throwOnInvalidCharacters : true ) ;
126+ var original = Encode ( "# existing comment\n " , encoding ) ;
127+ File . WriteAllBytes ( configPath , original ) ;
128+
129+ var manager = new WslGlobalConfigManager ( configPath , backupPath ) ;
130+
131+ Assert . Throws < InvalidDataException > ( ( ) => manager . ApplyMirroredNetworking ( ) ) ;
132+ Assert . Equal ( original , File . ReadAllBytes ( configPath ) ) ;
133+ Assert . False ( Directory . Exists ( backupPath ) ) ;
134+ }
135+
136+ [ Theory ]
137+ [ InlineData ( false , false ) ]
138+ [ InlineData ( false , true ) ]
139+ [ InlineData ( true , false ) ]
140+ [ InlineData ( true , true ) ]
141+ public void ApplyMirroredNetworking_MalformedUtf16DoesNotModifyFiles (
142+ bool bigEndian ,
143+ bool unpairedSurrogate )
144+ {
145+ Directory . CreateDirectory ( _root ) ;
146+ var configPath = Path . Combine ( _root , "wslconfig" ) ;
147+ var backupPath = Path . Combine ( _root , "backup" ) ;
148+ var encoding = new UnicodeEncoding ( bigEndian , byteOrderMark : true , throwOnInvalidBytes : true ) ;
149+ byte [ ] malformedPayload = unpairedSurrogate
150+ ? bigEndian ? [ 0xD8 , 0x00 ] : [ 0x00 , 0xD8 ]
151+ : [ 0x5B ] ;
152+ byte [ ] original = [ .. encoding . Preamble , .. malformedPayload ] ;
153+ File . WriteAllBytes ( configPath , original ) ;
154+
155+ var manager = new WslGlobalConfigManager ( configPath , backupPath ) ;
156+
157+ Assert . Throws < InvalidDataException > ( ( ) => manager . ApplyMirroredNetworking ( ) ) ;
158+ Assert . Equal ( original , File . ReadAllBytes ( configPath ) ) ;
159+ Assert . False ( Directory . Exists ( backupPath ) ) ;
160+ }
161+
162+ [ Theory ]
163+ [ InlineData ( false ) ]
164+ [ InlineData ( true ) ]
165+ public void ApplyMirroredNetworking_BomlessUtf16DoesNotModifyFiles ( bool bigEndian )
166+ {
167+ Directory . CreateDirectory ( _root ) ;
168+ var configPath = Path . Combine ( _root , "wslconfig" ) ;
169+ var backupPath = Path . Combine ( _root , "backup" ) ;
170+ var encoding = new UnicodeEncoding ( bigEndian , byteOrderMark : false , throwOnInvalidBytes : true ) ;
171+ var original = encoding . GetBytes ( "[wsl2]\n memory=8GB\n " ) ;
172+ File . WriteAllBytes ( configPath , original ) ;
173+
174+ var manager = new WslGlobalConfigManager ( configPath , backupPath ) ;
175+
176+ Assert . Throws < InvalidDataException > ( ( ) => manager . ApplyMirroredNetworking ( ) ) ;
177+ Assert . Equal ( original , File . ReadAllBytes ( configPath ) ) ;
178+ Assert . False ( Directory . Exists ( backupPath ) ) ;
179+ }
180+
181+ [ Fact ]
182+ public void ApplyMirroredNetworking_PreservesValidControlCharactersInUnknownContent ( )
183+ {
184+ Directory . CreateDirectory ( _root ) ;
185+ var configPath = Path . Combine ( _root , "wslconfig" ) ;
186+ var backupPath = Path . Combine ( _root , "backup" ) ;
187+ const string unknownContent = "[experimental]\n # legacy separator: \u001A \n " ;
188+ var original = Encoding . UTF8 . GetBytes ( $ "{ unknownContent } [wsl2]\n memory=8GB\n ") ;
189+ File . WriteAllBytes ( configPath , original ) ;
190+
191+ var manager = new WslGlobalConfigManager ( configPath , backupPath ) ;
192+
193+ Assert . True ( manager . ApplyMirroredNetworking ( ) . Changed ) ;
194+ Assert . StartsWith ( unknownContent , File . ReadAllText ( configPath ) ) ;
195+ Assert . Equal ( WslGlobalConfigRestoreResult . Restored , manager . RestoreIfUnchanged ( ) ) ;
196+ Assert . Equal ( original , File . ReadAllBytes ( configPath ) ) ;
197+ }
198+
199+ [ Fact ]
200+ public void ApplyMirroredNetworking_AppendsSectionWithoutReplacingUnknownContent ( )
201+ {
202+ Directory . CreateDirectory ( _root ) ;
203+ var configPath = Path . Combine ( _root , "wslconfig" ) ;
204+ var backupPath = Path . Combine ( _root , "backup" ) ;
205+ const string originalText = "[experimental]\n autoMemoryReclaim=gradual" ;
206+ var original = Encoding . UTF8 . GetBytes ( originalText ) ;
207+ File . WriteAllBytes ( configPath , original ) ;
208+
209+ var manager = new WslGlobalConfigManager ( configPath , backupPath ) ;
210+
211+ Assert . True ( manager . ApplyMirroredNetworking ( ) . Changed ) ;
212+ Assert . Equal (
213+ $ "{ originalText } \n [wsl2]\n networkingMode=mirrored\n ",
214+ File . ReadAllText ( configPath ) ) ;
215+ Assert . Equal ( WslGlobalConfigRestoreResult . Restored , manager . RestoreIfUnchanged ( ) ) ;
216+ Assert . Equal ( original , File . ReadAllBytes ( configPath ) ) ;
217+ }
218+
219+ [ Fact ]
220+ public void ApplyMirroredNetworking_CreatesAndRollsBackMissingFile ( )
221+ {
222+ Directory . CreateDirectory ( _root ) ;
223+ var configPath = Path . Combine ( _root , "wslconfig" ) ;
224+ var manager = new WslGlobalConfigManager ( configPath , Path . Combine ( _root , "backup" ) ) ;
225+
226+ Assert . True ( manager . ApplyMirroredNetworking ( ) . Changed ) ;
227+ Assert . Equal ( "[wsl2]\n networkingMode=mirrored\n " , File . ReadAllText ( configPath ) ) ;
228+ Assert . Equal ( WslGlobalConfigRestoreResult . Restored , manager . RestoreIfUnchanged ( ) ) ;
229+ Assert . False ( File . Exists ( configPath ) ) ;
230+ }
231+
106232 [ Fact ]
107233 public void RestoreIfUnchanged_PreservesAConcurrentUserEdit ( )
108234 {
0 commit comments