|
4 | 4 | using System;
|
5 | 5 | using System.IO;
|
6 | 6 | using System.Reflection;
|
| 7 | +using System.Runtime.InteropServices; |
7 | 8 | using System.Runtime.Loader;
|
8 | 9 | using TestLibrary;
|
9 | 10 | using Xunit;
|
@@ -130,6 +131,88 @@ public void TestAssembly()
|
130 | 131 | }
|
131 | 132 | }
|
132 | 133 |
|
| 134 | + public void TestAssemblyWithCaseDifferent() |
| 135 | + { |
| 136 | + // Testing case sensitive file name resolution |
| 137 | + // Host policy returns 2 file paths with the casing changed, |
| 138 | + // AssemblyDependencyResolver should not throw since the first path exists in the file system |
| 139 | + string assemblyDependencyPath = CreateMockAssembly("TestAssemblyWithCaseDifferent.dll"); |
| 140 | + string nameWOExtension = Path.GetFileNameWithoutExtension(assemblyDependencyPath); |
| 141 | + string nameWOExtensionCaseChanged = (Char.IsUpper(nameWOExtension[0]) ? nameWOExtension[0].ToString().ToLower() : nameWOExtension[0].ToString().ToUpper()) + nameWOExtension.Substring(1); |
| 142 | + string changeFile = Path.Combine(Path.GetDirectoryName(assemblyDependencyPath), (nameWOExtensionCaseChanged + Path.GetExtension(assemblyDependencyPath))); |
| 143 | + |
| 144 | + IntPtr previousWriter = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate( |
| 145 | + (HostPolicyMock.ErrorWriterDelegate)((string _) => { Assert.True(false, "Should never get here"); })); |
| 146 | + |
| 147 | + using (HostPolicyMock.MockValues_corehost_set_error_writer errorWriterMock = |
| 148 | + HostPolicyMock.Mock_corehost_set_error_writer(previousWriter)) |
| 149 | + { |
| 150 | + using (HostPolicyMock.Mock_corehost_resolve_component_dependencies( |
| 151 | + 0, |
| 152 | + $"{assemblyDependencyPath}{Path.PathSeparator}{changeFile}", |
| 153 | + "", |
| 154 | + "")) |
| 155 | + { |
| 156 | + AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(changeFile); |
| 157 | + |
| 158 | + string asmResolveName = resolver.ResolveAssemblyToPath(new AssemblyName(nameWOExtensionCaseChanged)); |
| 159 | + |
| 160 | + Assert.Equal( |
| 161 | + changeFile, asmResolveName, StringComparer.InvariantCultureIgnoreCase |
| 162 | + ); |
| 163 | + |
| 164 | + // After everything is done, the error writer should be reset to the original value. |
| 165 | + Assert.Equal(previousWriter, errorWriterMock.LastSetErrorWriterPtr); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + public void TestAssemblyWithCaseReversed() |
| 171 | + { |
| 172 | + // Testing case sensitive file name resolution |
| 173 | + // Host policy returns 2 file paths with the casing changed and names swapped. |
| 174 | + // AssemblyDependencyResolver should not throw but has different returned values, |
| 175 | + // Based on case sensitive nature of the file system since AssemblyDependencyResolver checks if file exists |
| 176 | + // Case insensitive file systems: a valid path is returned |
| 177 | + // Case sensitive file systems: null (since the first path does not exist in the system) |
| 178 | + string assemblyDependencyPath = CreateMockAssembly("TestAssemblyWithCaseReversed.dll"); |
| 179 | + string nameWOExtension = Path.GetFileNameWithoutExtension(assemblyDependencyPath); |
| 180 | + string nameWOExtensionCaseChanged = (Char.IsUpper(nameWOExtension[0]) ? nameWOExtension[0].ToString().ToLower() : nameWOExtension[0].ToString().ToUpper()) + nameWOExtension.Substring(1); |
| 181 | + string changeFile = Path.Combine(Path.GetDirectoryName(assemblyDependencyPath), (nameWOExtensionCaseChanged + Path.GetExtension(assemblyDependencyPath))); |
| 182 | + |
| 183 | + |
| 184 | + IntPtr previousWriter = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate( |
| 185 | + (HostPolicyMock.ErrorWriterDelegate)((string _) => { Assert.True(false, "Should never get here"); })); |
| 186 | + |
| 187 | + using (HostPolicyMock.MockValues_corehost_set_error_writer errorWriterMock = |
| 188 | + HostPolicyMock.Mock_corehost_set_error_writer(previousWriter)) |
| 189 | + { |
| 190 | + using (HostPolicyMock.Mock_corehost_resolve_component_dependencies( |
| 191 | + 0, |
| 192 | + $"{changeFile}{Path.PathSeparator}{assemblyDependencyPath}", |
| 193 | + "", |
| 194 | + "")) |
| 195 | + { |
| 196 | + AssemblyDependencyResolver resolver = new AssemblyDependencyResolver(changeFile); |
| 197 | + |
| 198 | + string asmResolveName = resolver.ResolveAssemblyToPath(new AssemblyName(nameWOExtensionCaseChanged)); |
| 199 | + |
| 200 | + // Case sensitive systems return null (see notes above) |
| 201 | + // We don't check the OS or the file system here since AssemblyDependencyResolver itself stays away from OS specific checks |
| 202 | + // In path resolutions |
| 203 | + if(asmResolveName != null) |
| 204 | + { |
| 205 | + Assert.Equal( |
| 206 | + assemblyDependencyPath, asmResolveName, StringComparer.InvariantCultureIgnoreCase |
| 207 | + ); |
| 208 | + } |
| 209 | + |
| 210 | + // After everything is done, the error writer should be reset to the original value. |
| 211 | + Assert.Equal(previousWriter, errorWriterMock.LastSetErrorWriterPtr); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + |
133 | 216 | public void TestAssemblyWithNoRecord()
|
134 | 217 | {
|
135 | 218 | // If the reqest is for assembly which is not listed in .deps.json
|
|
0 commit comments