1*1b3f573fSAndroid Build Coastguard Worker #region Copyright notice and license 2*1b3f573fSAndroid Build Coastguard Worker // Protocol Buffers - Google's data interchange format 3*1b3f573fSAndroid Build Coastguard Worker // Copyright 2008 Google Inc. All rights reserved. 4*1b3f573fSAndroid Build Coastguard Worker // https://developers.google.com/protocol-buffers/ 5*1b3f573fSAndroid Build Coastguard Worker // 6*1b3f573fSAndroid Build Coastguard Worker // Redistribution and use in source and binary forms, with or without 7*1b3f573fSAndroid Build Coastguard Worker // modification, are permitted provided that the following conditions are 8*1b3f573fSAndroid Build Coastguard Worker // met: 9*1b3f573fSAndroid Build Coastguard Worker // 10*1b3f573fSAndroid Build Coastguard Worker // * Redistributions of source code must retain the above copyright 11*1b3f573fSAndroid Build Coastguard Worker // notice, this list of conditions and the following disclaimer. 12*1b3f573fSAndroid Build Coastguard Worker // * Redistributions in binary form must reproduce the above 13*1b3f573fSAndroid Build Coastguard Worker // copyright notice, this list of conditions and the following disclaimer 14*1b3f573fSAndroid Build Coastguard Worker // in the documentation and/or other materials provided with the 15*1b3f573fSAndroid Build Coastguard Worker // distribution. 16*1b3f573fSAndroid Build Coastguard Worker // * Neither the name of Google Inc. nor the names of its 17*1b3f573fSAndroid Build Coastguard Worker // contributors may be used to endorse or promote products derived from 18*1b3f573fSAndroid Build Coastguard Worker // this software without specific prior written permission. 19*1b3f573fSAndroid Build Coastguard Worker // 20*1b3f573fSAndroid Build Coastguard Worker // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21*1b3f573fSAndroid Build Coastguard Worker // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*1b3f573fSAndroid Build Coastguard Worker // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23*1b3f573fSAndroid Build Coastguard Worker // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24*1b3f573fSAndroid Build Coastguard Worker // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25*1b3f573fSAndroid Build Coastguard Worker // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26*1b3f573fSAndroid Build Coastguard Worker // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27*1b3f573fSAndroid Build Coastguard Worker // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28*1b3f573fSAndroid Build Coastguard Worker // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29*1b3f573fSAndroid Build Coastguard Worker // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30*1b3f573fSAndroid Build Coastguard Worker // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31*1b3f573fSAndroid Build Coastguard Worker #endregion 32*1b3f573fSAndroid Build Coastguard Worker 33*1b3f573fSAndroid Build Coastguard Worker using NUnit.Framework; 34*1b3f573fSAndroid Build Coastguard Worker using System.Diagnostics; 35*1b3f573fSAndroid Build Coastguard Worker using System; 36*1b3f573fSAndroid Build Coastguard Worker using System.Reflection; 37*1b3f573fSAndroid Build Coastguard Worker using System.IO; 38*1b3f573fSAndroid Build Coastguard Worker 39*1b3f573fSAndroid Build Coastguard Worker namespace Google.Protobuf 40*1b3f573fSAndroid Build Coastguard Worker { 41*1b3f573fSAndroid Build Coastguard Worker public class RefStructCompatibilityTest 42*1b3f573fSAndroid Build Coastguard Worker { 43*1b3f573fSAndroid Build Coastguard Worker /// <summary> 44*1b3f573fSAndroid Build Coastguard Worker /// Checks that the generated code can be compiler with an old C# compiler. 45*1b3f573fSAndroid Build Coastguard Worker /// The reason why this test is needed is that even though dotnet SDK projects allow to set LangVersion, 46*1b3f573fSAndroid Build Coastguard Worker /// this setting doesn't accurately simulate compilation with an actual old pre-roslyn C# compiler. 47*1b3f573fSAndroid Build Coastguard Worker /// For instance, the "ref struct" types are only supported by C# 7.2 and higher, but even if 48*1b3f573fSAndroid Build Coastguard Worker /// LangVersion is set low, the roslyn compiler still understands the concept of ref struct 49*1b3f573fSAndroid Build Coastguard Worker /// and silently accepts them. Therefore we try to build the generated code with an actual old C# compiler 50*1b3f573fSAndroid Build Coastguard Worker /// to be able to catch these sort of compatibility problems. 51*1b3f573fSAndroid Build Coastguard Worker /// </summary> 52*1b3f573fSAndroid Build Coastguard Worker [Test] GeneratedCodeCompilesWithOldCsharpCompiler()53*1b3f573fSAndroid Build Coastguard Worker public void GeneratedCodeCompilesWithOldCsharpCompiler() 54*1b3f573fSAndroid Build Coastguard Worker { 55*1b3f573fSAndroid Build Coastguard Worker if (Environment.OSVersion.Platform != PlatformID.Win32NT) 56*1b3f573fSAndroid Build Coastguard Worker { 57*1b3f573fSAndroid Build Coastguard Worker // This tests needs old C# compiler which is only available on Windows. Skipping it on all other platforms. 58*1b3f573fSAndroid Build Coastguard Worker return; 59*1b3f573fSAndroid Build Coastguard Worker } 60*1b3f573fSAndroid Build Coastguard Worker 61*1b3f573fSAndroid Build Coastguard Worker var currentAssemblyDir = Path.GetDirectoryName(typeof(RefStructCompatibilityTest).GetTypeInfo().Assembly.Location); 62*1b3f573fSAndroid Build Coastguard Worker var testProtosProjectDir = Path.GetFullPath(Path.Combine(currentAssemblyDir, "..", "..", "..", "..", "Google.Protobuf.Test.TestProtos")); 63*1b3f573fSAndroid Build Coastguard Worker var testProtosOutputDir = (currentAssemblyDir.Contains("bin/Debug/") || currentAssemblyDir.Contains("bin\\Debug\\")) ? "bin\\Debug\\net462" : "bin\\Release\\net462"; 64*1b3f573fSAndroid Build Coastguard Worker 65*1b3f573fSAndroid Build Coastguard Worker // If "ref struct" types are used in the generated code, compilation with an old compiler will fail with the following error: 66*1b3f573fSAndroid Build Coastguard Worker // "XYZ is obsolete: 'Types with embedded references are not supported in this version of your compiler.'" 67*1b3f573fSAndroid Build Coastguard Worker // We build the code with GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE to avoid the use of ref struct in the generated code. 68*1b3f573fSAndroid Build Coastguard Worker var compatibilityFlag = "-define:GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE"; 69*1b3f573fSAndroid Build Coastguard Worker var sources = "*.cs"; // the generated sources from the TestProtos project 70*1b3f573fSAndroid Build Coastguard Worker var args = $"-langversion:3 -target:library {compatibilityFlag} -reference:{testProtosOutputDir}\\Google.Protobuf.dll -out:{testProtosOutputDir}\\TestProtos.RefStructCompatibilityTest.OldCompiler.dll {sources}"; 71*1b3f573fSAndroid Build Coastguard Worker RunOldCsharpCompilerAndCheckSuccess(args, testProtosProjectDir); 72*1b3f573fSAndroid Build Coastguard Worker } 73*1b3f573fSAndroid Build Coastguard Worker 74*1b3f573fSAndroid Build Coastguard Worker /// <summary> 75*1b3f573fSAndroid Build Coastguard Worker /// Invoke an old C# compiler in a subprocess and check it finished successful. 76*1b3f573fSAndroid Build Coastguard Worker /// </summary> 77*1b3f573fSAndroid Build Coastguard Worker /// <param name="args"></param> 78*1b3f573fSAndroid Build Coastguard Worker /// <param name="workingDirectory"></param> RunOldCsharpCompilerAndCheckSuccess(string args, string workingDirectory)79*1b3f573fSAndroid Build Coastguard Worker private void RunOldCsharpCompilerAndCheckSuccess(string args, string workingDirectory) 80*1b3f573fSAndroid Build Coastguard Worker { 81*1b3f573fSAndroid Build Coastguard Worker using (var process = new Process()) 82*1b3f573fSAndroid Build Coastguard Worker { 83*1b3f573fSAndroid Build Coastguard Worker // Get the path to the old C# 5 compiler from .NET framework. This approach is not 100% reliable, but works on most machines. 84*1b3f573fSAndroid Build Coastguard Worker // Alternative way of getting the framework path is System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() 85*1b3f573fSAndroid Build Coastguard Worker // but it only works with the net45 target. 86*1b3f573fSAndroid Build Coastguard Worker var oldCsharpCompilerPath = Path.Combine(Environment.GetEnvironmentVariable("WINDIR"), "Microsoft.NET", "Framework", "v4.0.30319", "csc.exe"); 87*1b3f573fSAndroid Build Coastguard Worker process.StartInfo.FileName = oldCsharpCompilerPath; 88*1b3f573fSAndroid Build Coastguard Worker process.StartInfo.RedirectStandardOutput = true; 89*1b3f573fSAndroid Build Coastguard Worker process.StartInfo.RedirectStandardError = true; 90*1b3f573fSAndroid Build Coastguard Worker process.StartInfo.UseShellExecute = false; 91*1b3f573fSAndroid Build Coastguard Worker process.StartInfo.Arguments = args; 92*1b3f573fSAndroid Build Coastguard Worker process.StartInfo.WorkingDirectory = workingDirectory; 93*1b3f573fSAndroid Build Coastguard Worker 94*1b3f573fSAndroid Build Coastguard Worker process.OutputDataReceived += (sender, e) => 95*1b3f573fSAndroid Build Coastguard Worker { 96*1b3f573fSAndroid Build Coastguard Worker if (e.Data != null) 97*1b3f573fSAndroid Build Coastguard Worker { 98*1b3f573fSAndroid Build Coastguard Worker Console.WriteLine(e.Data); 99*1b3f573fSAndroid Build Coastguard Worker } 100*1b3f573fSAndroid Build Coastguard Worker }; 101*1b3f573fSAndroid Build Coastguard Worker process.ErrorDataReceived += (sender, e) => 102*1b3f573fSAndroid Build Coastguard Worker { 103*1b3f573fSAndroid Build Coastguard Worker if (e.Data != null) 104*1b3f573fSAndroid Build Coastguard Worker { 105*1b3f573fSAndroid Build Coastguard Worker Console.WriteLine(e.Data); 106*1b3f573fSAndroid Build Coastguard Worker } 107*1b3f573fSAndroid Build Coastguard Worker }; 108*1b3f573fSAndroid Build Coastguard Worker 109*1b3f573fSAndroid Build Coastguard Worker process.Start(); 110*1b3f573fSAndroid Build Coastguard Worker 111*1b3f573fSAndroid Build Coastguard Worker process.BeginErrorReadLine(); 112*1b3f573fSAndroid Build Coastguard Worker process.BeginOutputReadLine(); 113*1b3f573fSAndroid Build Coastguard Worker 114*1b3f573fSAndroid Build Coastguard Worker process.WaitForExit(); 115*1b3f573fSAndroid Build Coastguard Worker Assert.AreEqual(0, process.ExitCode); 116*1b3f573fSAndroid Build Coastguard Worker } 117*1b3f573fSAndroid Build Coastguard Worker } 118*1b3f573fSAndroid Build Coastguard Worker } 119*1b3f573fSAndroid Build Coastguard Worker }