Exemplo n.º 1
0
        public void TestNtStringReadToString()
        {
            const string testStr = "TestString";

            using (DisposableNativeString write = new DisposableNativeString(testStr))
            {
                NtStringRead read = new NtStringRead(write.Buffer, write.Length);
                Assert.AreEqual(testStr, read.ToString());
                GC.KeepAlive(write);
            }
        }
Exemplo n.º 2
0
        internal static void SetServer(IList <NtIPAddress> servers)
        {
            uint[] uPorts = new uint[servers.Count];
            for (int i = 0; i < uPorts.Length; i++)
            {
                uPorts[i] = (uint)servers[i].Port;
            }
            IntPtr[] serv = new IntPtr[servers.Count];
            DisposableNativeString[] servHolder = new DisposableNativeString[servers.Count];
            UIntPtr len;

            for (int i = 0; i < serv.Length; i++)
            {
                servHolder[i] = new DisposableNativeString(servers[i].IpAddress);
                serv[i]       = servHolder[i].Buffer;
            }
            len = (UIntPtr)servers.Count;
            Interop.NT_SetServerMulti(len, serv, uPorts);
            foreach (var s in servHolder)
            {
                s.Dispose();
            }
        }
Exemplo n.º 3
0
        internal static bool SetEntryStringArray(string name, IList <string> value, bool force = false)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value), "Value array cannot be null");
            }
            var namePtr = CreateCachedUTF8String(name);

            DisposableNativeString[] ntStrings = new DisposableNativeString[value.Count];
            for (int i = 0; i < value.Count; i++)
            {
                ntStrings[i] = new DisposableNativeString(value[i]);
            }

            int retVal = Interop.NT_SetEntryStringArray(namePtr.Buffer, namePtr.Length, ntStrings, (UIntPtr)ntStrings.Length, force ? 1 : 0);

            foreach (var ntString in ntStrings)
            {
                ntString.Dispose();
            }

            return(retVal != 0);
        }
Exemplo n.º 4
0
        public void TestNtStringWriteCreateAndDispose()
        {
            DisposableNativeString write = new DisposableNativeString("TestString");

            write.Dispose();
        }
Exemplo n.º 5
0
        public void TestNtStringWriteArray()
        {
            object obj = new DisposableNativeString[6];

            Assert.DoesNotThrow(() => GCHandle.Alloc(obj, GCHandleType.Pinned).Free());
        }
Exemplo n.º 6
0
        public void TestNtRpcCallInfo()
        {
            uint   rpcId   = 10549;
            uint   callUid = 8085;
            string name    = "TestName";
            string param   = "Params\0Others";

            byte[] nameArr    = CreateUTF8String(name);
            byte[] paramArr   = CreateUTF8String(param);
            int    nativeSize = 0;

            IntPtr rpcCallInfoPtr = NT_GetRpcCallInfoForTesting(rpcId, callUid, nameArr, paramArr, (UIntPtr)(paramArr.Length - 1), ref nativeSize);



            Assert.That(Marshal.SizeOf(typeof(NtRpcCallInfo)), Is.EqualTo(nativeSize));
            Assert.That(IsBlittable(typeof(NtRpcCallInfo)));

            using (DisposableNativeString nameToWrite = new DisposableNativeString(name))
                using (DisposableNativeString paramToWrite = new DisposableNativeString(param))
                {
                    NtRpcCallInfo managedInfo = new NtRpcCallInfo(new NtStringRead(nameToWrite.Buffer, nameToWrite.Length),
                                                                  new NtStringRead(paramToWrite.Buffer, paramToWrite.Length), rpcId, callUid);

                    Assert.That(managedInfo.Name.ToString(), Is.EqualTo(name));
                    Assert.That(managedInfo.Param.ToString(), Is.EqualTo(param));
                    Assert.That(managedInfo.CallUid, Is.EqualTo(callUid));
                    Assert.That(managedInfo.RpcId, Is.EqualTo(rpcId));


                    List <byte> nativeArray  = new List <byte>();
                    List <byte> managedArray = new List <byte>();

                    int bytesToSkip = (Marshal.SizeOf(typeof(IntPtr)) == 8) ? 32 : 16;
                    unsafe
                    {
                        NtRpcCallInfo *callInfo = (NtRpcCallInfo *)rpcCallInfoPtr.ToPointer();
                        Assert.That(callInfo->Name.ToString(), Is.EqualTo(name));
                        Assert.That(callInfo->Param.ToString(), Is.EqualTo(param));
                        Assert.That(callInfo->CallUid, Is.EqualTo(callUid));
                        Assert.That(callInfo->RpcId, Is.EqualTo(rpcId));



                        byte *bp = (byte *)callInfo;
                        for (int i = 0; i < nativeSize - bytesToSkip; i++)
                        {
                            nativeArray.Add(bp[i]);
                        }

                        NtRpcCallInfo *managedRpc = &managedInfo;
                        byte *         mbp        = (byte *)managedRpc;

                        for (int i = 0; i < Marshal.SizeOf(typeof(NtRpcCallInfo)) - bytesToSkip; i++)
                        {
                            managedArray.Add(mbp[i]);
                        }
                    }

                    // Assert that everything past our pointer values are equivelent
                    Assert.That(nativeArray, Is.EquivalentTo(managedArray));
                }

            NT_DisposeRpcCallInfoIntPtr(rpcCallInfoPtr);
        }
