<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package org.apache.mina.proxy;

import static org.apache.mina.proxy.utils.ByteUtilities.asHex;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import junit.framework.TestCase;

import org.apache.mina.proxy.handlers.http.ntlm.NTLMResponses;
import org.apache.mina.proxy.handlers.http.ntlm.NTLMUtilities;
import org.apache.mina.proxy.utils.ByteUtilities;
import org.apache.mina.proxy.utils.MD4Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * NTLMTest.java - JUNIT tests of the NTLM authentication mechanism.
 * 
 * @author The Apache MINA Project (<EMAIL>)
 * @version $Rev$, $Date$
 * @since MINA 2.0.0-M3
 */
public class NTLMTest extends TestCase {
    private final static Logger logger = LoggerFactory
            .getLogger(NTLMTest.class);

    static {
        if (Security.getProvider("MINA") == null) {
            Security.addProvider(new MD4Provider());
        }
    }
    
    /**
     * Tests bytes manipulations.
     */
    public void testEncoding() throws UnsupportedEncodingException {
        assertEquals("d204", asHex(ByteUtilities.writeShort((short) 1234)));
        assertEquals("d2040000", asHex(ByteUtilities.writeInt(1234)));
        assertEquals("01000000", asHex(ByteUtilities.writeInt((short) 1)));
        assertEquals("4e544c4d53535000", asHex(NTLMUtilities.NTLM_SIGNATURE));

        assertEquals("680065006c006c006f00", asHex(ByteUtilities
                .getUTFStringAsByteArray("hello")));
        assertEquals("48454c4c4f", asHex(ByteUtilities
                .getOEMStringAsByteArray("HELLO")));
    }

    /**
     * Tests conversions to and from network byte order.
     */
    public void testMethods() {
        byte[] buf = new byte[4];
        ByteUtilities.intToNetworkByteOrder(1234, buf, 0, 4);
        assertEquals("000004d2", asHex(buf));
        assertEquals(1234, ByteUtilities.networkByteOrderToInt(buf, 0, 4));
    }

    /**
     * Tests security buffers.
     */
    public void testSecurityBuffer() {
        byte[] secBuf = new byte[8];
        NTLMUtilities.writeSecurityBuffer((short) 1234, (short) 1234, 4321,
                secBuf, 0);
        assertEquals("d204d204e1100000", asHex(secBuf));
    }

    /**
     * Tests creating a type 1 message.
     */
    public void testType1Message() {
        int customFlags = NTLMUtilities.FLAG_NEGOTIATE_UNICODE
                | NTLMUtilities.FLAG_NEGOTIATE_OEM
                | NTLMUtilities.FLAG_NEGOTIATE_NTLM
                | NTLMUtilities.FLAG_REQUEST_SERVER_AUTH_REALM
                | NTLMUtilities.FLAG_NEGOTIATE_DOMAIN_SUPPLIED
                | NTLMUtilities.FLAG_NEGOTIATE_WORKSTATION_SUPPLIED;

        byte[] osVer = new byte[8];
        NTLMUtilities
                .writeOSVersion((byte) 5, (byte) 0, (short) 2195, osVer, 0);

        String msgType1 = asHex(NTLMUtilities.createType1Message("WORKSTATION",
                "DOMAIN", customFlags, osVer));
        assertEquals(
                "4e544c4d53535000010000000732000006000600330000000b000b0028000000"
                        + "050093080000000f574f524b53544154494f4e444f4d41494e",
                msgType1);

        assertEquals("050093080000000f", asHex(osVer));
        
        //Microsoft Windows XP [version 5.1.2600]
        String os = System.getProperty("os.name");
        if (os != null && os.toUpperCase().contains("WINDOWS") && 
                "5.1".equals(System.getProperty("os.version"))) {
            assertEquals("0501280a0000000f", asHex(NTLMUtilities.getOsVersion()));
        }
    }

