Esempio n. 1
0
        public void ToNullableStructureTest()
        {
            Assert.That(IntPtr.Zero.ToNullableStructure <RECT>(), Is.Null);
            var h = SafeHGlobalHandle.CreateFromStructure(new RECT(10, 11, 12, 13));

            Assert.That(((IntPtr)h).ToNullableStructure <RECT>(), Is.Not.Null);
        }
Esempio n. 2
0
        public void EventTest()
        {
            var activId = Guid.Empty;

            Assert.That(EventActivityIdControl(EVENT_ACTIVITY_CTRL.EVENT_ACTIVITY_CTRL_CREATE_ID, ref activId), ResultIs.Successful);

            var provGuid = Guid.NewGuid();

            Assert.That(EventRegister(provGuid, Callback, IntPtr.Zero, out var hReg), ResultIs.Successful);
            Assert.That(hReg.IsInvalid, Is.False);

            using (hReg)
            {
                var desc = new EVENT_DESCRIPTOR(0, 0, 0, 0, 0, 0, ulong.MaxValue);
                Assert.That(EventEnabled(hReg, desc), Is.False);

                Assert.That(EventProviderEnabled(hReg, 0, ulong.MaxValue), Is.False);

                using (var mem = SafeHGlobalHandle.CreateFromStructure((byte)1))
                    Assert.That(EventSetInformation(hReg, EVENT_INFO_CLASS.EventProviderUseDescriptorType, mem, 1U), ResultIs.FailureCode(Win32Error.ERROR_NOT_SUPPORTED));

                Assert.That(EventWrite(hReg, desc, 0, null), ResultIs.FailureCode(Win32Error.ERROR_INVALID_PARAMETER));
                Assert.That(EventWriteEx(hReg, desc, 0, 0, IntPtr.Zero, IntPtr.Zero, 0, null), ResultIs.FailureCode(Win32Error.ERROR_INVALID_PARAMETER));
                Assert.That(EventWriteString(hReg, 0, ulong.MaxValue, "Dummy"), ResultIs.FailureCode(Win32Error.ERROR_INVALID_HANDLE));
                Assert.That(EventWriteTransfer(hReg, desc, IntPtr.Zero, IntPtr.Zero, 0, null), ResultIs.FailureCode(Win32Error.ERROR_INVALID_PARAMETER));
            }

            void Callback(in Guid SourceId, bool IsEnabled, byte Level, ulong MatchAnyKeyword, ulong MatchAllKeyword, IntPtr FilterData, IntPtr CallbackContext)
            {
            }
        }
Esempio n. 3
0
        public void SystemParametersInfoSetTest()
        {
            // Try set bool value
            Assert.That(SystemParametersInfo(SPI.SPI_SETCLIENTAREAANIMATION, 0, IntPtr.Zero, SPIF.SPIF_SENDCHANGE));

            // Try set generic bool value
            Assert.That(SystemParametersInfo(SPI.SPI_SETCLIENTAREAANIMATION, true));

            // Try set integral value
            Assert.That(SystemParametersInfo(SPI.SPI_SETFOCUSBORDERHEIGHT, 0, (IntPtr)3, SPIF.SPIF_SENDCHANGE));

            // Try set generic integral value
            Assert.That(SystemParametersInfo(SPI.SPI_SETFOCUSBORDERHEIGHT, 1u));

            // Try set struct value
            Assert.That(SystemParametersInfo(SPI.SPI_GETWORKAREA, out RECT area));
            area.right /= 2;
            var ptr = SafeHGlobalHandle.CreateFromStructure(area);

            Assert.That(SystemParametersInfo(SPI.SPI_SETWORKAREA, (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(RECT)), (IntPtr)ptr, SPIF.SPIF_SENDCHANGE));

            // Try set generic struct value
            area.right *= 2;
            Assert.That(SystemParametersInfo(SPI.SPI_SETWORKAREA, area));

            // Try set string value
            var sb = new System.Text.StringBuilder(Kernel32.MAX_PATH, Kernel32.MAX_PATH);

            Assert.That(SystemParametersInfo(SPI.SPI_GETDESKWALLPAPER, (uint)sb.Capacity, sb, 0));
            var wp = @"C:\Temp\tsnew256.png";

            Assert.That(SystemParametersInfo(SPI.SPI_SETDESKWALLPAPER, (uint)wp.Length, wp, SPIF.SPIF_SENDCHANGE));
            Assert.That(SystemParametersInfo(SPI.SPI_SETDESKWALLPAPER, (uint)sb.Length, sb.ToString(), SPIF.SPIF_SENDCHANGE));
        }
