/*
 * Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved.
 */

package javasoft.sqe.tests.base64binary.minlength.minlength_2;

import jakarta.xml.bind.*;
import jakarta.xml.bind.TypeConstraintException;
import jakarta.xml.bind.JAXBException;
import javasoft.sqe.javatest.Status;
import java.io.PrintWriter;
import javasoft.sqe.tests.api.jakarta.xml.bind.CTTest;

public class minLength2_ValidateCTTests extends CTTest {

    /**
     * Command line starter.
     */
    public static void main(String[] args) {
        minLength2_ValidateCTTests test = new minLength2_ValidateCTTests();
        PrintWriter err = new PrintWriter(System.err, true);
        PrintWriter out = new PrintWriter(System.out, true);
        Status status = test.run(args, err, out);
        err.flush();
        out.flush();
        status.exit();
    }

    final String[] validValues = {
      "eGZhbXBqYnNh",
      "bGpwc3RwYnV5cGNydHh5aW1mdXh0cnlvbg==",
      "ZWVpd2dhaXVqZmh5dGl5ZmJhbmFnbndzbmNsZmJ4cmlxd2puZ3Nub24=",
      "Z3l3cGdwb2N3ZHB0dnZhcGZwcGZoYmt1a2tvZGN3dGtwYmJ3aGRhcndnamVyaXluaHdqY3B3bGdy",
      "bXF0Z2Vza2NqZm9nc3VteWZicGdpdGNydGRjcmhmbnFoZmdvd2RmaXhpamtkaGRtY3FicGZrd3hwZmhtbHRhZmVxd2ptdXRtaw==",
    };
    
    final String[] invalidValues = {
        "", 
        "AA==",
        "AAA=",
        "eGZhbXBqYn4="
    };
    
    JAXBElement<byte[]> elemObj;

    static final String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    static byte [] getBytes(String base64) {
        if (base64.length()%4 != 0) {
            throw new TestFailureException("test error: wrong length of base64 encoded data:" + base64);
        }
        int nOctets = base64.length() / 4 * 3;
        if (base64.endsWith("==")) {
            nOctets -= 2;
        } else if (base64.endsWith("=")) {
            nOctets -= 1;
        }
        byte [] bytes = new byte[nOctets];
        for (int i = (base64.length() / 4) - 1; i >= 0; --i) {
            int threeBytes = ((base64Chars.indexOf(base64.charAt(i*4)) & 0x3f) << 18)
                       | ((base64Chars.indexOf(base64.charAt(i*4 + 1)) & 0x3f) << 12)
                       | ((base64Chars.indexOf(base64.charAt(i*4 + 2)) & 0x3f) << 6)
                       | ((base64Chars.indexOf(base64.charAt(i*4 + 3)) & 0x3f));
            bytes[i*3] = (byte)((threeBytes >> 16) & 0xff);
            if (base64.charAt(i*4 + 2) != '=') {
                bytes[i*3 + 1] = (byte)((threeBytes >> 8) & 0xff);
                if (base64.charAt(i*4 + 3) != '=') {
                    bytes[i*3 + 2] = (byte)((threeBytes) & 0xff);
                }
            }
        }
        return bytes;
    }

    Status testInvalid() {
        for (int i = 0; i < invalidValues.length; ++i){
            
                elemObj.setValue(getBytes(invalidValues[i]));
                Status status = validate(elemObj);
                if (status.isPassed()) {
                    return Status.failed("" + invalidValues[i] + " is unexpectedly valid");
                }
            
        }
        return Status.passed("OK");
    }

    Status testValid() {
        for (int i = 0; i < validValues.length; ++i){
            elemObj.setValue(getBytes(validValues[i]));
            Status status = validate(elemObj);
            if (!status.isPassed()){
                return Status.failed("" + validValues[i] + " is unexpectedly invalid");
            }
        }
        return Status.passed("OKAY");
    }

    public Status Unmarshalled001( ) {
        elemObj = (JAXBElement<byte[]>)unmarshal("minLength-2-4.xml");
        return testValid();
    }

    public Status Unmarshalled002( ) {
        elemObj = (JAXBElement<byte[]>)unmarshal("minLength-2-4.xml");
        return testInvalid();
    }

    public Status Created001( ) {
        elemObj = (JAXBElement<byte[]>)(new ObjectFactory()).createNISTSchemaBase64BinaryMinLength2(getBytes(validValues[0]));
        
        return testValid();
    }

    public Status Created002( ) {
        elemObj = (JAXBElement<byte[]>)(new ObjectFactory()).createNISTSchemaBase64BinaryMinLength2(getBytes(validValues[0]));
        
        return testInvalid();
    }


}

