xref: /aosp_15_r20/external/grpc-grpc/src/csharp/Grpc.Tools.Tests/ProtoToolsPlatformTaskTest.cs (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 #region Copyright notice and license
2 
3 // Copyright 2018 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System.Runtime.InteropServices;  // For NETCORE tests.
20 using Microsoft.Build.Framework;
21 using Moq;
22 using NUnit.Framework;
23 
24 namespace Grpc.Tools.Tests
25 {
26     public class ProtoToolsPlatformTaskTest
27     {
28         ProtoToolsPlatform _task;
29         int _cpuMatched, _osMatched;
30 
31         [OneTimeSetUp]
SetUp()32         public void SetUp()
33         {
34             var mockEng = new Mock<IBuildEngine>();
35             _task = new ProtoToolsPlatform() { BuildEngine = mockEng.Object };
36             _task.Execute();
37         }
38 
39         [OneTimeTearDown]
TearDown()40         public void TearDown()
41         {
42             Assert.AreEqual(1, _cpuMatched, "CPU type detection failed");
43             Assert.AreEqual(1, _osMatched, "OS detection failed");
44         }
45 
46 #if NETCORE
47         // PlatformAttribute not yet available in NUnit, coming soon:
48         // https://github.com/nunit/nunit/pull/3003.
49         // Use same test case names as under the full framework.
50         [Test]
CpuIsX86()51         public void CpuIsX86()
52         {
53             if (RuntimeInformation.OSArchitecture == Architecture.X86)
54             {
55                 _cpuMatched++;
56                 Assert.AreEqual("x86", _task.Cpu);
57             }
58         }
59 
60         [Test]
CpuIsX64()61         public void CpuIsX64()
62         {
63             if (RuntimeInformation.OSArchitecture == Architecture.X64)
64             {
65                 _cpuMatched++;
66                 Assert.AreEqual("x64", _task.Cpu);
67             }
68         }
69 
70         [Test]
CpuIsArm64()71         public void CpuIsArm64()
72         {
73             if (RuntimeInformation.OSArchitecture == Architecture.Arm64)
74             {
75                 _cpuMatched++;
76 
77                 // On macosx arm64, x64 is used until a native protoc is shipped
78                 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
79                 {
80                     Assert.AreEqual("x64", _task.Cpu);
81                 }
82                 // On windows arm64, x86 is used until a native protoc is shipped
83                 else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
84                 {
85                     Assert.AreEqual("x86", _task.Cpu);
86                 }
87                 else
88                 {
89                     Assert.AreEqual("arm64", _task.Cpu);
90                 }
91             }
92         }
93 
94         [Test]
OsIsWindows()95         public void OsIsWindows()
96         {
97             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
98             {
99                 _osMatched++;
100                 Assert.AreEqual("windows", _task.Os);
101             }
102         }
103 
104         [Test]
OsIsLinux()105         public void OsIsLinux()
106         {
107             if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
108             {
109                 _osMatched++;
110                 Assert.AreEqual("linux", _task.Os);
111             }
112         }
113 
114         [Test]
OsIsMacOsX()115         public void OsIsMacOsX()
116         {
117             if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
118             {
119                 _osMatched++;
120                 Assert.AreEqual("macosx", _task.Os);
121             }
122         }
123 
124 #else  // !NETCORE, i.e. full framework.
125 
126         [Test, Platform("32-Bit")]
CpuIsX86()127         public void CpuIsX86()
128         {
129             _cpuMatched++;
130             Assert.AreEqual("x86", _task.Cpu);
131         }
132 
133         [Test, Platform("64-Bit")]
CpuIsX64()134         public void CpuIsX64()
135         {
136             _cpuMatched++;
137             Assert.AreEqual("x64", _task.Cpu);
138         }
139 
140         [Test, Platform("Win")]
OsIsWindows()141         public void OsIsWindows()
142         {
143             _osMatched++;
144             Assert.AreEqual("windows", _task.Os);
145         }
146 
147         [Test, Platform("Linux")]
OsIsLinux()148         public void OsIsLinux()
149         {
150             _osMatched++;
151             Assert.AreEqual("linux", _task.Os);
152         }
153 
154         [Test, Platform("MacOSX")]
OsIsMacOsX()155         public void OsIsMacOsX()
156         {
157             _osMatched++;
158             Assert.AreEqual("macosx", _task.Os);
159         }
160 
161 #endif  // NETCORE
162     };
163 }
164