Esempio n. 4
0
            public static IEnumerable <EVENT_TRACE_PROPERTIES> QueryAllTraces()
            {
                const int len      = 64;
                var       props    = new List <SafeHGlobalHandle>(len);
                var       loadProp = EVENT_TRACE_PROPERTIES.Create();

                for (var i = 0; i < len; i++)
                {
                    props.Add(SafeHGlobalHandle.CreateFromStructure(loadProp));
                }
                try
                {
                    var pprops = props.Select(p => (IntPtr)p).ToArray();
                    AdvApi32.QueryAllTraces(pprops, len, out var count).ThrowIfFailed();
                    for (var i = 0; i < count; i++)
                    {
                        yield return(props[i].ToStructure <EVENT_TRACE_PROPERTIES>());
                    }
                }
                finally
                {
                    for (var i = 0; i < props.Count; i++)
                    {
                        props[i].Dispose();
                    }
                }
            }
Esempio n. 5
0
        public void ToStructureTest1()
        {
            using var h = SafeHGlobalHandle.CreateFromStructure(new PRECT(10, 11, 12, 13));
            var r = new PRECT(0, 0, 0, 0);

            Assert.That(() => ((IntPtr)h).ToStructure(r), Throws.Nothing);
            Assert.That(r.left, Is.EqualTo(10));
        }
Esempio n. 6
0
        public void ToStructureTest()
        {
            var h = SafeHGlobalHandle.CreateFromStructure(new RECT(10, 11, 12, 13));

            Assert.That(((IntPtr)h).ToStructure <RECT>().left == 10);
            h.Dispose();
            Assert.That(() => ((IntPtr)h).ToStructure <RECT>(), Throws.TypeOf <NullReferenceException>());
        }
Esempio n. 7
0
 public void GetEventLogInformationTest()
 {
     using (var hEL = OpenEventLog(null, eventLogName))
         using (var mem = SafeHGlobalHandle.CreateFromStructure <EVENTLOG_FULL_INFORMATION>())
         {
             Assert.That(GetEventLogInformation(hEL, 0, mem, mem.Size, out var req), ResultIs.Successful);
             mem.ToStructure <EVENTLOG_FULL_INFORMATION>().WriteValues();
         }
 }
Esempio n. 8
0
        public void CreateFromStructureTest()
        {
            var r = new RECT(5, 5, 5, 5);
            var h = SafeHGlobalHandle.CreateFromStructure(r);

            Assert.That(!h.IsClosed && !h.IsInvalid);
            Assert.That(h.Size, Is.EqualTo(Marshal.SizeOf <RECT>()));
            Assert.That(h.ToStructure <RECT>().X, Is.EqualTo(5));
        }
Esempio n. 9
0
 public void RECTTest()
 {
     using (var h = SafeHGlobalHandle.CreateFromStructure(new RECT(1, 2, 3, 4)))
     {
         var r = h.ToStructure <RECT>();
         Assert.That(r.left == 1);
         Assert.That(r.top == 2);
         Assert.That(r.right == 3);
         Assert.That(r.bottom == 4);
     }
 }
Esempio n. 10
0
 public void RegisterEventSourceTest()
 {
     using (var hES = RegisterEventSource(null, "TestSource"))
     {
         Assert.That(hES, ResultIs.ValidHandle);
         using (var mem = SafeHGlobalHandle.CreateFromStructure <EVENTLOG_FULL_INFORMATION>())
         {
             Assert.That(GetEventLogInformation(hES, 0, mem, mem.Size, out var req), ResultIs.Successful);
             mem.ToStructure <EVENTLOG_FULL_INFORMATION>().WriteValues();
         }
     }
 }
