InternalsVisibleToAndStrongNameTests.vb 67.0 KB
Newer Older
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.
P
Pilchie 已提交
2 3 4 5 6

Imports System.Collections.Immutable
Imports System.IO
Imports System.Reflection.Metadata
Imports System.Reflection.PortableExecutable
7
Imports System.Xml.Linq
P
Pilchie 已提交
8 9 10 11 12 13 14 15
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Collections
Imports Microsoft.CodeAnalysis.Emit
Imports Microsoft.CodeAnalysis.Test.Utilities
Imports Microsoft.CodeAnalysis.VisualBasic
Imports Microsoft.CodeAnalysis.VisualBasic.UnitTests
Imports Roslyn.Test.Utilities

16
Partial Public Class InternalsVisibleToAndStrongNameTests
P
Pilchie 已提交
17 18 19 20
    Inherits BasicTestBase

#Region "Helpers"

B
beep boop 已提交
21
    Public Sub New()
P
Pilchie 已提交
22 23 24
        SigningTestHelpers.InstallKey()
    End Sub

B
beep boop 已提交
25 26 27 28
    Private Shared ReadOnly s_keyPairFile As String = SigningTestHelpers.KeyPairFile
    Private Shared ReadOnly s_publicKeyFile As String = SigningTestHelpers.PublicKeyFile
    Private Shared ReadOnly s_publicKey As ImmutableArray(Of Byte) = SigningTestHelpers.PublicKey
    Private Shared ReadOnly s_defaultProvider As DesktopStrongNameProvider = New SigningTestHelpers.VirtualizedStrongNameProvider(ImmutableArray.Create(Of String)())
P
Pilchie 已提交
29 30 31 32 33 34 35 36 37 38 39

    Private Shared Function GetProviderWithPath(keyFilePath As String) As DesktopStrongNameProvider
        Return New SigningTestHelpers.VirtualizedStrongNameProvider(ImmutableArray.Create(keyFilePath))
    End Function

#End Region

#Region "Naming Tests"

    <Fact>
    Public Sub PubKeyFromKeyFileAttribute()
B
beep boop 已提交
40
        Dim x = s_keyPairFile
P
Pilchie 已提交
41 42 43 44 45
        Dim s = "<Assembly: System.Reflection.AssemblyKeyFile(""" & x & """)>" & vbCrLf &
                "Public Class C" & vbCrLf &
                "End Class"

        Dim g = Guid.NewGuid()
A
angocke 已提交
46
        Dim other = VisualBasicCompilation.Create(
P
Pilchie 已提交
47
            g.ToString(),
A
angocke 已提交
48
            {VisualBasicSyntaxTree.ParseText(s)},
P
Pilchie 已提交
49
            {MscorlibRef},
B
beep boop 已提交
50
            TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
51 52

        other.VerifyDiagnostics()
B
beep boop 已提交
53
        Assert.True(ByteSequenceComparer.Equals(s_publicKey, other.Assembly.Identity.PublicKey))
P
Pilchie 已提交
54 55 56 57
    End Sub

    <Fact>
    Public Sub PubKeyFromKeyFileAttribute_AssemblyKeyFileResolver()
B
beep boop 已提交
58 59
        Dim keyFileDir = Path.GetDirectoryName(s_keyPairFile)
        Dim keyFileName = Path.GetFileName(s_keyPairFile)
P
Pilchie 已提交
60 61 62 63 64 65 66 67

        Dim s = "<Assembly: System.Reflection.AssemblyKeyFile(""" & keyFileName & """)>" & vbCrLf &
                "Public Class C" & vbCrLf &
                "End Class"

        Dim syntaxTree = ParseAndVerify(s)

        ' verify failure with default assembly key file resolver
68
        Dim comp = CreateCompilationWithMscorlib({syntaxTree}, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
69
        comp.VerifyDiagnostics(
70
            Diagnostic(ERRID.ERR_PublicKeyFileFailure).WithArguments(keyFileName, CodeAnalysisResources.FileNotFound))
P
Pilchie 已提交
71 72 73 74

        Assert.True(comp.Assembly.Identity.PublicKey.IsEmpty)

        ' verify success with custom assembly key file resolver with keyFileDir added to search paths
A
angocke 已提交
75
        comp = VisualBasicCompilation.Create(
P
Pilchie 已提交
76 77 78
            GetUniqueName(),
            {syntaxTree},
            {MscorlibRef},
79
            TestOptions.ReleaseDll.WithStrongNameProvider(GetProviderWithPath(keyFileDir)))
P
Pilchie 已提交
80 81

        comp.VerifyDiagnostics()
B
beep boop 已提交
82
        Assert.True(ByteSequenceComparer.Equals(s_publicKey, comp.Assembly.Identity.PublicKey))
P
Pilchie 已提交
83 84 85 86
    End Sub

    <Fact>
    Public Sub PubKeyFromKeyFileAttribute_AssemblyKeyFileResolver_RelativeToCurrentParent()
B
beep boop 已提交
87 88
        Dim keyFileDir = Path.GetDirectoryName(s_keyPairFile)
        Dim keyFileName = Path.GetFileName(s_keyPairFile)
P
Pilchie 已提交
89 90 91 92 93 94 95 96

        Dim s = "<Assembly: System.Reflection.AssemblyKeyFile(""..\" & keyFileName & """)>" & vbCrLf &
                "Public Class C" & vbCrLf &
                "End Class"

        Dim syntaxTree = ParseAndVerify(s)

        ' verify failure with default assembly key file resolver
