git-tfs-id: [http://tfs.userrendszerhaz.hu:8080/tfs/DefaultCollection]$/MediaCube...
authorVásáry Dániel <daniel.vasary@userrendszerhaz.hu>
Tue, 17 Oct 2017 08:58:23 +0000 (08:58 +0000)
committerVásáry Dániel <daniel.vasary@userrendszerhaz.hu>
Tue, 17 Oct 2017 08:58:23 +0000 (08:58 +0000)
client/IntegrationTests/MaestroIT.cs
server/user.jobengine.osgi.commons/src/user/commons/nexio/NexioClipEventDispatcher.java
server/user.jobengine.osgi.commons/src/user/commons/nexio/api/ClipImpl.java
server/user.jobengine.osgi.commons/test/user/common/nexio/test/NexioDataMinerTest.java

index 0017041e18f6a65f2d3d3b4f647f9ebabd2c170b..e669ea94b31746c2974914fca0b9cff5870d39fd 100644 (file)
@@ -7,8 +7,32 @@ using Maestro.Commons;
 using System.Diagnostics;\r
 using SharpCifs.Smb;\r
 using Commons;\r
+using System.Runtime.InteropServices;\r
+using Microsoft.Win32.SafeHandles;\r
+using System.Runtime.ConstrainedExecution;\r
+using System.Security;\r
+using System.Security.Permissions;\r
+using System.Security.Principal;\r
 \r
 namespace IntegrationTests {\r
+    public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid {\r
+        private SafeTokenHandle()\r
+            : base(true) {\r
+        }\r
+\r
+        [DllImport("kernel32.dll")]\r
+        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]\r
+        [SuppressUnmanagedCodeSecurity]\r
+        [return: MarshalAs(UnmanagedType.Bool)]\r
+        private static extern bool CloseHandle(IntPtr handle);\r
+\r
+        protected override bool ReleaseHandle() {\r
+            return CloseHandle(handle);\r
+        }\r
+    }\r
+\r
+\r
+\r
     [TestClass]\r
     public class MaestroTests {\r
 \r
@@ -72,5 +96,71 @@ namespace IntegrationTests {
             result = result.Substring(0, result.LastIndexOf("."));\r
             Debug.WriteLine(result);\r
         }\r
+\r
+        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]\r
+        public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,\r
+                int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);\r
+\r
+        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]\r
+        public extern static bool CloseHandle(IntPtr handle);\r
+\r
+\r
+\r
+        [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]\r
+        [TestMethod]\r
+        public void TestImpersonate() {\r
+            SafeTokenHandle safeTokenHandle;\r
+            try {\r
+                string userName, domainName, password;\r
+                // Get the user token for the specified user, domain, and password using the\r
+                // unmanaged LogonUser method.\r
+                // The local machine name can be used for the domain name to impersonate a user on this machine.\r
+                domainName = ".";\r
+                userName = "mediacube";\r
+                password = "Broadca5T";\r
+                const int LOGON32_PROVIDER_DEFAULT = 0;\r
+                //This parameter causes LogonUser to create a primary token.\r
+                const int LOGON32_LOGON_INTERACTIVE = 2;\r
+\r
+                // Call LogonUser to obtain a handle to an access token.\r
+                bool returnValue = LogonUser(userName, domainName, password,\r
+                    LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,\r
+                    out safeTokenHandle);\r
+\r
+                Console.WriteLine("LogonUser called.");\r
+\r
+                if (false == returnValue) {\r
+                    int ret = Marshal.GetLastWin32Error();\r
+                    Console.WriteLine("LogonUser failed with error code : {0}", ret);\r
+                    throw new System.ComponentModel.Win32Exception(ret);\r
+                }\r
+                using (safeTokenHandle) {\r
+                    Console.WriteLine("Did LogonUser Succeed? " + (returnValue ? "Yes" : "No"));\r
+                    Console.WriteLine("Value of Windows NT token: " + safeTokenHandle);\r
+\r
+                    // Check the identity.\r
+                    Console.WriteLine("Before impersonation: "\r
+                        + WindowsIdentity.GetCurrent().Name);\r
+                    // Use the token handle returned by LogonUser.\r
+                    using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle())) {\r
+                        using (WindowsImpersonationContext impersonatedUser = newId.Impersonate()) {\r
+\r
+                            // Check the identity.\r
+                            Console.WriteLine("After impersonation: "\r
+                                + WindowsIdentity.GetCurrent().Name);\r
+                        }\r
+                    }\r
+                    // Releasing the context object stops the impersonation\r
+                    // Check the identity.\r
+                    Console.WriteLine("After closing the context: " + WindowsIdentity.GetCurrent().Name);\r
+                }\r
+            }\r
+            catch (Exception ex) {\r
+                Console.WriteLine("Exception occurred. " + ex.Message);\r
+            }\r
+\r
+        }\r
+\r
+\r
     }\r
 }
