public static void Check(VulnerabilityCollection vulnerabilities, SystemInfo sysInfo) { try { int version = SystemInfoHelpers.GetFileVersionInfoProductPrivatePart(sysInfo.WinPath + "\\gpprefcl.dll"); switch (sysInfo.OsBuild) { case "10240": if (version < 16942) { vulnerabilities.SetAsVulnerable("MS16-072"); } break; case "10586": if (version < 420) { vulnerabilities.SetAsVulnerable("MS16-072"); } break; } } catch (Exception ex) { vulnerabilities.AddError(ex.Message); } }
public static void Check(VulnerabilityCollection vulnerabilities, SystemInfo sysInfo) { try { int version = SystemInfoHelpers.GetFileVersionInfoProductPrivatePart(sysInfo.WinPath + "\\atmfd.dll"); switch (sysInfo.OsBuild) { case "9200": if (version < 243) { vulnerabilities.SetAsVulnerable("MS15-078"); } break; case "9600": if (version < 243) { vulnerabilities.SetAsVulnerable("MS15-078"); } break; } } catch (Exception ex) { vulnerabilities.AddError(ex.Message); } }
public static void Check(VulnerabilityCollection vulnerabilities, SystemInfo sysInfo) { try { int version = SystemInfoHelpers.GetFileVersionInfoProductPrivatePart(sysInfo.WinPath + "\\schedsvc.dll"); switch (sysInfo.OsBuild) { case "6001": if (version < 18551) { vulnerabilities.SetAsVulnerable("MS10-092"); } break; case "6002": if (version < 18342) { vulnerabilities.SetAsVulnerable("MS10-092"); } break; case "7600": if (version < 16699) { vulnerabilities.SetAsVulnerable("MS10-092"); } break; } } catch (Exception ex) { vulnerabilities.AddError(ex.Message); } }
public static void Check(VulnerabilityCollection vulnerabilities, SystemInfo sysInfo) { try { int version = SystemInfoHelpers.GetFileVersionInfoProductPrivatePart(sysInfo.WinPath + "\\win32kfull.sys"); switch (sysInfo.OsBuild) { case "10240": if (version < 16683) { vulnerabilities.SetAsVulnerable("MS16-014"); } if (version < 16724) { vulnerabilities.SetAsVulnerable("MS16-034"); } if (version < 16771) { vulnerabilities.SetAsVulnerable("MS16-039"); } break; case "10586": if (version < 103) { vulnerabilities.SetAsVulnerable("MS16-014"); } if (version < 162) { vulnerabilities.SetAsVulnerable("MS16-034"); } if (version < 212) { vulnerabilities.SetAsVulnerable("MS16-039"); } break; } } catch (Exception ex) { vulnerabilities.AddError(ex.Message); } }
public static void Check(VulnerabilityCollection vulnerabilities, SystemInfo sysInfo) { try { int version = SystemInfoHelpers.GetFileVersionInfoProductPrivatePart(sysInfo.WinPath + "\\seclogon.dll"); switch (sysInfo.OsBuild) { case "6002": if (version < 19598 && sysInfo.CpuCount > 1) { vulnerabilities.SetAsVulnerable("MS16-032"); } break; case "9600": if (version < 18230 && sysInfo.CpuCount > 1) { vulnerabilities.SetAsVulnerable("MS16-032"); } break; case "10240": if (version < 16724 && sysInfo.CpuCount > 1) { vulnerabilities.SetAsVulnerable("MS16-032"); } break; case "10586": if (version < 162 && sysInfo.CpuCount > 1) { vulnerabilities.SetAsVulnerable("MS16-032"); } break; } } catch (Exception ex) { vulnerabilities.AddError(ex.Message); } }
public static void Check(VulnerabilityCollection vulnerabilities, SystemInfo sysInfo) { try { int version = SystemInfoHelpers.GetFileVersionInfoProductPrivatePart(sysInfo.WinPath + "\\drivers\\mrxdav.sys"); switch (sysInfo.OsBuild) { case "6002": if (version < 19576) { vulnerabilities.SetAsVulnerable("MS16-016"); } break; case "9600": if (version < 18189) { vulnerabilities.SetAsVulnerable("MS16-016"); } break; case "10240": if (version < 16683) { vulnerabilities.SetAsVulnerable("MS16-016"); } break; case "10586": if (version < 103) { vulnerabilities.SetAsVulnerable("MS16-016"); } break; } } catch (Exception ex) { vulnerabilities.AddError(ex.Message); } }
public static void Check(VulnerabilityCollection vulnerabilities, SystemInfo sysInfo) { try { int version = SystemInfoHelpers.GetFileVersionInfoProductPrivatePart(sysInfo.WinPath + "\\pcadm.dll"); switch (sysInfo.OsBuild) { case "10240": if (version < 17861) { vulnerabilities.SetAsVulnerable("CVE-2018-8897"); } break; } } catch (Exception ex) { vulnerabilities.AddError(ex.Message); } }
public static void Check(VulnerabilityCollection vulnerabilities, SystemInfo sysInfo) { try { int version = SystemInfoHelpers.GetFileVersionInfoProductPrivatePart(sysInfo.WinPath + "\\gdi32.dll"); switch (sysInfo.OsBuild) { case "10240": if (version < 17319) { vulnerabilities.SetAsVulnerable("MS17-012"); vulnerabilities.SetAsVulnerable("MS17-017"); } if (version < 17394) { vulnerabilities.SetAsVulnerable("CVE-2017-0263"); } break; case "10586": if (version < 839) { vulnerabilities.SetAsVulnerable("MS17-012"); vulnerabilities.SetAsVulnerable("MS17-017"); } if (version < 916) { vulnerabilities.SetAsVulnerable("CVE-2017-0263"); } break; } } catch (Exception ex) { vulnerabilities.AddError(ex.Message); } }
public static void Check(VulnerabilityCollection vulnerabilities, SystemInfo sysInfo) { try { int version = SystemInfoHelpers.GetFileVersionInfoProductPrivatePart(sysInfo.WinPath + "\\drivers\\afd.sys"); switch (sysInfo.OsBuild) { case "6001": if (version < 18639) { vulnerabilities.SetAsVulnerable("MS11-046"); } break; case "6002": if (version < 18457) { vulnerabilities.SetAsVulnerable("MS11-046"); } break; case "7600": if (version < 16802) { vulnerabilities.SetAsVulnerable("MS11-046"); } break; } } catch (Exception ex) { vulnerabilities.AddError(ex.Message); } }
public bool runTest() { try { //////////////////////////////////////////////////////////////////////// // CreateViewStream() //////////////////////////////////////////////////////////////////////// long defaultCapacity = SystemInfoHelpers.GetPageSize(); MemoryMappedFileAccess defaultAccess = MemoryMappedFileAccess.ReadWrite; String fileContents = String.Empty; // Verify default values using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, 100)) { VerifyCreateViewStream("Loc101", mmf, Interop.IsWindows ? defaultCapacity : 100, // Windows rounds up to page size, but that seems like a bug defaultAccess, fileContents); } // default length is full MMF using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, defaultCapacity * 2)) { VerifyCreateViewStream("Loc102", mmf, defaultCapacity * 2, defaultAccess, fileContents); } // if MMF is read-only, default access throws using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, 100, MemoryMappedFileAccess.Read)) { VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc103", mmf); } //////////////////////////////////////////////////////////////////////// // CreateViewStream(long, long) //////////////////////////////////////////////////////////////////////// // capacity using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, defaultCapacity * 2)) { // 0 VerifyCreateViewStream("Loc201", mmf, 0, 0, defaultCapacity * 2, defaultAccess, fileContents); // >0 VerifyCreateViewStream("Loc202", mmf, 100, 0, defaultCapacity * 2 - 100, defaultAccess, fileContents); // >pagesize VerifyCreateViewStream("Loc203", mmf, defaultCapacity + 100, 0, defaultCapacity - 100, defaultAccess, fileContents); // <0 VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc204", mmf, -1, 0); // =MMF capacity VerifyCreateViewStream("Loc205", mmf, defaultCapacity * 2, 0, 0, defaultAccess, fileContents); // >MMF capacity VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc206", mmf, defaultCapacity * 2 + 1, 0); } // size using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, defaultCapacity * 2)) { // 0 VerifyCreateViewStream("Loc211", mmf, 1000, 0, defaultCapacity * 2 - 1000, defaultAccess, fileContents); // >0, <pagesize VerifyCreateViewStream("Loc212", mmf, 100, 1000, 1000, defaultAccess, fileContents); // =pagesize VerifyCreateViewStream("Loc213", mmf, 100, defaultCapacity, defaultCapacity, defaultAccess, fileContents); // >pagesize VerifyCreateViewStream("Loc214", mmf, 100, defaultCapacity + 1000, defaultCapacity + 1000, defaultAccess, fileContents); // <0 VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc215", mmf, 0, -1); VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc216", mmf, 0, -2); VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc217", mmf, 0, Int64.MinValue); // offset+size = MMF capacity VerifyCreateViewStream("Loc218", mmf, 0, defaultCapacity * 2, defaultCapacity * 2, defaultAccess, fileContents); VerifyCreateViewStream("Loc219", mmf, defaultCapacity, defaultCapacity, defaultCapacity, defaultAccess, fileContents); VerifyCreateViewStream("Loc220", mmf, defaultCapacity * 2 - 1, 1, 1, defaultAccess, fileContents); // offset+size > MMF capacity VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc221", mmf, 0, defaultCapacity * 2 + 1); VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc222", mmf, 1, defaultCapacity * 2); VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc223", mmf, defaultCapacity, defaultCapacity + 1); VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc224", mmf, defaultCapacity * 2, 1); // Int64.MaxValue - cannot exceed local address space if (IntPtr.Size == 4) { VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc225", mmf, 0, Int64.MaxValue); } else // 64-bit machine { VerifyCreateViewStreamException <IOException>("Loc225b", mmf, 0, Int64.MaxValue); // valid but too large } } //////////////////////////////////////////////////////////////////////// // CreateViewStream(long, long, MemoryMappedFileAccess) //////////////////////////////////////////////////////////////////////// // [] offset using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, defaultCapacity * 2)) { // 0 VerifyCreateViewStream("Loc401", mmf, 0, 0, defaultAccess, defaultCapacity * 2, defaultAccess, fileContents); // >0 VerifyCreateViewStream("Loc402", mmf, 100, 0, defaultAccess, defaultCapacity * 2 - 100, defaultAccess, fileContents); // >pagesize VerifyCreateViewStream("Loc403", mmf, defaultCapacity + 100, 0, defaultAccess, defaultCapacity - 100, defaultAccess, fileContents); // <0 VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc404", mmf, -1, 0, defaultAccess); // =MMF capacity VerifyCreateViewStream("Loc405", mmf, defaultCapacity * 2, 0, defaultAccess, 0, defaultAccess, fileContents); // >MMF capacity VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc406", mmf, defaultCapacity * 2 + 1, 0, defaultAccess); } // size using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, defaultCapacity * 2)) { // 0 VerifyCreateViewStream("Loc411", mmf, 1000, 0, defaultAccess, defaultCapacity * 2 - 1000, defaultAccess, fileContents); // >0, <pagesize VerifyCreateViewStream("Loc412", mmf, 100, 1000, defaultAccess, 1000, defaultAccess, fileContents); // =pagesize VerifyCreateViewStream("Loc413", mmf, 100, defaultCapacity, defaultAccess, defaultCapacity, defaultAccess, fileContents); // >pagesize VerifyCreateViewStream("Loc414", mmf, 100, defaultCapacity + 1000, defaultAccess, defaultCapacity + 1000, defaultAccess, fileContents); // <0 VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc415", mmf, 0, -1, defaultAccess); VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc416", mmf, 0, -2, defaultAccess); VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc417", mmf, 0, Int64.MinValue, defaultAccess); // offset+size = MMF capacity VerifyCreateViewStream("Loc418", mmf, 0, defaultCapacity * 2, defaultAccess, defaultCapacity * 2, defaultAccess, fileContents); VerifyCreateViewStream("Loc419", mmf, defaultCapacity, defaultCapacity, defaultAccess, defaultCapacity, defaultAccess, fileContents); VerifyCreateViewStream("Loc420", mmf, defaultCapacity * 2 - 1, 1, defaultAccess, 1, defaultAccess, fileContents); // offset+size > MMF capacity VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc421", mmf, 0, defaultCapacity * 2 + 1, defaultAccess); VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc422", mmf, 1, defaultCapacity * 2, defaultAccess); VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc423", mmf, defaultCapacity, defaultCapacity + 1, defaultAccess); VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc424", mmf, defaultCapacity * 2, 1, defaultAccess); // Int64.MaxValue - cannot exceed local address space if (IntPtr.Size == 4) { VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc425", mmf, 0, Int64.MaxValue, defaultAccess); } else // 64-bit machine { VerifyCreateViewStreamException <IOException>("Loc425b", mmf, 0, Int64.MaxValue, defaultAccess); // valid but too large } } // [] access long specifiedCapacity = 1000; long expectedCapacity = Interop.IsWindows ? defaultCapacity : // on Windows it uninentionally rounds up to the page size specifiedCapacity; MemoryMappedFileAccess[] accessList; // existing file is ReadWriteExecute using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, specifiedCapacity, MemoryMappedFileAccess.ReadWriteExecute)) { accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.Read, MemoryMappedFileAccess.Write, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileAccess.CopyOnWrite, MemoryMappedFileAccess.ReadExecute, MemoryMappedFileAccess.ReadWriteExecute, }; foreach (MemoryMappedFileAccess access in accessList) { VerifyCreateViewStream("Loc431_" + access, mmf, 0, 0, access, expectedCapacity, access, fileContents); } } // existing file is ReadExecute using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, specifiedCapacity, MemoryMappedFileAccess.ReadExecute)) { accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.Read, MemoryMappedFileAccess.CopyOnWrite, MemoryMappedFileAccess.ReadExecute, }; foreach (MemoryMappedFileAccess access in accessList) { VerifyCreateViewStream("Loc432_" + access, mmf, 0, 0, access, expectedCapacity, access, fileContents); } accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.Write, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileAccess.ReadWriteExecute, }; foreach (MemoryMappedFileAccess access in accessList) { VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc432_" + access, mmf, 0, 0, access); } } // existing file is CopyOnWrite using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, specifiedCapacity, MemoryMappedFileAccess.CopyOnWrite)) { accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.Read, MemoryMappedFileAccess.CopyOnWrite, }; foreach (MemoryMappedFileAccess access in accessList) { VerifyCreateViewStream("Loc433_" + access, mmf, 0, 0, access, expectedCapacity, access, fileContents); } accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.Write, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileAccess.ReadExecute, MemoryMappedFileAccess.ReadWriteExecute, }; foreach (MemoryMappedFileAccess access in accessList) { VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc433_" + access, mmf, 0, 0, access); } } // existing file is ReadWrite using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, specifiedCapacity, MemoryMappedFileAccess.ReadWrite)) { accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.Read, MemoryMappedFileAccess.Write, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileAccess.CopyOnWrite, }; foreach (MemoryMappedFileAccess access in accessList) { VerifyCreateViewStream("Loc434_" + access, mmf, 0, 0, access, expectedCapacity, access, fileContents); } accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.ReadExecute, MemoryMappedFileAccess.ReadWriteExecute, }; foreach (MemoryMappedFileAccess access in accessList) { VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc434_" + access, mmf, 0, 0, access); } } // existing file is Read using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, specifiedCapacity, MemoryMappedFileAccess.Read)) { accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.Read, MemoryMappedFileAccess.CopyOnWrite, }; foreach (MemoryMappedFileAccess access in accessList) { VerifyCreateViewStream("Loc435_" + access, mmf, 0, 0, access, expectedCapacity, access, fileContents); } accessList = new MemoryMappedFileAccess[] { MemoryMappedFileAccess.Write, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileAccess.ReadExecute, MemoryMappedFileAccess.ReadWriteExecute, }; foreach (MemoryMappedFileAccess access in accessList) { VerifyCreateViewStreamException <UnauthorizedAccessException>("Loc435_" + access, mmf, 0, 0, access); } } // invalid enum value using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, specifiedCapacity, MemoryMappedFileAccess.ReadWrite)) { accessList = new MemoryMappedFileAccess[] { (MemoryMappedFileAccess)(-1), (MemoryMappedFileAccess)(6), }; foreach (MemoryMappedFileAccess access in accessList) { VerifyCreateViewStreamException <ArgumentOutOfRangeException>("Loc436_" + ((int)access), mmf, 0, 0, access); } } // File-backed MemoryMappedFile size should not be constrained to size of system's logical address space TestLargeCapacity(); /// END TEST CASES if (iCountErrors == 0) { return(true); } else { Console.WriteLine("FAiL! iCountErrors==" + iCountErrors); return(false); } } catch (Exception ex) { Console.WriteLine("ERR999: Unexpected exception in runTest, {0}", ex); return(false); } }
public bool runTest() { uint pageSize = SystemInfoHelpers.GetPageSize(); Type[] testedTypes = new Type[] { typeof(Boolean), typeof(Char), typeof(Byte), typeof(Int16), typeof(Int32), typeof(Int64), typeof(SByte), typeof(UInt16), typeof(UInt32), typeof(UInt64), typeof(Single), typeof(Double), typeof(Decimal) }; Type[] oneByteTypes = new Type[] { typeof(Boolean), typeof(Byte), typeof(SByte) }; Type[] twoByteTypes = new Type[] { typeof(Char), typeof(Int16), typeof(UInt16) }; Type[] fourByteTypes = new Type[] { typeof(Single), typeof(Int32), typeof(UInt32) }; Type[] eightByteTypes = new Type[] { typeof(Double), typeof(Int64), typeof(UInt64) }; Type[] sixteenByteTypes = new Type[] { typeof(Decimal) }; Byte[] byteArray1 = new Byte[] { (byte)194, (byte)41, (byte)1, (byte)150, (byte)114, (byte)62, (byte)81, (byte)203, (byte)251, (byte)39, (byte)83, (byte)124, (byte)0, (byte)0, (byte)14, (byte)128 }; Byte[] byteArray2 = new Byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0 }; Byte[] byteArray3 = new Byte[] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }; try { using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("MMVA_ReadX0", pageSize * 10)) { // Default ViewAccessor size - whole MMF using (MemoryMappedViewAccessor view = mmf.CreateViewAccessor()) { // position = 0, default (zeros) VerifyRead <Boolean>("Loc001a", view, 0, false); VerifyRead <Byte>("Loc001b", view, 0, 0); VerifyRead <SByte>("Loc001c", view, 0, 0); VerifyRead <Char>("Loc001d", view, 0, (Char)0); VerifyRead <Int16>("Loc001e", view, 0, 0); VerifyRead <UInt16>("Loc001f", view, 0, 0); VerifyRead <Int32>("Loc001g", view, 0, 0); VerifyRead <UInt32>("Loc001h", view, 0, 0); VerifyRead <Int64>("Loc001i", view, 0, 0); VerifyRead <UInt64>("Loc001j", view, 0, 0); VerifyRead <Single>("Loc001k", view, 0, 0f); VerifyRead <Double>("Loc001l", view, 0, 0d); VerifyRead <Decimal>("Loc001m", view, 0, 0m); // position = 1 VerifyRead <Boolean>("Loc002a", view, 1, false); VerifyRead <Byte>("Loc002b", view, 1, 0); VerifyRead <SByte>("Loc002c", view, 1, 0); VerifyRead <Char>("Loc002d", view, 1, (Char)0); VerifyRead <Int16>("Loc002e", view, 1, 0); VerifyRead <UInt16>("Loc002f", view, 1, 0); VerifyRead <Int32>("Loc002g", view, 1, 0); VerifyRead <UInt32>("Loc002h", view, 1, 0); VerifyRead <Int64>("Loc002i", view, 1, 0); VerifyRead <UInt64>("Loc002j", view, 1, 0); VerifyRead <Single>("Loc002k", view, 1, 0f); VerifyRead <Double>("Loc002l", view, 1, 0d); VerifyRead <Decimal>("Loc002m", view, 1, 0m); // position is last possible for type VerifyRead <Boolean>("Loc003a", view, view.Capacity - 1, false); VerifyRead <Byte>("Loc003b", view, view.Capacity - 1, 0); VerifyRead <SByte>("Loc003c", view, view.Capacity - 1, 0); VerifyRead <Char>("Loc003d", view, view.Capacity - 2, (Char)0); VerifyRead <Int16>("Loc003e", view, view.Capacity - 2, 0); VerifyRead <UInt16>("Loc003f", view, view.Capacity - 2, 0); VerifyRead <Single>("Loc003k", view, view.Capacity - 4, 0f); VerifyRead <Int32>("Loc003g", view, view.Capacity - 4, 0); VerifyRead <UInt32>("Loc003h", view, view.Capacity - 4, 0); VerifyRead <Double>("Loc003l", view, view.Capacity - 8, 0d); VerifyRead <Int64>("Loc003i", view, view.Capacity - 8, 0); VerifyRead <UInt64>("Loc003j", view, view.Capacity - 8, 0); VerifyRead <Decimal>("Loc003m", view, view.Capacity - 16, 0m); // Write some data view.Write(0, (byte)1); view.Write(1000, (byte)1); view.Write(view.Capacity - 1, (byte)255); WriteByteArray(view, 4703, byteArray1, 0, 16); WriteByteArray(view, 12000 - 16, byteArray3, 0, 16); WriteByteArray(view, 2000, byteArray3, 0, 16); // position = 0 VerifyRead <Boolean>("Loc011a", view, 0, true); VerifyRead <Byte>("Loc011b", view, 0, 1); VerifyRead <SByte>("Loc11c", view, 0, 1); VerifyRead <Char>("Loc011d", view, 0, (Char)1); VerifyRead <Int16>("Loc011e", view, 0, 1); VerifyRead <UInt16>("Loc011f", view, 0, 1); VerifyRead <Int32>("Loc011g", view, 0, 1); VerifyRead <UInt32>("Loc011h", view, 0, 1); VerifyRead <Int64>("Loc011i", view, 0, 1); VerifyRead <UInt64>("Loc011j", view, 0, 1); VerifyRead <Single>("Loc011k", view, 0, Single.Epsilon); VerifyRead <Double>("Loc011l", view, 0, Double.Epsilon); VerifyRead <Decimal>("Loc011m", view, 0, 1m); //Byte[] byteArray1 = new Byte[] { (byte)194, (byte)41, (byte)1, (byte)150, (byte)114, (byte)62, (byte)81, (byte)203, (byte)251, (byte)39, (byte)83, (byte)124, (byte)0, (byte)0, (byte)14, (byte)128 }; // position > 0 int pos = 4703; VerifyRead <Boolean>("Loc012a1", view, pos, true); VerifyRead <Boolean>("Loc012a2", view, pos + 2, true); VerifyRead <Boolean>("Loc012a3", view, pos + 12, false); VerifyRead <Byte>("Loc012b1", view, pos, 194); VerifyRead <Byte>("Loc012b2", view, pos + 15, 128); VerifyRead <Byte>("Loc012b3", view, pos + 5, 62); VerifyRead <SByte>("Loc012c1", view, pos, -62); VerifyRead <SByte>("Loc012c2", view, pos + 15, -128); VerifyRead <SByte>("Loc012c3", view, pos + 5, 62); VerifyRead <Char>("Loc012d1", view, pos, (Char)10690); VerifyRead <Char>("Loc012d2", view, pos + 7, (Char)64459); VerifyRead <Char>("Loc012d3", view, pos + 15, (Char)128); VerifyRead <Int16>("Loc012e1", view, pos, 10690); VerifyRead <Int16>("Loc012e2", view, pos + 7, -1077); VerifyRead <Int16>("Loc012e3", view, pos + 5, 20798); VerifyRead <UInt16>("Loc012f1", view, pos, 10690); VerifyRead <UInt16>("Loc012f2", view, pos + 7, 64459); VerifyRead <UInt16>("Loc012f3", view, pos + 5, 20798); VerifyRead <Int32>("Loc012g1", view, pos + 4, BitConverter.ToInt32(byteArray1, 4)); VerifyRead <Int32>("Loc012g2", view, pos + 11, BitConverter.ToInt32(byteArray1, 11)); VerifyRead <Int32>("Loc012g3", view, pos + 15, 128); VerifyRead <UInt32>("Loc012h1", view, pos + 4, BitConverter.ToUInt32(byteArray1, 4)); VerifyRead <UInt32>("Loc012h2", view, pos + 11, BitConverter.ToUInt32(byteArray1, 11)); VerifyRead <UInt32>("Loc012h3", view, pos + 15, 128); VerifyRead <Int64>("Loc012i1", view, pos, BitConverter.ToInt64(byteArray1, 0)); VerifyRead <Int64>("Loc012i2", view, pos + 3, BitConverter.ToInt64(byteArray1, 3)); VerifyRead <Int64>("Loc012i3", view, pos + 15, 128); VerifyRead <UInt64>("Loc012j1", view, pos, BitConverter.ToUInt64(byteArray1, 0)); VerifyRead <UInt64>("Loc012j2", view, pos + 3, BitConverter.ToUInt64(byteArray1, 3)); VerifyRead <UInt64>("Loc012j3", view, pos + 15, 128); VerifyRead <Single>("Loc012k1", view, pos, BitConverter.ToSingle(byteArray1, 0)); VerifyRead <Single>("Loc012k2", view, pos + 5, BitConverter.ToSingle(byteArray1, 5)); VerifyRead <Double>("Loc012l1", view, pos, BitConverter.ToDouble(byteArray1, 0)); VerifyRead <Double>("Loc012l2", view, pos + 5, BitConverter.ToDouble(byteArray1, 5)); VerifyRead <Decimal>("Loc012m1", view, pos, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) })); VerifyRead <Decimal>("Loc012m2", view, pos + 4, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12), 0 })); // ensure last 4 bytes are zero // invalid values VerifyRead <Boolean>("Loc013a", view, 2000, true); VerifyReadException("Loc013d", typeof(Decimal), view, view.Capacity - 1, typeof(ArgumentException)); // position is last possible for type VerifyRead <Boolean>("Loc014a", view, view.Capacity - 1, true); VerifyRead <Byte>("Loc014b", view, view.Capacity - 1, 255); VerifyRead <SByte>("Loc014c", view, view.Capacity - 1, -1); VerifyRead <Char>("Loc014d", view, view.Capacity - 2, (Char)65280); VerifyRead <Int16>("Loc014e", view, view.Capacity - 2, -256); VerifyRead <UInt16>("Loc014f", view, view.Capacity - 2, 65280); VerifyRead <Single>("Loc014k", view, view.Capacity - 4, BitConverter.ToSingle(new Byte[] { 0, 0, 0, 255 }, 0)); VerifyRead <Int32>("Loc014g", view, view.Capacity - 4, (int)255 << 24); VerifyRead <UInt32>("Loc014h", view, view.Capacity - 4, (uint)255 << 24); VerifyRead <Double>("Loc014l", view, view.Capacity - 8, BitConverter.ToDouble(new Byte[] { 0, 0, 0, 0, 0, 0, 0, 255 }, 0)); VerifyRead <Int64>("Loc014i", view, view.Capacity - 8, ((long)255 << 56)); VerifyRead <UInt64>("Loc014j", view, view.Capacity - 8, ((ulong)255 << 56)); view.Write(view.Capacity - 1, (byte)128); view.Write(view.Capacity - 15, (byte)255); VerifyRead <Decimal>("Loc014m", view, view.Capacity - 16, -65280m); view.Write(view.Capacity - 1, (byte)255); // Exceptions foreach (Type type in testedTypes) { // position <0 VerifyReadException("Loc031", type, view, -1, typeof(ArgumentOutOfRangeException)); // position >= view.Capacity VerifyReadException("Loc032", type, view, view.Capacity, typeof(ArgumentOutOfRangeException)); VerifyReadException("Loc033", type, view, view.Capacity + 1, typeof(ArgumentOutOfRangeException)); } // position+sizeof(type) >= view.Capacity foreach (Type type in twoByteTypes) { VerifyReadException("Loc034", type, view, view.Capacity - 1, typeof(ArgumentException)); } foreach (Type type in fourByteTypes) { VerifyReadException("Loc035", type, view, view.Capacity - 3, typeof(ArgumentException)); } foreach (Type type in eightByteTypes) { VerifyReadException("Loc036", type, view, view.Capacity - 7, typeof(ArgumentException)); } foreach (Type type in sixteenByteTypes) { VerifyReadException("Loc037", type, view, view.Capacity - 15, typeof(ArgumentException)); } } // ViewAccessor starts at nonzero offset, spans remainder of MMF using (MemoryMappedViewAccessor view = mmf.CreateViewAccessor(1000, 0)) { // position = 0 VerifyRead <Boolean>("Loc111a", view, 0, true); VerifyRead <Byte>("Loc111b", view, 0, 1); VerifyRead <SByte>("Loc111c", view, 0, 1); VerifyRead <Char>("Loc111d", view, 0, (Char)1); VerifyRead <Int16>("Loc111e", view, 0, 1); VerifyRead <UInt16>("Loc111f", view, 0, 1); VerifyRead <Int32>("Loc111g", view, 0, 1); VerifyRead <UInt32>("Loc111h", view, 0, 1); VerifyRead <Int64>("Loc111i", view, 0, 1); VerifyRead <UInt64>("Loc111j", view, 0, 1); VerifyRead <Single>("Loc111k", view, 0, Single.Epsilon); VerifyRead <Double>("Loc111l", view, 0, Double.Epsilon); VerifyRead <Decimal>("Loc111m", view, 0, 1); // position > 0 int pos = 3703; // already written at 4703 in the previous accessor VerifyRead <Boolean>("Loc112a1", view, pos, true); VerifyRead <Boolean>("Loc112a2", view, pos + 2, true); VerifyRead <Boolean>("Loc112a3", view, pos + 12, false); VerifyRead <Byte>("Loc112b1", view, pos, 194); VerifyRead <Byte>("Loc112b2", view, pos + 15, 128); VerifyRead <Byte>("Loc112b3", view, pos + 5, 62); VerifyRead <SByte>("Loc112c1", view, pos, -62); VerifyRead <SByte>("Loc112c2", view, pos + 15, -128); VerifyRead <SByte>("Loc112c3", view, pos + 5, 62); VerifyRead <Char>("Loc112d1", view, pos, (Char)10690); VerifyRead <Char>("Loc112d2", view, pos + 7, (Char)64459); VerifyRead <Char>("Loc112d3", view, pos + 15, (Char)128); VerifyRead <Int16>("Loc112e1", view, pos, 10690); VerifyRead <Int16>("Loc112e2", view, pos + 7, -1077); VerifyRead <Int16>("Loc112e3", view, pos + 5, 20798); VerifyRead <UInt16>("Loc112f1", view, pos, 10690); VerifyRead <UInt16>("Loc112f2", view, pos + 7, 64459); VerifyRead <UInt16>("Loc112f3", view, pos + 5, 20798); VerifyRead <Int32>("Loc112g1", view, pos + 4, BitConverter.ToInt32(byteArray1, 4)); VerifyRead <Int32>("Loc112g2", view, pos + 11, BitConverter.ToInt32(byteArray1, 11)); VerifyRead <Int32>("Loc112g3", view, pos + 15, 128); VerifyRead <UInt32>("Loc112h1", view, pos + 4, BitConverter.ToUInt32(byteArray1, 4)); VerifyRead <UInt32>("Loc112h2", view, pos + 11, BitConverter.ToUInt32(byteArray1, 11)); VerifyRead <UInt32>("Loc112h3", view, pos + 15, 128); VerifyRead <Int64>("Loc112i1", view, pos, BitConverter.ToInt64(byteArray1, 0)); VerifyRead <Int64>("Loc112i2", view, pos + 3, BitConverter.ToInt64(byteArray1, 3)); VerifyRead <Int64>("Loc112i3", view, pos + 15, 128); VerifyRead <UInt64>("Loc112j1", view, pos, BitConverter.ToUInt64(byteArray1, 0)); VerifyRead <UInt64>("Loc112j2", view, pos + 3, BitConverter.ToUInt64(byteArray1, 3)); VerifyRead <UInt64>("Loc112j3", view, pos + 15, 128); VerifyRead <Single>("Loc112k1", view, pos, BitConverter.ToSingle(byteArray1, 0)); VerifyRead <Single>("Loc112k2", view, pos + 5, BitConverter.ToSingle(byteArray1, 5)); VerifyRead <Double>("Loc112l1", view, pos, BitConverter.ToDouble(byteArray1, 0)); VerifyRead <Double>("Loc112l2", view, pos + 5, BitConverter.ToDouble(byteArray1, 5)); VerifyRead <Decimal>("Loc112m1", view, pos, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) })); VerifyRead <Decimal>("Loc112m2", view, pos + 4, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12), 0 })); // ensure last 4 bytes are zero // position is last possible for type VerifyRead <Boolean>("Loc114a", view, view.Capacity - 1, true); VerifyRead <Byte>("Loc114b", view, view.Capacity - 1, 255); VerifyRead <SByte>("Loc114c", view, view.Capacity - 1, -1); VerifyRead <Char>("Loc114d", view, view.Capacity - 2, (Char)65280); VerifyRead <Int16>("Loc114e", view, view.Capacity - 2, -256); VerifyRead <UInt16>("Loc114f", view, view.Capacity - 2, 65280); VerifyRead <Single>("Loc114k", view, view.Capacity - 4, BitConverter.ToSingle(new Byte[] { 0, 0, 0, 255 }, 0)); VerifyRead <Int32>("Loc114g", view, view.Capacity - 4, (int)255 << 24); VerifyRead <UInt32>("Loc114h", view, view.Capacity - 4, (uint)255 << 24); VerifyRead <Double>("Loc114l", view, view.Capacity - 8, BitConverter.ToDouble(new Byte[] { 0, 0, 0, 0, 0, 0, 0, 255 }, 0)); VerifyRead <Int64>("Loc114i", view, view.Capacity - 8, ((long)255 << 56)); VerifyRead <UInt64>("Loc114j", view, view.Capacity - 8, ((ulong)255 << 56)); view.Write(view.Capacity - 1, (byte)128); view.Write(view.Capacity - 15, (byte)255); VerifyRead <Decimal>("Loc114m", view, view.Capacity - 16, -65280m); view.Write(view.Capacity - 1, (byte)255); // Exceptions foreach (Type type in testedTypes) { // position <0 VerifyReadException("Loc131", type, view, -1, typeof(ArgumentOutOfRangeException)); // position >= view.Capacity VerifyReadException("Loc132", type, view, view.Capacity, typeof(ArgumentOutOfRangeException)); VerifyReadException("Loc133", type, view, view.Capacity + 1, typeof(ArgumentOutOfRangeException)); } // position+sizeof(type) >= view.Capacity foreach (Type type in twoByteTypes) { VerifyReadException("Loc134", type, view, view.Capacity - 1, typeof(ArgumentException)); } foreach (Type type in fourByteTypes) { VerifyReadException("Loc135", type, view, view.Capacity - 3, typeof(ArgumentException)); } foreach (Type type in eightByteTypes) { VerifyReadException("Loc136", type, view, view.Capacity - 7, typeof(ArgumentException)); } foreach (Type type in sixteenByteTypes) { VerifyReadException("Loc137", type, view, view.Capacity - 15, typeof(ArgumentException)); } } // ViewAccessor starts at nonzero offset, with size shorter than MMF using (MemoryMappedViewAccessor view = mmf.CreateViewAccessor(2000, 10000)) { // position is last possible for type VerifyRead <Boolean>("Loc214a", view, view.Capacity - 1, true); VerifyRead <Byte>("Loc214b", view, view.Capacity - 1, Byte.MaxValue); VerifyRead <SByte>("Loc214c", view, view.Capacity - 1, -1); VerifyRead <Char>("Loc214d", view, view.Capacity - 2, Char.MaxValue); VerifyRead <Int16>("Loc214e", view, view.Capacity - 2, -1); VerifyRead <UInt16>("Loc214f", view, view.Capacity - 2, UInt16.MaxValue); VerifyRead <Single>("Loc214k", view, view.Capacity - 4, Single.NaN); VerifyRead <Int32>("Loc214g", view, view.Capacity - 4, -1); VerifyRead <UInt32>("Loc214h", view, view.Capacity - 4, UInt32.MaxValue); VerifyRead <Double>("Loc214l", view, view.Capacity - 8, Double.NaN); VerifyRead <Int64>("Loc214i", view, view.Capacity - 8, -1); VerifyRead <UInt64>("Loc214j", view, view.Capacity - 8, UInt64.MaxValue); WriteByteArray(view, view.Capacity - 16, byteArray2, 0, 16); VerifyRead <Decimal>("Loc214m", view, view.Capacity - 16, Decimal.MaxValue); // Exceptions foreach (Type type in testedTypes) { // position <0 VerifyReadException("Loc231", type, view, -1, typeof(ArgumentOutOfRangeException)); // position >= view.Capacity VerifyReadException("Loc232", type, view, view.Capacity, typeof(ArgumentOutOfRangeException)); VerifyReadException("Loc233", type, view, view.Capacity + 1, typeof(ArgumentOutOfRangeException)); } // position+sizeof(type) >= view.Capacity foreach (Type type in twoByteTypes) { VerifyReadException("Loc234", type, view, view.Capacity - 1, typeof(ArgumentException)); } foreach (Type type in fourByteTypes) { VerifyReadException("Loc235", type, view, view.Capacity - 3, typeof(ArgumentException)); } foreach (Type type in eightByteTypes) { VerifyReadException("Loc236", type, view, view.Capacity - 7, typeof(ArgumentException)); } foreach (Type type in sixteenByteTypes) { VerifyReadException("Loc237", type, view, view.Capacity - 15, typeof(ArgumentException)); } } // Accessor does not support reading using (MemoryMappedViewAccessor view = mmf.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Write)) { foreach (Type t in testedTypes) { VerifyReadException("Loc401_" + t.ToString(), t, view, 0, typeof(NotSupportedException)); } } // Call after view has been disposed MemoryMappedViewAccessor view1 = mmf.CreateViewAccessor(); view1.Dispose(); foreach (Type t in testedTypes) { VerifyReadException("Loc501_" + t.ToString(), t, view1, 0, typeof(ObjectDisposedException)); } } /// END TEST CASES if (iCountErrors == 0) { return(true); } else { Console.WriteLine("Fail! iCountErrors==" + iCountErrors); return(false); } } catch (Exception ex) { Console.WriteLine("ERR999: Unexpected exception in runTest, {0}", ex); return(false); } }
public bool runTest() { _random = new Random(-55); _pageSize = SystemInfoHelpers.GetPageSize(); Type[] testedTypes = new Type[] { typeof(Boolean), typeof(Char), typeof(Byte), typeof(Int16), typeof(Int32), typeof(Int64), typeof(SByte), typeof(UInt16), typeof(UInt32), typeof(UInt64), typeof(Single), typeof(Double), typeof(Decimal) }; Type[] oneByteTypes = new Type[] { typeof(Boolean), typeof(Byte), typeof(SByte) }; Type[] twoByteTypes = new Type[] { typeof(Char), typeof(Int16), typeof(UInt16) }; Type[] fourByteTypes = new Type[] { typeof(Single), typeof(Int32), typeof(UInt32) }; Type[] eightByteTypes = new Type[] { typeof(Double), typeof(Int64), typeof(UInt64) }; Type[] sixteenByteTypes = new Type[] { typeof(Decimal) }; try { using (FileStream fs = new FileStream(Path.GetTempFileName(), FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, 0x1000, FileOptions.DeleteOnClose)) using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fs, null, _pageSize * 10, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true)) { // Default ViewAccessor size - whole MMF using (MemoryMappedViewAccessor view = mmf.CreateViewAccessor()) { // position = 0 VerifyWrite <Boolean>("Loc011a", view, 0, true); VerifyWrite <Byte>("Loc011b", view, 0, 234); VerifyWrite <SByte>("Loc11c", view, 0, 127); VerifyWrite <Char>("Loc011d", view, 0, (Char)23974); VerifyWrite <Int16>("Loc011e", view, 0, 12434); VerifyWrite <UInt16>("Loc011f", view, 0, 24512); VerifyWrite <Int32>("Loc011g", view, 0, 928734); VerifyWrite <UInt32>("Loc011h", view, 0, 210991212); VerifyWrite <Int64>("Loc011i", view, 0, -1029830128231); VerifyWrite <UInt64>("Loc011j", view, 0, 10293891274); VerifyWrite <Single>("Loc011k", view, 0, -0.12243098f); VerifyWrite <Double>("Loc011l", view, 0, 213.1209382093d); VerifyWrite <Decimal>("Loc011m", view, 0, 110293.1123m); // position > 0 VerifyWrite <Boolean>("Loc012a1", view, RandPos(), true); VerifyWrite <Boolean>("Loc012a2", view, RandPos(), false); VerifyWrite <Byte>("Loc012b1", view, RandPos(), 194); VerifyWrite <Byte>("Loc012b2", view, RandPos(), Byte.MinValue); VerifyWrite <Byte>("Loc012b3", view, RandPos(), Byte.MaxValue); VerifyWrite <SByte>("Loc012c1", view, RandPos(), -62); VerifyWrite <SByte>("Loc012c2", view, RandPos(), SByte.MinValue); VerifyWrite <SByte>("Loc012c3", view, RandPos(), SByte.MaxValue); VerifyWrite <Char>("Loc012d1", view, RandPos(), (Char)10690); VerifyWrite <Char>("Loc012d2", view, RandPos(), Char.MinValue); VerifyWrite <Char>("Loc012d3", view, RandPos(), Char.MaxValue); VerifyWrite <Int16>("Loc012e1", view, RandPos(), -1077); VerifyWrite <Int16>("Loc012e2", view, RandPos(), Int16.MinValue); VerifyWrite <Int16>("Loc012e3", view, RandPos(), Int16.MaxValue); VerifyWrite <UInt16>("Loc012f1", view, RandPos(), 20798); VerifyWrite <UInt16>("Loc012f2", view, RandPos(), UInt16.MinValue); VerifyWrite <UInt16>("Loc012f3", view, RandPos(), UInt16.MaxValue); VerifyWrite <Int32>("Loc012g1", view, RandPos(), 1212938472); VerifyWrite <Int32>("Loc012g2", view, RandPos(), Int32.MinValue); VerifyWrite <Int32>("Loc012g3", view, RandPos(), Int32.MaxValue); VerifyWrite <UInt32>("Loc012h1", view, RandPos(), 127983047); VerifyWrite <UInt32>("Loc012h2", view, RandPos(), UInt32.MinValue); VerifyWrite <UInt32>("Loc012h3", view, RandPos(), UInt32.MaxValue); VerifyWrite <Int64>("Loc012i1", view, RandPos(), -12039842392110123); VerifyWrite <Int64>("Loc012i2", view, RandPos(), Int64.MinValue); VerifyWrite <Int64>("Loc012i3", view, RandPos(), Int64.MaxValue); VerifyWrite <UInt64>("Loc012j1", view, RandPos(), 938059802); VerifyWrite <UInt64>("Loc012j2", view, RandPos(), UInt64.MinValue); VerifyWrite <UInt64>("Loc012j3", view, RandPos(), UInt64.MaxValue); VerifyWrite <Single>("Loc012k1", view, RandPos(), 0f); VerifyWrite <Single>("Loc012k2", view, RandPos(), Single.MinValue); VerifyWrite <Single>("Loc012k3", view, RandPos(), Single.MaxValue); VerifyWrite <Single>("Loc012k4", view, RandPos(), Single.NegativeInfinity); VerifyWrite <Single>("Loc012k5", view, RandPos(), Single.PositiveInfinity); VerifyWrite <Single>("Loc012k6", view, RandPos(), Single.NaN); VerifyWrite <Double>("Loc012l1", view, RandPos(), 0d); VerifyWrite <Double>("Loc012l2", view, RandPos(), Double.MinValue); VerifyWrite <Double>("Loc012l3", view, RandPos(), Double.MaxValue); VerifyWrite <Double>("Loc012l4", view, RandPos(), Double.NegativeInfinity); VerifyWrite <Double>("Loc012l5", view, RandPos(), Double.PositiveInfinity); VerifyWrite <Double>("Loc012l6", view, RandPos(), Double.NaN); VerifyWrite <Decimal>("Loc012m1", view, RandPos(), -1230912.12312m); VerifyWrite <Decimal>("Loc012m2", view, RandPos(), Decimal.MinValue); VerifyWrite <Decimal>("Loc012m3", view, RandPos(), Decimal.MaxValue); // position is last possible for type VerifyWrite <Boolean>("Loc014a", view, view.Capacity - 1, true); VerifyWrite <Byte>("Loc014b", view, view.Capacity - 1, 255); VerifyWrite <SByte>("Loc014c", view, view.Capacity - 1, -1); VerifyWrite <Char>("Loc014d", view, view.Capacity - 2, (Char)65280); VerifyWrite <Int16>("Loc014e", view, view.Capacity - 2, -256); VerifyWrite <UInt16>("Loc014f", view, view.Capacity - 2, 65280); VerifyWrite <Single>("Loc014k", view, view.Capacity - 4, -0.000001f); VerifyWrite <Int32>("Loc014g", view, view.Capacity - 4, 123); VerifyWrite <UInt32>("Loc014h", view, view.Capacity - 4, 19238); VerifyWrite <Double>("Loc014l", view, view.Capacity - 8, -0.00000001d); VerifyWrite <Int64>("Loc014i", view, view.Capacity - 8, 1029380); VerifyWrite <UInt64>("Loc014j", view, view.Capacity - 8, 9235); VerifyWrite <Decimal>("Loc014m", view, view.Capacity - 16, -65280m); // Exceptions foreach (Type type in testedTypes) { // position <0 VerifyWriteException("Loc031", type, view, -1, typeof(ArgumentOutOfRangeException)); // position >= view.Capacity VerifyWriteException("Loc032", type, view, view.Capacity, typeof(ArgumentOutOfRangeException)); VerifyWriteException("Loc033", type, view, view.Capacity + 1, typeof(ArgumentOutOfRangeException)); } // position+sizeof(type) >= view.Capacity foreach (Type type in twoByteTypes) { VerifyWriteException("Loc034", type, view, view.Capacity - 1, typeof(ArgumentException)); } foreach (Type type in fourByteTypes) { VerifyWriteException("Loc035", type, view, view.Capacity - 3, typeof(ArgumentException)); } foreach (Type type in eightByteTypes) { VerifyWriteException("Loc036", type, view, view.Capacity - 7, typeof(ArgumentException)); } foreach (Type type in sixteenByteTypes) { VerifyWriteException("Loc037", type, view, view.Capacity - 15, typeof(ArgumentException)); } // ViewAccessor starts at nonzero offset, spans remainder of MMF using (MemoryMappedViewAccessor view2 = mmf.CreateViewAccessor(1000, 0)) { // position = 0 VerifyWrite <Boolean>("Loc111a", view2, 0, true); VerifyWrite <Byte>("Loc111b", view2, 0, 1); VerifyWrite <SByte>("Loc111c", view2, 0, 1); VerifyWrite <Char>("Loc111d", view2, 0, (Char)1); VerifyWrite <Int16>("Loc111e", view2, 0, 1); VerifyWrite <UInt16>("Loc111f", view2, 0, 1); VerifyWrite <Int32>("Loc111g", view2, 0, 1); VerifyWrite <UInt32>("Loc111h", view2, 0, 1); VerifyWrite <Int64>("Loc111i", view2, 0, 1); VerifyWrite <UInt64>("Loc111j", view2, 0, 1); VerifyWrite <Single>("Loc111k", view2, 0, Single.Epsilon); VerifyWrite <Double>("Loc111l", view2, 0, Double.Epsilon); VerifyWrite <Decimal>("Loc111m", view2, 0, 1.00001m); // position = 0 of original view should be left untouched VerifyRead <Decimal>("Loc111y", view, 0, 110293.1123m); // Original view can read new values at offset 1000 VerifyRead <Decimal>("Loc111z", view, 1000, 1.00001m); // Exceptions foreach (Type type in testedTypes) { // position <0 VerifyWriteException("Loc131", type, view, -1, typeof(ArgumentOutOfRangeException)); // position >= view.Capacity VerifyWriteException("Loc132", type, view, view.Capacity, typeof(ArgumentOutOfRangeException)); VerifyWriteException("Loc133", type, view, view.Capacity + 1, typeof(ArgumentOutOfRangeException)); } // position+sizeof(type) >= view.Capacity foreach (Type type in twoByteTypes) { VerifyWriteException("Loc134", type, view, view.Capacity - 1, typeof(ArgumentException)); } foreach (Type type in fourByteTypes) { VerifyWriteException("Loc135", type, view, view.Capacity - 3, typeof(ArgumentException)); } foreach (Type type in eightByteTypes) { VerifyWriteException("Loc136", type, view, view.Capacity - 7, typeof(ArgumentException)); } foreach (Type type in sixteenByteTypes) { VerifyWriteException("Loc137", type, view, view.Capacity - 15, typeof(ArgumentException)); } } } // ViewAccessor starts at nonzero offset, with size shorter than MMF using (MemoryMappedViewAccessor view = mmf.CreateViewAccessor(2000, 10000)) { // position is last possible for type VerifyWrite <Boolean>("Loc214a", view, view.Capacity - 1, true); VerifyWrite <Byte>("Loc214b", view, view.Capacity - 1, (byte)251); VerifyWrite <SByte>("Loc214c", view, view.Capacity - 1, -42); VerifyWrite <Char>("Loc214d", view, view.Capacity - 2, (Char)2132); VerifyWrite <Int16>("Loc214e", view, view.Capacity - 2, -9187); VerifyWrite <UInt16>("Loc214f", view, view.Capacity - 2, 42354); VerifyWrite <Single>("Loc214k", view, view.Capacity - 4, 3409.12f); VerifyWrite <Int32>("Loc214g", view, view.Capacity - 4, 792351320); VerifyWrite <UInt32>("Loc214h", view, view.Capacity - 4, 2098312120); VerifyWrite <Double>("Loc214l", view, view.Capacity - 8, -0.12398721342d); VerifyWrite <Int64>("Loc214i", view, view.Capacity - 8, (long)98176239824); VerifyWrite <UInt64>("Loc214j", view, view.Capacity - 8, (ulong)1029831212091029); VerifyWrite <Decimal>("Loc214m", view, view.Capacity - 16, 0.216082743618029m); // Exceptions foreach (Type type in testedTypes) { // position <0 VerifyWriteException("Loc231", type, view, -1, typeof(ArgumentOutOfRangeException)); // position >= view.Capacity VerifyWriteException("Loc232", type, view, view.Capacity, typeof(ArgumentOutOfRangeException)); VerifyWriteException("Loc233", type, view, view.Capacity + 1, typeof(ArgumentOutOfRangeException)); } // position+sizeof(type) >= view.Capacity foreach (Type type in twoByteTypes) { VerifyWriteException("Loc234", type, view, view.Capacity - 1, typeof(ArgumentException)); } foreach (Type type in fourByteTypes) { VerifyWriteException("Loc235", type, view, view.Capacity - 3, typeof(ArgumentException)); } foreach (Type type in eightByteTypes) { VerifyWriteException("Loc236", type, view, view.Capacity - 7, typeof(ArgumentException)); } foreach (Type type in sixteenByteTypes) { VerifyWriteException("Loc237", type, view, view.Capacity - 15, typeof(ArgumentException)); } } // Accessor does not support reading using (MemoryMappedViewAccessor view = mmf.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read)) { foreach (Type t in testedTypes) { VerifyWriteException("Loc401_" + t.ToString(), t, view, 0, typeof(NotSupportedException)); } } // Call after view has been disposed MemoryMappedViewAccessor view1 = mmf.CreateViewAccessor(); view1.Dispose(); foreach (Type t in testedTypes) { VerifyWriteException("Loc501_" + t.ToString(), t, view1, 0, typeof(ObjectDisposedException)); } } /// END TEST CASES if (iCountErrors == 0) { return(true); } else { Console.WriteLine("Fail! iCountErrors==" + iCountErrors); return(false); } } catch (Exception ex) { Console.WriteLine("ERR999: Unexpected exception in runTest, {0}", ex); return(false); } }
public bool runTest() { uint pageSize = SystemInfoHelpers.GetPageSize(); Byte[] byteArray1 = new Byte[] { 194, 58, 217, 150, 114, 62, 81, 203, 251, 39, 83, 124, 0, 0, 14, 128 }; Byte[] byteArray2 = new Byte[] { 0x35, 0x4C, 0x4A, 0x79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 1, 0x4A, 0xCE, 0x96, 0x0C, 0, 0, 0 }; Byte[] byteArray3 = new Byte[] { 0x35, 0x4C, 0x4A, 0x79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0 }; Byte[] byteArray3b = new Byte[] { 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; Byte[] byteArray4 = new Byte[] { 1 }; Byte[] byteArray5 = new Byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 1, 0xBB, 0x68, 0x01, 0x00, 0, 0, 0 }; Decimal dec = 20349.12309m; DateTime date = DateTime.Now; try { if (File.Exists("file1.dat")) { File.Delete("file1.dat"); } using (FileStream fileStream = new FileStream("file1.dat", FileMode.Create, FileAccess.ReadWrite)) { using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fileStream, null, pageSize * 10, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false)) { // Default ViewStream size - whole MMF using (MemoryMappedViewStream view = mmf.CreateViewStream()) { BinaryReader reader = new BinaryReader(view); BinaryWriter writer = new BinaryWriter(view); // nothing written - read zeros VerifyRead("Loc001", view, new Byte[24], 0, 24, new Byte[24], 24); VerifyReadBoolean("Loc002", reader, false); VerifyReadUInt16("Loc003", reader, 0); VerifyReadInt32("Loc004", reader, 0); VerifyReadDouble("Loc005", reader, 0d); VerifyReadDecimal("Loc006", reader, 0m); // Write to ViewStream view.Seek(0, SeekOrigin.Begin); view.Write(byteArray1, 0, 16); view.Seek(0, SeekOrigin.Begin); VerifyRead("Loc011", view, new Byte[16], 0, 16, byteArray1, 16); reader.BaseStream.Seek(0, SeekOrigin.Begin); VerifyReadBoolean("Loc012", reader, true); VerifyReadUInt16("Loc013", reader, 55610); VerifyReadInt32("Loc014", reader, 1363047062); VerifyReadDouble("Loc015", reader, BitConverter.ToDouble(byteArray1, 7)); reader.BaseStream.Seek(0, SeekOrigin.Begin); VerifyReadDecimal("Loc016", reader, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) })); // Write to BinaryWriter writer.BaseStream.Seek(2161, SeekOrigin.Begin); writer.Write(dec); writer.Write(true); writer.Write(211209802); view.Seek(2161, SeekOrigin.Begin); VerifyRead("Loc021", view, new Byte[24], 0, 24, byteArray2, 24); reader.BaseStream.Seek(2161, SeekOrigin.Begin); VerifyReadDecimal("Loc022", reader, dec); VerifyReadBoolean("Loc023", reader, true); VerifyReadInt32("Loc024", reader, 211209802); // Write to end of stream view.Seek(-16, SeekOrigin.End); view.Write(byteArray2, 0, 16); // now at end of stream VerifyRead("Loc031", view, new Byte[16], 0, 16, new Byte[16], 0); view.Seek(-16, SeekOrigin.End); VerifyRead("Loc032", view, new Byte[16], 0, 16, byteArray3, 16); view.Seek(-8, SeekOrigin.End); VerifyRead("Loc033", view, new Byte[16], 0, 16, byteArray3b, 8); // read partial array // BinaryReader reader.BaseStream.Seek(-16, SeekOrigin.End); VerifyReadDecimal("Loc034", reader, dec); // now at end of stream VerifyReadDecimalException <IOException>("Loc035", reader); reader.BaseStream.Seek(-8, SeekOrigin.End); VerifyRead("Loc036", reader, new Byte[16], 0, 16, byteArray3b, 8); // read partial array // Write to end of stream as calculated from viewstream capacity view.Seek(view.Capacity - 1, SeekOrigin.Begin); view.Write(byteArray2, 16, 1); // now at end of stream VerifyRead("Loc041", view, new Byte[16], 0, 16, new Byte[16], 0); view.Seek(view.Capacity - 1, SeekOrigin.Begin); VerifyRead("Loc042", view, new Byte[1], 0, 1, byteArray4, 1); reader.BaseStream.Seek(view.Capacity - 1, SeekOrigin.Begin); VerifyReadBoolean("Loc043", reader, true); // now at end of stream VerifyReadBooleanException <EndOfStreamException>("Loc044", reader); // Write past end of stream view.Seek(0, SeekOrigin.End); VerifyWriteException <NotSupportedException>("Loc051", view, new Byte[16], 0, 16); writer.Seek(0, SeekOrigin.End); VerifyWriteException <NotSupportedException>("Loc052", writer, Byte.MaxValue); // Seek past end view.Seek(1, SeekOrigin.End); VerifyRead("Loc061", view, new Byte[1], 0, 1, new Byte[1], 0); view.Seek((int)(view.Capacity + 1), SeekOrigin.Begin); VerifyRead("Loc062", view, new Byte[1], 0, 1, new Byte[1], 0); // Seek before beginning VerifySeekException <IOException>("Loc065", view, -1, SeekOrigin.Begin); VerifySeekException <IOException>("Loc066", view, (int)(-view.Capacity - 1), SeekOrigin.End); VerifySeekException <IOException>("Loc067", reader.BaseStream, -1, SeekOrigin.Begin); VerifySeekException <IOException>("Loc068", reader.BaseStream, (int)(-view.Capacity - 1), SeekOrigin.End); view.Flush(); // Verify file state BinaryReader reader2 = new BinaryReader(fileStream); reader2.BaseStream.Seek(0, SeekOrigin.Begin); VerifyReadBoolean("Loc071", reader2, true); VerifyReadUInt16("Loc072", reader2, 55610); VerifyReadInt32("Loc073", reader2, 1363047062); VerifyReadDouble("Loc074", reader2, BitConverter.ToDouble(byteArray1, 7)); reader2.BaseStream.Seek(0, SeekOrigin.Begin); VerifyReadDecimal("Loc075", reader2, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) })); reader2.BaseStream.Seek(2161, SeekOrigin.Begin); VerifyReadDecimal("Loc076", reader2, dec); VerifyReadBoolean("Loc077", reader2, true); VerifyReadInt32("Loc078", reader2, 211209802); reader2.BaseStream.Seek(-1, SeekOrigin.End); VerifyReadBoolean("Loc079", reader2, true); } // ViewStream starts at nonzero offset, spans remainder of MMF using (MemoryMappedViewStream view = mmf.CreateViewStream(1000, 0)) { BinaryReader reader = new BinaryReader(view); BinaryWriter writer = new BinaryWriter(view); // nothing written - read zeros VerifyRead("Loc101", view, new Byte[24], 0, 24, new Byte[24], 24); VerifyReadBoolean("Loc102", reader, false); VerifyReadUInt16("Loc103", reader, 0); VerifyReadInt32("Loc104", reader, 0); VerifyReadDouble("Loc105", reader, 0d); VerifyReadDecimal("Loc106", reader, 0m); // Write to ViewStream view.Seek(0, SeekOrigin.Begin); view.Write(byteArray1, 0, 16); view.Seek(0, SeekOrigin.Begin); VerifyRead("Loc111", view, new Byte[16], 0, 16, byteArray1, 16); reader.BaseStream.Seek(0, SeekOrigin.Begin); VerifyReadBoolean("Loc112", reader, true); VerifyReadUInt16("Loc113", reader, 55610); VerifyReadInt32("Loc114", reader, 1363047062); VerifyReadDouble("Loc115", reader, BitConverter.ToDouble(byteArray1, 7)); reader.BaseStream.Seek(0, SeekOrigin.Begin); VerifyReadDecimal("Loc116", reader, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) })); // Read existing values view.Seek(1161, SeekOrigin.Begin); VerifyRead("Loc117", view, new Byte[24], 0, 24, byteArray2, 24); reader.BaseStream.Seek(1161, SeekOrigin.Begin); VerifyReadDecimal("Loc118", reader, dec); VerifyReadBoolean("Loc119", reader, true); VerifyReadInt32("Loc120", reader, 211209802); // Write to BinaryWriter writer.BaseStream.Seek(3000, SeekOrigin.Begin); writer.Write(Decimal.MaxValue); writer.Write(true); writer.Write(92347); view.Seek(3000, SeekOrigin.Begin); VerifyRead("Loc121", view, new Byte[24], 0, 24, byteArray5, 24); reader.BaseStream.Seek(3000, SeekOrigin.Begin); VerifyReadDecimal("Loc122", reader, Decimal.MaxValue); VerifyReadBoolean("Loc123", reader, true); VerifyReadInt32("Loc124", reader, 92347); // Write to end of stream view.Seek(-16, SeekOrigin.End); view.Write(byteArray2, 0, 16); // now at end of stream VerifyRead("Loc131", view, new Byte[16], 0, 16, new Byte[16], 0); view.Seek(-16, SeekOrigin.End); VerifyRead("Loc132", view, new Byte[16], 0, 16, byteArray3, 16); view.Seek(-8, SeekOrigin.End); VerifyRead("Loc133", view, new Byte[16], 0, 16, byteArray3b, 8); // read partial array // BinaryReader reader.BaseStream.Seek(-16, SeekOrigin.End); VerifyReadDecimal("Loc134", reader, dec); // now at end of stream VerifyReadDecimalException <IOException>("Loc135", reader); reader.BaseStream.Seek(-8, SeekOrigin.End); VerifyRead("Loc136", reader, new Byte[16], 0, 16, byteArray3b, 8); // read partial array // Write to end of stream as calculated from viewstream capacity view.Seek(view.Capacity - 1, SeekOrigin.Begin); view.Write(byteArray2, 16, 1); // now at end of stream VerifyRead("Loc141", view, new Byte[16], 0, 16, new Byte[16], 0); view.Seek(view.Capacity - 1, SeekOrigin.Begin); VerifyRead("Loc142", view, new Byte[1], 0, 1, byteArray4, 1); reader.BaseStream.Seek(view.Capacity - 1, SeekOrigin.Begin); VerifyReadBoolean("Loc143", reader, true); // now at end of stream VerifyReadBooleanException <IOException>("Loc144", reader); // Write past end of stream view.Seek(-8, SeekOrigin.End); VerifyWriteException <NotSupportedException>("Loc151", view, new Byte[16], 0, 16); writer.Seek(-8, SeekOrigin.End); VerifyWriteException <NotSupportedException>("Loc152", writer, Decimal.MaxValue); // Seek past end view.Seek(1, SeekOrigin.End); VerifyRead("Loc161", view, new Byte[1], 0, 1, new Byte[1], 0); view.Seek((int)(view.Capacity + 1), SeekOrigin.Begin); VerifyRead("Loc162", view, new Byte[1], 0, 1, new Byte[1], 0); // Seek before beginning VerifySeekException <IOException>("Loc165", view, -1, SeekOrigin.Begin); VerifySeekException <IOException>("Loc166", view, (int)(-view.Capacity - 1), SeekOrigin.End); VerifySeekException <IOException>("Loc167", reader.BaseStream, -1, SeekOrigin.Begin); VerifySeekException <IOException>("Loc168", reader.BaseStream, (int)(-view.Capacity - 1), SeekOrigin.End); } // ViewStream starts at nonzero offset, with size shorter than MMF using (MemoryMappedViewStream view = mmf.CreateViewStream(2000, 10000)) { BinaryReader reader = new BinaryReader(view); BinaryWriter writer = new BinaryWriter(view); // nothing written - read zeros VerifyRead("Loc201", view, new Byte[24], 0, 24, new Byte[24], 24); VerifyReadBoolean("Loc202", reader, false); VerifyReadUInt16("Loc203", reader, 0); VerifyReadInt32("Loc204", reader, 0); VerifyReadDouble("Loc205", reader, 0d); VerifyReadDecimal("Loc206", reader, 0m); // Write to ViewStream view.Seek(0, SeekOrigin.Begin); view.Write(byteArray1, 0, 16); view.Seek(0, SeekOrigin.Begin); VerifyRead("Loc211", view, new Byte[16], 0, 16, byteArray1, 16); reader.BaseStream.Seek(0, SeekOrigin.Begin); VerifyReadBoolean("Loc212", reader, true); VerifyReadUInt16("Loc213", reader, 55610); VerifyReadInt32("Loc214", reader, 1363047062); VerifyReadDouble("Loc215", reader, BitConverter.ToDouble(byteArray1, 7)); reader.BaseStream.Seek(0, SeekOrigin.Begin); VerifyReadDecimal("Loc216", reader, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) })); // Read existing values view.Seek(161, SeekOrigin.Begin); VerifyRead("Loc217", view, new Byte[24], 0, 24, byteArray2, 24); reader.BaseStream.Seek(161, SeekOrigin.Begin); VerifyReadDecimal("Loc218", reader, dec); VerifyReadBoolean("Loc219", reader, true); VerifyReadInt32("Loc220", reader, 211209802); // Write to BinaryWriter writer.BaseStream.Seek(3000, SeekOrigin.Begin); writer.Write(Decimal.MaxValue); writer.Write(true); writer.Write(92347); view.Seek(3000, SeekOrigin.Begin); VerifyRead("Loc221", view, new Byte[24], 0, 24, byteArray5, 24); reader.BaseStream.Seek(3000, SeekOrigin.Begin); VerifyReadDecimal("Loc222", reader, Decimal.MaxValue); VerifyReadBoolean("Loc223", reader, true); VerifyReadInt32("Loc224", reader, 92347); // Write to end of stream view.Seek(-16, SeekOrigin.End); view.Write(byteArray2, 0, 16); // now at end of stream VerifyRead("Loc231", view, new Byte[16], 0, 16, new Byte[16], 0); view.Seek(-16, SeekOrigin.End); VerifyRead("Loc232", view, new Byte[16], 0, 16, byteArray3, 16); view.Seek(-8, SeekOrigin.End); VerifyRead("Loc233", view, new Byte[16], 0, 16, byteArray3b, 8); // read partial array // BinaryReader reader.BaseStream.Seek(-16, SeekOrigin.End); VerifyReadDecimal("Loc234", reader, dec); // now at end of stream VerifyReadDecimalException <IOException>("Loc235", reader); reader.BaseStream.Seek(-8, SeekOrigin.End); VerifyRead("Loc236", reader, new Byte[16], 0, 16, byteArray3b, 8); // read partial array // Write to end of stream as calculated from viewstream capacity view.Seek(view.Capacity - 1, SeekOrigin.Begin); view.Write(byteArray2, 16, 1); // now at end of stream VerifyRead("Loc241", view, new Byte[16], 0, 16, new Byte[16], 0); view.Seek(view.Capacity - 1, SeekOrigin.Begin); VerifyRead("Loc242", view, new Byte[1], 0, 1, byteArray4, 1); reader.BaseStream.Seek(view.Capacity - 1, SeekOrigin.Begin); VerifyReadBoolean("Loc243", reader, true); // now at end of stream VerifyReadBooleanException <IOException>("Loc244", reader); // Write past end of stream view.Seek(-1, SeekOrigin.End); VerifyWriteException <NotSupportedException>("Loc251", view, new Byte[16], 0, 2); writer.Seek(-1, SeekOrigin.End); VerifyWriteException <NotSupportedException>("Loc252", writer, Char.MaxValue); // Seek past end view.Seek(1, SeekOrigin.End); VerifyRead("Loc261", view, new Byte[1], 0, 1, new Byte[1], 0); view.Seek((int)(view.Capacity + 1), SeekOrigin.Begin); VerifyRead("Loc262", view, new Byte[1], 0, 1, new Byte[1], 0); // Seek before beginning VerifySeekException <IOException>("Loc265", view, -1, SeekOrigin.Begin); VerifySeekException <IOException>("Loc266", view, (int)(-view.Capacity - 1), SeekOrigin.End); VerifySeekException <IOException>("Loc267", reader.BaseStream, -1, SeekOrigin.Begin); VerifySeekException <IOException>("Loc268", reader.BaseStream, (int)(-view.Capacity - 1), SeekOrigin.End); } } } if (Interop.PlatformDetection.OperatingSystem == Interop.OperatingSystem.Windows) // currently don't support sharing views of pagefile backed MMFs on Unix { // Use a pagefile backed MMF instead of file backed using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, pageSize * 10)) { using (MemoryMappedViewStream view = mmf.CreateViewStream()) { BinaryReader reader = new BinaryReader(view); BinaryWriter writer = new BinaryWriter(view); // nothing written - read zeros VerifyRead("Loc401", view, new Byte[24], 0, 24, new Byte[24], 24); VerifyReadBoolean("Loc402", reader, false); VerifyReadUInt16("Loc403", reader, 0); VerifyReadInt32("Loc404", reader, 0); VerifyReadDouble("Loc405", reader, 0d); VerifyReadDecimal("Loc406", reader, 0m); // Write to ViewStream view.Seek(0, SeekOrigin.Begin); view.Write(byteArray1, 0, 16); view.Seek(0, SeekOrigin.Begin); VerifyRead("Loc411", view, new Byte[16], 0, 16, byteArray1, 16); reader.BaseStream.Seek(0, SeekOrigin.Begin); VerifyReadBoolean("Loc412", reader, true); VerifyReadUInt16("Loc413", reader, 55610); VerifyReadInt32("Loc414", reader, 1363047062); VerifyReadDouble("Loc415", reader, BitConverter.ToDouble(byteArray1, 7)); reader.BaseStream.Seek(0, SeekOrigin.Begin); VerifyReadDecimal("Loc416", reader, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) })); // Write to BinaryWriter writer.BaseStream.Seek(2161, SeekOrigin.Begin); writer.Write(dec); writer.Write(true); writer.Write(211209802); view.Seek(2161, SeekOrigin.Begin); VerifyRead("Loc421", view, new Byte[24], 0, 24, byteArray2, 24); reader.BaseStream.Seek(2161, SeekOrigin.Begin); VerifyReadDecimal("Loc422", reader, dec); VerifyReadBoolean("Loc423", reader, true); VerifyReadInt32("Loc424", reader, 211209802); // Write to end of stream view.Seek(-16, SeekOrigin.End); view.Write(byteArray2, 0, 16); // now at end of stream VerifyRead("Loc431", view, new Byte[16], 0, 16, new Byte[16], 0); view.Seek(-16, SeekOrigin.End); VerifyRead("Loc432", view, new Byte[16], 0, 16, byteArray3, 16); view.Seek(-8, SeekOrigin.End); VerifyRead("Loc433", view, new Byte[16], 0, 16, byteArray3b, 8); // read partial array // BinaryReader reader.BaseStream.Seek(-16, SeekOrigin.End); VerifyReadDecimal("Loc434", reader, dec); // now at end of stream VerifyReadDecimalException <IOException>("Loc435", reader); reader.BaseStream.Seek(-8, SeekOrigin.End); VerifyRead("Loc436", reader, new Byte[16], 0, 16, byteArray3b, 8); // read partial array // Write to end of stream as calculated from viewstream capacity view.Seek(view.Capacity - 1, SeekOrigin.Begin); view.Write(byteArray2, 16, 1); // now at end of stream VerifyRead("Loc441", view, new Byte[16], 0, 16, new Byte[16], 0); view.Seek(view.Capacity - 1, SeekOrigin.Begin); VerifyRead("Loc442", view, new Byte[1], 0, 1, byteArray4, 1); reader.BaseStream.Seek(view.Capacity - 1, SeekOrigin.Begin); VerifyReadBoolean("Loc443", reader, true); // now at end of stream VerifyReadBooleanException <IOException>("Loc444", reader); // Write past end of stream view.Seek(0, SeekOrigin.End); VerifyWriteException <NotSupportedException>("Loc451", view, new Byte[16], 0, 16); writer.Seek(0, SeekOrigin.End); VerifyWriteException <NotSupportedException>("Loc452", writer, Byte.MaxValue); // Seek past end view.Seek(1, SeekOrigin.End); VerifyRead("Loc461", view, new Byte[1], 0, 1, new Byte[1], 0); view.Seek((int)(view.Capacity + 1), SeekOrigin.Begin); VerifyRead("Loc462", view, new Byte[1], 0, 1, new Byte[1], 0); // Seek before beginning VerifySeekException <IOException>("Loc465", view, -1, SeekOrigin.Begin); VerifySeekException <IOException>("Loc466", view, (int)(-view.Capacity - 1), SeekOrigin.End); VerifySeekException <IOException>("Loc467", reader.BaseStream, -1, SeekOrigin.Begin); VerifySeekException <IOException>("Loc468", reader.BaseStream, (int)(-view.Capacity - 1), SeekOrigin.End); } using (MemoryMappedViewStream view = mmf.CreateViewStream(0, 10000, MemoryMappedFileAccess.CopyOnWrite)) { BinaryReader reader = new BinaryReader(view); BinaryWriter writer = new BinaryWriter(view); // Read existing values view.Seek(2161, SeekOrigin.Begin); VerifyRead("Loc501", view, new Byte[24], 0, 24, byteArray2, 24); reader.BaseStream.Seek(2161, SeekOrigin.Begin); VerifyReadDecimal("Loc502", reader, dec); VerifyReadBoolean("Loc503", reader, true); VerifyReadInt32("Loc504", reader, 211209802); // Write to ViewStream view.Seek(4000, SeekOrigin.Begin); view.Write(byteArray1, 0, 16); view.Seek(4000, SeekOrigin.Begin); VerifyRead("Loc511", view, new Byte[16], 0, 16, byteArray1, 16); reader.BaseStream.Seek(4000, SeekOrigin.Begin); VerifyReadBoolean("Loc512", reader, true); VerifyReadUInt16("Loc513", reader, 55610); VerifyReadInt32("Loc514", reader, 1363047062); VerifyReadDouble("Loc515", reader, BitConverter.ToDouble(byteArray1, 7)); reader.BaseStream.Seek(4000, SeekOrigin.Begin); VerifyReadDecimal("Loc516", reader, new Decimal(new Int32[] { BitConverter.ToInt32(byteArray1, 0), BitConverter.ToInt32(byteArray1, 4), BitConverter.ToInt32(byteArray1, 8), BitConverter.ToInt32(byteArray1, 12) })); } using (MemoryMappedViewStream view = mmf.CreateViewStream(0, 10000, MemoryMappedFileAccess.Read)) { BinaryReader reader = new BinaryReader(view); // Read existing values view.Seek(2161, SeekOrigin.Begin); VerifyRead("Loc601", view, new Byte[24], 0, 24, byteArray2, 24); reader.BaseStream.Seek(2161, SeekOrigin.Begin); VerifyReadDecimal("Loc602", reader, dec); VerifyReadBoolean("Loc603", reader, true); VerifyReadInt32("Loc604", reader, 211209802); // Values from CopyOnWrite ViewStream were not preserved view.Seek(4000, SeekOrigin.Begin); VerifyRead("Loc611", view, new Byte[16], 0, 16, new Byte[16], 16); reader.BaseStream.Seek(4000, SeekOrigin.Begin); VerifyReadBoolean("Loc612", reader, false); VerifyReadUInt16("Loc613", reader, 0); VerifyReadInt32("Loc614", reader, 0); VerifyReadDouble("Loc615", reader, 0d); reader.BaseStream.Seek(4000, SeekOrigin.Begin); VerifyReadDecimal("Loc616", reader, 0m); } } } /// END TEST CASES if (iCountErrors == 0) { return(true); } else { Console.WriteLine("Fail! iCountErrors==" + iCountErrors); return(false); } } catch (Exception ex) { Console.WriteLine("ERR999: Unexpected exception in runTest, {0}", ex); return(false); } }
public static void Check(VulnerabilityCollection vulnerabilities, SystemInfo sysInfo) { try { int version = SystemInfoHelpers.GetFileVersionInfoProductPrivatePart(sysInfo.WinPath + "\\win32k.sys"); switch (sysInfo.OsBuild) { case "6001": if (version < 18523) { vulnerabilities.SetAsVulnerable("MS10-073"); } break; case "6002": if (version < 18305) { vulnerabilities.SetAsVulnerable("MS10-073"); } if (version < 18739) { vulnerabilities.SetAsVulnerable("MS13-005"); } if (version < 18974) { vulnerabilities.SetAsVulnerable("MS13-101"); } if (version < 19372) { vulnerabilities.SetAsVulnerable("MS15-051"); } if (version < 19597) { vulnerabilities.SetAsVulnerable("MS16-034"); } break; case "7600": if (version < 16667) { vulnerabilities.SetAsVulnerable("MS10-073"); } if (version < 17017) { vulnerabilities.SetAsVulnerable("MS12-042"); } if (version < 17175) { vulnerabilities.SetAsVulnerable("MS13-005"); } break; case "9200": if (version < 16468) { vulnerabilities.SetAsVulnerable("MS13-005"); } if (version < 16758) { vulnerabilities.SetAsVulnerable("MS13-101"); } if (version < 17130) { vulnerabilities.SetAsVulnerable("MS14-058"); } if (version < 17343) { vulnerabilities.SetAsVulnerable("MS15-051"); } break; case "9600": if (version < 16457) { vulnerabilities.SetAsVulnerable("MS13-101"); } if (version < 17796) { vulnerabilities.SetAsVulnerable("MS15-051"); } if (version < 17353) { vulnerabilities.SetAsVulnerable("MS14-058"); } if (version < 18228) { vulnerabilities.SetAsVulnerable("MS16-034"); } break; } } catch (Exception ex) { vulnerabilities.AddError(ex.Message); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider svp, ILoggerFactory logFactory) { _httpContextAccessor = svp.GetRequiredService <IHttpContextAccessor>(); ContentRootPath = env.ContentRootPath; app.UseForwardedHeaders(new ForwardedHeadersOptions() { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); PEngine.Core.Shared.Settings.Startup(env.ContentRootPath); // Add Handling for Status Codes and Exceptions app.UseStatusCodePagesWithReExecute("/error/{0}"); app.UseExceptionHandler("/error/500"); // Add Support for JWTs passed in cookies app.UseMiddleware <TokenCookieMiddleware>(); app.UseAuthentication(); // Add Response Compression if (!Settings.Current.DisableResponseCompression) { app.UseResponseCompression(); } // Add JWT generation var secretKey = PEngine.Core.Shared.Settings.Current.SecretKey.ToString(); var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)); var providerOptions = new TokenProviderOptions { Audience = PEngine.Core.Shared.Settings.Current.DefaultTitle, Issuer = PEngine.Core.Shared.Settings.Current.DefaultTitle, SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256), }; app.UseMiddleware <TokenProviderMiddleware>(Options.Create(providerOptions)); app.Use(async(context, next) => { if (!context.Response.Headers.ContainsKey("X-Frame-Options")) { context.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN"); } //Disable Google FLoC tracking if (!context.Response.Headers.ContainsKey("Permissions-Policy")) { context.Response.Headers.Add("Permissions-Policy", "interest-cohort=()"); } await next(); }); if (!Settings.Current.DisableSwagger) { // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(); // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), // specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Presentation Engine API V1"); }); } app.UseMvc(m => { m.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); app.UseStaticFiles(new StaticFileOptions() { DefaultContentType = "application/octet-stream", ServeUnknownFileTypes = true, OnPrepareResponse = ctx => { var contentCacheDuration = 86400; if (PEngine.Core.Shared.Settings.Current.CacheControlSeconds > 0) { contentCacheDuration = PEngine.Core.Shared.Settings.Current.CacheControlSeconds; } ctx.Context.Response.Headers[HeaderNames.CacheControl] = $"public,max-age={contentCacheDuration}"; } }); SystemInfoHelpers.Init(env.ContentRootPath); Security.XSRF.Startup(logFactory); PEngine.Core.Data.Database.Startup(env.ContentRootPath, new SQLiteDataProvider()).Wait(); PEngine.Core.Logic.FeedManager.Startup(env.ContentRootPath, svp.GetRequiredService <IPostService>()).Wait(); }
public static void Check(VulnerabilityCollection vulnerabilities, SystemInfo sysInfo) { try { int version = SystemInfoHelpers.GetFileVersionInfoProductPrivatePart(sysInfo.WinPath + "\\ntoskrnl.exe"); switch (sysInfo.OsBuild) { case "6000": if (version < 16973) { vulnerabilities.SetAsVulnerable("MS10-015"); } break; case "6001": if (version < 18377) { vulnerabilities.SetAsVulnerable("MS10-015"); } break; case "6002": if (version < 18160) { vulnerabilities.SetAsVulnerable("MS10-015"); } break; case "10240": if (version < 17113) { vulnerabilities.SetAsVulnerable("MS16-111"); } if (version < 17184) { vulnerabilities.SetAsVulnerable("MS16-135"); vulnerabilities.SetAsVulnerable("MS16-138"); } if (version < 17946) { vulnerabilities.SetAsVulnerable("CVE-2018-0952"); } if (version < 17976) { vulnerabilities.SetAsVulnerable("CVE-2018-8440"); } break; case "10586": if (version < 589) { vulnerabilities.SetAsVulnerable("MS16-111"); } if (version < 672) { vulnerabilities.SetAsVulnerable("MS16-135"); vulnerabilities.SetAsVulnerable("MS16-138"); } break; case "14393": if (version < 953) { vulnerabilities.SetAsVulnerable("MS17-012"); vulnerabilities.SetAsVulnerable("MS17-017"); } if (version < 1198) { vulnerabilities.SetAsVulnerable("CVE-2017-0263"); } if (version < 2248) { vulnerabilities.SetAsVulnerable("CVE-2018-8897"); } if (version < 2430) { vulnerabilities.SetAsVulnerable("CVE-2018-0952"); } if (version < 2485) { vulnerabilities.SetAsVulnerable("CVE-2018-8440"); } break; case "15063": if (version < 296) { vulnerabilities.SetAsVulnerable("CVE-2017-0263"); } if (version < 483) { vulnerabilities.SetAsVulnerable("MS16-111"); } if (version < 608) { vulnerabilities.SetAsVulnerable("MS16-039"); vulnerabilities.SetAsVulnerable("MS16-123"); } if (version < 1266) { vulnerabilities.SetAsVulnerable("CVE-2018-0952"); } if (version < 1324) { vulnerabilities.SetAsVulnerable("CVE-2018-8440"); } break; case "16299": if (version < 611) { vulnerabilities.SetAsVulnerable("CVE-2018-0952"); } if (version < 665) { vulnerabilities.SetAsVulnerable("CVE-2018-8440"); } break; case "17134": if (version < 48) { vulnerabilities.SetAsVulnerable("CVE-2018-8897"); } if (version < 228) { vulnerabilities.SetAsVulnerable("CVE-2018-0952"); } if (version < 285) { vulnerabilities.SetAsVulnerable("CVE-2018-8440"); } break; } } catch (Exception ex) { vulnerabilities.AddError(ex.Message); } }