97
        Dim comp As Compilation = CreateCompilationWithMscorlib({syntaxTree}, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
98
        comp.VerifyDiagnostics(
99
            Diagnostic(ERRID.ERR_PublicKeyFileFailure).WithArguments("..\" & keyFileName, CodeAnalysisResources.FileNotFound))
P
Pilchie 已提交
100 101 102 103

        Assert.True(comp.Assembly.Identity.PublicKey.IsEmpty)

        ' verify success with custom assembly key file resolver with keyFileDir\TempSubDir added to search paths
A
angocke 已提交
104
        comp = VisualBasicCompilation.Create(
P
Pilchie 已提交
105 106 107
            GetUniqueName(),
            references:={MscorlibRef},
            syntaxTrees:={syntaxTree},
108
            options:=TestOptions.ReleaseDll.WithStrongNameProvider(GetProviderWithPath(PathUtilities.CombineAbsoluteAndRelativePaths(keyFileDir, "TempSubDir\"))))
P
Pilchie 已提交
109 110

        comp.VerifyDiagnostics()
B
beep boop 已提交
111
        Assert.True(ByteSequenceComparer.Equals(s_publicKey, comp.Assembly.Identity.PublicKey))
P
Pilchie 已提交
112 113 114 115
    End Sub

    <Fact>
    Public Sub PubKeyFromKeyContainerAttribute()
A
angocke 已提交
116
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
117 118 119 120 121 122 123 124 125
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblyKeyName("roslynTestContainer")>
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
126
</compilation>, TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
127 128

        other.VerifyDiagnostics()
B
beep boop 已提交
129
        Assert.True(ByteSequenceComparer.Equals(s_publicKey, other.Assembly.Identity.PublicKey))
P
Pilchie 已提交
130 131 132 133
    End Sub

    <Fact>
    Public Sub PubKeyFromKeyFileOptions()
A
angocke 已提交
134
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
135 136 137 138 139 140 141 142 143
<compilation>
    <file name="a.vb"><![CDATA[
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
</compilation>,
B
beep boop 已提交
144
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
145 146

        other.VerifyDiagnostics()
B
beep boop 已提交
147
        Assert.True(ByteSequenceComparer.Equals(s_publicKey, other.Assembly.Identity.PublicKey))
P
Pilchie 已提交
148 149 150 151
    End Sub

    <Fact>
    Public Sub PubKeyFromKeyFileOptions_ReferenceResolver()
B
beep boop 已提交
152 153
        Dim keyFileDir = Path.GetDirectoryName(s_keyPairFile)
        Dim keyFileName = Path.GetFileName(s_keyPairFile)
P
Pilchie 已提交
154 155 156 157 158 159 160 161 162 163 164

        Dim source = <![CDATA[
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
        Dim references = {MscorlibRef}
        Dim syntaxTrees = {ParseAndVerify(source)}

        ' verify failure with default resolver
A
angocke 已提交
165
        Dim comp = VisualBasicCompilation.Create(
P
Pilchie 已提交
166 167 168
            GetUniqueName(),
            references:=references,
            syntaxTrees:=syntaxTrees,
B
beep boop 已提交
169
            options:=TestOptions.ReleaseDll.WithCryptoKeyFile(keyFileName).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
170 171

        comp.VerifyDiagnostics(
172
            Diagnostic(ERRID.ERR_PublicKeyFileFailure).WithArguments(keyFileName, CodeAnalysisResources.FileNotFound))
P
Pilchie 已提交
173 174 175 176

        Assert.True(comp.Assembly.Identity.PublicKey.IsEmpty)

        ' verify success with custom assembly key file resolver with keyFileDir added to search paths
A
angocke 已提交
177
        comp = VisualBasicCompilation.Create(
P
Pilchie 已提交
178 179 180
            GetUniqueName(),
            references:=references,
            syntaxTrees:=syntaxTrees,
181
            options:=TestOptions.ReleaseDll.WithCryptoKeyFile(keyFileName).WithStrongNameProvider(GetProviderWithPath(keyFileDir)))
P
Pilchie 已提交
182 183

        comp.VerifyDiagnostics()
B
beep boop 已提交
184
        Assert.True(ByteSequenceComparer.Equals(s_publicKey, comp.Assembly.Identity.PublicKey))
P
Pilchie 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198
    End Sub

    <Fact>
    Public Sub PubKeyFromKeyFileOptionsJustPublicKey()
        Dim s =
            <compilation>
                <file name="Clavelle.vb"><![CDATA[
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
                </file>
            </compilation>
B
beep boop 已提交
199
        Dim other = CreateCompilationWithMscorlib(s, options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_publicKeyFile).WithDelaySign(True).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
200 201

        Assert.Empty(other.GetDiagnostics())
202
        Assert.True(ByteSequenceComparer.Equals(TestResources.General.snPublicKey.AsImmutableOrNull(), other.Assembly.Identity.PublicKey))
P
Pilchie 已提交
203 204 205 206
    End Sub

    <Fact>
    Public Sub PubKeyFromKeyFileOptionsJustPublicKey_ReferenceResolver()
B
beep boop 已提交
207 208
        Dim publicKeyFileDir = Path.GetDirectoryName(s_publicKeyFile)
        Dim publicKeyFileName = Path.GetFileName(s_publicKeyFile)
P
Pilchie 已提交
209 210 211 212 213 214 215 216 217 218 219 220

        Dim source = <![CDATA[
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>

        Dim references = {MscorlibRef}
        Dim syntaxTrees = {ParseAndVerify(source)}

        ' verify failure with default resolver
A
angocke 已提交
221
        Dim comp = VisualBasicCompilation.Create(
P
Pilchie 已提交
222 223 224
            GetUniqueName(),
            references:=references,
            syntaxTrees:=syntaxTrees,
B
beep boop 已提交
225
            options:=TestOptions.ReleaseDll.WithCryptoKeyFile(publicKeyFileName).WithDelaySign(True).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
226 227 228 229

        ' error CS7027: Error extracting public key from file 'PublicKeyFile.snk' -- File not found.
        ' warning CS7033: Delay signing was specified and requires a public key, but no public key was specified
        comp.VerifyDiagnostics(
230
            Diagnostic(ERRID.ERR_PublicKeyFileFailure).WithArguments(publicKeyFileName, CodeAnalysisResources.FileNotFound),
P
Pilchie 已提交
231 232 233 234
            Diagnostic(ERRID.WRN_DelaySignButNoKey))
        Assert.True(comp.Assembly.Identity.PublicKey.IsEmpty)

        ' verify success with custom assembly key file resolver with publicKeyFileDir added to search paths
A
angocke 已提交
235
        comp = VisualBasicCompilation.Create(
P
Pilchie 已提交
236 237 238
            GetUniqueName(),
            references:=references,
            syntaxTrees:=syntaxTrees,
239
            options:=TestOptions.ReleaseDll.WithCryptoKeyFile(publicKeyFileName).WithDelaySign(True).WithStrongNameProvider(GetProviderWithPath(publicKeyFileDir)))
P
Pilchie 已提交
240 241

        comp.VerifyDiagnostics()
B
beep boop 已提交
242
        Assert.True(ByteSequenceComparer.Equals(s_publicKey, comp.Assembly.Identity.PublicKey))
P
Pilchie 已提交
243 244 245 246
    End Sub

    <Fact>
    Public Sub PubKeyFileNotFoundOptions()
A
angocke 已提交
247
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
248 249 250 251 252 253 254 255 256
<compilation>
    <file name="a.vb"><![CDATA[
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
</compilation>,
B
beep boop 已提交
257
        options:=TestOptions.ReleaseExe.WithCryptoKeyFile("foo").WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
258 259 260

        CompilationUtils.AssertTheseDeclarationDiagnostics(other,
            <errors>
261 262
BC36980: Error extracting public key from file 'foo': <%= CodeAnalysisResources.FileNotFound %>
            </errors>)
P
Pilchie 已提交
263 264 265 266 267 268
        Assert.True(other.Assembly.Identity.PublicKey.IsEmpty)
    End Sub


    <Fact>
    Public Sub KeyFileAttributeEmpty()
A
angocke 已提交
269
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
270 271 272 273 274 275 276 277 278
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblyKeyFile("")>
Public Class C
 Friend Sub Foo()
    End Sub
End Class
]]>
    </file>
B
beep boop 已提交
279
</compilation>, TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
280 281 282 283 284 285 286

        other.VerifyDiagnostics()
        Assert.True(other.Assembly.Identity.PublicKey.IsEmpty)
    End Sub

    <Fact>
    Public Sub KeyContainerEmpty()
A
angocke 已提交
287
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
288 289 290 291 292 293 294 295 296
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblyKeyName("")>
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
297
</compilation>, TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
298 299 300 301 302

        other.VerifyDiagnostics()
        Assert.True(other.Assembly.Identity.PublicKey.IsEmpty)
    End Sub

303 304 305 306 307 308 309 310 311 312 313 314
    <Fact>
    Public Sub PublicKeyFromOptions_DelaySigned()
        Dim source =
<compilation>
    <file name="a.vb"><![CDATA[
<assembly: System.Reflection.AssemblyDelaySign(True)>
Public Class C 
End Class
]]>
    </file>
</compilation>

B
beep boop 已提交
315
        Dim c = CreateCompilationWithMscorlib(source, options:=TestOptions.ReleaseDll.WithCryptoPublicKey(s_publicKey))
316
        c.VerifyDiagnostics()
B
beep boop 已提交
317
        Assert.True(ByteSequenceComparer.Equals(s_publicKey, c.Assembly.Identity.PublicKey))
318 319 320 321 322

        Dim Metadata = ModuleMetadata.CreateFromImage(c.EmitToArray())
        Dim identity = Metadata.Module.ReadAssemblyIdentityOrThrow()

        Assert.True(identity.HasPublicKey)
B
beep boop 已提交
323
        AssertEx.Equal(identity.PublicKey, s_publicKey)
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
        Assert.Equal(CorFlags.ILOnly, Metadata.Module.PEReaderOpt.PEHeaders.CorHeader.Flags)
    End Sub

    <Fact>
    Public Sub PublicKeyFromOptions_OssSigned()
        ' attributes are ignored
        Dim source =
<compilation>
    <file name="a.vb"><![CDATA[
<assembly: System.Reflection.AssemblyKeyName("roslynTestContainer")>
<assembly: System.Reflection.AssemblyKeyFile("some file")>
Public Class C
End Class
]]>
    </file>
</compilation>

B
beep boop 已提交
341
        Dim c = CreateCompilationWithMscorlib(source, options:=TestOptions.ReleaseDll.WithCryptoPublicKey(s_publicKey))
342
        c.VerifyDiagnostics()
B
beep boop 已提交
343
        Assert.True(ByteSequenceComparer.Equals(s_publicKey, c.Assembly.Identity.PublicKey))
344 345 346 347 348

        Dim Metadata = ModuleMetadata.CreateFromImage(c.EmitToArray())
        Dim identity = Metadata.Module.ReadAssemblyIdentityOrThrow()

        Assert.True(identity.HasPublicKey)
B
beep boop 已提交
349
        AssertEx.Equal(identity.PublicKey, s_publicKey)
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        Assert.Equal(CorFlags.ILOnly Or CorFlags.StrongNameSigned, Metadata.Module.PEReaderOpt.PEHeaders.CorHeader.Flags)
    End Sub

    <Fact>
    Public Sub PublicKeyFromOptions_InvalidCompilationOptions()
        Dim source =
<compilation>
    <file name="a.vb"><![CDATA[
Public Class C 
End Class
]]>
    </file>
</compilation>

        Dim c = CreateCompilationWithMscorlib(source, options:=TestOptions.ReleaseDll.
            WithCryptoPublicKey(ImmutableArray.Create(Of Byte)(1, 2, 3)).
            WithCryptoKeyContainer("roslynTestContainer").
            WithCryptoKeyFile("file.snk").
B
beep boop 已提交
368
            WithStrongNameProvider(s_defaultProvider))
369 370 371 372 373 374 375 376 377

        AssertTheseDiagnostics(c,
<error>
BC2014: the value '01-02-03' is invalid for option 'CryptoPublicKey'
BC2046: Compilation options 'CryptoPublicKey' and 'CryptoKeyContainer' can't both be specified at the same time.
BC2046: Compilation options 'CryptoPublicKey' and 'CryptoKeyFile' can't both be specified at the same time.
</error>)
    End Sub

P
Pilchie 已提交
378 379 380
    <Fact>
    Public Sub PubKeyFileBogusOptions()
        Dim tmp = Temp.CreateFile().WriteAllBytes(New Byte() {1, 2, 3, 4})
A
angocke 已提交
381
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
382 383 384 385 386 387 388 389 390 391
<compilation>
    <file>
        <![CDATA[
Public Class C
Friend Sub Foo()
End Sub
End Class
]]>
    </file>
</compilation>,
392
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(tmp.Path).WithStrongNameProvider(New DesktopStrongNameProvider()))
P
Pilchie 已提交
393 394

        other.VerifyDiagnostics(
395
            Diagnostic(ERRID.ERR_PublicKeyFileFailure).WithArguments(tmp.Path, New ArgumentException().Message))
P
Pilchie 已提交
396 397 398 399 400 401

        Assert.True(other.Assembly.Identity.PublicKey.IsEmpty)
    End Sub

    <Fact>
    Public Sub PubKeyContainerBogusOptions()
A
angocke 已提交
402
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
403 404 405 406 407 408 409 410
<compilation>
    <file name="a.vb"><![CDATA[
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
411
</compilation>, options:=TestOptions.ReleaseExe.WithCryptoKeyContainer("foo").WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
412

413 414 415 416 417 418 419
        '        CompilationUtils.AssertTheseDeclarationDiagnostics(other,
        '            <errors>
        'BC36981: Error extracting public key from container 'foo': Keyset does not exist (Exception from HRESULT: 0x80090016)                    
        '                </errors>)
        Dim err = other.GetDeclarationDiagnostics().Single()

        Assert.Equal(ERRID.ERR_PublicKeyContainerFailure, err.Code)
420
        Assert.Equal(2, err.Arguments.Count)
421
        Assert.Equal("foo", DirectCast(err.Arguments(0), String))
422
        Assert.True(DirectCast(err.Arguments(1), String).EndsWith(" HRESULT: 0x80090016)", StringComparison.Ordinal))
423

P
Pilchie 已提交
424 425 426 427 428 429 430
        Assert.True(other.Assembly.Identity.PublicKey.IsEmpty)
    End Sub
#End Region

#Region "IVT Access checking"
    <Fact>
    Public Sub IVTBasicCompilation()
A
angocke 已提交
431
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
432 433 434 435 436 437 438 439 440
<compilation name="HasIVTToCompilation">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("WantsIVTAccess")>
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
441
</compilation>, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
442 443 444

        other.VerifyDiagnostics()

A
angocke 已提交
445
        Dim c As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
446 447 448 449 450 451 452 453 454 455 456
<compilation name="WantsIVTAccessButCantHave">
    <file name="a.vb"><![CDATA[
Public Class A
    Friend Class B
        Protected Sub New(o As C)
          o.Foo()
        End Sub
    End Class
End Class
]]>
    </file>
B
beep boop 已提交
457
</compilation>, {New VisualBasicCompilationReference(other)}, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
458 459 460 461 462 463 464 465 466 467

        'compilation should not succeed, and internals should not be imported.
        c.GetDiagnostics()

        CompilationUtils.AssertTheseDiagnostics(c, <error>
BC30390: 'C.Friend Sub Foo()' is not accessible in this context because it is 'Friend'.
          o.Foo()
          ~~~~~
</error>)

A
angocke 已提交
468
        Dim c2 As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
469 470 471 472 473 474 475 476 477 478 479
<compilation name="WantsIVTAccess">
    <file name="a.vb"><![CDATA[
Public Class A
    Friend Class B
        Protected Sub New(o As C)
          o.Foo()
        End Sub
    End Class
End Class
]]>
    </file>
B
beep boop 已提交
480
</compilation>, {New VisualBasicCompilationReference(other)}, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
481 482 483 484 485 486

        c2.VerifyDiagnostics()
    End Sub

    <Fact>
    Public Sub IVTBasicMetadata()
A
angocke 已提交
487
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
488 489 490 491 492 493 494 495 496
<compilation name="HasIVTToCompilation">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("WantsIVTAccess")>
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
497
</compilation>, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
498 499 500

        Dim otherImage = other.EmitToArray()

A
angocke 已提交
501
        Dim c As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
502 503 504 505 506 507 508 509 510 511 512
<compilation name="WantsIVTAccessButCantHave">
    <file name="a.vb"><![CDATA[
Public Class A
    Friend Class B
        Protected Sub New(o As C)
          o.Foo()
        End Sub
    End Class
End Class
]]>
    </file>
B
beep boop 已提交
513
</compilation>, {MetadataReference.CreateFromImage(otherImage)}, TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
514 515 516 517 518 519 520 521 522 523 524 525

        'compilation should not succeed, and internals should not be imported.
        c.GetDiagnostics()

        'gives "is not a member" error because internals were not imported because no IVT was found
        'on HasIVTToCompilation that referred to WantsIVTAccessButCantHave
        CompilationUtils.AssertTheseDiagnostics(c, <error>
BC30456: 'Foo' is not a member of 'C'.
          o.Foo()
          ~~~~~
</error>)

A
angocke 已提交
526
        Dim c2 As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
527 528 529 530 531 532 533 534 535 536 537
<compilation name="WantsIVTAccess">
    <file name="a.vb"><![CDATA[
Public Class A
    Friend Class B
        Protected Sub New(o As C)
          o.Foo()
        End Sub
    End Class
End Class
]]>
    </file>
B
beep boop 已提交
538
</compilation>, {MetadataReference.CreateFromImage(otherImage)}, TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
539 540 541 542 543 544

        c2.VerifyDiagnostics()
    End Sub

    <Fact>
    Public Sub SignModuleKeyContainerBogus()
A
angocke 已提交
545
        Dim c1 As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
546 547 548 549 550 551 552
<compilation name="WantsIVTAccess">
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblyKeyName("bogus")>
Public Class A
End Class
]]>
    </file>
B
beep boop 已提交
553
</compilation>, TestOptions.ReleaseModule.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
554 555 556 557

        'shouldn't have an error. The attribute's contents are checked when the module is added.
        Dim reference = c1.EmitToImageReference()

A
angocke 已提交
558
        Dim c2 As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
559 560 561 562 563 564
(<compilation name="WantsIVTAccess">
     <file name="a.vb"><![CDATA[
Public Class C
End Class
]]>
     </file>
B
beep boop 已提交
565
 </compilation>), {reference}, TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
566

567 568 569 570 571 572
        'c2.VerifyDiagnostics(Diagnostic(ERRID.ERR_PublicKeyContainerFailure).WithArguments("bogus", "Keyset does not exist (Exception from HRESULT: 0x80090016)"))
        Dim err = c2.GetDiagnostics(CompilationStage.Emit).Single()

        Assert.Equal(ERRID.ERR_PublicKeyContainerFailure, err.Code)
        Assert.Equal(2, err.Arguments.Count)
        Assert.Equal("bogus", DirectCast(err.Arguments(0), String))
573
        Assert.True(DirectCast(err.Arguments(1), String).EndsWith(" HRESULT: 0x80090016)", StringComparison.Ordinal))
P
Pilchie 已提交
574 575 576 577
    End Sub

    <Fact>
    Public Sub SignModuleKeyFileBogus()
A
angocke 已提交
578
        Dim c1 As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
579 580 581 582 583 584 585
<compilation name="WantsIVTAccess">
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblyKeyFile("bogus")>
Public Class A
End Class
]]>
    </file>
B
beep boop 已提交
586
</compilation>, TestOptions.ReleaseModule.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
587 588 589 590

        'shouldn't have an error. The attribute's contents are checked when the module is added.
        Dim reference = c1.EmitToImageReference()

A
angocke 已提交
591
        Dim c2 As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
592 593 594 595 596 597
(<compilation name="WantsIVTAccess">
     <file name="a.vb"><![CDATA[
Public Class C
End Class
]]>
     </file>
B
beep boop 已提交
598
 </compilation>), {reference}, TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
599

600
        c2.VerifyDiagnostics(Diagnostic(ERRID.ERR_PublicKeyFileFailure).WithArguments("bogus", CodeAnalysisResources.FileNotFound))
P
Pilchie 已提交
601 602 603 604
    End Sub

    <Fact>
    Public Sub IVTSigned()
A
angocke 已提交
605
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
606 607 608 609 610 611 612 613 614
<compilation name="Paul">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("John, PublicKey=00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb")>
Friend Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
615
</compilation>, options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithDelaySign(True).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
616 617 618

        other.VerifyDiagnostics()

A
angocke 已提交
619
        Dim requestor As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
620 621 622 623 624 625 626 627 628 629
<compilation name="John">
    <file name="a.vb"><![CDATA[
Public Class A
    Private Sub New(o As C)
        o.Foo()
    End Sub
End Class
]]>
    </file>
</compilation>,
B
beep boop 已提交
630
{New VisualBasicCompilationReference(other)}, TestOptions.ReleaseDll.WithCryptoKeyContainer("roslynTestContainer").WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
631

632
        Dim unused = requestor.Assembly.Identity
P
Pilchie 已提交
633 634 635 636 637
        requestor.VerifyDiagnostics()
    End Sub

    <Fact>
    Public Sub IVTErrorNotBothSigned()
A
angocke 已提交
638
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
639 640 641 642 643 644 645 646 647
<compilation name="Paul">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("John, PublicKey=00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb")>
Friend Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
648
</compilation>, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
649 650 651

        other.VerifyDiagnostics()

A
angocke 已提交
652
        Dim requestor As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
653 654 655 656 657 658 659 660 661
<compilation name="John">
    <file name="a.vb"><![CDATA[
Public Class A
    Private Sub New(o As C)
        o.Foo()
    End Sub
End Class
]]>
    </file>
B
beep boop 已提交
662
</compilation>, {New VisualBasicCompilationReference(other)}, TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithDelaySign(True).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
663

664
        Dim unused = requestor.Assembly.Identity
P
Pilchie 已提交
665 666 667 668 669 670 671 672 673 674
        'gives "is not accessible" error because internals were imported because IVT was found
        CompilationUtils.AssertTheseDiagnostics(requestor, <error>BC30389: 'C' is not accessible in this context because it is 'Friend'.
    Private Sub New(o As C)
                         ~
</error>)

    End Sub

    <Fact>
    Public Sub IVTDeferredSuccess()
A
angocke 已提交
675
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
676 677 678 679 680 681 682 683 684
<compilation name="Paul">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("John, PublicKey=00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb")>
Friend Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
685
</compilation>, options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithDelaySign(True).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
686 687
        other.VerifyDiagnostics()

A
angocke 已提交
688
        Dim requestor As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
689 690 691 692 693 694 695 696
<compilation name="John">
    <file name="a.vb"><![CDATA[
Imports MyC=C 'causes optimistic granting
<Assembly: System.Reflection.AssemblyKeyName("roslynTestContainer")>
Public Class A
End Class
]]>
    </file>
B
beep boop 已提交
697
</compilation>, {New VisualBasicCompilationReference(other)}, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
698

699
        Dim unused = requestor.Assembly.Identity
P
Pilchie 已提交
700 701 702 703 704 705
        Assert.True(DirectCast(other.Assembly, IAssemblySymbol).GivesAccessTo(requestor.Assembly))
        requestor.AssertNoDiagnostics()
    End Sub

    <Fact>
    Public Sub IVTDeferredFailSignMismatch()
A
angocke 已提交
706
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
707 708 709 710 711 712 713 714 715
<compilation name="Paul">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("John, PublicKey=00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb")>
Friend Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
716
</compilation>, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
717 718 719

        other.VerifyDiagnostics()

A
angocke 已提交
720
        Dim requestor As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
721 722 723 724 725 726 727 728
<compilation name="John">
    <file name="a.vb"><![CDATA[
Imports MyC=C
<Assembly: System.Reflection.AssemblyKeyName("roslynTestContainer")>
Public Class A
End Class
]]>
    </file>
B
beep boop 已提交
729
</compilation>, {New VisualBasicCompilationReference(other)}, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
730

731
        Dim unused = requestor.Assembly.Identity
P
Pilchie 已提交
732 733 734 735 736 737
        CompilationUtils.AssertTheseDiagnostics(requestor,
            <error>BC36958: Friend access was granted by 'Paul, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null', but the strong name signing state of the output assembly does not match that of the granting assembly.</error>)
    End Sub

    <Fact>
    Public Sub IVTDeferredFailKeyMismatch()
A
angocke 已提交
738
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
739 740 741 742 743 744 745 746 747 748
<compilation name="Paul">
    <file name="a.vb"><![CDATA[
'key is wrong in the first digit
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("John, PublicKey=10240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb")>
Friend Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
749
</compilation>, options:=TestOptions.ReleaseDll.WithCryptoKeyContainer("roslynTestContainer").WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
750 751 752

        other.VerifyDiagnostics()

A
angocke 已提交
753
        Dim requestor As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
754 755 756 757 758 759 760 761
<compilation name="John">
    <file name="a.vb"><![CDATA[
Imports MyC=C
<Assembly: System.Reflection.AssemblyKeyName("roslynTestContainer")>
Public Class A
End Class
]]>
    </file>
B
beep boop 已提交
762
</compilation>, {New VisualBasicCompilationReference(other)}, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
763

764
        Dim unused = requestor.Assembly.Identity
P
Pilchie 已提交
765 766 767 768 769 770 771
        CompilationUtils.AssertTheseDiagnostics(requestor, <errors>BC36957: Friend access was granted by 'Paul, Version=0.0.0.0, Culture=neutral, PublicKeyToken=ce65828c82a341f2', but the public key of the output assembly does not match that specified by the attribute in the granting assembly.</errors>)

    End Sub


    <Fact>
    Public Sub IVTSuccessThroughIAssembly()
A
angocke 已提交
772
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
773 774 775 776 777 778 779 780 781
<compilation name="Paul">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("John, PublicKey=00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb")>
Friend Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
782
</compilation>, options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithDelaySign(True).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
783 784 785

        other.VerifyDiagnostics()

A
angocke 已提交
786
        Dim requestor As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
787 788 789 790 791 792 793 794
<compilation name="John">
    <file name="a.vb"><![CDATA[
Imports MyC=C 'causes optimistic granting
<Assembly: System.Reflection.AssemblyKeyName("roslynTestContainer")>
Public Class A
End Class
]]>
    </file>
B
beep boop 已提交
795
</compilation>, {New VisualBasicCompilationReference(other)}, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
796 797 798 799 800 801

        Assert.True(DirectCast(other.Assembly, IAssemblySymbol).GivesAccessTo(requestor.Assembly))
    End Sub

    <Fact>
    Public Sub IVTFailSignMismatchThroughIAssembly()
A
angocke 已提交
802
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
803 804 805 806 807 808 809 810 811
<compilation name="Paul">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("John, PublicKey=00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb")>
Friend Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
812
</compilation>, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
813 814 815

        other.VerifyDiagnostics()

A
angocke 已提交
816
        Dim requestor As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
817 818 819 820 821 822 823 824
<compilation name="John">
    <file name="a.vb"><![CDATA[
Imports MyC=C
<Assembly: System.Reflection.AssemblyKeyName("roslynTestContainer")>
Public Class A
End Class
]]>
    </file>
B
beep boop 已提交
825
</compilation>, {New VisualBasicCompilationReference(other)}, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
826 827 828 829

        Assert.False(DirectCast(other.Assembly, IAssemblySymbol).GivesAccessTo(requestor.Assembly))
    End Sub

830
    <WorkItem(820450, "DevDiv")>
P
Pilchie 已提交
831 832
    <Fact>
    Public Sub IVTGivesAccessToUsingDifferentKeys()
A
angocke 已提交
833
        Dim giver As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
834 835 836 837 838 839 840 841 842 843 844
<compilation name="Paul">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("John, PublicKey=00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb")>
Namespace ClassLibrary
    Friend Class FriendClass
     Public Sub Foo()
     End Sub
    End Class
end Namespace
]]>
    </file>
B
beep boop 已提交
845
</compilation>, options:=TestOptions.ReleaseDll.WithCryptoKeyFile(SigningTestHelpers.KeyPairFile2).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
846 847 848

        giver.VerifyDiagnostics()

A
angocke 已提交
849
        Dim requestor As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
850 851 852 853 854 855 856 857
<compilation name="John">
    <file name="a.vb"><![CDATA[
Public Class ClassWithFriendMethod
    Friend Sub Test(A as ClassLibrary.FriendClass)
    End Sub
End Class
]]>
    </file>
B
beep boop 已提交
858
</compilation>, {New VisualBasicCompilationReference(giver)}, options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
859 860 861 862 863 864 865 866 867

        Assert.True(DirectCast(giver.Assembly, IAssemblySymbol).GivesAccessTo(requestor.Assembly))
        Assert.Empty(requestor.GetDiagnostics())
    End Sub
#End Region

#Region "IVT instantiations"
    <Fact>
    Public Sub IVTHasCulture()
A
angocke 已提交
868
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
869 870 871 872 873 874 875 876 877 878
<compilation name="Sam">
    <file name="a.vb"><![CDATA[
Imports System.Runtime.CompilerServices
<Assembly: InternalsVisibleTo("WantsIVTAccess, Culture=neutral")>
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
879
</compilation>, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
880 881 882 883 884 885 886 887 888 889 890

        Dim expectedErrors = <error><![CDATA[
BC31534: Friend assembly reference 'WantsIVTAccess, Culture=neutral' is invalid. InternalsVisibleTo declarations cannot have a version, culture, public key token, or processor architecture specified.
<Assembly: InternalsVisibleTo("WantsIVTAccess, Culture=neutral")>
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
]]></error>
        CompilationUtils.AssertTheseDeclarationDiagnostics(other, expectedErrors)
    End Sub

    <Fact>
    Public Sub IVTNoKey()
A
angocke 已提交
891
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
892 893 894 895 896 897 898 899 900 901
<compilation name="Sam">
    <file name="a.vb"><![CDATA[
Imports System.Runtime.CompilerServices
<Assembly: InternalsVisibleTo("WantsIVTAccess")>
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
902
</compilation>, options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
903 904 905 906 907 908 909 910 911 912 913 914 915

        Dim expectedErrors = <error><![CDATA[
BC31535: Friend assembly reference 'WantsIVTAccess' is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations.
<Assembly: InternalsVisibleTo("WantsIVTAccess")>
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
]]></error>
        CompilationUtils.AssertTheseDeclarationDiagnostics(other, expectedErrors)
    End Sub
#End Region

#Region "Signing"
    <Fact>
    Public Sub SignIt()
A
angocke 已提交
916
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
917 918 919 920 921 922 923 924 925
<compilation name="Sam">
    <file name="a.vb"><![CDATA[
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
</compilation>,
B
beep boop 已提交
926
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
927 928 929 930 931 932 933

        Dim peHeaders = New PEHeaders(other.EmitToStream())
        Assert.Equal(CorFlags.StrongNameSigned, peHeaders.CorHeader.Flags And CorFlags.StrongNameSigned)
    End Sub

    <Fact>
    Public Sub SignItWithOnlyPublicKey()
A
angocke 已提交
934
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
935 936 937 938 939 940 941 942 943
<compilation name="Sam">
    <file name="a.vb"><![CDATA[
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
</compilation>,
B
beep boop 已提交
944
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_publicKeyFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
945 946 947 948 949 950

        Using outStrm = New MemoryStream()
            Dim emitResult = other.Emit(outStrm)

            CompilationUtils.AssertTheseDiagnostics(emitResult.Diagnostics,
<errors>
B
beep boop 已提交
951
BC36961: Key file '<%= s_publicKeyFile %>' is missing the private key needed for signing.
P
Pilchie 已提交
952 953 954
</errors>)
        End Using

B
beep boop 已提交
955
        other = other.WithOptions(TestOptions.ReleaseModule.WithCryptoKeyFile(s_publicKeyFile))
P
Pilchie 已提交
956

A
angocke 已提交
957
        Dim assembly As VisualBasicCompilation = CreateCompilationWithMscorlibAndReferences(
P
Pilchie 已提交
958 959 960 961 962
<compilation name="Sam2">
    <file name="a.vb">
    </file>
</compilation>,
        {other.EmitToImageReference()},
B
beep boop 已提交
963
        options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
964 965 966 967 968 969

        Using outStrm = New MemoryStream()
            Dim emitResult = assembly.Emit(outStrm)

            CompilationUtils.AssertTheseDiagnostics(emitResult.Diagnostics,
<errors>
B
beep boop 已提交
970
BC36961: Key file '<%= s_publicKeyFile %>' is missing the private key needed for signing.
P
Pilchie 已提交
971 972 973 974 975 976
</errors>)
        End Using
    End Sub

    <Fact>
    Public Sub DelaySignItWithOnlyPublicKey()
A
angocke 已提交
977
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
978 979 980 981 982 983 984 985 986 987
<compilation name="Sam">
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblyDelaySign(True)>
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
</compilation>,
B
beep boop 已提交
988
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_publicKeyFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
989 990 991 992 993 994

        CompileAndVerify(other)
    End Sub

    <Fact>
    Public Sub DelaySignButNoKey()
A
angocke 已提交
995
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
996 997 998 999 1000 1001 1002 1003 1004
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblyDelaySign(True)>
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
1005
</compilation>, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

        Dim outStrm = New MemoryStream()
        Dim emitResult = other.Emit(outStrm)
        ' Dev11: vbc : warning BC40010: Possible problem detected while building assembly 'VBTestD': Delay signing was requested, but no key was given
        '              warning BC41008: Use command-line option '/delaysign' or appropriate project settings instead of 'System.Reflection.AssemblyDelaySignAttribute'.
        CompilationUtils.AssertTheseDiagnostics(emitResult.Diagnostics, <errors>BC40060: Delay signing was specified and requires a public key, but no public key was specified.</errors>)
        Assert.True(emitResult.Success)
    End Sub

    <Fact>
    Public Sub SignInMemory()
A
angocke 已提交
1017
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
1018 1019 1020 1021 1022 1023 1024 1025
<compilation>
    <file name="a.vb"><![CDATA[
Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
B
beep boop 已提交
1026
</compilation>, options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1027 1028 1029 1030 1031 1032

        Dim outStrm = New MemoryStream()
        Dim emitResult = other.Emit(outStrm)
        Assert.True(emitResult.Success)
    End Sub

1033 1034
    <WorkItem(545720, "DevDiv")>
    <WorkItem(530050, "DevDiv")>
P
Pilchie 已提交
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    <Fact>
    Public Sub InvalidAssemblyName()

        Dim il = <![CDATA[
.assembly extern mscorlib { }
.assembly asm1
{
    .custom instance void [mscorlib]System.Runtime.CompilerServices.InternalsVisibleToAttribute::.ctor(string) = ( 01 00 09 2F 5C 3A 2A 3F 27 3C 3E 7C 00 00 ) // .../\:*?'<>|..
}

.class private auto ansi beforefieldinit Base
       extends [mscorlib]System.Object
{
  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    ldarg.0
    call       instance void [mscorlib]System.Object::.ctor()
    ret
  }
}
]]>

        Dim vb = <compilation>
                     <file name="a.vb"><![CDATA[
Public Class Derived
    Inherits Base
End Class
]]>
                     </file>
                 </compilation>

        Dim ilRef = CompileIL(il.Value, appendDefaultHeader:=False)

B
beep boop 已提交
1069
        Dim comp = CreateCompilationWithMscorlibAndReferences(vb, {ilRef}, TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096

        ' NOTE: dev10 reports ERR_FriendAssemblyNameInvalid, but Roslyn won't (DevDiv #15099).
        comp.VerifyDiagnostics(
            Diagnostic(ERRID.ERR_InaccessibleSymbol2, "Base").WithArguments("Base", "Friend"))
    End Sub

    <Fact>
    Public Sub DelaySignWithAssemblySignatureKey()
        '//Note that this SignatureKey is some random one that I found in the devdiv build.
        '//It is not related to the other keys we use in these tests.

        '//In the native compiler, when the AssemblySignatureKey attribute is present, and
        '//the binary is configured for delay signing, the contents of the assemblySignatureKey attribute
        '//(rather than the contents of the keyfile or container) are used to compute the size needed to 
        '//reserve in the binary for its signature. Signing using this key is only supported via sn.exe

        Dim other = CreateCompilationWithReferences(
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblyDelaySign(True)>
<Assembly: System.Reflection.AssemblySignatureKey("002400000c800000140100000602000000240000525341310008000001000100613399aff18ef1a2c2514a273a42d9042b72321f1757102df9ebada69923e2738406c21e5b801552ab8d200a65a235e001ac9adc25f2d811eb09496a4c6a59d4619589c69f5baf0c4179a47311d92555cd006acc8b5959f2bd6e10e360c34537a1d266da8085856583c85d81da7f3ec01ed9564c58d93d713cd0172c8e23a10f0239b80c96b07736f5d8b022542a4e74251a5f432824318b3539a5a087f8e53d2f135f9ca47f3bb2e10aff0af0849504fb7cea3ff192dc8de0edad64c68efde34c56d302ad55fd6e80f302d5efcdeae953658d3452561b5f36c542efdbdd9f888538d374cef106acf7d93a4445c3c73cd911f0571aaf3d54da12b11ddec375b3", "a5a866e1ee186f807668209f3b11236ace5e21f117803a3143abb126dd035d7d2f876b6938aaf2ee3414d5420d753621400db44a49c486ce134300a2106adb6bdb433590fef8ad5c43cba82290dc49530effd86523d9483c00f458af46890036b0e2c61d077d7fbac467a506eba29e467a87198b053c749aa2a4d2840c784e6d")>
Public Class C
 Friend Sub Foo()
    End Sub
End Class
]]>
    </file>
B
beep boop 已提交
1097
</compilation>, {MscorlibRef_v4_0_30316_17626}, TestOptions.ReleaseDll.WithDelaySign(True).WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106

        ' confirm header has expected SN signature size
        Dim peHeaders = New PEHeaders(other.EmitToStream())
        Assert.Equal(256, peHeaders.CorHeader.StrongNameSignatureDirectory.Size)
    End Sub

    ''' <summary>
    ''' Won't fix (easy to be tested here)
    ''' </summary>
1107
    <Fact(), WorkItem(529953, "DevDiv"), WorkItem(530112, "DevDiv")>
P
Pilchie 已提交
1108 1109 1110
    Public Sub DeclareAssemblyKeyNameAndFile_BC41008()

        Dim src = "<Assembly: System.Reflection.AssemblyKeyName(""Key1"")>" & vbCrLf &
B
beep boop 已提交
1111
                "<Assembly: System.Reflection.AssemblyKeyFile(""" & s_keyPairFile & """)>" & vbCrLf &
P
Pilchie 已提交
1112 1113 1114 1115
              "Public Class C" & vbCrLf &
              "End Class"

        Dim tree = ParseAndVerify(src)
1116
        Dim comp = CreateCompilationWithMscorlib({tree}, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146

        ' Native Compiler:
        'warning BC41008: Use command-line option '/keycontainer' or appropriate project settings instead of 'System.Reflection.AssemblyKeyNameAttribute() '.
        ' <Assembly: System.Reflection.AssemblyKeyName("Key1")>
        '            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        'warning BC41008: Use command-line option '/keyfile' or appropriate project settings instead of 'System.Reflection.AssemblyKeyFileAttribute() '.
        '<Assembly: System.Reflection.AssemblyKeyFile("Key2.snk")>
        '  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        comp.VerifyDiagnostics()
        '   Diagnostic(ERRID.WRN_UseSwitchInsteadOfAttribute, "System.Reflection.AssemblyKeyName(""Key1""").WithArguments("/keycontainer"),
        '   Diagnostic(ERRID.WRN_UseSwitchInsteadOfAttribute, "System.Reflection.AssemblyKeyFile(""Key2.snk""").WithArguments("/keyfile"))

        Dim outStrm = New MemoryStream()
        Dim emitResult = comp.Emit(outStrm)
        Assert.True(emitResult.Success)

    End Sub

    Private Sub ConfirmModuleAttributePresentAndAddingToAssemblyResultsInSignedOutput(
        moduleContents As Stream,
        expectedModuleAttr As AttributeDescription
    )
        ' a module doesn't get signed for real. It should have either a keyfile or keycontainer attribute
        ' parked on a typeRef named 'AssemblyAttributesGoHere.' When the module is added to an assembly, the
        ' resulting assembly is signed with the key referred to by the aforementioned attribute.

        Dim success As EmitResult
        Dim tempFile = Temp.CreateFile()
        moduleContents.Position = 0

1147
        Using metadata = ModuleMetadata.CreateFromStream(moduleContents)
P
Pilchie 已提交
1148 1149 1150
            Dim flags = metadata.Module.PEReaderOpt.PEHeaders.CorHeader.Flags
            ' confirm file does not claim to be signed
            Assert.Equal(0, CInt(flags And CorFlags.StrongNameSigned))
1151
            Dim token As EntityHandle = metadata.Module.GetTypeRef(metadata.Module.GetAssemblyRef("mscorlib"), "System.Runtime.CompilerServices", "AssemblyAttributesGoHere")
P
Pilchie 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
            Assert.False(token.IsNil)   ' could the magic type ref be located? If not then the attribute's not there.
            Dim attrInfos = metadata.Module.FindTargetAttributes(token, expectedModuleAttr)
            Assert.Equal(1, attrInfos.Count())

            Dim source =
<compilation>
    <file name="a.vb"><![CDATA[
Public Class Z
End Class
]]>
    </file>
</compilation>

1165
            ' now that the module checks out, ensure that adding it to a compilation outputting a dll
P
Pilchie 已提交
1166
            ' results in a signed assembly.
B
beep boop 已提交
1167
            Dim assemblyComp = CreateCompilationWithMscorlibAndReferences(source, {metadata.GetReference()}, TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

            Using finalStrm = tempFile.Open()
                success = assemblyComp.Emit(finalStrm)
            End Using
        End Using

        success.Diagnostics.Verify()

        Assert.True(success.Success)
        AssertFileIsSigned(tempFile)
    End Sub

    Private Shared Sub AssertFileIsSigned(file As TempFile)
        ' TODO should check to see that the output was actually signed
        Using peStream = New FileStream(file.Path, FileMode.Open)
            Dim flags = New PEHeaders(peStream).CorHeader.Flags
            Assert.Equal(CorFlags.StrongNameSigned, flags And CorFlags.StrongNameSigned)
        End Using
    End Sub

    <Fact>
    Public Sub SignModuleKeyFileAttr()
B
beep boop 已提交
1190
        Dim x = s_keyPairFile
P
Pilchie 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201

        Dim source =
<compilation>
    <file name="a.vb">
        <![CDATA[<]]>Assembly: System.Reflection.AssemblyKeyFile("<%= x %>")>

Public Class C
End Class
    </file>
</compilation>

B
beep boop 已提交
1202
        Dim other = CreateCompilationWithMscorlib(source, TestOptions.ReleaseModule.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

        ConfirmModuleAttributePresentAndAddingToAssemblyResultsInSignedOutput(other.EmitToStream(), AttributeDescription.AssemblyKeyFileAttribute)
    End Sub

    <Fact>
    Public Sub SignModuleKeyContainerAttr()
        Dim source =
<compilation>
    <file name="a.vb">
        <![CDATA[<]]>Assembly: System.Reflection.AssemblyKeyName("roslynTestContainer")>

Public Class C
End Class
    </file>
</compilation>

B
beep boop 已提交
1219
        Dim other = CreateCompilationWithMscorlib(source, TestOptions.ReleaseModule.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1220 1221 1222 1223 1224 1225 1226 1227

        Dim outStrm = New MemoryStream()
        Dim success = other.Emit(outStrm)
        Assert.True(success.Success)

        ConfirmModuleAttributePresentAndAddingToAssemblyResultsInSignedOutput(outStrm, AttributeDescription.AssemblyKeyNameAttribute)
    End Sub

1228
    <WorkItem(531195, "DevDiv")>
P
Pilchie 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
    <Fact>
    Public Sub SignModuleKeyContainerCmdLine()
        Dim source =
<compilation>
    <file name="a.vb">
Public Class C
End Class
    </file>
</compilation>

B
beep boop 已提交
1239
        Dim other = CreateCompilationWithMscorlib(source, TestOptions.ReleaseModule.WithCryptoKeyContainer("roslynTestContainer").WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1240 1241 1242 1243 1244 1245 1246 1247

        Dim outStrm = New MemoryStream()
        Dim success = other.Emit(outStrm)
        Assert.True(success.Success)

        ConfirmModuleAttributePresentAndAddingToAssemblyResultsInSignedOutput(outStrm, AttributeDescription.AssemblyKeyNameAttribute)
    End Sub

1248
    <WorkItem(531195, "DevDiv")>
P
Pilchie 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
    <Fact>
    Public Sub SignModuleKeyContainerCmdLine_1()
        Dim source =
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblyKeyName("roslynTestContainer")>

Public Class C
End Class
    ]]></file>
</compilation>

B
beep boop 已提交
1261
        Dim other = CreateCompilationWithMscorlib(source, TestOptions.ReleaseModule.WithCryptoKeyContainer("roslynTestContainer").WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1262 1263 1264 1265 1266 1267 1268 1269

        Dim outStrm = New MemoryStream()
        Dim success = other.Emit(outStrm)
        Assert.True(success.Success)

        ConfirmModuleAttributePresentAndAddingToAssemblyResultsInSignedOutput(outStrm, AttributeDescription.AssemblyKeyNameAttribute)
    End Sub

1270
    <WorkItem(531195, "DevDiv")>
P
Pilchie 已提交
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
    <Fact>
    Public Sub SignModuleKeyContainerCmdLine_2()
        Dim source =
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblyKeyName("bogus")>

Public Class C
End Class
    ]]></file>
</compilation>

B
beep boop 已提交
1283
        Dim other = CreateCompilationWithMscorlib(source, TestOptions.ReleaseModule.WithCryptoKeyContainer("roslynTestContainer").WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1284 1285 1286 1287 1288 1289 1290

        AssertTheseDiagnostics(other,
<expected>
BC37207: Attribute 'System.Reflection.AssemblyKeyNameAttribute' given in a source file conflicts with option 'CryptoKeyContainer'.
</expected>)
    End Sub

1291
    <WorkItem(531195, "DevDiv")>
P
Pilchie 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
    <Fact>
    Public Sub SignModuleKeyFileCmdLine()
        Dim source =
<compilation>
    <file name="a.vb">
Public Class C
End Class
    </file>
</compilation>

B
beep boop 已提交
1302
        Dim other = CreateCompilationWithMscorlib(source, TestOptions.ReleaseModule.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1303 1304 1305 1306 1307 1308 1309 1310

        Dim outStrm = New MemoryStream()
        Dim success = other.Emit(outStrm)
        Assert.True(success.Success)

        ConfirmModuleAttributePresentAndAddingToAssemblyResultsInSignedOutput(outStrm, AttributeDescription.AssemblyKeyFileAttribute)
    End Sub

1311
    <WorkItem(531195, "DevDiv")>
P
Pilchie 已提交
1312 1313
    <Fact>
    Public Sub SignModuleKeyFileCmdLine_1()
B
beep boop 已提交
1314
        Dim x = s_keyPairFile
P
Pilchie 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
        Dim source =
<compilation>
    <file name="a.vb">
        <![CDATA[<]]>assembly: System.Reflection.AssemblyKeyFile("<%= x %>")>        

Public Class C
End Class
    </file>
</compilation>

B
beep boop 已提交
1325
        Dim other = CreateCompilationWithMscorlib(source, TestOptions.ReleaseModule.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345

        Dim outStrm = New MemoryStream()
        Dim success = other.Emit(outStrm)
        Assert.True(success.Success)

        ConfirmModuleAttributePresentAndAddingToAssemblyResultsInSignedOutput(outStrm, AttributeDescription.AssemblyKeyFileAttribute)
    End Sub

    <Fact>
    Public Sub SignModuleKeyFileCmdLine_2()
        Dim source =
<compilation>
    <file name="a.vb">
        <![CDATA[<]]>assembly: System.Reflection.AssemblyKeyFile("bogus")>        

Public Class C
End Class
    </file>
</compilation>

B
beep boop 已提交
1346
        Dim other = CreateCompilationWithMscorlib(source, TestOptions.ReleaseModule.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1347 1348 1349 1350 1351 1352 1353

        AssertTheseDiagnostics(other,
<expected>
BC37207: Attribute 'System.Reflection.AssemblyKeyFileAttribute' given in a source file conflicts with option 'CryptoKeyFile'.
</expected>)
    End Sub

1354
    <Fact> <WorkItem(529779, "DevDiv")>
P
Pilchie 已提交
1355 1356
    Public Sub Bug529779_1()

A
angocke 已提交
1357
        Dim unsigned As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
1358 1359 1360 1361 1362 1363 1364
<compilation>
    <file name="a.vb"><![CDATA[
Public Class C1
End Class
]]>
    </file>
</compilation>,
B
beep boop 已提交
1365
        options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1366

A
angocke 已提交
1367
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
<compilation>
    <file name="a.vb"><![CDATA[
Public Class C
 Friend Sub Foo()
    Dim x as New System.Guid()
    System.Console.WriteLine(x)
 End Sub
End Class
]]>
    </file>
</compilation>,
B
beep boop 已提交
1379
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1380 1381 1382

        CompileAndVerify(other.WithReferences({other.References(0), New VisualBasicCompilationReference(unsigned)})).VerifyDiagnostics()

1383
        CompileAndVerify(other.WithReferences({other.References(0), MetadataReference.CreateFromImage(unsigned.EmitToArray)})).VerifyDiagnostics()
P
Pilchie 已提交
1384 1385
    End Sub

1386
    <Fact> <WorkItem(529779, "DevDiv")>
P
Pilchie 已提交
1387 1388
    Public Sub Bug529779_2()

A
angocke 已提交
1389
        Dim unsigned As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
1390 1391 1392 1393 1394 1395 1396
<compilation name="Unsigned">
    <file name="a.vb"><![CDATA[
Public Class C1
End Class
]]>
    </file>
</compilation>,
B
beep boop 已提交
1397
        options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1398

A
angocke 已提交
1399
        Dim other As VisualBasicCompilation = CreateCompilationWithMscorlib(
P
Pilchie 已提交
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
<compilation>
    <file name="a.vb"><![CDATA[
Public Class C
 Friend Sub Foo()
    Dim x as New C1()
    System.Console.WriteLine(x)
 End Sub
End Class
]]>
    </file>
</compilation>,
B
beep boop 已提交
1411
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1412 1413

        Dim comps = {other.WithReferences({other.References(0), New VisualBasicCompilationReference(unsigned)}),
1414
                     other.WithReferences({other.References(0), MetadataReference.CreateFromImage(unsigned.EmitToArray)})}
P
Pilchie 已提交
1415 1416 1417 1418 1419

        For Each comp In comps
            Dim outStrm = New MemoryStream()
            Dim emitResult = comp.Emit(outStrm)

1420 1421
            ' Dev12 reports an error
            Assert.True(emitResult.Success)
P
Pilchie 已提交
1422

1423 1424 1425 1426
            AssertTheseDiagnostics(emitResult.Diagnostics,
<expected>
BC41997: Referenced assembly 'Unsigned, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have a strong name.
</expected>)
P
Pilchie 已提交
1427 1428 1429 1430 1431
        Next
    End Sub

    <Fact>
    Public Sub AssemblySignatureKeyAttribute_1()
A
angocke 已提交
1432
        Dim other As VisualBasicCompilation = CreateCompilationWithReferences(
P
Pilchie 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblySignatureKeyAttribute(
"00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb",
"bc6402e37ad723580b576953f40475ceae4b784d3661b90c3c6f5a1f7283388a7880683e0821610bee977f70506bb75584080e01b2ec97483c4d601ce1c981752a07276b420d78594d0ef28f8ec016d0a5b6d56cfc22e9f25a2ed9545942ccbf2d6295b9528641d98776e06a3273ab233271a3c9f53099b4d4e029582a6d5819")>

Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
</compilation>, {MscorlibRef_v4_0_30316_17626},
B
beep boop 已提交
1446
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1447 1448 1449 1450 1451 1452 1453

        Dim peHeaders = New PEHeaders(other.EmitToStream())
        Assert.Equal(CorFlags.StrongNameSigned, peHeaders.CorHeader.Flags And CorFlags.StrongNameSigned)
    End Sub

    <Fact>
    Public Sub AssemblySignatureKeyAttribute_2()
A
angocke 已提交
1454
        Dim other As VisualBasicCompilation = CreateCompilationWithReferences(
P
Pilchie 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblySignatureKeyAttribute(
"xxx 00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb",
"bc6402e37ad723580b576953f40475ceae4b784d3661b90c3c6f5a1f7283388a7880683e0821610bee977f70506bb75584080e01b2ec97483c4d601ce1c981752a07276b420d78594d0ef28f8ec016d0a5b6d56cfc22e9f25a2ed9545942ccbf2d6295b9528641d98776e06a3273ab233271a3c9f53099b4d4e029582a6d5819")>

Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
</compilation>, {MscorlibRef_v4_0_30316_17626},
B
beep boop 已提交
1468
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484

        Dim outStrm = New MemoryStream()
        Dim emitResult = other.Emit(outStrm)

        Assert.False(emitResult.Success)

        AssertTheseDiagnostics(emitResult.Diagnostics,
<expected>
BC37209: Invalid signature public key specified in AssemblySignatureKeyAttribute.
"xxx 00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb",
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
</expected>)
    End Sub

    <Fact>
    Public Sub AssemblySignatureKeyAttribute_3()
A
angocke 已提交
1485
        Dim other As VisualBasicCompilation = CreateCompilationWithReferences(
P
Pilchie 已提交
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblySignatureKeyAttribute(
"00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb",
"FFFFbc6402e37ad723580b576953f40475ceae4b784d3661b90c3c6f5a1f7283388a7880683e0821610bee977f70506bb75584080e01b2ec97483c4d601ce1c981752a07276b420d78594d0ef28f8ec016d0a5b6d56cfc22e9f25a2ed9545942ccbf2d6295b9528641d98776e06a3273ab233271a3c9f53099b4d4e029582a6d5819")>

Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
</compilation>, {MscorlibRef_v4_0_30316_17626},
B
beep boop 已提交
1499
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_keyPairFile).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1500 1501 1502 1503 1504 1505

        Dim outStrm = New MemoryStream()
        Dim emitResult = other.Emit(outStrm)

        Assert.False(emitResult.Success)

1506 1507 1508 1509 1510 1511 1512 1513
        '        AssertTheseDiagnostics(emitResult.Diagnostics,
        '<expected>
        'BC36980: Error extracting public key from file '<%= KeyPairFile %>': Invalid countersignature specified in AssemblySignatureKeyAttribute. (Exception from HRESULT: 0x80131423)
        '</expected>)
        Dim err = emitResult.Diagnostics.Single()

        Assert.Equal(ERRID.ERR_PublicKeyFileFailure, err.Code)
        Assert.Equal(2, err.Arguments.Count)
B
beep boop 已提交
1514
        Assert.Equal(s_keyPairFile, DirectCast(err.Arguments(0), String))
1515
        Assert.True(DirectCast(err.Arguments(1), String).EndsWith(" HRESULT: 0x80131423)", StringComparison.Ordinal))
P
Pilchie 已提交
1516 1517 1518 1519
    End Sub

    <Fact>
    Public Sub AssemblySignatureKeyAttribute_4()
A
angocke 已提交
1520
        Dim other As VisualBasicCompilation = CreateCompilationWithReferences(
P
Pilchie 已提交
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblySignatureKeyAttribute(
"xxx 00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb",
"bc6402e37ad723580b576953f40475ceae4b784d3661b90c3c6f5a1f7283388a7880683e0821610bee977f70506bb75584080e01b2ec97483c4d601ce1c981752a07276b420d78594d0ef28f8ec016d0a5b6d56cfc22e9f25a2ed9545942ccbf2d6295b9528641d98776e06a3273ab233271a3c9f53099b4d4e029582a6d5819")>

Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
</compilation>, {MscorlibRef_v4_0_30316_17626},
B
beep boop 已提交
1534
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_publicKeyFile).WithDelaySign(True).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550

        Dim outStrm = New MemoryStream()
        Dim emitResult = other.Emit(outStrm)

        Assert.False(emitResult.Success)

        AssertTheseDiagnostics(emitResult.Diagnostics,
<expected>
BC37209: Invalid signature public key specified in AssemblySignatureKeyAttribute.
"xxx 00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb",
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
</expected>)
    End Sub

    <Fact>
    Public Sub AssemblySignatureKeyAttribute_5()
A
angocke 已提交
1551
        Dim other As VisualBasicCompilation = CreateCompilationWithReferences(
P
Pilchie 已提交
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblySignatureKeyAttribute(
"00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb",
"FFFFbc6402e37ad723580b576953f40475ceae4b784d3661b90c3c6f5a1f7283388a7880683e0821610bee977f70506bb75584080e01b2ec97483c4d601ce1c981752a07276b420d78594d0ef28f8ec016d0a5b6d56cfc22e9f25a2ed9545942ccbf2d6295b9528641d98776e06a3273ab233271a3c9f53099b4d4e029582a6d5819")>

Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
</compilation>, {MscorlibRef_v4_0_30316_17626},
B
beep boop 已提交
1565
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_publicKeyFile).WithDelaySign(True).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1566 1567 1568 1569 1570 1571

        CompileAndVerify(other)
    End Sub

    <Fact>
    Public Sub AssemblySignatureKeyAttribute_6()
A
angocke 已提交
1572
        Dim other As VisualBasicCompilation = CreateCompilationWithReferences(
P
Pilchie 已提交
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblySignatureKeyAttribute(
Nothing,
"bc6402e37ad723580b576953f40475ceae4b784d3661b90c3c6f5a1f7283388a7880683e0821610bee977f70506bb75584080e01b2ec97483c4d601ce1c981752a07276b420d78594d0ef28f8ec016d0a5b6d56cfc22e9f25a2ed9545942ccbf2d6295b9528641d98776e06a3273ab233271a3c9f53099b4d4e029582a6d5819")>

Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
</compilation>, {MscorlibRef_v4_0_30316_17626},
B
beep boop 已提交
1586
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_publicKeyFile).WithDelaySign(True).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602

        Dim outStrm = New MemoryStream()
        Dim emitResult = other.Emit(outStrm)

        Assert.False(emitResult.Success)

        AssertTheseDiagnostics(emitResult.Diagnostics,
<expected>
BC37209: Invalid signature public key specified in AssemblySignatureKeyAttribute.
Nothing,
~~~~~~~
</expected>)
    End Sub

    <Fact>
    Public Sub AssemblySignatureKeyAttribute_7()
A
angocke 已提交
1603
        Dim other As VisualBasicCompilation = CreateCompilationWithReferences(
P
Pilchie 已提交
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
<compilation>
    <file name="a.vb"><![CDATA[
<Assembly: System.Reflection.AssemblySignatureKeyAttribute(
"00240000048000009400000006020000002400005253413100040000010001002b986f6b5ea5717d35c72d38561f413e267029efa9b5f107b9331d83df657381325b3a67b75812f63a9436ceccb49494de8f574f8e639d4d26c0fcf8b0e9a1a196b80b6f6ed053628d10d027e032df2ed1d60835e5f47d32c9ef6da10d0366a319573362c821b5f8fa5abc5bb22241de6f666a85d82d6ba8c3090d01636bd2bb",
Nothing)>

Public Class C
 Friend Sub Foo()
 End Sub
End Class
]]>
    </file>
</compilation>, {MscorlibRef_v4_0_30316_17626},
B
beep boop 已提交
1617
        options:=TestOptions.ReleaseDll.WithCryptoKeyFile(s_publicKeyFile).WithDelaySign(True).WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1618 1619 1620 1621 1622 1623

        CompileAndVerify(other)
    End Sub

#End Region

1624
    <Fact, WorkItem(769840, "DevDiv")>
P
Pilchie 已提交
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
    Public Sub Bug769840()
        Dim ca = CreateCompilationWithMscorlib(
<compilation name="Bug769840_A">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Bug769840_B, PublicKey=0024000004800000940000000602000000240000525341310004000001000100458a131798af87d9e33088a3ab1c6101cbd462760f023d4f41d97f691033649e60b42001e94f4d79386b5e087b0a044c54b7afce151b3ad19b33b332b83087e3b8b022f45b5e4ff9b9a1077b0572ff0679ce38f884c7bd3d9b4090e4a7ee086b7dd292dc20f81a3b1b8a0b67ee77023131e59831c709c81d11c6856669974cc4")>

Friend Class A
	Public Value As Integer = 3
End Class
]]></file>
B
beep boop 已提交
1635
</compilation>, options:=TestOptions.ReleaseDll.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647

        CompileAndVerify(ca)

        Dim cb = CreateCompilationWithMscorlibAndReferences(
<compilation name="Bug769840_B">
    <file name="a.vb"><![CDATA[
Friend Class B
    Public Function GetA() As A
        Return New A()
    End Function
End Class
]]></file>
B
beep boop 已提交
1648
</compilation>, {New VisualBasicCompilationReference(ca)}, options:=TestOptions.ReleaseModule.WithStrongNameProvider(s_defaultProvider))
P
Pilchie 已提交
1649 1650 1651 1652

        CompileAndVerify(cb, verify:=False).Diagnostics.Verify()
    End Sub

1653 1654
    <Fact, WorkItem(1072350, "DevDiv")>
    Public Sub Bug1072350()
1655
        Dim sourceA As XElement =
1656 1657 1658 1659 1660 1661 1662 1663 1664
<compilation name="ClassLibrary2">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("X ")>
Friend Class A
    Friend Shared I As Integer = 42
End Class]]>
    </file>
</compilation>

1665
        Dim sourceB As XElement =
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
<compilation name="X">
    <file name="b.vb"><![CDATA[
Class B
    Shared Sub Main()
        System.Console.Write(A.I)
    End Sub
End Class]]>
    </file>
</compilation>

        Dim ca = CreateCompilationWithMscorlib(sourceA, options:=TestOptions.ReleaseDll)
        CompileAndVerify(ca)

1679
        Dim cb = CreateCompilationWithMscorlib(sourceB, options:=TestOptions.ReleaseExe, references:={New VisualBasicCompilationReference(ca)})
T
Tomas Matousek 已提交
1680
        CompileAndVerify(cb, expectedOutput:="42").Diagnostics.Verify()
1681 1682 1683 1684
    End Sub

    <Fact, WorkItem(1072339, "DevDiv")>
    Public Sub Bug1072339()
1685
        Dim sourceA As XElement =
1686 1687 1688 1689 1690 1691 1692 1693 1694
<compilation name="ClassLibrary2">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("x")>
Friend Class A
    Friend Shared I As Integer = 42
End Class]]>
    </file>
</compilation>

1695
        Dim sourceB As XElement =
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
<compilation name="x">
    <file name="b.vb"><![CDATA[
Class B
    Shared Sub Main()
        System.Console.Write(A.I)
    End Sub
End Class]]>
    </file>
</compilation>

        Dim ca = CreateCompilationWithMscorlib(sourceA, options:=TestOptions.ReleaseDll)
        CompileAndVerify(ca)

1709
        Dim cb = CreateCompilationWithMscorlib(sourceB, options:=TestOptions.ReleaseExe, references:={New VisualBasicCompilationReference(ca)})
T
Tomas Matousek 已提交
1710
        CompileAndVerify(cb, expectedOutput:="42").Diagnostics.Verify()
1711 1712
    End Sub

1713 1714
    <Fact, WorkItem(1095618, "DevDiv")>
    Public Sub Bug1095618()
1715
        Dim source As XElement =
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
<compilation name="a">
    <file name="a.vb"><![CDATA[
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo("System.Runtime.Serialization, PublicKey = 10000000000000000400000000000000")>
    ]]></file>
</compilation>

        CreateCompilationWithMscorlib(source).VerifyDiagnostics(
            Diagnostic(ERRID.ERR_FriendAssemblyNameInvalid, "Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""System.Runtime.Serialization, PublicKey = 10000000000000000400000000000000"")").WithArguments("System.Runtime.Serialization, PublicKey = 10000000000000000400000000000000").WithLocation(1, 2))
    End Sub

P
Pilchie 已提交
1726
End Class