\ No newline at end of file
index 95065b8f3434f2ef3612469b14aa2922a654ce30..c4ae372db5108d2423cbcfe8b984fcfd6b53e506 100644 (file)
@@ -52,6 +52,17 @@ public class NexioClipEventDispatcher implements ClipEventListener {
        static private final byte[] GET_SPECIAL_ID_ATTRIBUTES_REQ = { (byte) 0xC8, (byte) 0x84 };\r
        static private final char[] hexArray = "0123456789ABCDEF".toCharArray();\r
 \r
+       public static String bytesToBin(byte[] bytes) {\r
+               StringBuilder result = new StringBuilder();\r
+               for (byte b : bytes) {\r
+                       String str = byteToStr(b);\r
+                       result.append(str);\r
+                       if (result.length() % 4 == 0)\r
+                               result.append(" ");\r
+               }\r
+               return result.toString();\r
+       }\r
+\r
        public static String bytesToHex(byte[] bytes) {\r
                char[] hexChars = new char[bytes.length * 2];\r
                for (int j = 0; j < bytes.length; j++) {\r
@@ -62,6 +73,18 @@ public class NexioClipEventDispatcher implements ClipEventListener {
                return new String(hexChars);\r
        }\r
 \r
+       public static String byteToStr(byte b) {\r
+               String binaryString = Integer.toBinaryString(b & 0xFF);\r
+               String str = String.format("%8s", binaryString).replace(' ', '0');\r
+               return str;\r
+       }\r
+\r
+       public static String longToStr(long b) {\r
+               String binaryString = Long.toBinaryString(b & 0xFFFF);\r
+               String str = String.format("%16s", binaryString).replace(' ', '0');\r
+               return str;\r
+       }\r
+\r
        private EventListenerList progressListenerList;\r
        private ProgressEvent progressEvent = new ProgressEvent(this, 0);\r
        private INexioChangeListener nexioChangeListener;\r
@@ -195,8 +218,7 @@ public class NexioClipEventDispatcher implements ClipEventListener {
                }\r
 \r
                try {\r
-                       if ("2931346-3".equals(jsClip.getString(LONGNAMEID)))\r
-                               jsClip.put(KILLDATE, clip.getKillDate());\r
+                       jsClip.put(KILLDATE, clip.getKillDate());\r
                } catch (Exception e) {\r
                        //                      System.out.println(e);\r
                }\r
index a722d73e4eed4ce389dd13533e82799320377212..ece5fd4b82b30e513b03661deb080a39e946f308 100644 (file)
@@ -3,13 +3,16 @@ package user.commons.nexio.api;
 import java.io.IOException;
 import java.util.Calendar;
 
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
 import user.commons.nexio.server.protocol.Id;
 import user.commons.nexio.server.protocol.NexioServerProtocol;
 import user.commons.nexio.server.protocol.ProtocolException;
 import user.commons.nexio.server.protocol.Xid;
 
 class ClipImpl implements Clip {
-
+       private static final Logger logger = LogManager.getLogger();
        private static final String PROTOCOL = "protocol";
        private static final String ID = "id";
        private static final String XID = "xid";
@@ -90,11 +93,27 @@ class ClipImpl implements Clip {
                        metadata = protocol.executeGetIDMetadata(id);
                        checkClipExists(metadata);
                }
-               byte[] filetime = { metadata[42], metadata[43] };
-
                Calendar result = Calendar.getInstance();
-
-               return null;
+               byte[] dtArray = { metadata[43], metadata[42] };
+
+               long data = dtArray[0] & 0xFF;
+               data <<= 8;
+               data |= (dtArray[1] & 0xFF);
+
+               //logger.info("--- {}  .. {}", NexioClipEventDispatcher.bytesToBin(dtArray), NexioClipEventDispatcher.longToStr(data));
+
+               int day = (int) (data >> 11);
+               int month = (int) ((data & 0x7FF) >> 7);
+               int year = 1900 + (int) (data & 0x7F);
+               //System.out.println(String.format("day %s month %s year %s", day, month, year));
+               result.set(Calendar.YEAR, year);
+               result.set(Calendar.MONTH, month - 1);
+               result.set(Calendar.DAY_OF_MONTH, day);
+               result.set(Calendar.HOUR_OF_DAY, 0);
+               result.set(Calendar.MINUTE, 0);
+               result.set(Calendar.SECOND, 0);
+               result.set(Calendar.MILLISECOND, 0);
+               return result;
        }
 
        @Override
index 16474a188a74f86b0c4a979014f6aa4647a9f0f2..6052a2c7782e488df40ccf1e3daa0d4a2e1619e1 100644 (file)
@@ -12,6 +12,32 @@ import user.commons.nexio.NexioDataMiner;
 import user.commons.nexio.api.Controller;\r
 \r
 public class NexioDataMinerTest {\r
+       public static String bytesToBin(byte[] bytes) {\r
+               StringBuilder result = new StringBuilder();\r
+               for (byte b : bytes) {\r
+                       String str = byteToStr(b);\r
+                       result.append(str);\r
+                       if (result.length() % 4 == 0)\r
+                               result.append(" ");\r
+               }\r
+               return result.toString();\r
+       }\r
+\r
+       public static long bytesToLong(byte[] b) {\r
+               long result = 0;\r
+               for (int i = 0; i < 2; i++) {\r
+                       result <<= 8;\r
+                       result |= (b[i] & 0xFF);\r
+               }\r
+               return result;\r
+       }\r
+\r
+       public static String byteToStr(byte b) {\r
+               String binaryString = Integer.toBinaryString(b & 0xFF);\r
+               String str = String.format("%8s", binaryString).replace(' ', '0');\r
+               return str;\r
+       }\r
+\r
        @BeforeClass\r
        public static void initialize() throws Exception {\r
                //Naplozas\r
@@ -30,6 +56,12 @@ public class NexioDataMinerTest {
                System.getProperties().putAll(properties);\r
        }\r
 \r
+       public static String longToStr(long b) {\r
+               String binaryString = Long.toBinaryString(b & 0xFFFF);\r
+               String str = String.format("%16s", binaryString).replace(' ', '0');\r
+               return str;\r
+       }\r
+\r
        private NexioDataMiner sut;\r
 \r
        @Test\r
@@ -50,4 +82,17 @@ public class NexioDataMinerTest {
                c.connect();\r
                c.getMediabase().getProtocol().setDescription("%0000205", "VALAMI");\r
        }\r
+\r
+       @Test\r
+       public void testKillDateConverter() throws Exception {\r
+               byte[] data = { 8, -58 };\r
+               long num = bytesToLong(data);\r
+               System.out.println(String.format("%s .. %s", bytesToBin(data), longToStr(num)));\r
+               int day = (int) (num >> 11);\r
+               int month = (int) ((num & 0x7FF) >> 7);\r
+               int year = (int) (num & 0x7F);\r
+               System.out.println(String.format("day %s month %s year %s", day, month, year));\r
+\r
+       }\r
+\r
 }\r