Esempio n. 11
0
        //********************************************************************************************
        // Function: GetPreferredAddress
        //
        // Description: This function sorts a list of Ipv4 & Ipv6 addresses, and returns the "best" address that stack determines
        //
        //********************************************************************************************
        static Win32Error GetPreferredAddress(SOCKADDR_IN6[] pAddrList, out SOCKADDR_STORAGE pPreferredAddr)
        {
            Win32Error dwErr = Win32Error.ERROR_SUCCESS;

            pPreferredAddr = default;

            // Initialize WinSock
            using var wsa = SafeWSA.Initialize();

            // create socket
            var socketIoctl = WSASocket(ADDRESS_FAMILY.AF_INET6, SOCK.SOCK_DGRAM, 0, IntPtr.Zero, 0, WSA_FLAG.WSA_FLAG_OVERLAPPED);

            if (socketIoctl == SOCKET.INVALID_SOCKET)
            {
                dwErr = WSAGetLastError();
                Console.Write("WSASocket failed, (dwErr = {0}).", dwErr);
                return(dwErr);
            }

            var numElement      = pAddrList.Length;
            var pSockAddrList   = Array.ConvertAll(pAddrList, v6 => new SOCKADDR(v6));
            var pSocketAddrList = new SOCKET_ADDRESS_LIST {
                iAddressCount = numElement, Address = Array.ConvertAll(pSockAddrList, sa => new SOCKET_ADDRESS {
                    iSockaddrLength = sa.Size, lpSockaddr = sa.DangerousGetHandle()
                })
            };

            // sort addresses
            using var ppSocketAddrList = SafeHGlobalHandle.CreateFromStructure(pSocketAddrList);
            uint dwSize = 4U + (uint)(Marshal.SizeOf <SOCKET_ADDRESS>() * pSocketAddrList.iAddressCount);

            dwErr           = WSAIoctl(socketIoctl, WinSockIOControlCode.SIO_ADDRESS_LIST_SORT, ppSocketAddrList, dwSize, ppSocketAddrList, dwSize, out var dwBytes);
            pSocketAddrList = ppSocketAddrList.ToStructure <SOCKET_ADDRESS_LIST>();

            if (dwErr == SOCKET_ERROR)
            {
                dwErr = WSAGetLastError();
                Console.Write("WSAIoctl sort address failed, (dwErr = {0}).", dwErr);
                return(dwErr);
            }

            var pBestAddress = new IPAddress(pSocketAddrList.Address[0].lpSockaddr.ToArray <byte>(pSocketAddrList.Address[0].iSockaddrLength));

            pPreferredAddr = (SOCKADDR_STORAGE)(pBestAddress.IsIPv4MappedToIPv6 ? pBestAddress.MapToIPv4() : pBestAddress);

            if (socketIoctl != SOCKET.INVALID_SOCKET)
            {
                closesocket(socketIoctl);
                socketIoctl = SOCKET.INVALID_SOCKET;
            }

            return(dwErr);
        }
Esempio n. 12
0
 public void PRECTTest()
 {
     Assert.That(Marshal.SizeOf(typeof(RECT)) == Marshal.SizeOf(typeof(PRECT)));
     using (var h = SafeHGlobalHandle.CreateFromStructure(new PRECT(1, 2, 3, 4)))
     {
         var r = h.ToStructure <RECT>();
         Assert.That(r.left == 1);
         Assert.That(r.top == 2);
         Assert.That(r.right == 3);
         Assert.That(r.bottom == 4);
     }
 }
Esempio n. 13
0
 public void SIZETest()
 {
     using (var h = SafeHGlobalHandle.CreateFromStructure(new SIZE(1, 2)))
     {
         var sz = h.ToStructure <SIZE>();
         Assert.That(sz.cx == 1);
         Assert.That(sz.cy == 2);
         Size ms = sz;
         Assert.That(ms, Is.EqualTo(new Size(1, 2)));
         SIZE sz2 = ms;
         Assert.That(sz2, Is.EqualTo(sz));
     }
 }
