/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *  
 */
/*
 * EXTENSION
 * ISSUE: DVCORE-2521
 * FIXED BY: Roman Pedchenko
 * DATE: 2014-12-05
 * ADOPTED FROM: TEIID-2153, 2171, 2359, 2149, 2358, 2410, 2271, 2467, 2429, 2363, 2160, 2461, 2797, 2326, 1557
 * END EXTENSION
 */
// https://issues.jboss.org/browse/TEIID-4532
// https://issues.jboss.org/browse/TEIID-2476
// https://issues.redhat.com/browse/TEIID-5959
// https://issues.redhat.com/browse/TEIID-6064
package org.teiid.query.processor;

import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.teiid.adminapi.DataPolicy;
import org.teiid.api.exception.query.QueryValidatorException;
import org.teiid.core.TeiidComponentException;
import org.teiid.dqp.internal.process.AuthorizationValidator;
import org.teiid.metadata.AbstractMetadataRecord;
import org.teiid.query.metadata.QueryMetadataInterface;
import org.teiid.query.optimizer.TestOptimizer;
import org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder;
import org.teiid.query.sql.lang.Command;
import org.teiid.query.unittest.RealMetadataFactory;
import org.teiid.query.util.CommandContext;

@SuppressWarnings("nls")
public class TestSecurityFunctions {

	private class NoRoleAuthValidator implements AuthorizationValidator {
 	    @Override
 	    public boolean validate(String[] originalSql, Command command,
 	            QueryMetadataInterface metadata,
 	            CommandContext commandContext, CommandType commandType)
 	            throws QueryValidatorException, TeiidComponentException {
			return false;
 	    }

		@Override
 	    public boolean hasRole(String roleName,
 	            CommandContext commandContext) {
			return false;
 	    }

		@Override
		public boolean isAccessible(AbstractMetadataRecord record,
 	            CommandContext commandContext) {
			return true;
 	    }

		@Override
		public boolean isAccessible(String fullName, CommandContext commandContext, DataPolicy.PermissionType action) {
			return true;
		}
	}

    /**
     *  hasRole should be true without a service
     */
    @Test public void testHasRoleWithoutService() throws Exception {
        
        String sql = "select pm1.g1.e2 from pm1.g1 where true = hasRole('data', pm1.g1.e1)";  //$NON-NLS-1$
        
        // Create expected results
        List[] expected = new List[] { 
            Arrays.asList(new Object[] { new Integer(0) }),
        };    
        
        // Construct data manager with data
        HardcodedDataManager dataManager = new HardcodedDataManager();
        
        dataManager.addData("SELECT pm1.g1.e1, pm1.g1.e2 FROM pm1.g1", new List[] { //$NON-NLS-1$
            Arrays.asList(new Object[] { "fooRole", new Integer(0) }), //$NON-NLS-1$  
        }); 
        
        Command command = TestProcessor.helpParse(sql);   
        ProcessorPlan plan = TestProcessor.helpGetPlan(command, RealMetadataFactory.example1Cached());
        
        // Run query
        TestProcessor.helpProcess(plan, dataManager, expected);
    }

	@Test public void testHasRoleWithService() throws Exception {

        String sql = "select pm1.g1.e2 from pm1.g1 where true = hasRole('data', pm1.g1.e1)";  //$NON-NLS-1$

        // Create expected results
		List[] expected = new List[]{};

        // Construct data manager with data
        HardcodedDataManager dataManager = new HardcodedDataManager();

		dataManager.addData("SELECT pm1.g1.e1, pm1.g1.e2 FROM pm1.g1", new List[]{ //$NON-NLS-1$
				Arrays.asList(new Object[]{"fooRole", new Integer(0)}), //$NON-NLS-1$
		});

        CommandContext context = new CommandContext();
		context.setAuthoriziationValidator(new NoRoleAuthValidator());

		Command command = TestProcessor.helpParse(sql);
		ProcessorPlan plan = TestProcessor.helpGetPlan(command, RealMetadataFactory.example1Cached(), new DefaultCapabilitiesFinder(), context);

		// Run query
		TestProcessor.helpProcess(plan, context, dataManager, expected);
			}