Exemplo n.º 7
0
        public void TestNtConnectionInfo()
        {
            string name       = "Testing";
            string ip         = "localhost";
            uint   port       = 1756;
            ulong  lastUpdate = 26;
            uint   protoRev   = 0x0300;
            int    nativeSize = 0;

            byte[] nameArr           = CreateUTF8String(name);
            byte[] ipArr             = CreateUTF8String(ip);
            IntPtr connectionInfoPtr = NT_GetConnectionInfoForTesting(nameArr, ipArr, port, lastUpdate, protoRev,
                                                                      ref nativeSize);

            Assert.That(Marshal.SizeOf(typeof(NtConnectionInfo)), Is.EqualTo(nativeSize));
            Assert.That(IsBlittable(typeof(NtConnectionInfo)));

            using (DisposableNativeString nameToWrite = new DisposableNativeString(name))
                using (DisposableNativeString ipToWrite = new DisposableNativeString(ip))
                {
                    NtConnectionInfo managedInfo = new NtConnectionInfo(new NtStringRead(nameToWrite.Buffer, nameToWrite.Length),
                                                                        new NtStringRead(ipToWrite.Buffer, ipToWrite.Length), port, lastUpdate, protoRev);

                    Assert.That(managedInfo.RemoteId.ToString(), Is.EqualTo(name));
                    Assert.That(managedInfo.RemoteIp.ToString(), Is.EqualTo(ip));
                    Assert.That(managedInfo.RemotePort, Is.EqualTo(port));
                    Assert.That(managedInfo.LastUpdate, Is.EqualTo(lastUpdate));
                    Assert.That(managedInfo.ProtocolVersion, Is.EqualTo(protoRev));


                    List <byte> nativeArray  = new List <byte>();
                    List <byte> managedArray = new List <byte>();

                    int bytesToSkip = (Marshal.SizeOf(typeof(IntPtr)) == 8) ? 32 : 16;
                    unsafe
                    {
                        NtConnectionInfo *connInfo = (NtConnectionInfo *)connectionInfoPtr.ToPointer();
                        Assert.That(connInfo->RemoteId.ToString(), Is.EqualTo(name));
                        Assert.That(connInfo->RemoteIp.ToString(), Is.EqualTo(ip));
                        Assert.That(connInfo->RemotePort, Is.EqualTo(port));
                        Assert.That(connInfo->LastUpdate, Is.EqualTo(lastUpdate));
                        Assert.That(connInfo->ProtocolVersion, Is.EqualTo(protoRev));



                        byte *bp = (byte *)connInfo;
                        for (int i = bytesToSkip; i < nativeSize; i++)
                        {
                            nativeArray.Add(bp[i]);
                        }

                        NtConnectionInfo *managedConn = &managedInfo;
                        byte *            mbp         = (byte *)managedConn;

                        for (int i = bytesToSkip; i < Marshal.SizeOf(typeof(NtConnectionInfo)); i++)
                        {
                            managedArray.Add(mbp[i]);
                        }
                    }

                    // Assert that everything past our pointer values are equivelent
                    Assert.That(nativeArray, Is.EquivalentTo(managedArray));
                }

            NT_FreeConnectionInfoForTesting(connectionInfoPtr);
        }