PageRenderTime 29ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Compilers/VisualBasic/Test/Emit/Emit/ResourceTests.vb

https://gitlab.com/sharadag/TestProject2
Visual Basic | 814 lines | 629 code | 153 blank | 32 comment | 0 complexity | 8e54bb6f73a2950f0b24c5bb00924219 MD5 | raw file
  1. ' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. Imports System.ComponentModel
  3. Imports System.IO
  4. Imports System.Reflection
  5. Imports System.Runtime.InteropServices
  6. Imports Microsoft.CodeAnalysis.Emit
  7. Imports Microsoft.CodeAnalysis.Test.Utilities
  8. Imports Roslyn.Test.Utilities
  9. Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Emit
  10. Public Class ResourceTests
  11. Inherits BasicTestBase
  12. <DllImport("kernel32.dll", SetLastError:=True)> Public Shared Function _
  13. LoadLibraryEx(lpFileName As String, hFile As IntPtr, dwFlags As UInteger) As IntPtr
  14. End Function
  15. <DllImport("kernel32.dll", SetLastError:=True)> Public Shared Function _
  16. FreeLibrary(hFile As IntPtr) As Boolean
  17. End Function
  18. <Fact>
  19. Public Sub DefaultVersionResource()
  20. Dim source =
  21. <compilation name="Win32VerNoAttrs">
  22. <file>
  23. Public Class Main
  24. Public Shared Sub Main()
  25. End Sub
  26. End Class
  27. </file>
  28. </compilation>
  29. Dim c1 = CreateCompilationWithMscorlib(source, options:=TestOptions.ReleaseExe)
  30. Dim exe = Temp.CreateFile()
  31. Using output As FileStream = exe.Open()
  32. c1.Emit(output, win32Resources:=c1.CreateDefaultWin32Resources(True, False, Nothing, Nothing))
  33. End Using
  34. c1 = Nothing
  35. 'Open as data
  36. Dim [lib] As IntPtr = IntPtr.Zero
  37. Dim versionData As String
  38. Dim mftData As String
  39. Try
  40. [lib] = LoadLibraryEx(exe.Path, IntPtr.Zero, &H2)
  41. If [lib] = IntPtr.Zero Then
  42. Throw New Win32Exception(Marshal.GetLastWin32Error())
  43. End If
  44. 'the manifest and version primitives are tested elsewhere. This is to test that the default
  45. 'values are passed to the primitives that assemble the resources.
  46. Dim size As UInteger
  47. Dim versionRsrc As IntPtr = Win32Res.GetResource([lib], "#1", "#16", size)
  48. versionData = Win32Res.VersionResourceToXml(versionRsrc)
  49. Dim mftSize As UInteger
  50. Dim mftRsrc As IntPtr = Win32Res.GetResource([lib], "#1", "#24", mftSize)
  51. mftData = Win32Res.ManifestResourceToXml(mftRsrc, mftSize)
  52. Finally
  53. If [lib] <> IntPtr.Zero Then
  54. FreeLibrary([lib])
  55. End If
  56. End Try
  57. Dim expected As String =
  58. "<?xml version=""1.0"" encoding=""utf-16""?>" & vbCrLf &
  59. "<VersionResource Size=""612"">" & vbCrLf &
  60. " <VS_FIXEDFILEINFO FileVersionMS=""00000000"" FileVersionLS=""00000000"" ProductVersionMS=""00000000"" ProductVersionLS=""00000000"" />" & vbCrLf &
  61. " <KeyValuePair Key=""FileDescription"" Value="" "" />" & vbCrLf &
  62. " <KeyValuePair Key=""FileVersion"" Value=""0.0.0.0"" />" & vbCrLf &
  63. " <KeyValuePair Key=""InternalName"" Value=""Win32VerNoAttrs.exe"" />" & vbCrLf &
  64. " <KeyValuePair Key=""LegalCopyright"" Value="" "" />" & vbCrLf &
  65. " <KeyValuePair Key=""OriginalFilename"" Value=""Win32VerNoAttrs.exe"" />" & vbCrLf &
  66. " <KeyValuePair Key=""ProductVersion"" Value=""0.0.0.0"" />" & vbCrLf &
  67. " <KeyValuePair Key=""Assembly Version"" Value=""0.0.0.0"" />" & vbCrLf &
  68. "</VersionResource>"
  69. Assert.Equal(versionData, expected)
  70. expected =
  71. "<?xml version=""1.0"" encoding=""utf-16""?>" & vbCrLf &
  72. "<ManifestResource Size=""490"">" & vbCrLf &
  73. " <Contents><![CDATA[<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>" & vbCrLf &
  74. "" & vbCrLf &
  75. "<assembly xmlns=""urn:schemas-microsoft-com:asm.v1"" manifestVersion=""1.0"">" & vbCrLf &
  76. " <assemblyIdentity version=""1.0.0.0"" name=""MyApplication.app""/>" & vbCrLf &
  77. " <trustInfo xmlns=""urn:schemas-microsoft-com:asm.v2"">" & vbCrLf &
  78. " <security>" & vbCrLf &
  79. " <requestedPrivileges xmlns=""urn:schemas-microsoft-com:asm.v3"">" & vbCrLf &
  80. " <requestedExecutionLevel level=""asInvoker"" uiAccess=""false""/>" & vbCrLf &
  81. " </requestedPrivileges>" & vbCrLf &
  82. " </security>" & vbCrLf &
  83. " </trustInfo>" & vbCrLf &
  84. "</assembly>]]></Contents>" & vbCrLf &
  85. "</ManifestResource>"
  86. Assert.Equal(expected, mftData)
  87. 'look at the same data through the FileVersion API.
  88. 'If the codepage and resource language information is not
  89. 'written correctly into the internal resource directory of
  90. 'the PE, then GetVersionInfo will fail to find the FileVersionInfo.
  91. 'Once upon a time in Roslyn, the codepage and lang info was not written correctly.
  92. Dim fileVer = FileVersionInfo.GetVersionInfo(exe.Path)
  93. Assert.Equal(" ", fileVer.LegalCopyright)
  94. End Sub
  95. <Fact>
  96. Public Sub ResourcesInCoff()
  97. 'this is to test that resources coming from a COFF can be added to a binary.
  98. Dim source =
  99. <compilation>
  100. <file>
  101. Public Class Main
  102. Public Shared Sub Main()
  103. End Sub
  104. End Class
  105. </file>
  106. </compilation>
  107. Dim c1 = CreateCompilationWithMscorlib(source, options:=TestOptions.ReleaseExe)
  108. Dim exe = Temp.CreateFile()
  109. Using output As FileStream = exe.Open()
  110. Dim memStream = New MemoryStream(TestResources.General.nativeCOFFResources)
  111. c1.Emit(output, win32Resources:=memStream)
  112. End Using
  113. c1 = Nothing
  114. 'Open as data
  115. Dim [lib] As IntPtr = IntPtr.Zero
  116. Dim versionData As String
  117. Try
  118. [lib] = LoadLibraryEx(exe.Path, IntPtr.Zero, &H2)
  119. If [lib] = IntPtr.Zero Then
  120. Throw New Win32Exception(Marshal.GetLastWin32Error())
  121. End If
  122. Dim size As UInteger
  123. Dim rsrc As IntPtr = Win32Res.GetResource([lib], "#1", "#16", size)
  124. versionData = Win32Res.VersionResourceToXml(rsrc)
  125. rsrc = Win32Res.GetResource([lib], "#1", "#6", size)
  126. Assert.NotNull(rsrc)
  127. rsrc = Win32Res.GetResource([lib], "#1", "#11", size)
  128. Assert.NotNull(rsrc)
  129. rsrc = Win32Res.GetResource([lib], "#1", "WEVT_TEMPLATE", size)
  130. Assert.NotNull(rsrc)
  131. Finally
  132. If [lib] <> IntPtr.Zero Then
  133. FreeLibrary([lib])
  134. End If
  135. End Try
  136. Dim expected As String =
  137. "<?xml version=""1.0"" encoding=""utf-16""?>" & vbCrLf &
  138. "<VersionResource Size=""1104"">" & vbCrLf &
  139. " <VS_FIXEDFILEINFO FileVersionMS=""000b0000"" FileVersionLS=""eacc0000"" ProductVersionMS=""000b0000"" ProductVersionLS=""eacc0000"" />" & vbCrLf &
  140. " <KeyValuePair Key=""CompanyName"" Value=""Microsoft Corporation"" />" & vbCrLf &
  141. " <KeyValuePair Key=""FileDescription"" Value=""Team Foundation Server Object Model"" />" & vbCrLf &
  142. " <KeyValuePair Key=""FileVersion"" Value=""11.0.60108.0 built by: TOOLSET_ROSLYN(GNAMBOO-DEV-GNAMBOO)"" />" & vbCrLf &
  143. " <KeyValuePair Key=""InternalName"" Value=""Microsoft.TeamFoundation.Framework.Server.dll"" />" & vbCrLf &
  144. " <KeyValuePair Key=""LegalCopyright"" Value=""© Microsoft Corporation. All rights reserved."" />" & vbCrLf &
  145. " <KeyValuePair Key=""OriginalFilename"" Value=""Microsoft.TeamFoundation.Framework.Server.dll"" />" & vbCrLf &
  146. " <KeyValuePair Key=""ProductName"" Value=""Microsoft® Visual Studio® 2012"" />" & vbCrLf &
  147. " <KeyValuePair Key=""ProductVersion"" Value=""11.0.60108.0"" />" & vbCrLf &
  148. "</VersionResource>"
  149. Assert.Equal(expected, versionData)
  150. 'look at the same data through the FileVersion API.
  151. 'If the codepage and resource language information is not
  152. 'written correctly into the internal resource directory of
  153. 'the PE, then GetVersionInfo will fail to find the FileVersionInfo.
  154. 'Once upon a time in Roslyn, the codepage and lang info was not written correctly.
  155. Dim fileVer = FileVersionInfo.GetVersionInfo(exe.Path)
  156. Assert.Equal("Microsoft Corporation", fileVer.CompanyName)
  157. End Sub
  158. <Fact()>
  159. Public Sub FaultyResourceDataProvider()
  160. Dim c1 = VisualBasicCompilation.Create("foo", references:={MscorlibRef}, options:=TestOptions.ReleaseDll)
  161. Dim result = c1.Emit(New MemoryStream(),
  162. manifestResources:={New ResourceDescription("r2", "file", Function()
  163. Throw New Exception("bad stuff")
  164. End Function, False)})
  165. result.Diagnostics.Verify(Diagnostic(ERRID.ERR_UnableToOpenResourceFile1).WithArguments("file", "bad stuff"))
  166. result = c1.Emit(New MemoryStream(),
  167. manifestResources:={New ResourceDescription("r2", "file", Function() Nothing, False)})
  168. result.Diagnostics.Verify(Diagnostic(ERRID.ERR_UnableToOpenResourceFile1).WithArguments("file", CodeAnalysisResources.ResourceDataProviderShouldReturnNonNullStream))
  169. End Sub
  170. <Fact>
  171. Public Sub AddManagedResource()
  172. ' Use a unique guid as a compilation name to prevent conflicts with other assemblies loaded via Assembly.ReflectionOnlyLoad:
  173. Dim c1 As VisualBasicCompilation = CreateCompilationWithMscorlibAndVBRuntime(
  174. <compilation><file name="a.vb">
  175. Module Module1
  176. Sub Main()
  177. System.Console.WriteLine()
  178. End Sub
  179. End Module
  180. </file></compilation>)
  181. Dim output As New IO.MemoryStream
  182. Dim resourceFileName = "RoslynResourceFile.foo"
  183. Dim r1Name As String = "some.dotted.NAME"
  184. Dim r2Name As String = "another.DoTtEd.NAME"
  185. Dim arrayOfEmbeddedData() As Byte = {1, 2, 3, 4, 5}
  186. Dim resourceFileData() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  187. Dim result As EmitResult = c1.Emit(output, manifestResources:=New ResourceDescription(1) _
  188. {
  189. New ResourceDescription(r1Name, Function() New IO.MemoryStream(arrayOfEmbeddedData), True),
  190. New ResourceDescription(r2Name, resourceFileName, Function() New IO.MemoryStream(resourceFileData), False)
  191. })
  192. Assert.True(result.Success)
  193. Dim assembly As Reflection.Assembly = Reflection.Assembly.ReflectionOnlyLoad(output.ToArray())
  194. Dim resourceNames As String() = assembly.GetManifestResourceNames()
  195. Assert.Equal(2, resourceNames.Length)
  196. Dim rInfo As Reflection.ManifestResourceInfo = assembly.GetManifestResourceInfo(r1Name)
  197. Assert.Equal(Reflection.ResourceLocation.Embedded Or Reflection.ResourceLocation.ContainedInManifestFile, rInfo.ResourceLocation)
  198. Dim rData As IO.Stream = assembly.GetManifestResourceStream(r1Name)
  199. Dim rBytes(CInt(rData.Length - 1)) As Byte
  200. rData.Read(rBytes, 0, CInt(rData.Length))
  201. Assert.Equal(arrayOfEmbeddedData, rBytes)
  202. rInfo = assembly.GetManifestResourceInfo(r2Name)
  203. Assert.Equal(resourceFileName, rInfo.FileName)
  204. End Sub
  205. <Fact>
  206. Public Sub AddManagedLinkedResourceFail()
  207. Dim c1 As VisualBasicCompilation = CreateCompilationWithMscorlibAndVBRuntime(
  208. <compilation><file name="a.vb">
  209. Module Module1
  210. Sub Main()
  211. System.Console.WriteLine()
  212. End Sub
  213. End Module
  214. </file></compilation>)
  215. Dim output As New IO.MemoryStream
  216. Dim r2Name As String = "another.DoTtEd.NAME"
  217. Dim result As EmitResult = c1.Emit(output, manifestResources:=New ResourceDescription(0) _
  218. {
  219. New ResourceDescription(r2Name, Function()
  220. Throw New NotSupportedException()
  221. End Function, False)
  222. })
  223. Assert.False(result.Success)
  224. result.Diagnostics.Verify(
  225. Diagnostic(ERRID.ERR_UnableToOpenResourceFile1).WithArguments("another.DoTtEd.NAME", New NotSupportedException().Message))
  226. End Sub
  227. <Fact>
  228. Public Sub AddManagedEmbeddedResourceFail()
  229. Dim c1 As VisualBasicCompilation = CreateCompilationWithMscorlibAndVBRuntime(
  230. <compilation><file name="a.vb">
  231. Module Module1
  232. Sub Main()
  233. System.Console.WriteLine()
  234. End Sub
  235. End Module
  236. </file></compilation>)
  237. Dim output As New IO.MemoryStream
  238. Dim r1Name As String = "some.dotted.NAME"
  239. Dim result As EmitResult = c1.Emit(output, manifestResources:=New ResourceDescription(0) _
  240. {
  241. New ResourceDescription(r1Name, Function()
  242. Throw New NotSupportedException()
  243. End Function, True)
  244. })
  245. Assert.False(result.Success)
  246. result.Diagnostics.Verify(
  247. Diagnostic(ERRID.ERR_UnableToOpenResourceFile1).WithArguments("some.dotted.NAME", New NotSupportedException().Message))
  248. End Sub
  249. <Fact>
  250. Public Sub ResourceWithAttrSettings()
  251. Dim c1 As VisualBasicCompilation = CreateCompilationWithMscorlibAndVBRuntime(
  252. <compilation name="Win32VerAttrs">
  253. <file name="a.vb"><![CDATA[
  254. <Assembly: System.Reflection.AssemblyVersion("1.2.3.4")>
  255. <Assembly: System.Reflection.AssemblyFileVersion("5.6.7.8")>
  256. <Assembly: System.Reflection.AssemblyTitle("One Hundred Years of Solitude")>
  257. <Assembly: System.Reflection.AssemblyDescription("A classic of magical realist literature")>
  258. <Assembly: System.Reflection.AssemblyCompany("MossBrain")>
  259. <Assembly: System.Reflection.AssemblyProduct("Sound Cannon")>
  260. <Assembly: System.Reflection.AssemblyCopyright("circle C")>
  261. <Assembly: System.Reflection.AssemblyTrademark("circle R")>
  262. <Assembly: System.Reflection.AssemblyInformationalVersion("1.2.3garbage")>
  263. Module Module1
  264. Sub Main()
  265. System.Console.WriteLine()
  266. End Sub
  267. End Module
  268. ]]>
  269. </file>
  270. </compilation>, options:=TestOptions.ReleaseExe)
  271. Dim exeFile = Temp.CreateFile()
  272. Using output As FileStream = exeFile.Open()
  273. c1.Emit(output, win32Resources:=c1.CreateDefaultWin32Resources(True, False, Nothing, Nothing))
  274. End Using
  275. c1 = Nothing
  276. Dim versionData As String
  277. 'Open as data
  278. Dim [lib] As IntPtr = IntPtr.Zero
  279. Try
  280. [lib] = LoadLibraryEx(exeFile.Path, IntPtr.Zero, &H2)
  281. Assert.True([lib] <> IntPtr.Zero, String.Format("LoadLibrary failed with HResult: {0:X}", Marshal.GetLastWin32Error()))
  282. 'the manifest and version primitives are tested elsewhere. This is to test that the default
  283. 'values are passed to the primitives that assemble the resources.
  284. Dim size As UInteger
  285. Dim versionRsrc As IntPtr = Win32Res.GetResource([lib], "#1", "#16", size)
  286. versionData = Win32Res.VersionResourceToXml(versionRsrc)
  287. Finally
  288. If [lib] <> IntPtr.Zero Then
  289. FreeLibrary([lib])
  290. End If
  291. End Try
  292. Dim expected As String =
  293. "<?xml version=""1.0"" encoding=""utf-16""?>" & vbCrLf &
  294. "<VersionResource Size=""964"">" & vbCrLf &
  295. " <VS_FIXEDFILEINFO FileVersionMS=""00050006"" FileVersionLS=""00070008"" ProductVersionMS=""00000000"" ProductVersionLS=""00000000"" />" & vbCrLf &
  296. " <KeyValuePair Key=""Comments"" Value=""A classic of magical realist literature"" />" & vbCrLf &
  297. " <KeyValuePair Key=""CompanyName"" Value=""MossBrain"" />" & vbCrLf &
  298. " <KeyValuePair Key=""FileDescription"" Value=""One Hundred Years of Solitude"" />" & vbCrLf &
  299. " <KeyValuePair Key=""FileVersion"" Value=""5.6.7.8"" />" & vbCrLf &
  300. " <KeyValuePair Key=""InternalName"" Value=""Win32VerAttrs.exe"" />" & vbCrLf &
  301. " <KeyValuePair Key=""LegalCopyright"" Value=""circle C"" />" & vbCrLf &
  302. " <KeyValuePair Key=""LegalTrademarks"" Value=""circle R"" />" & vbCrLf &
  303. " <KeyValuePair Key=""OriginalFilename"" Value=""Win32VerAttrs.exe"" />" & vbCrLf &
  304. " <KeyValuePair Key=""ProductName"" Value=""Sound Cannon"" />" & vbCrLf &
  305. " <KeyValuePair Key=""ProductVersion"" Value=""1.2.3garbage"" />" & vbCrLf &
  306. " <KeyValuePair Key=""Assembly Version"" Value=""1.2.3.4"" />" & vbCrLf &
  307. "</VersionResource>"
  308. Assert.Equal(expected, versionData)
  309. End Sub
  310. <WorkItem(543501, "DevDiv")>
  311. <Fact()>
  312. Public Sub BC31502_DuplicateMainfestResourceIdentifier()
  313. Dim c1 As VisualBasicCompilation = CreateCompilationWithMscorlibAndVBRuntime(
  314. <compilation><file name="a.vb">
  315. Module Module1
  316. Sub Main()
  317. System.Console.WriteLine()
  318. End Sub
  319. End Module
  320. </file></compilation>)
  321. Dim output As New IO.MemoryStream
  322. Dim dataProvider = Function() New IO.MemoryStream(New Byte() {})
  323. Dim result As EmitResult = c1.Emit(output, manifestResources:=New ResourceDescription(1) _
  324. {
  325. New ResourceDescription("A", "x.foo", dataProvider, True),
  326. New ResourceDescription("A", "y.foo", dataProvider, True)
  327. })
  328. ' error BC31502: Resource name 'A' cannot be used more than once.
  329. result.Diagnostics.Verify(Diagnostic(ERRID.ERR_DuplicateResourceName1).WithArguments("A"))
  330. End Sub
  331. <Fact()>
  332. Public Sub AddResourceToModule()
  333. Dim source =
  334. <compilation><file name="a.vb">
  335. </file></compilation>
  336. Dim c1 As VisualBasicCompilation = CreateCompilationWithMscorlibAndVBRuntime(source, TestOptions.ReleaseModule)
  337. Dim output As New IO.MemoryStream
  338. Dim dataProvider = Function() New IO.MemoryStream(New Byte() {})
  339. Dim r1Name As String = "some.dotted.NAME"
  340. Dim r2Name As String = "another.DoTtEd.NAME"
  341. Dim arrayOfEmbeddedData() As Byte = {1, 2, 3, 4, 5}
  342. Dim resourceFileData() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  343. Dim result As EmitResult = c1.Emit(output, manifestResources:=
  344. {
  345. New ResourceDescription(r1Name, Function() New IO.MemoryStream(arrayOfEmbeddedData), True),
  346. New ResourceDescription("A", "y.foo", dataProvider, True)
  347. })
  348. Assert.False(result.Success)
  349. result.Diagnostics.Verify(Diagnostic(ERRID.ERR_ResourceInModule))
  350. result = c1.Emit(output, manifestResources:=
  351. {
  352. New ResourceDescription("A", "y.foo", dataProvider, True),
  353. New ResourceDescription(r1Name, Function() New IO.MemoryStream(arrayOfEmbeddedData), True)
  354. })
  355. Assert.False(result.Success)
  356. result.Diagnostics.Verify(Diagnostic(ERRID.ERR_ResourceInModule))
  357. result = c1.Emit(output, manifestResources:=
  358. {
  359. New ResourceDescription("A", "y.foo", dataProvider, True)
  360. })
  361. Assert.False(result.Success)
  362. result.Diagnostics.Verify(Diagnostic(ERRID.ERR_ResourceInModule))
  363. Dim c_mod1 = CreateCompilationWithMscorlib(source, TestOptions.ReleaseModule)
  364. Dim output_mod1 = New MemoryStream()
  365. result = c_mod1.Emit(output_mod1, manifestResources:=
  366. {
  367. New ResourceDescription(r1Name, Function() New IO.MemoryStream(arrayOfEmbeddedData), True)
  368. })
  369. Assert.True(result.Success)
  370. Dim mod1 = ModuleMetadata.CreateFromImage(output_mod1.ToImmutable())
  371. Dim ref_mod1 = mod1.GetReference()
  372. Assert.Equal(ManifestResourceAttributes.Public, mod1.Module.GetEmbeddedResourcesOrThrow()(0).Attributes)
  373. If True Then
  374. Dim C2 = CreateCompilationWithMscorlibAndReferences(source, {ref_mod1}, TestOptions.ReleaseDll)
  375. Dim output2 = New MemoryStream()
  376. Dim result2 = C2.Emit(output2)
  377. Assert.True(result2.Success)
  378. Dim assembly = System.Reflection.Assembly.ReflectionOnlyLoad(output2.ToArray())
  379. AddHandler assembly.ModuleResolve, Function(sender As Object, e As ResolveEventArgs)
  380. If (e.Name.Equals(c_mod1.SourceModule.Name)) Then
  381. Return assembly.LoadModule(e.Name, output_mod1.ToArray())
  382. End If
  383. Return Nothing
  384. End Function
  385. Dim resourceNames As String() = assembly.GetManifestResourceNames()
  386. Assert.Equal(1, resourceNames.Length)
  387. Dim rInfo = assembly.GetManifestResourceInfo(r1Name)
  388. Assert.Equal(System.Reflection.ResourceLocation.Embedded, rInfo.ResourceLocation)
  389. Assert.Equal(c_mod1.SourceModule.Name, rInfo.FileName)
  390. Dim rData = assembly.GetManifestResourceStream(r1Name)
  391. Dim rBytes = New Byte(CInt(rData.Length) - 1) {}
  392. rData.Read(rBytes, 0, CInt(rData.Length))
  393. Assert.Equal(arrayOfEmbeddedData, rBytes)
  394. End If
  395. Dim c_mod2 = CreateCompilationWithMscorlib(source, TestOptions.ReleaseModule)
  396. Dim output_mod2 = New MemoryStream()
  397. result = c_mod2.Emit(output_mod2, manifestResources:=
  398. {
  399. New ResourceDescription(r1Name, Function() New MemoryStream(arrayOfEmbeddedData), True),
  400. New ResourceDescription(r2Name, Function() New MemoryStream(resourceFileData), True)
  401. })
  402. Assert.True(result.Success)
  403. Dim ref_mod2 = ModuleMetadata.CreateFromImage(output_mod2.ToImmutable()).GetReference()
  404. If True Then
  405. Dim C3 = CreateCompilationWithMscorlibAndReferences(source, {ref_mod2}, TestOptions.ReleaseDll)
  406. Dim output3 = New MemoryStream()
  407. Dim result3 = C3.Emit(output3)
  408. Assert.True(result3.Success)
  409. Dim assembly = System.Reflection.Assembly.ReflectionOnlyLoad(output3.ToArray())
  410. AddHandler assembly.ModuleResolve, Function(sender As Object, e As ResolveEventArgs)
  411. If (e.Name.Equals(c_mod2.SourceModule.Name)) Then
  412. Return assembly.LoadModule(e.Name, output_mod2.ToArray())
  413. End If
  414. Return Nothing
  415. End Function
  416. Dim resourceNames As String() = assembly.GetManifestResourceNames()
  417. Assert.Equal(2, resourceNames.Length)
  418. Dim rInfo = assembly.GetManifestResourceInfo(r1Name)
  419. Assert.Equal(ResourceLocation.Embedded, rInfo.ResourceLocation)
  420. Assert.Equal(c_mod2.SourceModule.Name, rInfo.FileName)
  421. Dim rData = assembly.GetManifestResourceStream(r1Name)
  422. Dim rBytes = New Byte(CInt(rData.Length) - 1) {}
  423. rData.Read(rBytes, 0, CInt(rData.Length))
  424. Assert.Equal(arrayOfEmbeddedData, rBytes)
  425. rInfo = assembly.GetManifestResourceInfo(r2Name)
  426. Assert.Equal(ResourceLocation.Embedded, rInfo.ResourceLocation)
  427. Assert.Equal(c_mod2.SourceModule.Name, rInfo.FileName)
  428. rData = assembly.GetManifestResourceStream(r2Name)
  429. rBytes = New Byte(CInt(rData.Length) - 1) {}
  430. rData.Read(rBytes, 0, CInt(rData.Length))
  431. Assert.Equal(resourceFileData, rBytes)
  432. End If
  433. Dim c_mod3 = CreateCompilationWithMscorlib(source, TestOptions.ReleaseModule)
  434. Dim output_mod3 = New MemoryStream()
  435. result = c_mod3.Emit(output_mod3, manifestResources:=
  436. {
  437. New ResourceDescription(r2Name, Function() New MemoryStream(resourceFileData), False)
  438. })
  439. Assert.True(result.Success)
  440. Dim mod3 = ModuleMetadata.CreateFromImage(output_mod3.ToImmutable())
  441. Dim ref_mod3 = mod3.GetReference()
  442. Assert.Equal(ManifestResourceAttributes.Private, mod3.Module.GetEmbeddedResourcesOrThrow()(0).Attributes)
  443. If True Then
  444. Dim C4 = CreateCompilationWithMscorlibAndReferences(source, {ref_mod3}, TestOptions.ReleaseDll)
  445. Dim output4 = New MemoryStream()
  446. Dim result4 = C4.Emit(output4, manifestResources:=
  447. {
  448. New ResourceDescription(r1Name, Function() New MemoryStream(arrayOfEmbeddedData), False)
  449. })
  450. Assert.True(result4.Success)
  451. Dim assembly = System.Reflection.Assembly.ReflectionOnlyLoad(output4.ToArray())
  452. AddHandler assembly.ModuleResolve, Function(sender As Object, e As ResolveEventArgs)
  453. If (e.Name.Equals(c_mod3.SourceModule.Name)) Then
  454. Return assembly.LoadModule(e.Name, output_mod3.ToArray())
  455. End If
  456. Return Nothing
  457. End Function
  458. Dim resourceNames As String() = assembly.GetManifestResourceNames()
  459. Assert.Equal(2, resourceNames.Length)
  460. Dim rInfo = assembly.GetManifestResourceInfo(r1Name)
  461. Assert.Equal(ResourceLocation.Embedded Or ResourceLocation.ContainedInManifestFile, rInfo.ResourceLocation)
  462. Dim rData = assembly.GetManifestResourceStream(r1Name)
  463. Dim rBytes = New Byte(CInt(rData.Length) - 1) {}
  464. rData.Read(rBytes, 0, CInt(rData.Length))
  465. Assert.Equal(arrayOfEmbeddedData, rBytes)
  466. rInfo = assembly.GetManifestResourceInfo(r2Name)
  467. Assert.Equal(ResourceLocation.Embedded, rInfo.ResourceLocation)
  468. Assert.Equal(c_mod3.SourceModule.Name, rInfo.FileName)
  469. rData = assembly.GetManifestResourceStream(r2Name)
  470. rBytes = New Byte(CInt(rData.Length) - 1) {}
  471. rData.Read(rBytes, 0, CInt(rData.Length))
  472. Assert.Equal(resourceFileData, rBytes)
  473. End If
  474. If True Then
  475. Dim c5 = CreateCompilationWithMscorlibAndReferences(source, {ref_mod1, ref_mod3}, TestOptions.ReleaseDll)
  476. Dim output5 = New MemoryStream()
  477. Dim result5 = c5.Emit(output5)
  478. Assert.True(result5.Success)
  479. Dim assembly = System.Reflection.Assembly.ReflectionOnlyLoad(output5.ToArray())
  480. AddHandler assembly.ModuleResolve, Function(sender As Object, e As ResolveEventArgs)
  481. If (e.Name.Equals(c_mod1.SourceModule.Name)) Then
  482. Return assembly.LoadModule(e.Name, output_mod1.ToArray())
  483. ElseIf (e.Name.Equals(c_mod3.SourceModule.Name)) Then
  484. Return assembly.LoadModule(e.Name, output_mod3.ToArray())
  485. End If
  486. Return Nothing
  487. End Function
  488. Dim resourceNames As String() = assembly.GetManifestResourceNames()
  489. Assert.Equal(2, resourceNames.Length)
  490. Dim rInfo = assembly.GetManifestResourceInfo(r1Name)
  491. Assert.Equal(System.Reflection.ResourceLocation.Embedded, rInfo.ResourceLocation)
  492. Assert.Equal(c_mod1.SourceModule.Name, rInfo.FileName)
  493. Dim rData = assembly.GetManifestResourceStream(r1Name)
  494. Dim rBytes = New Byte(CInt(rData.Length) - 1) {}
  495. rData.Read(rBytes, 0, CInt(rData.Length))
  496. Assert.Equal(arrayOfEmbeddedData, rBytes)
  497. rInfo = assembly.GetManifestResourceInfo(r2Name)
  498. Assert.Equal(System.Reflection.ResourceLocation.Embedded, rInfo.ResourceLocation)
  499. Assert.Equal(c_mod3.SourceModule.Name, rInfo.FileName)
  500. rData = assembly.GetManifestResourceStream(r2Name)
  501. rBytes = New Byte(CInt(rData.Length) - 1) {}
  502. rData.Read(rBytes, 0, CInt(rData.Length))
  503. Assert.Equal(resourceFileData, rBytes)
  504. End If
  505. If True Then
  506. Dim c6 = CreateCompilationWithMscorlibAndReferences(source, {ref_mod1, ref_mod2}, TestOptions.ReleaseDll)
  507. Dim output6 = New MemoryStream()
  508. Dim result6 = c6.Emit(output6)
  509. Assert.False(result6.Success)
  510. AssertTheseDiagnostics(result6.Diagnostics,
  511. <expected>
  512. BC31502: Resource name 'some.dotted.NAME' cannot be used more than once.
  513. </expected>)
  514. result6 = c6.Emit(output6, manifestResources:=
  515. {
  516. New ResourceDescription(r2Name, Function() New IO.MemoryStream(resourceFileData), False)
  517. })
  518. Assert.False(result6.Success)
  519. AssertTheseDiagnostics(result6.Diagnostics,
  520. <expected>
  521. BC31502: Resource name 'another.DoTtEd.NAME' cannot be used more than once.
  522. BC31502: Resource name 'some.dotted.NAME' cannot be used more than once.
  523. </expected>)
  524. c6 = CreateCompilationWithMscorlibAndReferences(source, {ref_mod1, ref_mod2}, TestOptions.ReleaseModule)
  525. result6 = c6.Emit(output6, manifestResources:=
  526. {
  527. New ResourceDescription(r2Name, Function() New IO.MemoryStream(resourceFileData), False)
  528. })
  529. Assert.True(result6.Success)
  530. End If
  531. End Sub
  532. <WorkItem(543501, "DevDiv")>
  533. <Fact()>
  534. Public Sub BC31502_DuplicateMainfestResourceIdentifier_EmbeddedResource()
  535. Dim c1 As VisualBasicCompilation = CreateCompilationWithMscorlibAndVBRuntime(
  536. <compilation><file name="a.vb">
  537. Module Module1
  538. Sub Main()
  539. System.Console.WriteLine()
  540. End Sub
  541. End Module
  542. </file></compilation>)
  543. Dim output As New IO.MemoryStream
  544. Dim dataProvider = Function() New IO.MemoryStream(New Byte() {})
  545. Dim result As EmitResult = c1.Emit(output, manifestResources:=New ResourceDescription(1) _
  546. {
  547. New ResourceDescription("A", dataProvider, True),
  548. New ResourceDescription("A", Nothing, dataProvider, True, isEmbedded:=True, checkArgs:=True)
  549. })
  550. ' error BC31502: Resource name 'A' cannot be used more than once.
  551. result.Diagnostics.Verify(Diagnostic(ERRID.ERR_DuplicateResourceName1).WithArguments("A"))
  552. ' file name ignored for embedded manifest resources
  553. result = c1.Emit(output, manifestResources:=New ResourceDescription(1) _
  554. {
  555. New ResourceDescription("A", "x.foo", dataProvider, True, isEmbedded:=True, checkArgs:=True),
  556. New ResourceDescription("A", "x.foo", dataProvider, True, isEmbedded:=False, checkArgs:=True)
  557. })
  558. ' error BC31502: Resource name 'A' cannot be used more than once.
  559. result.Diagnostics.Verify(Diagnostic(ERRID.ERR_DuplicateResourceName1).WithArguments("A"))
  560. End Sub
  561. <WorkItem(543501, "DevDiv"), WorkItem(546298, "DevDiv")>
  562. <Fact()>
  563. Public Sub BC35003_DuplicateMainfestResourceFileName()
  564. Dim c1 As Compilation = CreateCompilationWithMscorlibAndVBRuntime(
  565. <compilation>
  566. <file name="a.vb">
  567. Module Module1
  568. Sub Main()
  569. System.Console.WriteLine()
  570. End Sub
  571. End Module
  572. </file>
  573. </compilation>)
  574. Dim output As New IO.MemoryStream
  575. Dim dataProvider = Function() New IO.MemoryStream(New Byte() {})
  576. Dim result = c1.Emit(output, manifestResources:=New ResourceDescription(1) _
  577. {
  578. New ResourceDescription("A", "x.foo", dataProvider, True),
  579. New ResourceDescription("B", "x.foo", dataProvider, True)
  580. })
  581. ' error BC35003: Each linked resource and module must have a unique filename. Filename 'x.foo' is specified more than once in this assembly.
  582. result.Diagnostics.Verify(Diagnostic(ERRID.ERR_DuplicateResourceFileName1).WithArguments("x.foo"))
  583. result = c1.Emit(output, manifestResources:=New ResourceDescription(0) _
  584. {
  585. New ResourceDescription("A", "C.dll", dataProvider, True)
  586. })
  587. result.Diagnostics.Verify()
  588. Dim netModule1 = TestReferences.SymbolsTests.netModule.netModule1
  589. c1 = VisualBasicCompilation.Create("foo", references:={MscorlibRef, netModule1}, options:=TestOptions.ReleaseDll)
  590. result = c1.Emit(output, manifestResources:=New ResourceDescription(0) _
  591. {
  592. New ResourceDescription("A", "netmodule1.netmodule", dataProvider, True)
  593. })
  594. ' Native compiler gives BC30144 which is no longer used in Roslyn
  595. ' error BC35003: Each linked resource and module must have a unique filename. Filename 'netmodule1.netmodule' is specified more than once in this assembly
  596. result.Diagnostics.Verify(Diagnostic(ERRID.ERR_DuplicateResourceFileName1).WithArguments("netModule1.netmodule"))
  597. End Sub
  598. <WorkItem(543501, "DevDiv")>
  599. <Fact()>
  600. Public Sub NoDuplicateMainfestResourceFileNameDiagnosticForEmbeddedResources()
  601. Dim c1 As VisualBasicCompilation = CreateCompilationWithMscorlibAndVBRuntime(
  602. <compilation><file name="a.vb">
  603. Module Module1
  604. Sub Main()
  605. System.Console.WriteLine()
  606. End Sub
  607. End Module
  608. </file></compilation>)
  609. Dim output As New IO.MemoryStream
  610. Dim dataProvider = Function() New IO.MemoryStream(New Byte() {})
  611. Dim result As EmitResult = c1.Emit(output, manifestResources:=New ResourceDescription(1) _
  612. {
  613. New ResourceDescription("A", dataProvider, True),
  614. New ResourceDescription("B", Nothing, dataProvider, True, isEmbedded:=True, checkArgs:=True)
  615. })
  616. result.Diagnostics.Verify()
  617. ' file name ignored for embedded manifest resources
  618. result = c1.Emit(output, manifestResources:=New ResourceDescription(1) _
  619. {
  620. New ResourceDescription("A", "x.foo", dataProvider, True, isEmbedded:=True, checkArgs:=True),
  621. New ResourceDescription("B", "x.foo", dataProvider, True, isEmbedded:=False, checkArgs:=True)
  622. })
  623. result.Diagnostics.Verify()
  624. End Sub
  625. <WorkItem(543501, "DevDiv")>
  626. <Fact()>
  627. Public Sub BC31502_BC35003_DuplicateMainfestResourceDiagnostics()
  628. Dim c1 As VisualBasicCompilation = CreateCompilationWithMscorlibAndVBRuntime(
  629. <compilation><file name="a.vb">
  630. Module Module1
  631. Sub Main()
  632. System.Console.WriteLine()
  633. End Sub
  634. End Module
  635. </file></compilation>)
  636. Dim output As New IO.MemoryStream
  637. Dim dataProvider = Function() New IO.MemoryStream(New Byte() {})
  638. Dim result As EmitResult = c1.Emit(output, manifestResources:=New ResourceDescription(1) _
  639. {
  640. New ResourceDescription("A", "x.foo", dataProvider, True),
  641. New ResourceDescription("A", "x.foo", dataProvider, True)
  642. })
  643. ' error BC31502: Resource name 'A' cannot be used more than once.
  644. ' error BC35003: Each linked resource and module must have a unique filename. Filename 'x.foo' is specified more than once in this assembly.
  645. result.Diagnostics.Verify(
  646. Diagnostic(ERRID.ERR_DuplicateResourceName1).WithArguments("A"),
  647. Diagnostic(ERRID.ERR_DuplicateResourceFileName1).WithArguments("x.foo"))
  648. result = c1.Emit(output, manifestResources:=New ResourceDescription(2) _
  649. {
  650. New ResourceDescription("A", "x.foo", dataProvider, True),
  651. New ResourceDescription("B", "x.foo", dataProvider, True),
  652. New ResourceDescription("B", "y.foo", dataProvider, True)
  653. })
  654. ' error BC35003: Each linked resource andmust have a unique filename. Filename 'x.foo' is specified more than once in this assembly.
  655. ' error BC31502: Resource name 'B' cannot be used more than once.
  656. result.Diagnostics.Verify(
  657. Diagnostic(ERRID.ERR_DuplicateResourceFileName1).WithArguments("x.foo"),
  658. Diagnostic(ERRID.ERR_DuplicateResourceName1).WithArguments("B"))
  659. End Sub
  660. End Class
  661. End Namespace