    /**
     * Tests creating a type 3 message.
     * WARNING: Will silently fail if no MD4 digest provider is available.
     */
    public void testType3Message() throws Exception {
        try {
            MessageDigest.getInstance("MD4");
        } catch (NoSuchAlgorithmException ex) {
            logger.warn("No MD4 digest provider found !");
            return;
        }

        int flags = 0x00000001 | 0x00000200 | 0x00010000 | 0x00800000;
        String msg = "4e544c4d53535000020000000c000c003000000001028100"
                + "0123456789abcdef0000000000000000620062003c000000"
                + "44004f004d00410049004e0002000c0044004f004d004100"
                + "49004e0001000c0053004500520056004500520004001400"<commit_msg>Made OS version test more subtle <commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package org.apache.mina.proxy;

import static org.apache.mina.proxy.utils.ByteUtilities.asHex;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import junit.framework.TestCase;

import org.apache.mina.proxy.handlers.http.ntlm.NTLMResponses;
import org.apache.mina.proxy.handlers.http.ntlm.NTLMUtilities;
import org.apache.mina.proxy.utils.ByteUtilities;
import org.apache.mina.proxy.utils.MD4Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * NTLMTest.java - JUNIT tests of the NTLM authentication mechanism.
 * 
 * @author The Apache MINA Project (<EMAIL>)
 * @version $Rev$, $Date$
 * @since MINA 2.0.0-M3
 */
public class NTLMTest extends TestCase {
    private final static Logger logger = LoggerFactory
            .getLogger(NTLMTest.class);

    static {
        if (Security.getProvider("MINA") == null) {
            Security.addProvider(new MD4Provider());
        }
    }
    
    /**
     * Tests bytes manipulations.
     */
    public void testEncoding() throws UnsupportedEncodingException {
        assertEquals("d204", asHex(ByteUtilities.writeShort((short) 1234)));
        assertEquals("d2040000", asHex(ByteUtilities.writeInt(1234)));
        assertEquals("01000000", asHex(ByteUtilities.writeInt((short) 1)));
        assertEquals("4e544c4d53535000", asHex(NTLMUtilities.NTLM_SIGNATURE));

        assertEquals("680065006c006c006f00", asHex(ByteUtilities
                .getUTFStringAsByteArray("hello")));
        assertEquals("48454c4c4f", asHex(ByteUtilities
                .getOEMStringAsByteArray("HELLO")));
    }

    /**
     * Tests conversions to and from network byte order.
     */
    public void testMethods() {
        byte[] buf = new byte[4];
        ByteUtilities.intToNetworkByteOrder(1234, buf, 0, 4);
        assertEquals("000004d2", asHex(buf));
        assertEquals(1234, ByteUtilities.networkByteOrderToInt(buf, 0, 4));
    }

    /**
     * Tests security buffers.
     */
    public void testSecurityBuffer() {
        byte[] secBuf = new byte[8];
        NTLMUtilities.writeSecurityBuffer((short) 1234, (short) 1234, 4321,
                secBuf, 0);
        assertEquals("d204d204e1100000", asHex(secBuf));
    }

    /**
     * Tests creating a type 1 message.
     */
    public void testType1Message() {
        int customFlags = NTLMUtilities.FLAG_NEGOTIATE_UNICODE
                | NTLMUtilities.FLAG_NEGOTIATE_OEM
                | NTLMUtilities.FLAG_NEGOTIATE_NTLM
                | NTLMUtilities.FLAG_REQUEST_SERVER_AUTH_REALM
                | NTLMUtilities.FLAG_NEGOTIATE_DOMAIN_SUPPLIED
                | NTLMUtilities.FLAG_NEGOTIATE_WORKSTATION_SUPPLIED;

        byte[] osVer = new byte[8];
        NTLMUtilities
                .writeOSVersion((byte) 5, (byte) 0, (short) 2195, osVer, 0);

        String msgType1 = asHex(NTLMUtilities.createType1Message("WORKSTATION",
                "DOMAIN", customFlags, osVer));
        assertEquals(
                "4e544c4d53535000010000000732000006000600330000000b000b0028000000"
                        + "050093080000000f574f524b53544154494f4e444f4d41494e",
                msgType1);

        assertEquals("050093080000000f", asHex(osVer));
        
        //Microsoft Windows XP [version 5.1.2600]
        String os = System.getProperty("os.name");
        if (os != null && os.toUpperCase().contains("WINDOWS") && 
                "5.1".equals(System.getProperty("os.version"))) {
            String hex = asHex(NTLMUtilities.getOsVersion());
        	assertEquals("0501", hex.substring(0, 4));
        	assertEquals(16, hex.length());
        }
    }

    /**
     * Tests creating a type 3 message.
     * WARNING: Will silently fail if no MD4 digest provider is available.
     */
    public void testType3Message() throws Exception {
        try {
            MessageDigest.getInstance("MD4");
        } catch (NoSuchAlgorithmException ex) {
            logger.warn("No MD4 digest provider found !");
            return;
        }

        int flags = 0x00000001 | 0x00000200 | 0x00010000 | 0x00800000;
        String msg = "4e544c4d53535000020000000c000c003000000001028100"
                + "0123456789abcdef0000000000000000620062003c000000"
                + "44004f004d00410049004e0002000c0044004f004d004100"
                + "49004e0001000c0053004500520056004500520004001400"
{'fraction_non_alphanumeric': 0.09763705103969754, 'fraction_numerical': 0.09149338374291115, 'mean_word_length': 2.864499634769905, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 17, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5996288', 'n_tokens_mistral': 3768, 'n_tokens_neox': 3108, 'n_words': 711}