Esempio n. 14
0
        public void OpenProcessCloseTraceTest()
        {
            var callbackCount = 0;

            Assert.That(() =>
            {
                var logFile           = new EventLogFile(logfilePath);
                logFile.EventCallback = EvtCallback;
                using (var log = new EventTraceSingleLog(logFile))
                {
                    using (var mem = SafeHGlobalHandle.CreateFromStructure <ETW_TRACE_PARTITION_INFORMATION>())
                    {
                        Assert.That(QueryTraceProcessingHandle(log.Handle, ETW_PROCESS_HANDLE_INFO_TYPE.EtwQueryPartitionInformation, default, 0, mem, mem.Size, out var retLen), ResultIs.Successful);
Esempio n. 15
0
        public void LsaCallAuthenticationPackageTest()
        {
            Assert.That(LsaLookupAuthenticationPackage(hLsaConn, MICROSOFT_KERBEROS_NAME, out var pkg), Is.EqualTo((NTStatus)0));

            var krr = new KERB_RETRIEVE_TKT_REQUEST {
                MessageType = KERB_PROTOCOL_MESSAGE_TYPE.KerbRetrieveTicketMessage
            };
            var mem = SafeHGlobalHandle.CreateFromStructure(krr);

            Assert.That(LsaCallAuthenticationPackage(hLsaConn, pkg, (IntPtr)mem, (uint)mem.Size, out var buf, out var len, out var status), Is.EqualTo((NTStatus)0));
            Assert.That(status, Is.EqualTo((NTStatus)0));
            Assert.That(len, Is.GreaterThan(0));
            var respTick = buf.ToStructure <KERB_RETRIEVE_TKT_RESPONSE>().Ticket;
        }
Esempio n. 16
0
        private static SafeAllocatedMemoryHandle GetRwForType(TCP_ESTATS_TYPE type, bool enable)
        {
            switch (type)
            {
            case TCP_ESTATS_TYPE.TcpConnectionEstatsData:
                return(SafeHGlobalHandle.CreateFromStructure(new TCP_ESTATS_DATA_RW_v0 {
                    EnableCollection = enable
                }));

            case TCP_ESTATS_TYPE.TcpConnectionEstatsSndCong:
                return(SafeHGlobalHandle.CreateFromStructure(new TCP_ESTATS_SND_CONG_RW_v0 {
                    EnableCollection = enable
                }));

            case TCP_ESTATS_TYPE.TcpConnectionEstatsPath:
                return(SafeHGlobalHandle.CreateFromStructure(new TCP_ESTATS_PATH_RW_v0 {
                    EnableCollection = enable
                }));

            case TCP_ESTATS_TYPE.TcpConnectionEstatsSendBuff:
                return(SafeHGlobalHandle.CreateFromStructure(new TCP_ESTATS_SEND_BUFF_RW_v0 {
                    EnableCollection = enable
                }));

            case TCP_ESTATS_TYPE.TcpConnectionEstatsRec:
                return(SafeHGlobalHandle.CreateFromStructure(new TCP_ESTATS_REC_RW_v0 {
                    EnableCollection = enable
                }));

            case TCP_ESTATS_TYPE.TcpConnectionEstatsObsRec:
                return(SafeHGlobalHandle.CreateFromStructure(new TCP_ESTATS_OBS_REC_RW_v0 {
                    EnableCollection = enable
                }));

            case TCP_ESTATS_TYPE.TcpConnectionEstatsBandwidth:
                var operation = enable ? TCP_BOOLEAN_OPTIONAL.TcpBoolOptEnabled : TCP_BOOLEAN_OPTIONAL.TcpBoolOptDisabled;
                return(SafeHGlobalHandle.CreateFromStructure(new TCP_ESTATS_BANDWIDTH_RW_v0 {
                    EnableCollectionInbound = operation, EnableCollectionOutbound = operation
                }));

            case TCP_ESTATS_TYPE.TcpConnectionEstatsFineRtt:
                return(SafeHGlobalHandle.CreateFromStructure(new TCP_ESTATS_FINE_RTT_RW_v0 {
                    EnableCollection = enable
                }));

            default:
                return(null);
            }
        }
Esempio n. 17
0
        public void GlobalLockTest()
        {
            var bp = GlobalLock(new IntPtr(1));

            Assert.That(bp, Is.EqualTo(IntPtr.Zero));
            Assert.That(Marshal.GetLastWin32Error(), Is.Not.Zero);

            using (var hMem = SafeHGlobalHandle.CreateFromStructure(1L))
            {
                Assert.That(hMem.IsInvalid, Is.False);
                var ptr = GlobalLock(hMem.DangerousGetHandle());
                Assert.That(ptr, Is.EqualTo(hMem.DangerousGetHandle()));
                GlobalUnlock(hMem.DangerousGetHandle());
            }
        }
Esempio n. 18
0
        public void SaferGetSetPolicyInformationTest()
        {
            using (var mem = SafeHGlobalHandle.CreateFromStructure <uint>())
            {
                Assert.That(SaferGetPolicyInformation(SAFER_SCOPEID.SAFER_SCOPEID_USER, SAFER_POLICY_INFO_CLASS.SaferPolicyDefaultLevel, mem.Size, mem, out _), ResultIs.Successful);
                var defLvl = mem.ToStructure <SAFER_LEVELID>();
                Assert.That((uint)defLvl, Is.Not.Zero);

                mem.Write(SAFER_LEVELID.SAFER_LEVELID_NORMALUSER);
                Assert.That(SaferSetPolicyInformation(SAFER_SCOPEID.SAFER_SCOPEID_USER, SAFER_POLICY_INFO_CLASS.SaferPolicyDefaultLevel, mem.Size, mem), ResultIs.Successful);
                Assert.That(SaferGetPolicyInformation(SAFER_SCOPEID.SAFER_SCOPEID_USER, SAFER_POLICY_INFO_CLASS.SaferPolicyDefaultLevel, mem.Size, mem, out _), ResultIs.Successful);
                Assert.That(mem.ToStructure <SAFER_LEVELID>(), Is.EqualTo(SAFER_LEVELID.SAFER_LEVELID_NORMALUSER));

                mem.Write(defLvl);
                Assert.That(SaferSetPolicyInformation(SAFER_SCOPEID.SAFER_SCOPEID_USER, SAFER_POLICY_INFO_CLASS.SaferPolicyDefaultLevel, mem.Size, mem), ResultIs.Successful);
            }
        }
Esempio n. 19
0
        public void RtlInstallFunctionTableCallbackTest()
        {
            IntPtr pEntry = default;

            using (SafeHGlobalHandle mem = new SafeHGlobalHandle(4096), entry = SafeHGlobalHandle.CreateFromStructure(new IMAGE_RUNTIME_FUNCTION_ENTRY {
                BeginAddress = 0, EndAddress = 4096, UnwindInfoAddress = 2048
            }))
            {
                var baseAddr = (ulong)((IntPtr)mem).ToInt64();
                pEntry = (IntPtr)entry;
                var id = baseAddr | 0x3;
                Assert.That(RtlInstallFunctionTableCallback(id, baseAddr, 4096, callbk), ResultIs.Successful);
                Assert.That(RtlDeleteFunctionTable(id), ResultIs.Successful);
            }

            IntPtr callbk(IntPtr ControlPc, IntPtr Context) => pEntry;
        }
Esempio n. 20
0
        public void QueryCredentialsAttributesTest()
        {
            using (var hCred = AcqCredHandle())
            {
                using (var mem = SafeHGlobalHandle.CreateFromStructure <SecPkgCredentials_Names>())
                {
                    Assert.That(QueryCredentialsAttributes(hCred, SECPKG_CRED_ATTR.SECPKG_CRED_ATTR_NAMES, (IntPtr)mem), Is.EqualTo((HRESULT)0));
                    Assert.That(mem.ToStructure <SecPkgCredentials_Names>().sUserName, Is.Not.Null);

                    Assert.That(SetCredentialsAttributes(hCred, SECPKG_CRED_ATTR.SECPKG_CRED_ATTR_NAMES, (IntPtr)mem, (uint)mem.Size), Is.EqualTo((HRESULT)0));
                }
                using (var mem = SafeHGlobalHandle.CreateFromStructure <SecPkgCredentials_Names>())
                {
                    Assert.That(QueryCredentialsAttributesEx(hCred, SECPKG_CRED_ATTR.SECPKG_CRED_ATTR_NAMES, (IntPtr)mem, (uint)mem.Size), Is.EqualTo((HRESULT)0));
                    Assert.That(mem.ToStructure <SecPkgCredentials_Names>().sUserName, Is.Not.Null);
                }
            }
        }
Esempio n. 21
0
        public void QuerySetContextAttributesTest()
        {
            using (var hCred = AcqCredHandle())
                using (var hCtx = GetSecContext(hCred, out _))
                {
                    using (var mem = SafeHGlobalHandle.CreateFromStructure <SecPkgContext_SessionAppData>())
                    {
                        Assert.That(QueryContextAttributes(hCtx, SECPKG_ATTR.SECPKG_ATTR_APP_DATA, (IntPtr)mem), Is.EqualTo((HRESULT)0));
                        Assert.That(mem.ToStructure <SecPkgContext_SessionAppData>().cbAppData, Is.GreaterThan(0));

                        Assert.That(SetContextAttributes(hCtx, SECPKG_ATTR.SECPKG_ATTR_APP_DATA, (IntPtr)mem, (uint)mem.Size), Is.EqualTo((HRESULT)0));
                    }
                    using (var mem = SafeHGlobalHandle.CreateFromStructure <SecPkgContext_SessionAppData>())
                    {
                        Assert.That(QueryContextAttributesEx(hCtx, SECPKG_ATTR.SECPKG_ATTR_APP_DATA, (IntPtr)mem, (uint)mem.Size), Is.EqualTo((HRESULT)0));
                        Assert.That(mem.ToStructure <SecPkgContext_SessionAppData>().cbAppData, Is.GreaterThan(0));
                    }
                }
        }
Esempio n. 22
0
        public void EnDecryptMessageTest()
        {
            const string msg = "This is the message.";

            using (var hCred = AcqCredHandle(UNISP_NAME, SECPKG_CRED.SECPKG_CRED_OUTBOUND))
                using (var pOut = new SafeSecBufferDesc())
                {
                    pOut.Add(SecBufferType.SECBUFFER_TOKEN);
                    pOut.Add(SecBufferType.SECBUFFER_EMPTY);
                    using (var hCtx = GetSecContext(hCred, pOut, Environment.MachineName))
                        using (var memSz = SafeHGlobalHandle.CreateFromStructure <SecPkgContext_Sizes>())
                        {
                            SecPkgContext_Sizes szs = default;
                            Assert.That(() => szs = QueryContextAttributes <SecPkgContext_Sizes>(hCtx, SECPKG_ATTR.SECPKG_ATTR_SIZES), Throws.Nothing);

                            using (var edesc = new SafeSecBufferDesc())
                            {
                                edesc.Add((int)szs.cbSecurityTrailer, SecBufferType.SECBUFFER_TOKEN);
                                edesc.Add(SecBufferType.SECBUFFER_DATA, msg);
                                edesc.Add((int)szs.cbBlockSize, SecBufferType.SECBUFFER_PADDING);

                                Assert.That(EncryptMessage(hCtx, 0, ref edesc.GetRef(), 0), Is.EqualTo((HRESULT)0));

                                using (var ddesc = new SafeSecBufferDesc())
                                    using (var mem = new SafeHGlobalHandle(edesc[1].cbBuffer + edesc[2].cbBuffer))
                                    {
                                        edesc[1].pvBuffer.CopyTo((IntPtr)mem, edesc[1].cbBuffer);
                                        edesc[2].pvBuffer.CopyTo(((IntPtr)mem).Offset(edesc[1].cbBuffer), edesc[2].cbBuffer);

                                        ddesc.Add(new SecBuffer(SecBufferType.SECBUFFER_STREAM)
                                        {
                                            pvBuffer = (IntPtr)mem, cbBuffer = mem.Size
                                        });
                                        ddesc.Add(new SecBuffer(SecBufferType.SECBUFFER_DATA));

                                        Assert.That(DecryptMessage(hCtx, ref ddesc.GetRef(), 0, out _), Is.EqualTo((HRESULT)0));
                                        Assert.That(StringHelper.GetString(ddesc[1].pvBuffer, CharSet.Unicode, ddesc[1].cbBuffer), Is.EqualTo(msg));
                                    }
                            }
                        }
                }
        }
Esempio n. 23
0
        public void SystemParametersInfoGetTest()
        {
            // Try get bool value
            var ptr = new SafeHGlobalHandle(4);

            Assert.That(SystemParametersInfo(SPI.SPI_GETCLIENTAREAANIMATION, 0, (IntPtr)ptr, 0));
            var bval1 = ptr.ToStructure <uint>() > 0;

            Assert.That(bval1, Is.True);

            // Try get generic bool value
            Assert.That(SystemParametersInfo(SPI.SPI_GETCLIENTAREAANIMATION, out bool bval2));
            Assert.That(bval2, Is.True);

            // Try get integral value
            ptr = new SafeHGlobalHandle(4);
            Assert.That(SystemParametersInfo(SPI.SPI_GETFOCUSBORDERHEIGHT, 0, (IntPtr)ptr, 0));
            var uval1 = ptr.ToStructure <uint>();

            Assert.That(uval1, Is.Not.Zero);

            // Try get generic integral value
            Assert.That(SystemParametersInfo <uint>(SPI.SPI_GETFOCUSBORDERHEIGHT, out var uval2));
            Assert.That(uval2, Is.EqualTo(uval1));

            // Try get struct value
            ptr = SafeHGlobalHandle.CreateFromStructure <RECT>();
            Assert.That(SystemParametersInfo(SPI.SPI_GETWORKAREA, 0, (IntPtr)ptr, 0));
            var rval1 = ptr.ToStructure <RECT>();

            Assert.That(rval1.IsEmpty, Is.False);

            // Try get generic struct value
            Assert.That(SystemParametersInfo(SPI.SPI_GETWORKAREA, out RECT rval2));
            Assert.That(rval2, Is.EqualTo(rval1));

            // Try get string value
            var sb = new System.Text.StringBuilder(Kernel32.MAX_PATH, Kernel32.MAX_PATH);

            Assert.That(SystemParametersInfo(SPI.SPI_GETDESKWALLPAPER, (uint)sb.Capacity, sb, 0));
            Assert.That(sb, Has.Length.GreaterThan(0));
        }
Esempio n. 24
0
 public void InitializeSecurityContextTest()
 {
     using (var hCred = AcqCredHandle())
     {
         const ASC_REQ STANDARD_CONTEXT_ATTRIBUTES = ASC_REQ.ASC_REQ_CONNECTION | ASC_REQ.ASC_REQ_ALLOCATE_MEMORY;
         var           un          = WindowsIdentity.GetCurrent().Name;
         var           safeBuffMem = SafeHGlobalHandle.CreateFromStructure(new SecBuffer {
             BufferType = SecBufferType.SECBUFFER_TOKEN
         });
         var sbb = SecBufferDesc.Default;
         sbb.cBuffers = 1;
         sbb.pBuffers = (IntPtr)safeBuffMem;
         var hNewCtxt = CtxtHandle.Null;
         var hr       = InitializeSecurityContext(hCred, IntPtr.Zero, un, STANDARD_CONTEXT_ATTRIBUTES, 0,
                                                  DREP.SECURITY_NATIVE_DREP, IntPtr.Zero, 0, ref hNewCtxt, ref sbb, out var attr, out var exp);
         Assert.That(hr, Is.EqualTo((HRESULT)0).Or.Property("Succeeded").True);
         Assert.That(DeleteSecurityContext(hNewCtxt), Is.EqualTo((HRESULT)0));
         Assert.That(FreeContextBuffer(safeBuffMem.ToStructure <SecBuffer>().pvBuffer), Is.EqualTo((HRESULT)0));
     }
 }
Esempio n. 25
0
        public void LsaLogonUserTest()
        {
            const string user = "******", domain = "contoso", pwd = "password";

            Assert.That(LsaLookupAuthenticationPackage(hLsaConn, MICROSOFT_KERBEROS_NAME, out var pkg), Is.EqualTo((NTStatus)0));
            var kerb = new KERB_INTERACTIVE_LOGON
            {
                MessageType     = KERB_LOGON_SUBMIT_TYPE.KerbInteractiveLogon,
                LogonDomainName = new LSA_UNICODE_STRING(domain),
                UserName        = new LSA_UNICODE_STRING(user),
                Password        = new LSA_UNICODE_STRING(pwd)
            };
            var mem = SafeHGlobalHandle.CreateFromStructure(kerb);

            AllocateLocallyUniqueId(out var srcLuid);
            var source = new TOKEN_SOURCE {
                SourceName = "foobar12".ToCharArray(), SourceIdentifier = srcLuid
            };

            Assert.That(LsaLogonUser(hLsaConn, "TestApp", SECURITY_LOGON_TYPE.Interactive, pkg, (IntPtr)mem, (uint)mem.Size, IntPtr.Zero, source,
                                     out var profBuf, out var profBufLen, out var logonId, out var hToken, out var quotas, out var subStat), Is.EqualTo((NTStatus)0));
        }