	@Disabled("DVCORE-7868, determinism of some functions is changed")
	@Test public void testHasRoleBranchPruning() throws Exception {

		String sql = "select * from (\n" +
				"            select e1, e2 from \"pm1.g1\" \n" +
				"            union all\n" +
				"            select e1, e2 from \"pm1.g2\" where hasRole('report-role')) as a\n" +
				"            where e2 = 1 limit 100 ;";

		// Create expected results
		List[] expected = new List[] { Arrays.asList("a", 1) };

		// Construct data manager with data
		HardcodedDataManager dataManager = new HardcodedDataManager();

		dataManager.addData("SELECT g_0.e1, g_0.e2 FROM pm1.g1 AS g_0 WHERE g_0.e2 = 1", new List[] { //$NON-NLS-1$
				Arrays.asList("a", 1), //$NON-NLS-1$
        });

		CommandContext context = new CommandContext();
		context.setAuthoriziationValidator(new NoRoleAuthValidator());

        Command command = TestProcessor.helpParse(sql);
		ProcessorPlan plan = TestProcessor.helpGetPlan(command, RealMetadataFactory.example1Cached(), new DefaultCapabilitiesFinder(TestOptimizer.getTypicalCapabilities()), context);

        TestProcessor.helpProcess(plan, context, dataManager, expected);
    }

	@Test public void testHashes() {
		String sql = "select cast(to_chars(md5_binary(pm1.g1.e1), 'hex') as string), cast(to_chars(sha1(X'61'), 'hex') as string) from pm1.g1";  //$NON-NLS-1$

		List<?>[] expected = new List[] { Arrays.asList("0CC175B9C0F1B6A831C399E269772661", "86F7E437FAA5A7FCE15D1DDCB9EAEAEA377667B8")}; //$NON-NLS-1$ //$NON-NLS-2$

		HardcodedDataManager dataManager = new HardcodedDataManager();

		dataManager.addData("SELECT pm1.g1.e1 FROM pm1.g1", new List[] { //$NON-NLS-1$
				Arrays.asList(new Object[] { "a" }), //$NON-NLS-1$
		});

		Command command = TestProcessor.helpParse(sql);
		ProcessorPlan plan = TestProcessor.helpGetPlan(command, RealMetadataFactory.example1Cached(), new DefaultCapabilitiesFinder());

		TestProcessor.helpProcess(plan, dataManager, expected);
    }

	/**
	 * Not strictly a security issue, but was logged using the hasRole function
	 *
	 * RuleMergeVirtual needs to look for null nodes
	 */
	@Disabled("DVCORE-7868, determinism of some functions is changed")
	@Test public void testRaiseNullWithMultipleNullSources() throws Exception {
		String sql = "select 1\n" +
				"from\n" +
				"    \"v2a\" a\n" +
				"    join \"v2a\" b on a.salesorderid=b.salesorderid";

		String ddl = "create foreign table salesorderdetail (carriertrackingnumber integer, rowguid string, salesorderid integer);"
				+ "create view v1\n" +
				"as\n" +
				"select\n" +
				"    \"carriertrackingnumber\"\n" +
				"    ,\"rowguid\"\n" +
				"    ,\"salesorderid\"\n" +
				"from\n" +
				"    \"salesorderdetail\"\n" +
				"where hasRole('data','a_role2')\n" +
				"union all\n" +
				"select\n" +
				"    \"carriertrackingnumber\"\n" +
				"    ,\"rowguid\"\n" +
				"    ,\"salesorderid\"\n" +
				"from\n" +
				"    \"salesorderdetail\"\n" +
				"where hasRole('data','a_role2');\n" +
				"create view v2a as\n" +
				"select a.salesOrderId\n" +
				"from\n" +
				"    \"v1\" a\n" +
				"    join \"salesorderdetail\" b on a.salesorderid=b.salesorderid;";

		List[] expected = new List[] { };
		HardcodedDataManager dataManager = new HardcodedDataManager();
		dataManager.addData("SELECT pm1.g1.e1, pm1.g1.e2 FROM pm1.g1", new List[] { //$NON-NLS-1$
				Arrays.asList(new Object[] { "fooRole", 1 }), //$NON-NLS-1$
		});

		CommandContext context = new CommandContext();
		context.setAuthoriziationValidator(new NoRoleAuthValidator());

		Command command = TestProcessor.helpParse(sql);
		ProcessorPlan plan = TestProcessor.helpGetPlan(command, RealMetadataFactory.fromDDL(ddl, "x", "y"), new DefaultCapabilitiesFinder(), context);

		// Run query
		TestProcessor.helpProcess(plan, context, dataManager, expected);
	}

}
