text:
using System;
using System.Collections.Generic;
using System.Text.Json;
using Azure.Core;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using System.ComponentModel;
using Azure.Core.Pipeline;

namespace Azure.Security.Attestation
{
    /// <summary>
    /// Represents an Attestation Token object.
    /// </summary>
    public class AttestationToken
    {
        protected private string _token;
        private readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions();

        private object _deserializedBody;
        private object _statelock = new object();

        private AttestationToken(string token, ClientDiagnostics diagnostics)
        {
            _token = token;
            ConstructFromToken(_token);
            ClientDiagnostics = diagnostics;
        }
        private void ConstructFromToken(string token)
        {
            string[] decomposedToken = token.Split('.');
            if (decomposedToken.Length != 3)
            {
                throw new ArgumentException("JSON Web Tokens must have 3 components delimited by '.' characters.");
            }
            TokenHeaderBytes = Base64Url.Decode(decomposedToken[0]);
            TokenBodyBytes = Base64Url.Decode(decomposedToken[1]);
            TokenSignatureBytes = Base64Url.Decode(decomposedToken[2]);
        }

        /// <summary>
        /// Creates a new attestation token, used for mocking.
        /// </summary>
        protected AttestationToken()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AttestationToken"/> class as an unsecured JSON Web Token, with <paramref name="body"/> as the body of the token.
        /// </summary>
        /// <param name="body">Body of the token to be generated, serialized as a byte array.</param>
        public AttestationToken(BinaryData body)
        {
            _token = CreateUnsecuredJwt(body);
            ConstructFromToken(_token);
        }

        /// <summary>
        /// Creates a new attestation token based on the supplied body signed with the specified signing key.
        /// </summary>
        /// <param name="body">The body of the generated token, serialized as a byte array.</param>
        /// <param name="signingKey"><see cref="AttestationTokenSigningKey"/> which will be used to sign the generated token.</param>
        public AttestationToken(BinaryData body, AttestationTokenSigningKey signingKey)
        {
            _token = signingKey != null ? GenerateSecuredJsonWebToken(body, signingKey) : CreateUnsecuredJwt(body);
            ConstructFromToken(_token);
        }

        /// <summary>
        /// Creates a new unsecured attestation token with an empty body. Used for the <see cref="AttestationAdministrationClient.ResetPolicy(AttestationType, AttestationTokenSigningKey, System.Threading.CancellationToken)"/> API.
        /// </summary>
        /// <param name="signingKey"><see cref="AttestationTokenSigningKey"/> which will be used to sign the generated token.</param>
        public AttestationToken(AttestationTokenSigningKey signingKey)
        {
            _token = signingKey != null ? GenerateSecuredJsonWebToken(null, signingKey) : CreateUnsecuredJwt(null);
        }

        internal ClientDiagnostics ClientDiagnostics { get; set; }

        /// <summary>
        /// Returns the thumbprint of the X.509 certificate which was used to verify the attestation token.
        ///
        /// Null until the <see cref="AttestationToken.ValidateToken(AttestationTokenValidationOptions, IReadOnlyList{AttestationSigner}, CancellationToken)"/> method has been called.
        /// </summary>
        public string CertificateThumbprint { get; private set; }

        /// <summary>
        /// Returns the X.509 certificate which was used to verify the attestation token.
        ///
        /// Null until the <see cref="AttestationToken.ValidateToken(AttestationTokenValidationOptions, IReadOnlyList{AttestationSigner}, CancellationToken)"/> method has been called.
        /// </summary>
        public AttestationSigner SigningCertificate { get; private set; }

        /// <summary>
        /// Decoded header for the attestation token. See https://tools.ietf.org/html/rfc7515 for more details.
        /// </summary>
        public ReadOnlyMemory<byte> TokenHeaderBytes { get; private set; }

        /// <summary>
        /// Decoded body for the attestation token. See https://tools.ietf.org/html/rfc7515 for more details.
        /// </summary>
        public ReadOnlyMemory<byte> TokenBodyBytes { get; private set; }

        /// <summary>
        /// Decoded signature for the attestation token. See https://tools.ietf.org/html/rfc7515 for more details.
        /// </summary>
        public ReadOnlyMemory<byte> TokenSignatureBytes { get; private set; }

        // Standard JSON Web Signature/Json Web Token header values.

        /// <summary>
        /// Json Web Token Header "algorithm". See https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.1 for details.
        /// If the value of <see cref="Algorithm"/> is "none" it indicates that the token is unsecured.
        /// </summary>
        public string Algorithm => Header.Algorithm;

        /// <summary>
        /// Json Web Token Header "type". See https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.9 for details.
        ///
        /// If present, the value for this field is normally "JWT".
        /// </summary>
        public string Type => Header.Type;

        /// <summary>
        /// Json Web Token Header "content type". See https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.10 for details.
        /// </summary>
        public string ContentType => Header.ContentType;
        /// <summary>
        /// Json Web Token Header "Key URL". See https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.2 for details.
        /// </summary>
        public Uri KeyUrl => Header.JWKUri;
        /// <summary>
        /// Json Web Token Header "Key ID". See https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.4 for details.
        /// </summary>
        public string KeyId => Header.KeyId;

        /// <summary>
        /// Json Web Token Header "X509 URL". See https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.5 for details.
        /// </summary>
        public Uri X509Url => Header.X509Uri;

        /// <summary>
        /// Json Web Token Body Issuer. See https://www.rfc-editor.org/rfc/rfc7519.html#section-4.1.1 for details.
        /// </summary>
        public string Issuer => Payload.Issuer;

        /// <summary>
        /// An array of <see cref="X509Certificate"/> which represent a certificate chain used to sign the token.  <seealso href="https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.6">RFC 7515 section 4.1.6</seealso> for details.
        /// </summary>
        public X509Certificate2[] X509CertificateChain {
            get
            {
                List<X509Certificate2> certificates = new List<X509Certificate2>();
                foreach (var certificate in Header.X509CertificateChain)
                {
                    certificates.Add(new X509Certificate2(certificate));
                }
                return certificates.ToArray();
            }
        }

        /// <summary>
        /// The "thumbprint" of the certificate used to sign the request. <seealso href="https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.7">RFC 7515 section 4.1.7</seealso> for details.
        /// </summary>
        public string X509CertificateThumbprint => Header.X509CertificateThumbprint;

        /// <summary>
        /// The "thumbprint" of the certificate used to sign the request generated using the SHA256 algorithm. <seealso href="https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.8">RFC 7515 section 4.1.8</seealso> for details.
        /// </summary>
        public string X509CertificateSha256Thumbprint => Header.X509CertificateSha256Thumbprint;

        /// <summary>
        /// JSON Web Token Header "Critical". <seealso href="https://www.rfc-editor.org/rfc/rfc7515.html#section-4.1.11">RFC 7515 section 4.1.11</seealso> for details.
        /// </summary>
        public bool? Critical => Header.Critical;

        /// <summary>
        /// Returns the standard properties in the JSON Web Token header. <seealso href="https://tools.ietf.org/html/rfc7515">RFC 7515</seealso> for more details.
        /// </summary>
        internal JsonWebTokenHeader Header { get => JsonSerializer.Deserialize<JsonWebTokenHeader>(TokenHeaderBytes.ToArray()); }

        /// <summary>
        /// Returns the standard properties in the JSON Web Token header. See https://tools.ietf.org/html/rfc7515 for more details.
        /// </summary>
        private JsonWebTokenBody Payload { get => JsonSerializer.Deserialize<JsonWebTokenBody>(TokenBodyBytes.ToArray()); }

        /// <summary>
        /// Expiration time for the token.
        /// </summary>
        public DateTimeOffset? ExpirationTime
        {
            get
            {
                if (Payload.ExpirationTime.HasValue)
                {
                    return DateTimeOffset.FromUnixTimeSeconds((long)Payload.ExpirationTime.Value);
                }
                return null;
            }
        }

        /// <summary>
        /// Time before which this token is not valid.
        /// </summary>
        public DateTimeOffset? NotBeforeTime
        {
            get
            {
                if (Payload.NotBeforeTime.HasValue)
                {
                    return DateTimeOffset.FromUnixTimeSeconds((long)Payload.NotBeforeTime.Value);
                }
                return null;
            }
        }

        /// <summary>
        /// Time at which this token was issued.
        /// </summary>
        public DateTimeOffset? IssuedAtTime
        {
            get
            {
                if (Payload.IssuedAtTime.HasValue)
                {
                    return DateTimeOffset.FromUnixTimeSeconds((long)Payload.IssuedAtTime.Value);
                }
                return null;
            }
        }

        /// <summary>
        /// Validate a JSON Web Token returned by the MAA.
        /// <para/>
        /// If the caller provides a set of signers, than that set of signers will be used as the complete set of candidates for signing.
        /// If the caller does not provide a set of signers, then the <see cref="ValidateTokenAsync(AttestationTokenValidationOptions, IReadOnlyList{AttestationSigner}, CancellationToken)"/>
        /// API will a set of callers derived from the contents of the attestation token.
        /// </summary>
        /// <param name="options">Options used while validating the attestation token.</param>
        /// <param name="attestationSigningCertificates">Signing Certificates used to validate the token.</param>
        /// <param name="cancellationToken">Token used to cancel this operation if necessary.</param>
        /// <returns>true if the token was valid, false otherwise.</returns>
        /// <exception cref="ArgumentNullException">Thrown if the signing certificates provided are invalid.</exception>
        /// <exception cref="Exception">Thrown if validation fails.</exception>
        public virtual async Task<bool> ValidateTokenAsync(AttestationTokenValidationOptions options, IReadOnlyList<AttestationSigner> attestationSigningCertificates, CancellationToken cancellationToken = default)
            => await ValidateTokenInternal(options, attestationSigningCertificates, true, cancellationToken).ConfigureAwait(false);

        /// <summary>
        /// Validate a JSON Web Token returned by the MAA.
        /// <para/>
        /// If the caller provides a set of signers, than that set of signers will be used as the complete set of candidates for signing.
        /// If the caller does not provide a set of signers, then the <see cref="ValidateToken(AttestationTokenValidationOptions, IReadOnlyList{AttestationSigner}, CancellationToken)"/>
        /// API will a set of callers derived from the contents of the attestation token.
        /// </summary>
        /// <param name="options">Options used while validating the attestation token.</param>
        /// <param name="attestationSigningCertificates">Signing Certificates used to validate the token.</param>
        /// <param name="cancellationToken">Token used to cancel this operation if necessary.</param>
        /// <returns>true if the token was valid, false otherwise.</returns>
        /// <exception cref="ArgumentNullException">Thrown if the signing certificates provided are invalid.</exception>
        /// <exception cref="Exception">Thrown if validation fails.</exception>
        public virtual bool ValidateToken(AttestationTokenValidationOptions options, IReadOnlyList<AttestationSigner> attestationSigningCertificates, CancellationToken cancellationToken = default)
            => ValidateTokenInternal(options, attestationSigningCertificates, false, cancellationToken).EnsureCompleted();

        /// <summary>
        /// Validate a JSON Web Token returned by the MAA.
        /// <para/>
        /// If the caller provides a set of signers, than that set of signers will be used as the complete set of candidates for signing.
        /// If the caller does not provide a set of signers, then the <see cref="ValidateTokenAsync(AttestationTokenValidationOptions, IReadOnlyList{AttestationSigner}, CancellationToken)"/>
        /// API will a set of callers derived from the contents of the attestation token.
        /// </summary>
        /// <param name="options">Options used while validating the attestation token.</param>
        /// <param name="attestationSigningCertificates">Signing Certificates used to validate the token.</param>
        /// <param name="async">If true, execute the function asynchronously.</param>
        /// <param name="cancellationToken">Token used to cancel this operation if necessary.</param>
        /// <returns>true if the token was valid, false otherwise.</returns>
        /// <exception cref="ArgumentNullException">Thrown if the signing certificates provided are invalid.</exception>
        /// <exception cref="Exception">Thrown if validation fails.</exception>
        internal async Task<bool> ValidateTokenInternal(AttestationTokenValidationOptions options, IReadOnlyList<AttestationSigner> attestationSigningCertificates, bool async, CancellationToken cancellationToken = default)
        {
            // Early out if the caller doesn't want us to validate the token.
            if (!options.ValidateToken)
            {
                return true;
            }

            // Before we waste a lot of time, see if the token is unsecured. If it is, then validation is simple.
            if (Header.Algorithm.Equals("none", StringComparison.OrdinalIgnoreCase))
            {
                if (!ValidateCommonProperties(options))
                {
                    return false;
                }

#pragma warning disable AZC0110 // DO NOT use await keyword in possibly synchronous scope.
                return await CallValidationCallbackAsync(options, this, SigningCertificate, ClientDiagnostics, !async, cancellationToken).ConfigureAwait(false);
#pragma warning restore AZC0110 // DO NOT use await keyword in possibly synchronous scope.
            }

            // This token is a secured attestation token. If the caller provided signing certificates, then
            // they need to have provided at least one certificate.
            if (attestationSigningCertificates != null)
            {
                Argument.AssertNotNull(attestationSigningCertificates[0], nameof(attestationSigningCertificates));
            }

            AttestationSigner[] possibleCertificates = await GetCandidateSigningCertificatesInternalAsync(attestationSigningCertificates, async, cancellationToken).ConfigureAwait(false);
            if (possibleCertificates.Length == 0)
            {
                throw new Exception($"Unable to find any certificates which can be used to validate the token.");
            }

            if (!ValidateTokenSignature(possibleCertificates))
            {
                throw new Exception($"Could not find a certificate which was used to sign the token.");
            }

            if (!ValidateCommonProperties(options))
            {
                return false;
            }

#pragma warning disable AZC0110 // DO NOT use await keyword in possibly synchronous scope.
            return await CallValidationCallbackAsync(options, this, SigningCertificate, ClientDiagnostics, !async, cancellationToken).ConfigureAwait(false);
#pragma warning restore AZC0110 // DO NOT use await keyword in possibly synchronous scope.
        }

        private static async Task<bool> CallValidationCallbackAsync(AttestationTokenValidationOptions options, AttestationToken token, AttestationSigner signer, ClientDiagnostics diagnostics, bool isRunningSynchronously, CancellationToken cancellationToken)
        {
            return await options.RaiseValidationCallbackAsync(token, signer, diagnostics, isRunningSynchronously, cancellationToken).ConfigureAwait(false);
        }

        /// <summary>
        /// Returns a set of candidate signing certificates based on the contents of the token.
        /// </summary>
        /// <param name="attestationSigners">The desired set of signers for this token - if present, then this is the exclusive set of signers for the token.</param>
        /// <param name="async">true if this operation is asynchronous.</param>
        /// <param name="cancellationToken">Cancellation token used to cancel this operation.</param>
        /// <returns>The set of <see cref="AttestationSigner"/> which might be used to sign the attestation token.</returns>
        private async Task<AttestationSigner[]> GetCandidateSigningCertificatesInternalAsync(IReadOnlyList<AttestationSigner> attestationSigners, bool async, CancellationToken cancellationToken = default)
        {
            string desiredKeyId = Header.KeyId;

            if (cancellationToken.IsCancellationRequested)
            {
                throw new OperationCanceledException();
            }

            // Reference the async parameter. If/when JKU/X5U support is added, remove this.
            if (async)
            {
                await Task.Yield();
            }

            List<AttestationSigner> candidateCertificates = new List<AttestationSigner>();
            if (desiredKeyId != null)
            {
                // We start looking for candidate signers by looking in the provided attestation signers.
                // The list of attestation signers trumps
                foreach (var signer in attestationSigners)
                {
                    if (signer.CertificateKeyId == desiredKeyId)
                    {
                        candidateCertificates.Add(new AttestationSigner(signer.SigningCertificates.ToArray(), desiredKeyId));
                        break;
                    }
                }

                // We didn't find a certificate from the attestation signers. That means we need to look in the
                // Header to see if we can find one there.
                if (candidateCertificates.Count == 0)
                {
                    if (Header.JsonWebKey != null)
                    {
                        // If the key matches the header's JWK, then the signer is going to be the leaf certificate
                        // of the certificates in the chain.
                        if (desiredKeyId == Header.JsonWebKey.Kid)
                        {
                            candidateCertificates.Add(new AttestationSigner(
                                ConvertBase64CertificateArrayToCertificateChain(Header.JsonWebKey.X5C),
                                desiredKeyId));
                        }
                    }
                }
            }
            else
            {
                // We don't have a desired Key ID, so we need to try all the possible signing certificates :(.

                // Start with the attestationSigners provided by the caller. Remember that attestation signers
                // trump all signers in the header.
                if (attestationSigners != null)
                {
                    // Copy the signers to the candidate certificates.
                    //
                    // Note: because we don't have a KeyID, we neuter the KeyID in the AttestationSigner.
                    foreach (var signer in attestationSigners)
                    {
                        candidateCertificates.Add(new AttestationSigner(signer.SigningCertificates.ToArray(), null));
                    }
                }
                else
                {
                    if (Header.X509CertificateChain != null)
                    {
                        candidateCertificates.Add(new AttestationSigner(ConvertBase64CertificateArrayToCertificateChain(Header.X509CertificateChain), null));
                    }
                    if (Header.JsonWebKey != null)
                    {
                        candidateCertificates.Add(new AttestationSigner(ConvertBase64CertificateArrayToCertificateChain(Header.JsonWebKey.X5C), null));
                    }
                }
            }

            return candidateCertificates.ToArray();
        }

        private static X509Certificate2[] ConvertBase64CertificateArrayToCertificateChain(IList<string> base64certificates)
        {
            X509Certificate2[] jwkCertificates = new X509Certificate2[base64certificates.Count];
            int i = 0;
            foreach (var base64Cert in base64certificates)
            {
                jwkCertificates[i] = new X509Certificate2(Convert.FromBase64String(base64Cert));
                i += 1;
            }
            return jwkCertificates;
        }

        /// <summary>
        /// Iterate over the set of possible signers looking for a signer which can validate the signature
        /// </summary>
        /// <param name="possibleSigners">A set of attestation signers which might have been used to sign the token.</param>
        /// <returns>true if one of the possibleSigners signed the token, false otherwise</returns>
        private bool ValidateTokenSignature(AttestationSigner[] possibleSigners)
        {
            bool signatureValidated = false;
            AttestationSigner actualSigner = null;

            foreach (var signer in possibleSigners)
            {
                // The leaf certificate is defined as the certificate which signed the token, so we just need to look
                // at the first certificate in the chain.
                AsymmetricAlgorithm asymmetricAlgorithm = signer.SigningCertificates[0].PublicKey.Key;
                if (asymmetricAlgorithm is RSA rsaKey)
                {
                    signatureValidated = rsaKey.VerifyData(
                        Encoding.UTF8.GetBytes(Base64Url.Encode(this.TokenHeaderBytes.ToArray()) + "." + Base64Url.Encode(this.TokenBodyBytes.ToArray())),
                        this.TokenSignatureBytes.ToArray(),
                        HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
                    if (signatureValidated)
                    {
                        actualSigner = signer;
                    }
                    break;
                }
                else if (asymmetricAlgorithm is ECDsa ecdsa)
                {
                    signatureValidated = ecdsa.VerifyData(
                        Encoding.UTF8.GetBytes(Base64Url.Encode(this.TokenHeaderBytes.ToArray()) + "." + Base64Url.Encode(this.TokenBodyBytes.ToArray())),
                        this.TokenSignatureBytes.ToArray(),
                        HashAlgorithmName.SHA256);
                    if (signatureValidated)
                    {
                        actualSigner = signer;
                    }
                    break;
                }
            }

            if (actualSigner != null)
            {
                this.CertificateThumbprint = actualSigner.SigningCertificates[0].Thumbprint;
                this.SigningCertificate = actualSigner;
            }
            return actualSigner != null;
        }

        /// <summary>
        /// Validate the common properties of the attestation token - ensure that the expiration time for the token is "reasonable", if present.
        /// </summary>
        /// <returns>true if the common properties are valid.</returns>
        /// <exception cref="Exception">Thrown if the attestation token is not value.</exception>
        private bool ValidateCommonProperties(AttestationTokenValidationOptions options)
        {
            if ((options?.ValidateIssuer).GetValueOrDefault())
            {
                Argument.AssertNotNull(options?.ExpectedIssuer, nameof(options.ExpectedIssuer));
            }
            DateTimeOffset timeNow = DateTimeOffset.Now;
            if (Payload.ExpirationTime.HasValue && (options?.ValidateExpirationTime ?? true))
            {
                if (timeNow.CompareTo(ExpirationTime.Value) > 0)
                {
                    if (options?.TimeValidationSlack != 0)
                    {
                        var delta = timeNow.Subtract(ExpirationTime.Value);
                        if (delta.Seconds > options?.TimeValidationSlack)
                        {
                            throw new Exception("Attestation token expired before the slack time provided.");
                        }
                    }
                    else
                    {
                        throw new Exception("Attestation token expired.");
                    }
                }
            }

            if (Payload.NotBeforeTime.HasValue && (options?.ValidateNotBeforeTime?? true))
            {
                if (DateTimeOffset.Now.CompareTo(NotBeforeTime.Value) < 0)
                {
                    if (options?.TimeValidationSlack != 0)
                    {
                        var delta = timeNow.Subtract(NotBeforeTime.Value);
                        if (Math.Abs(delta.Seconds) > options?.TimeValidationSlack)
                        {
                            throw new Exception("Attestation token will not become valid before the slack time provided.");
                        }
                    }
                    else
                    {
                        throw new Exception("Attestation token is not yet valid.");
                    }
                }
            }

            if (Payload.Issuer != null && (options?.ValidateIssuer ?? true) && options?.ExpectedIssuer != null)
            {
                if (options?.ExpectedIssuer != Payload.Issuer)
                {
                    throw new Exception($"Unknown Issuer for attestation token. Expected {options.ExpectedIssuer}, found {Payload.Issuer}");
                }
            }
            return true;
        }

        /// <summary>
        /// Retrieves the body of the AttestationToken as the specified type.
        /// </summary>
        /// <typeparam name="T">Underlying type for the token body.</typeparam>
        /// <returns>Returns the body of the attestation token.</returns>
        public virtual T GetBody<T>()
            where T: class
        {
            lock (_statelock)
            {
                if (_deserializedBody == null || _deserializedBody.GetType() != typeof(T))
                {
                    _deserializedBody = JsonSerializer.Deserialize<T>(TokenBodyBytes.ToArray(), _serializerOptions);
                }
                return (T)_deserializedBody;
            }
        }

        /// <summary>
        /// Serializes the attestation token to a JSON Web Token
        /// </summary>
        /// <returns>The value of the AttestationToken as a JSON Web Token.</returns>
        public string Serialize()
        {
            return _token ?? GetType().Name;
        }

        /// <summary>
        /// Creates a new instance of the <see cref="AttestationToken"/> class based on a specified JSON Web Token.
        /// </summary>
        /// <param name="token">string JWT to Deserialize.</param>
        public static AttestationToken Deserialize(string token)
        {
            return new AttestationToken(token, null);
        }

        /// <summary>
        /// Creates a new instance of the <see cref="AttestationToken"/> class based on a specified JSON Web Token.
        /// </summary>
        /// <param name="token">string JWT to Deserialize.</param>
        /// <param name="diagnostics">Client Diagnostics object, used when raising Validation events.</param>
        internal static AttestationToken Deserialize(string token, ClientDiagnostics diagnostics)
        {
            return new AttestationToken(token, diagnostics);
        }

        /// <summary>
        /// Create an unsecured JSON Web Token based on the specified token body.
        /// </summary>
        /// <param name="body">Object to be embeeded as the body of the attestation token.</param>
        /// <returns>Returns an secured JWT whose body is the serialized value of the <paramref name="body"/> parameter.</returns>
        private static string CreateUnsecuredJwt(BinaryData body)
        {
            if (body != null)
            {
                JsonDocument parsedBody = JsonDocument.Parse(body);
                if (parsedBody.RootElement.ValueKind != JsonValueKind.Object)
                {
                    throw new ArgumentException("AttestationToken body must be a serialized JSON Object.");
                }
            }
            // Base64Url encoded '{"alg":"none"}'. See https://www.rfc-editor.org/rfc/rfc7515.html#appendix-A.5 for more information.
            string returnValue = "eyJhbGciOiJub25lIn0.";

            string encodedDocument;
            if (body != null)
            {
                encodedDocument = Base64Url.Encode(body.ToArray());
            }
            else
            {
                encodedDocument = string.Empty;
            }

            returnValue += encodedDocument;
            returnValue += ".";

            return returnValue;
        }

        /// <summary>
        /// Create a secured JSON Web Token based on the specified token body and the specified signing key.
        /// </summary>
        /// <param name="body">Object to be embeeded as the body of the attestation token.</param>
        /// <param name="signingKey">Key used to sign the attestation token.</param>
        /// <returns>Returns a secured JWT whose body is the serialized value of the <paramref name="body"/> parameter.</returns>
        private static string GenerateSecuredJsonWebToken(BinaryData body, AttestationTokenSigningKey signingKey)
        {
            if (body != null)
            {
                JsonDocument parsedBody = JsonDocument.Parse(body);
                if (parsedBody.RootElement.ValueKind != JsonValueKind.Object)
                {
                    throw new ArgumentException("AttestationToken body must be a serialized JSON Object.");
                }
            }

            Argument.AssertNotNull(signingKey, nameof(signingKey));

            AsymmetricAlgorithm signer;
            if (signingKey.Certificate.HasPrivateKey)
            {
                signer = signingKey.Certificate.PrivateKey;
            }
            else
            {
                signer = signingKey.Signer;
            }

            JsonWebTokenHeader header = new JsonWebTokenHeader
            {
                Algorithm = signingKey.Signer is RSA ? "RSA256" : (signingKey.Signer is ECDsa ? "ECDH256" : null),
                X509CertificateChain = new string[] { Convert.ToBase64String(signingKey.Certificate.Export(X509ContentType.Cert)) },
            };
            var serializationOptions = new JsonSerializerOptions
            {
                IgnoreNullValues = true,
            };
            byte[] jwtHeader = JsonSerializer.SerializeToUtf8Bytes<JsonWebTokenHeader>(header, serializationOptions);
            string encodedHeader = Base64Url.Encode(jwtHeader);

            byte[] jwtBody = null;
            if (body != null)
            {
                jwtBody = body.ToArray();
            }

            string encodedBody = jwtBody != null ? Base64Url.Encode(jwtBody) : string.Empty;

            string signedData = encodedHeader + '.' + encodedBody;

            byte[] signature;
            if (signingKey.Signer is RSA rsaKey)
            {
                signature = rsaKey.SignData(Encoding.UTF8.GetBytes(signedData), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
            }
            else if (signingKey.Signer is ECDsa ecdh)
            {
                signature = ecdh.SignData(Encoding.UTF8.GetBytes(signedData), HashAlgorithmName.SHA256);
            }
            else
            {
                throw new ArgumentException("Signing Key must be either RSA or ECDsa. Unknown signing key found");
            }
            string jwt = signedData + '.' + Base64Url.Encode(signature);

            return jwt;
        }
    }
}


text:
import pygame_toolbox.tilegame_tools as pttt
import pygame, sys

# Create a custom Tile class
class Tile(pttt.Tile):
    def __init__(self,file,size):
        # Pass the file and size arguments to the init of the class
        # in the toolbox
        pttt.Tile.__init__(self,file,size)
        # Add a shade to the tiles shade dictionary
        pttt.Tile.initialize_shade(self,"orange red",(255,69,0),150)

# Create the tilemap object that will handle displaying all of the
# tiles
class Tilemap(pttt.Tilemap):
    def __init__(self,tilelist):
        pttt.Tilemap.__init__(self,(800,600),tilelist,1)

class Main:
    def __init__(self):
        # Create a 4x4 matrix of tiles and use the tilelist class to
        # obtain additional functionality for the matrix.
        self.tilelist = pttt.Tilelist([[Tile('forrest.png',(200,150)) for i in range(0,4)],
                                       [Tile('forrest.png',(200,150)) for i in range(0,4)],
                                       [Tile('forrest.png',(200,150)) for i in range(0,4)],
                                       [Tile('forrest.png',(200,150)) for i in range(0,4)]])
        self.clock = pygame.time.Clock()
        self.progress = 1
    def update(self,screen):
        while True:
            if self.progress == 1:
                # When a tile is clicked shade the 4 tiles around it
                # using the orange red shade we created
                tileclicked = Tilemap(self.tilelist).update(screen,self.clock)
                adjtiles = self.tilelist.adjacent_tiles(tileclicked,'p')
                for i in adjtiles:
                    i.toggle_shade("orange red")

# Initialize pygame and run the script.
if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800,600))
    Main().update(screen)

redpajama_set_name: RedPajamaGithub


text:
import structlog
from lxml import etree
import netconf.nc_common.error as ncerror

log = structlog.get_logger()

class RpcResponse():
    def __init__(self, capabilities):
        self.is_error = False
        # if there is an error then the reply_node will contains an Error
        # object
        self.reply_node = None
        self.close_session = False
        self.capabilities = capabilities
        self.custom_rpc = False

    def build_xml_response(self, request, voltha_response, custom_rpc=False):
        if request is None:
            return
        voltha_xml_string = etree.tostring(voltha_response)

        # Remove the leading and trailing <yang> tags
        if voltha_xml_string.startswith('<yang>'):
            voltha_xml_string = voltha_xml_string[len('<yang>'):]
            if voltha_xml_string.endswith('</yang>'):
                voltha_xml_string = voltha_xml_string[:-len('</yang>')]
        # Empty response
        elif voltha_xml_string.startswith('<yang/>'):
            voltha_xml_string = ''

        # Replace any True/False data to true/false
        voltha_xml_string = voltha_xml_string.replace('>False<', '>false<')
        voltha_xml_string = voltha_xml_string.replace('>True<', '>true<')

        if not custom_rpc:
            # Create the xml body as
            if request.has_key('subclass'):
                body = ''.join([
                    '<data>',
                    '<',
                    request['class'],
                    ' xmlns="',
                    request['namespace'],
                    '">',
                    '<',
                    request['subclass'],
                    '>',
                    voltha_xml_string,
                    '</',
                    request['subclass'],
                    '>',
                    '</',
                    request['class'],
                    '>',
                    '</data>'
                ])
            else:
                body = ''.join([
                    '<data>',
                    '<',
                    request['class'],
                    ' xmlns="urn:opencord:params:xml:ns:voltha:voltha">',
                    voltha_xml_string,
                    '</',
                    request['class'],
                    '>',
                    '</data>'
                ])
        else:  # custom_rpc
            body = ''.join([
                '<rpc-reply',
                ' xmlns="urn:opencord:params:xml:ns:voltha:voltha">',
                voltha_xml_string,
                '</rpc-reply>',
            ])

        return etree.fromstring(body)

    def add_node(self, new_node, tree):
        if new_node.tag == 'ignore':
            # We want only sub-elements
            for elem in list(new_node):
                tree.append(elem)
        else:
            tree.append(new_node)

    def copy_basic_element(self, elm):
        new_elem = etree.Element(elm.tag)
        new_elem.text = elm.text
        return new_elem

    def process_inline_option(self, elem):
        inline_option = False
        inline_node_name = None
        for elm in list(elem):
            if elm.tag == 'yang_field_option':
                inline_option = True
            if elm.tag == 'name':
                inline_node_name = elm.text
        if not inline_option:
            new_elem = etree.Element(elem.tag)
            return new_elem, elem

        # look for the node with the inline_node_name
        for elm in list(elem):
            if elm.tag == inline_node_name:
                new_elem = etree.Element('ignore')
                return new_elem, elm

    def process_element(self, elem):
        attrib = elem.get('type')
        if (attrib == 'list'):
            if list(elem) is None:
                return self.copy_basic_element(elem)
            new_elem = etree.Element('ignore')
            for elm in list(elem):
                elm.tag = elem.tag
                if elm.get('type') in ['list', 'dict']:
                    self.add_node(self.process_element(elm), new_elem)
                else:
                    new_elem.append(self.copy_basic_element(elm))
            return new_elem
        elif (attrib == 'dict'):
            # Empty case
            if list(elem) is None:
                return self.copy_basic_element(elem)

            # Process field option.
            new_elem, elem = self.process_inline_option(elem)

            for elm in list(elem):
                if elm.get('type') in ['list', 'dict']:
                    self.add_node(self.process_element(elm), new_elem)
                else:
                    new_elem.append(self.copy_basic_element(elm))
            return new_elem
        else:
            return self.copy_basic_element(elem)

    def to_yang_xml(self, from_xml, request, yang_options=None,
                    custom_rpc=False):
        # Parse from_xml as follows:
        # 1.  Any element having a list attribute shoud have each item move 1 level
        #     up and retag using the parent tag
        # 2.  Any element having a dict attribute and has a <yang_field_option>
        #     sub-element should have all it's items move to teh parent level
        top = etree.Element('yang')
        elms = list(from_xml)
        xml_tag = yang_options[0]
        list_items_name = yang_options[1]
        # Handle the special case when the the xml contains 1 element which
        # is a list type
        if len(elms) == 1:
            item = elms[0]
            if item.get('type') == 'list':
                if custom_rpc:  # custom rpc request
                    if list_items_name == 'items': # no pre-processing required
                        self.add_node(self.process_element(item), top)
                        return top
                    if xml_tag:
                        item.tag = xml_tag
                    else:
                        item.tag = 'ignore'
                else:
                    # Default netconf operations - may end up needing
                    # specific parsing per request type
                    if request.has_key('subclass'):
                        item.tag = request['subclass']
                        # remove the subclass element in request to avoid duplicate tag
                        del request['subclass']
                    elif list_items_name == 'items':
                        item.tag = xml_tag
                    else:
                        item.tag = 'ignore'
                self.add_node(self.process_element(item), top)
                return top

        # Process normally for all other cases
        for elm in elms:
            self.add_node(self.process_element(elm), top)

        return top

    # Helper method to sort the xml message based on the xml tags
    def sort_xml_response(self, xml):
        for parent in xml.xpath('//*[./*]'):  # Search for parent elements
            parent[:] = sorted(parent, key=lambda x: x.tag)
        return xml

    # custom_rpc refers to custom RPCs different from Netconf default RPCs
    # like get, get-config, edit-config, etc
    def build_yang_response(self, root, request, yang_options=None,
                            custom_rpc=False):
        try:
            self.custom_rpc = custom_rpc
            yang_xml = self.to_yang_xml(root, request, yang_options,
                                        custom_rpc)
            log.info('yang-xml', yang_xml=etree.tounicode(yang_xml,
                                                          pretty_print=True))
            return self.build_xml_response(request, yang_xml, custom_rpc)
        except Exception as e:
            log.exception('error-building-yang-response', request=request,
                          xml=etree.tostring(root))
            self.rpc_response.is_error = True
            self.rpc_response.node = ncerror.BadMsg(request)
            return


text:
package org.apache.druid.server.log;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.java.util.common.logger.Logger;
import org.apache.druid.query.DataSource;
import org.apache.druid.query.Query;
import org.apache.druid.query.QueryDataSource;
import org.apache.druid.query.TableDataSource;
import org.apache.druid.query.UnionDataSource;
import org.apache.druid.server.RequestLogLine;
import org.slf4j.MDC;

import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;

public class LoggingRequestLogger implements RequestLogger
{
  private static final Logger LOG = new Logger(LoggingRequestLogger.class);

  private final ObjectMapper mapper;
  private final boolean setMDC;
  private final boolean setContextMDC;

  public LoggingRequestLogger(
      ObjectMapper mapper,
      boolean setMDC,
      boolean setContextMDC
  )
  {
    this.mapper = mapper;
    this.setMDC = setMDC;
    this.setContextMDC = setContextMDC;
  }

  @Override
  public void logNativeQuery(RequestLogLine requestLogLine) throws IOException
  {
    final Map mdc = MDC.getCopyOfContextMap();
    // MDC must be set during the `LOG.info` call at the end of the try block.
    try {
      if (setMDC) {
        try {
          final Query query = requestLogLine.getQuery();
          MDC.put("queryId", query.getId());
          MDC.put("sqlQueryId", StringUtils.nullToEmptyNonDruidDataString(query.getSqlQueryId()));
          MDC.put("dataSource", findInnerDatasource(query).toString());
          MDC.put("queryType", query.getType());
          MDC.put("isNested", String.valueOf(!(query.getDataSource() instanceof TableDataSource)));
          MDC.put("hasFilters", Boolean.toString(query.hasFilters()));
          MDC.put("remoteAddr", requestLogLine.getRemoteAddr());
          MDC.put("duration", query.getDuration().toString());
          MDC.put("descending", Boolean.toString(query.isDescending()));
          if (setContextMDC) {
            final Iterable<Map.Entry<String, Object>> entries = query.getContext() == null
                                                                ? ImmutableList.of()
                                                                : query.getContext().entrySet();
            for (Map.Entry<String, Object> entry : entries) {
              MDC.put(entry.getKey(), entry.getValue() == null ? "NULL" : entry.getValue().toString());
            }
          }
        }
        catch (RuntimeException re) {
          LOG.error(re, "Error preparing MDC");
        }
      }
      final String line = requestLogLine.getNativeQueryLine(mapper);

      // MDC must be set here
      LOG.info("%s", line);
    }
    finally {
      if (setMDC) {
        if (mdc != null) {
          MDC.setContextMap(mdc);
        } else {
          MDC.clear();
        }
      }
    }
  }

  @Override
  public void logSqlQuery(RequestLogLine requestLogLine) throws IOException
  {
    final String line = requestLogLine.getSqlQueryLine(mapper);
    LOG.info("%s", line);
  }

  public boolean isSetMDC()
  {
    return setMDC;
  }

  public boolean isSetContextMDC()
  {
    return setContextMDC;
  }

  private Object findInnerDatasource(Query query)
  {
    DataSource _ds = query.getDataSource();
    if (_ds instanceof TableDataSource) {
      return ((TableDataSource) _ds).getName();
    }
    if (_ds instanceof QueryDataSource) {
      return findInnerDatasource(((QueryDataSource) _ds).getQuery());
    }
    if (_ds instanceof UnionDataSource) {
      return Joiner.on(",")
                   .join(
                       ((UnionDataSource) _ds)
                           .getDataSources()
                           .stream()
                           .map(TableDataSource::getName)
                           .collect(Collectors.toList())
                   );
    } else {
      // should not come here
      return query.getDataSource();
    }
  }

  @Override
  public String toString()
  {
    return "LoggingRequestLogger{" +
           "setMDC=" + setMDC +
           ", setContextMDC=" + setContextMDC +
           '}';
  }
}


text:
var express = require('express');
var router = express.Router();
var collector = require('../libs/collector.js');
var async = require('async');
var reddit = require('snoocore');

/* GET users listing. */
router.get('/:userName', function(req, res, next) {

    var userName = req.params.userName;
    console.log('user: ' + userName);
    /*
    call all collectors in parallel.
    then gather them into fields & send to view
     */

    //perform name verification here

    async.parallel([

        //gathers comments
        function (callback) {
            parallelDevice(userName, 'comments', function (err, res) {
                callback(err, res)
            })
        },

        //gathers submissions
        function (callback) {
            parallelDevice(userName, 'submitted', function (err, res) {
                callback(err, res)
            })
        }

    ], function(err, results) {

        console.log('after parallel');
        var obj;

        //checks for true error status of all calls made
        function isError(elem, index, array) {
            return elem[0] == true;
        }

        if (results.some(isError)) {
            var errorString = results[0][1] + '\n' + results[1][1]

            obj = {
                error: true,
                info: errorString
            }

        } else {
            var date = getDateTime();
            var coms = results[0][1];
            var subs = results[1][1];

            obj = {
                error: false,
                time: date,
                comments: coms,
                submissions: subs
            }
        }

        //offfff to the view!
        res.json(obj);
    })

});

function parallelDevice(userName, type, callback) {
    console.log('enter ' + type + ' collector');

    collector(userName, type, function (errorStatus, data) {
        console.log('left ' + type + ' collector');
        //console.log(errorStatus)
        //console.log(data)

        var arr = [errorStatus, data];
        callback(null, arr);
    });

}

function getDateTime() {
  var now     = new Date();
  var year    = now.getFullYear();
  var month   = now.getMonth()+1;
  var day     = now.getDate();
  var hour    = now.getHours();
  var minute  = now.getMinutes();
  var second  = now.getSeconds();
  if(month.toString().length == 1) {
      var month = '0'+month;
  }
  if(day.toString().length == 1) {
      var day = '0'+day;
  }
  if(hour.toString().length == 1) {
      var hour = '0'+hour;
  }
  if(minute.toString().length == 1) {
      var minute = '0'+minute;
  }
  if(second.toString().length == 1) {
      var second = '0'+second;
  }
  var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;
  console.log('THE DATE: ' + dateTime);
  return dateTime;
}

module.exports = router;


text:
<?php namespace Rancherize\Blueprint\PublishUrls\PublishUrlsYamlWriter\PartWriter\Traefik;

use Rancherize\Blueprint\PublishUrls\PublishUrlsExtraInformation\PublishUrlsExtraInformation;
use Rancherize\Blueprint\PublishUrls\PublishUrlsYamlWriter\PartWriter\PartWriter;

/**
 * Class TraefikDomainWriter
 * @package Rancherize\Blueprint\PublishUrls\PublishUrlsYamlWriter\PartWriter\Traefik
 */
class TraefikDomainWriter implements PartWriter {

	/**
	 * @param PublishUrlsExtraInformation $extraInformation
	 * @param array $dockerService
	 */
	public function write( PublishUrlsExtraInformation $extraInformation, array &$dockerService ) {
		$url = $extraInformation->getUrl();
		$fullPath = parse_url($url, PHP_URL_HOST);

		$matches = [];
		if( preg_match('/[^\.]*/', $fullPath, $matches) !== 1 )
			return;
		$hostname = $matches[0];

		$domainName = substr($fullPath, strlen($hostname) + 1);

		$dockerService['labels']['traefik.domain'] = $domainName;
	}
}


text:
using System;
using System.Data.SqlClient;
using System.IO;

namespace Get_Villains_Names
{
    public class Startup
    {
        public static void Main()
        {
            var sqlConnection = new SqlConnection("Server=LETUM;Integrated Security=true;");
            sqlConnection.Open();

            var querry = File.ReadAllText("../../VillainsWhitMoreThanOneMinion.sql");
            var getVillainsCommand = new SqlCommand(querry, sqlConnection);

            using (sqlConnection)
            {
                var reader = getVillainsCommand.ExecuteReader();

                using (reader)
                {
                    while (true)
                    {
                        if (!reader.Read())
                        {
                            break;
                        }

                        var villainName = reader["Name"].ToString();
                        var minionsCount = int.Parse(reader["MinionsCount"].ToString());

                        Console.WriteLine($"{villainName} {minionsCount}");
                    }
                }
            }
        }
    }
}


text:
local SpatialDropout, Parent = torch.class('nn.SpatialDropout', 'nn.Module')

function SpatialDropout:__init(p)
   Parent.__init(self)
   self.p = p or 0.5
   self.train = true
   self.noise = torch.Tensor()
end

function SpatialDropout:updateOutput(input)
   self.output:resizeAs(input):copy(input)
   if self.train then
      if input:dim() == 4 then
        self.noise:resize(input:size(1), input:size(2), 1, 1)
      elseif input:dim() == 3 then
        self.noise:resize(input:size(1), 1, 1)
      else
        error('Input must be 4D (nbatch, nfeat, h, w) or 3D (nfeat, h, w)')
      end
      self.noise:bernoulli(1-self.p)
      -- We expand the random dropouts to the entire feature map because the
      -- features are likely correlated accross the map and so the dropout
      -- should also be correlated.
      self.output:cmul(torch.expandAs(self.noise, input))
   else
      self.output:mul(1-self.p)
   end
   return self.output
end

function SpatialDropout:updateGradInput(input, gradOutput)
   if self.train then
      self.gradInput:resizeAs(gradOutput):copy(gradOutput)
      self.gradInput:cmul(torch.expandAs(self.noise, input)) -- simply mask the gradients with the noise vector
   else
      error('backprop only defined while training')
   end
   return self.gradInput
end

function SpatialDropout:setp(p)
   self.p = p
end

function SpatialDropout:__tostring__()
  return string.format('%s(%f)', torch.type(self), self.p)
end


text:
//
// Copyright 2019 ZetaSQL Authors
//
// Licensed 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.
//

#include "zetasql/public/table_valued_function.h"

#include "zetasql/base/testing/status_matchers.h"
#include "zetasql/proto/function.pb.h"
#include "zetasql/public/type.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "zetasql/base/statusor.h"

namespace zetasql {

void ExpectEqualTVFSchemaColumn(const TVFSchemaColumn& column1,
                                const TVFSchemaColumn& column2) {
  EXPECT_EQ(column1.name, column2.name);
  EXPECT_EQ(column1.is_pseudo_column, column2.is_pseudo_column);
  EXPECT_TRUE(column1.type->Equals(column2.type));
  EXPECT_EQ(column1.name_parse_location_range,
            column2.name_parse_location_range);
  EXPECT_EQ(column1.type_parse_location_range,
            column2.type_parse_location_range);
}

void ExpectEqualTVFRelations(const TVFRelation& relation1,
                             const TVFRelation& relation2) {
  EXPECT_EQ(relation1.is_value_table(), relation2.is_value_table());
  ASSERT_EQ(relation1.num_columns(), relation2.num_columns());
  for (int i = 0; i < relation1.num_columns(); ++i) {
    ExpectEqualTVFSchemaColumn(relation1.column(i), relation2.column(i));
  }
}

// Serializes given TVFRelation first. Then deserializes and returns the
// deserialized TVFRelation.
void SerializeDeserializeAndCompare(const TVFRelation& relation) {
  FileDescriptorSetMap file_descriptor_set_map;
  TVFRelationProto tvf_relation_proto;
  ZETASQL_ASSERT_OK(relation.Serialize(&file_descriptor_set_map, &tvf_relation_proto));

  std::vector<const google::protobuf::DescriptorPool*> pools(
      file_descriptor_set_map.size());
  for (const auto& pair : file_descriptor_set_map) {
    pools[pair.second->descriptor_set_index] = pair.first;
  }

  TypeFactory type_factory;
  ZETASQL_ASSERT_OK_AND_ASSIGN(
      TVFRelation result,
      TVFRelation::Deserialize(tvf_relation_proto, pools, &type_factory));
  ExpectEqualTVFRelations(relation, result);
}

// Serializes given TVFSchemaColumn first. Then deserializes and returns the
// deserialized TVFSchemaColumn.
void SerializeDeserializeAndCompare(const TVFSchemaColumn& column) {
  FileDescriptorSetMap file_descriptor_set_map;
  ZETASQL_ASSERT_OK_AND_ASSIGN(TVFRelationColumnProto tvf_schema_column,
                       column.ToProto(&file_descriptor_set_map));

  std::vector<const google::protobuf::DescriptorPool*> pools(
      file_descriptor_set_map.size());
  for (const auto& pair : file_descriptor_set_map) {
    pools[pair.second->descriptor_set_index] = pair.first;
  }

  TypeFactory type_factory;
  ZETASQL_ASSERT_OK_AND_ASSIGN(
      TVFSchemaColumn result,
      TVFSchemaColumn::FromProto(tvf_schema_column, pools, &type_factory));

  ExpectEqualTVFSchemaColumn(column, result);
}

// Check serialization and deserialization of TVFRelation.
TEST(TVFTest, TVFRelationSerializationAndDeserialization) {
  TVFRelation::Column column("Col1", zetasql::types::DoubleType());
  TVFRelation relation({column});

  SerializeDeserializeAndCompare(relation);
}

TEST(TVFTest, TVFRelationSerializationAndDeserializationWithColumnLocations) {
  ParseLocationRange location_range1, location_range2;
  location_range1.set_start(ParseLocationPoint::FromByteOffset("file1", 17));
  location_range1.set_end(ParseLocationPoint::FromByteOffset("file1", 25));

  location_range2.set_start(ParseLocationPoint::FromByteOffset("file1", 29));
  location_range2.set_end(ParseLocationPoint::FromByteOffset("file1", 32));

  TVFRelation::Column column("Col1", zetasql::types::DoubleType());
  // Set parse locations for TVFSchema Column.
  column.name_parse_location_range = location_range1;
  column.type_parse_location_range = location_range2;
  TVFRelation relation({column});

  SerializeDeserializeAndCompare(relation);
}

// Check serialization and deserialization of TVFSchemaColumn
TEST(TVFTest,
     TVFSchemaColumnSerializationAndDeserializationWithColumnLocations) {
  ParseLocationRange location_range1, location_range2;
  location_range1.set_start(ParseLocationPoint::FromByteOffset("file1", 17));
  location_range1.set_end(ParseLocationPoint::FromByteOffset("file1", 25));

  location_range2.set_start(ParseLocationPoint::FromByteOffset("file1", 29));
  location_range2.set_end(ParseLocationPoint::FromByteOffset("file1", 32));

  TVFRelation::Column column("Col1", zetasql::types::DoubleType());
  // Set parse locations for TVFSchema Column.
  column.name_parse_location_range = location_range1;
  column.type_parse_location_range = location_range2;

  SerializeDeserializeAndCompare(column);
}

TEST(TVFTest, TestInvalidColumnNameForTVFWithExtraColumns) {
  TypeFactory factory;
  std::unique_ptr<TableValuedFunction> tvf;

  EXPECT_DEATH(
      tvf.reset(new ForwardInputSchemaToOutputSchemaWithAppendedColumnTVF(
          {"tvf_append_column_empty_name"},
          FunctionSignature(ARG_TYPE_RELATION, {ARG_TYPE_RELATION}, -1),
          {TVFSchemaColumn("", zetasql::types::Int64Type())})),
      "invalid empty column name in extra columns");
}

TEST(TVFTest, TestDuplicateColumnNameForTVFWithExtraColumns) {
  TypeFactory factory;
  std::unique_ptr<TableValuedFunction> tvf;
  TVFSchemaColumn int64_col =
      TVFSchemaColumn("int64_col", zetasql::types::Int64Type());

  EXPECT_DEATH(
      tvf.reset(new ForwardInputSchemaToOutputSchemaWithAppendedColumnTVF(
          {"tvf_append_column_with_duplicated_names"},
          FunctionSignature(ARG_TYPE_RELATION, {ARG_TYPE_RELATION}, -1),
          {int64_col, int64_col})),
      "extra columns have duplicated column names: int64_col");
}

TEST(TVFTest, TestInvalidNonTemplatedArgumentForTVFWithExtraColumns) {
  TypeFactory factory;
  std::unique_ptr<TableValuedFunction> tvf;
  // Generate an output schema that returns an int64_t value table.
  TVFRelation output_schema_int64_value_table =
      TVFRelation::ValueTable(zetasql::types::Int64Type());
  TVFSchemaColumn int64_col =
      TVFSchemaColumn("int64_col", zetasql::types::Int64Type());
  TVFRelation::ColumnList columns = {int64_col};
  TVFRelation tvf_relation(columns);

  EXPECT_DEATH(
      tvf.reset(new ForwardInputSchemaToOutputSchemaWithAppendedColumnTVF(
          {"tvf_append_column_with_value_table"},
          FunctionSignature(
              FunctionArgumentType::RelationWithSchema(
                  tvf_relation,
                  /*extra_relation_input_columns_allowed=*/false),
              {FunctionArgumentType::RelationWithSchema(
                  output_schema_int64_value_table,
                  /*extra_relation_input_columns_allowed=*/false)},
              -1),
          {int64_col})),
      "Does not support non-templated argument type");
}

TEST(TVFTest, TestInvalidConcreteSignatureTVFWithExtraColumns) {
  TypeFactory factory;
  std::unique_ptr<TableValuedFunction> tvf;
  TVFRelation::Column int64_col =
      TVFSchemaColumn("int64_col", zetasql::types::Int64Type());
  TVFRelation::ColumnList columns = {int64_col};
  TVFRelation tvf_relation(columns);
  FunctionArgumentType arg_type = FunctionArgumentType::RelationWithSchema(
      tvf_relation, /*extra_relation_input_columns_allowed=*/false);

  EXPECT_DEATH(
      tvf.reset(new ForwardInputSchemaToOutputSchemaWithAppendedColumnTVF(
          {"tvf_append_column_input_table_has_concrete_signature"},
          FunctionSignature(arg_type, {arg_type}, -1), {int64_col})),
      "Does not support non-templated argument type");
}

TEST(TVFTest, TestPseudoColumnForTVFWithExtraColumns) {
  TypeFactory factory;
  std::unique_ptr<TableValuedFunction> tvf;
  TVFSchemaColumn pseudo_column =
      TVFSchemaColumn("pseudo_column", zetasql::types::Int64Type(), true);

  EXPECT_DEATH(
      tvf.reset(new ForwardInputSchemaToOutputSchemaWithAppendedColumnTVF(
          {"tvf_append_pseudo_column"},
          FunctionSignature(ARG_TYPE_RELATION, {ARG_TYPE_RELATION}, -1),
          {pseudo_column})),
      "extra columns cannot be pseudo column");
}

TEST(TVFTest, TestInputTableWithPseudoColumnForTVFWithExtraColumns) {
  TypeFactory factory;
  std::unique_ptr<TableValuedFunction> tvf;
  TVFRelation::Column int64_col =
      TVFSchemaColumn("int64_col", zetasql::types::Int64Type());
  TVFRelation::Column double_col =
      TVFSchemaColumn("double_col", zetasql::types::DoubleType());
  TVFRelation::Column pseudo_column =
      TVFSchemaColumn("pseudo_column", zetasql::types::Int64Type(), true);
  TVFRelation::ColumnList columns = {int64_col, pseudo_column};
  TVFRelation tvf_relation(columns);
  FunctionArgumentType arg_type = FunctionArgumentType::RelationWithSchema(
      tvf_relation, /*extra_relation_input_columns_allowed=*/false);

  EXPECT_DEATH(
      tvf.reset(new ForwardInputSchemaToOutputSchemaWithAppendedColumnTVF(
          {"tvf_append_pseudo_column"},
          FunctionSignature(ARG_TYPE_RELATION, {arg_type}, -1), {double_col})),
      "Does not support non-templated argument type");
}
}  // namespace zetasql


text:
from __future__ import with_statement, absolute_import

from django.contrib import admin
from django.contrib.admin.options import IncorrectLookupParameters
from django.contrib.admin.views.main import ChangeList, SEARCH_VAR, ALL_VAR
from django.contrib.auth.models import User
from django.template import Context, Template
from django.test import TestCase
from django.test.client import RequestFactory

from .admin import (ChildAdmin, QuartetAdmin, BandAdmin, ChordsBandAdmin,
    GroupAdmin, ParentAdmin, DynamicListDisplayChildAdmin,
    DynamicListDisplayLinksChildAdmin, CustomPaginationAdmin,
    FilteredChildAdmin, CustomPaginator, site as custom_site,
    SwallowAdmin)
from .models import (Child, Parent, Genre, Band, Musician, Group, Quartet,
    Membership, ChordsMusician, ChordsBand, Invitation, Swallow,
    UnorderedObject, OrderedObject)

class ChangeListTests(TestCase):
    urls = "regressiontests.admin_changelist.urls"

    def setUp(self):
        self.factory = RequestFactory()

    def _create_superuser(self, username):
        return User.objects.create(username=username, is_superuser=True)

    def _mocked_authenticated_request(self, url, user):
        request = self.factory.get(url)
        request.user = user
        return request

    def test_select_related_preserved(self):
        """
        Regression test for #10348: ChangeList.get_query_set() shouldn't
        overwrite a custom select_related provided by ModelAdmin.queryset().
        """
        m = ChildAdmin(Child, admin.site)
        request = self.factory.get('/child/')
        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
                m.list_filter, m.date_hierarchy, m.search_fields,
                m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m)
        self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}})

    def test_result_list_empty_changelist_value(self):
        """
        Regression test for #14982: EMPTY_CHANGELIST_VALUE should be honored
        for relationship fields
        """
        new_child = Child.objects.create(name='name', parent=None)
        request = self.factory.get('/child/')
        m = ChildAdmin(Child, admin.site)
        list_display = m.get_list_display(request)
        list_display_links = m.get_list_display_links(request, list_display)
        cl = ChangeList(request, Child, list_display, list_display_links,
                m.list_filter, m.date_hierarchy, m.search_fields,
                m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m)
        cl.formset = None
        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
        context = Context({'cl': cl})
        table_output = template.render(context)
        row_html = '

name	(None)

' % new_child.id
        self.assertFalse(table_output.find(row_html) == -1,
            'Failed to find expected row element: %s' % table_output)

    def test_result_list_html(self):
        """
        Verifies that inclusion tag result_list generates a table when with
        default ModelAdmin settings.
        """
        new_parent = Parent.objects.create(name='parent')
        new_child = Child.objects.create(name='name', parent=new_parent)
        request = self.factory.get('/child/')
        m = ChildAdmin(Child, admin.site)
        list_display = m.get_list_display(request)
        list_display_links = m.get_list_display_links(request, list_display)
        cl = ChangeList(request, Child, list_display, list_display_links,
                m.list_filter, m.date_hierarchy, m.search_fields,
                m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m)
        cl.formset = None
        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
        context = Context({'cl': cl})
        table_output = template.render(context)
        row_html = '

name	Parent object

' % new_child.id
        self.assertFalse(table_output.find(row_html) == -1,
            'Failed to find expected row element: %s' % table_output)

    def test_result_list_editable_html(self):
        """
        Regression tests for #11791: Inclusion tag result_list generates a
        table and this checks that the items are nested within the table
        element tags.
        Also a regression test for #13599, verifies that hidden fields
        when list_editable is enabled are rendered in a div outside the
        table.
        """
        new_parent = Parent.objects.create(name='parent')
        new_child = Child.objects.create(name='name', parent=new_parent)
        request = self.factory.get('/child/')
        m = ChildAdmin(Child, admin.site)

        # Test with list_editable fields
        m.list_display = ['id', 'name', 'parent']
        m.list_display_links = ['id']
        m.list_editable = ['name']
        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
                m.list_filter, m.date_hierarchy, m.search_fields,
                m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m)
        FormSet = m.get_changelist_formset(request)
        cl.formset = FormSet(queryset=cl.result_list)
        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
        context = Context({'cl': cl})
        table_output = template.render(context)
        # make sure that hidden fields are in the correct place
        hiddenfields_div = '
<input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" />
' % new_child.id
        self.assertFalse(table_output.find(hiddenfields_div) == -1,
            'Failed to find hidden fields in: %s' % table_output)
        # make sure that list editable fields are rendered in divs correctly
        editable_name_field = '<input name="form-0-name" value="name" class="vTextField" maxlength="30" type="text" id="id_form-0-name" />'
        self.assertFalse('%s	' % editable_name_field == -1,
            'Failed to find "name" list_editable field in: %s' % table_output)

    def test_result_list_editable(self):
        """
        Regression test for #14312: list_editable with pagination
        """

        new_parent = Parent.objects.create(name='parent')
        for i in range(200):
            new_child = Child.objects.create(name='name %s' % i, parent=new_parent)
        request = self.factory.get('/child/', data={'p': -1})  # Anything outside range
        m = ChildAdmin(Child, admin.site)

        # Test with list_editable fields
        m.list_display = ['id', 'name', 'parent']
        m.list_display_links = ['id']
        m.list_editable = ['name']
        self.assertRaises(IncorrectLookupParameters, lambda: \
            ChangeList(request, Child, m.list_display, m.list_display_links,
                    m.list_filter, m.date_hierarchy, m.search_fields,
                    m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m))

    def test_custom_paginator(self):
        new_parent = Parent.objects.create(name='parent')
        for i in range(200):
            new_child = Child.objects.create(name='name %s' % i, parent=new_parent)

        request = self.factory.get('/child/')
        m = CustomPaginationAdmin(Child, admin.site)

        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
                m.list_filter, m.date_hierarchy, m.search_fields,
                m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m)

        cl.get_results(request)
        self.assertIsInstance(cl.paginator, CustomPaginator)

    def test_distinct_for_m2m_in_list_filter(self):
        """
        Regression test for #13902: When using a ManyToMany in list_filter,
        results shouldn't apper more than once. Basic ManyToMany.
        """
        blues = Genre.objects.create(name='Blues')
        band = Band.objects.create(name='B.B. King Review', nr_of_members=11)

        band.genres.add(blues)
        band.genres.add(blues)

        m = BandAdmin(Band, admin.site)
        request = self.factory.get('/band/', data={'genres': blues.pk})

        cl = ChangeList(request, Band, m.list_display,
                m.list_display_links, m.list_filter, m.date_hierarchy,
                m.search_fields, m.list_select_related, m.list_per_page,
                m.list_max_show_all, m.list_editable, m)

        cl.get_results(request)

        # There's only one Group instance
        self.assertEqual(cl.result_count, 1)

    def test_distinct_for_through_m2m_in_list_filter(self):
        """
        Regression test for #13902: When using a ManyToMany in list_filter,
        results shouldn't apper more than once. With an intermediate model.
        """
        lead = Musician.objects.create(name='Vox')
        band = Group.objects.create(name='The Hype')
        Membership.objects.create(group=band, music=lead, role='lead voice')
        Membership.objects.create(group=band, music=lead, role='bass player')

        m = GroupAdmin(Group, admin.site)
        request = self.factory.get('/group/', data={'members': lead.pk})

        cl = ChangeList(request, Group, m.list_display,
                m.list_display_links, m.list_filter, m.date_hierarchy,
                m.search_fields, m.list_select_related, m.list_per_page,
                m.list_max_show_all, m.list_editable, m)

        cl.get_results(request)

        # There's only one Group instance
        self.assertEqual(cl.result_count, 1)

    def test_distinct_for_inherited_m2m_in_list_filter(self):
        """
        Regression test for #13902: When using a ManyToMany in list_filter,
        results shouldn't apper more than once. Model managed in the
        admin inherits from the one that defins the relationship.
        """
        lead = Musician.objects.create(name='John')
        four = Quartet.objects.create(name='The Beatles')
        Membership.objects.create(group=four, music=lead, role='lead voice')
        Membership.objects.create(group=four, music=lead, role='guitar player')

        m = QuartetAdmin(Quartet, admin.site)
        request = self.factory.get('/quartet/', data={'members': lead.pk})

        cl = ChangeList(request, Quartet, m.list_display,
                m.list_display_links, m.list_filter, m.date_hierarchy,
                m.search_fields, m.list_select_related, m.list_per_page,
                m.list_max_show_all, m.list_editable, m)

        cl.get_results(request)

        # There's only one Quartet instance
        self.assertEqual(cl.result_count, 1)

    def test_distinct_for_m2m_to_inherited_in_list_filter(self):
        """
        Regression test for #13902: When using a ManyToMany in list_filter,
        results shouldn't apper more than once. Target of the relationship
        inherits from another.
        """
        lead = ChordsMusician.objects.create(name='Player A')
        three = ChordsBand.objects.create(name='The Chords Trio')
        Invitation.objects.create(band=three, player=lead, instrument='guitar')
        Invitation.objects.create(band=three, player=lead, instrument='bass')

        m = ChordsBandAdmin(ChordsBand, admin.site)
        request = self.factory.get('/chordsband/', data={'members': lead.pk})

        cl = ChangeList(request, ChordsBand, m.list_display,
                m.list_display_links, m.list_filter, m.date_hierarchy,
                m.search_fields, m.list_select_related, m.list_per_page,
                m.list_max_show_all, m.list_editable, m)

        cl.get_results(request)

        # There's only one ChordsBand instance
        self.assertEqual(cl.result_count, 1)

    def test_distinct_for_non_unique_related_object_in_list_filter(self):
        """
        Regressions tests for #15819: If a field listed in list_filters
        is a non-unique related object, distinct() must be called.
        """
        parent = Parent.objects.create(name='Mary')
        # Two children with the same name
        Child.objects.create(parent=parent, name='Daniel')
        Child.objects.create(parent=parent, name='Daniel')

        m = ParentAdmin(Parent, admin.site)
        request = self.factory.get('/parent/', data={'child__name': 'Daniel'})

        cl = ChangeList(request, Parent, m.list_display, m.list_display_links,
                        m.list_filter, m.date_hierarchy, m.search_fields,
                        m.list_select_related, m.list_per_page,
                        m.list_max_show_all, m.list_editable, m)

        # Make sure distinct() was called
        self.assertEqual(cl.query_set.count(), 1)

    def test_distinct_for_non_unique_related_object_in_search_fields(self):
        """
        Regressions tests for #15819: If a field listed in search_fields
        is a non-unique related object, distinct() must be called.
        """
        parent = Parent.objects.create(name='Mary')
        Child.objects.create(parent=parent, name='Danielle')
        Child.objects.create(parent=parent, name='Daniel')

        m = ParentAdmin(Parent, admin.site)
        request = self.factory.get('/parent/', data={SEARCH_VAR: 'daniel'})

        cl = ChangeList(request, Parent, m.list_display, m.list_display_links,
                        m.list_filter, m.date_hierarchy, m.search_fields,
                        m.list_select_related, m.list_per_page,
                        m.list_max_show_all, m.list_editable, m)

        # Make sure distinct() was called
        self.assertEqual(cl.query_set.count(), 1)

    def test_pagination(self):
        """
        Regression tests for #12893: Pagination in admins changelist doesn't
        use queryset set by modeladmin.
        """
        parent = Parent.objects.create(name='anything')
        for i in range(30):
            Child.objects.create(name='name %s' % i, parent=parent)
            Child.objects.create(name='filtered %s' % i, parent=parent)

        request = self.factory.get('/child/')

        # Test default queryset
        m = ChildAdmin(Child, admin.site)
        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
                m.list_filter, m.date_hierarchy, m.search_fields,
                m.list_select_related, m.list_per_page, m.list_max_show_all,
                m.list_editable, m)
        self.assertEqual(cl.query_set.count(), 60)
        self.assertEqual(cl.paginator.count, 60)
        self.assertEqual(cl.paginator.page_range, [1, 2, 3, 4, 5, 6])

        # Test custom queryset
        m = FilteredChildAdmin(Child, admin.site)
        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
                m.list_filter, m.date_hierarchy, m.search_fields,
                m.list_select_related, m.list_per_page, m.list_max_show_all,
                m.list_editable, m)
        self.assertEqual(cl.query_set.count(), 30)
        self.assertEqual(cl.paginator.count, 30)
        self.assertEqual(cl.paginator.page_range, [1, 2, 3])

    def test_dynamic_list_display(self):
        """
        Regression tests for #14206: dynamic list_display support.
        """
        parent = Parent.objects.create(name='parent')
        for i in range(10):
            Child.objects.create(name='child %s' % i, parent=parent)

        user_noparents = self._create_superuser('noparents')
        user_parents = self._create_superuser('parents')

        # Test with user 'noparents'
        m = custom_site._registry[Child]
        request = self._mocked_authenticated_request('/child/', user_noparents)
        response = m.changelist_view(request)
        self.assertNotContains(response, 'Parent object')

        list_display = m.get_list_display(request)
        list_display_links = m.get_list_display_links(request, list_display)
        self.assertEqual(list_display, ['name', 'age'])
        self.assertEqual(list_display_links, ['name'])

        # Test with user 'parents'
        m = DynamicListDisplayChildAdmin(Child, admin.site)
        request = self._mocked_authenticated_request('/child/', user_parents)
        response = m.changelist_view(request)
        self.assertContains(response, 'Parent object')

        custom_site.unregister(Child)

        list_display = m.get_list_display(request)
        list_display_links = m.get_list_display_links(request, list_display)
        self.assertEqual(list_display, ('parent', 'name', 'age'))
        self.assertEqual(list_display_links, ['parent'])

        # Test default implementation
        custom_site.register(Child, ChildAdmin)
        m = custom_site._registry[Child]
        request = self._mocked_authenticated_request('/child/', user_noparents)
        response = m.changelist_view(request)
        self.assertContains(response, 'Parent object')

    def test_show_all(self):
        parent = Parent.objects.create(name='anything')
        for i in range(30):
            Child.objects.create(name='name %s' % i, parent=parent)
            Child.objects.create(name='filtered %s' % i, parent=parent)

        # Add "show all" parameter to request
        request = self.factory.get('/child/', data={ALL_VAR: ''})

        # Test valid "show all" request (number of total objects is under max)
        m = ChildAdmin(Child, admin.site)
        # 200 is the max we'll pass to ChangeList
        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
                m.list_filter, m.date_hierarchy, m.search_fields,
                m.list_select_related, m.list_per_page, 200, m.list_editable, m)
        cl.get_results(request)
        self.assertEqual(len(cl.result_list), 60)

        # Test invalid "show all" request (number of total objects over max)
        # falls back to paginated pages
        m = ChildAdmin(Child, admin.site)
        # 30 is the max we'll pass to ChangeList for this test
        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
                m.list_filter, m.date_hierarchy, m.search_fields,
                m.list_select_related, m.list_per_page, 30, m.list_editable, m)
        cl.get_results(request)
        self.assertEqual(len(cl.result_list), 10)

    def test_dynamic_list_display_links(self):
        """
        Regression tests for #16257: dynamic list_display_links support.
        """
        parent = Parent.objects.create(name='parent')
        for i in range(1, 10):
            Child.objects.create(id=i, name='child %s' % i, parent=parent, age=i)

        m = DynamicListDisplayLinksChildAdmin(Child, admin.site)
        superuser = self._create_superuser('superuser')
        request = self._mocked_authenticated_request('/child/', superuser)
        response = m.changelist_view(request)
        for i in range(1, 10):
            self.assertContains(response, '%s' % (i, i))

        list_display = m.get_list_display(request)
        list_display_links = m.get_list_display_links(request, list_display)
        self.assertEqual(list_display, ('parent', 'name', 'age'))
        self.assertEqual(list_display_links, ['age'])

    def test_tuple_list_display(self):
        """
        Regression test for #17128
        (ChangeList failing under Python 2.5 after r16319)
        """
        swallow = Swallow.objects.create(
            origin='Africa', load='12.34', speed='22.2')
        model_admin = SwallowAdmin(Swallow, admin.site)
        superuser = self._create_superuser('superuser')
        request = self._mocked_authenticated_request('/swallow/', superuser)
        response = model_admin.changelist_view(request)
        # just want to ensure it doesn't blow up during rendering
        self.assertContains(response, unicode(swallow.origin))
        self.assertContains(response, unicode(swallow.load))
        self.assertContains(response, unicode(swallow.speed))

    def test_deterministic_order_for_unordered_model(self):
        """
        Ensure that the primary key is systematically used in the ordering of
        the changelist's results to guarantee a deterministic order, even
        when the Model doesn't have any default ordering defined.
        Refs #17198.
        """
        superuser = self._create_superuser('superuser')

        for counter in range(1, 51):
            UnorderedObject.objects.create(id=counter, bool=True)

        class UnorderedObjectAdmin(admin.ModelAdmin):
            list_per_page = 10

        def check_results_order(ascending=False):
            admin.site.register(UnorderedObject, UnorderedObjectAdmin)
            model_admin = UnorderedObjectAdmin(UnorderedObject, admin.site)
            counter = 0 if ascending else 51
            for page in range (0, 5):
                request = self._mocked_authenticated_request('/unorderedobject/?p=%s' % page, superuser)
                response = model_admin.changelist_view(request)
                for result in response.context_data['cl'].result_list:
                    counter += 1 if ascending else -1
                    self.assertEqual(result.id, counter)
            admin.site.unregister(UnorderedObject)

        # When no order is defined at all, everything is ordered by '-pk'.
        check_results_order()

        # When an order field is defined but multiple records have the same
        # value for that field, make sure everything gets ordered by -pk as well.
        UnorderedObjectAdmin.ordering = ['bool']
        check_results_order()

        # When order fields are defined, including the pk itself, use them.
        UnorderedObjectAdmin.ordering = ['bool', '-pk']
        check_results_order()
        UnorderedObjectAdmin.ordering = ['bool', 'pk']
        check_results_order(ascending=True)
        UnorderedObjectAdmin.ordering = ['-id', 'bool']
        check_results_order()
        UnorderedObjectAdmin.ordering = ['id', 'bool']
        check_results_order(ascending=True)

    def test_deterministic_order_for_model_ordered_by_its_manager(self):
        """
        Ensure that the primary key is systematically used in the ordering of
        the changelist's results to guarantee a deterministic order, even
        when the Model has a manager that defines a default ordering.
        Refs #17198.
        """
        superuser = self._create_superuser('superuser')

        for counter in range(1, 51):
            OrderedObject.objects.create(id=counter, bool=True, number=counter)

        class OrderedObjectAdmin(admin.ModelAdmin):
            list_per_page = 10

        def check_results_order(ascending=False):
            admin.site.register(OrderedObject, OrderedObjectAdmin)
            model_admin = OrderedObjectAdmin(OrderedObject, admin.site)
            counter = 0 if ascending else 51
            for page in range (0, 5):
                request = self._mocked_authenticated_request('/orderedobject/?p=%s' % page, superuser)
                response = model_admin.changelist_view(request)
                for result in response.context_data['cl'].result_list:
                    counter += 1 if ascending else -1
                    self.assertEqual(result.id, counter)
            admin.site.unregister(OrderedObject)

        # When no order is defined at all, use the model's default ordering (i.e. 'number')
        check_results_order(ascending=True)

        # When an order field is defined but multiple records have the same
        # value for that field, make sure everything gets ordered by -pk as well.
        OrderedObjectAdmin.ordering = ['bool']
        check_results_order()

        # When order fields are defined, including the pk itself, use them.
        OrderedObjectAdmin.ordering = ['bool', '-pk']
        check_results_order()
        OrderedObjectAdmin.ordering = ['bool', 'pk']
        check_results_order(ascending=True)
        OrderedObjectAdmin.ordering = ['-id', 'bool']
        check_results_order()
        OrderedObjectAdmin.ordering = ['id', 'bool']
        check_results_order(ascending=True)


text:
<?php

namespace CycloneSlider\Grafika\Gd\ImageHash;

use CycloneSlider\Grafika\Gd\Editor;
use CycloneSlider\Grafika\Gd\Image;

/**
 * AverageHash
 *
 * Algorithm:
 * Reduce size. Remove high frequencies and detail by shrinking to 8x8 so that there are 64 total pixels.
 * Reduce color. The tiny 8x8 picture is converted to a grayscale.
 * Average the colors. Compute the mean value of the 64 colors.
 * Compute the bits. Each bit is simply set based on whether the color value is above or below the mean.
 * Construct the hash. Set the 64 bits into a 64-bit integer. The order does not matter, just as long as you are consistent.
 *
 * http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
 *
 * @package Grafika\Gd\ImageHash
 */
class AverageHash
{

    /**
     * Generate and get the average hash of the image.
     *
     * @param Image $image
     *
     * @return string
     */
    public function hash(Image $image)
    {
        // Resize the image.
        $width = 8;
        $height = 8;

        $editor = new Editor();
        $editor->setImage($image);
        $editor->resizeExact( $width, $height); // Resize to exactly 9x8
        $gd = $editor->getImage()->getCore();

        // Create an array of greyscale pixel values.
        $pixels = array();
        for ($y = 0; $y < $height; $y++) {
            for ($x = 0; $x < $width; $x++) {
                $rgba = imagecolorat($gd, $x, $y);
                $r = ($rgba >> 16) & 0xFF;
                $g = ($rgba >> 8) & 0xFF;
                $b = $rgba & 0xFF;

                $pixels[] = floor(($r + $g + $b) / 3); // Gray
            }
        }

        // Get the average pixel value.
        $average = floor(array_sum($pixels) / count($pixels));
        // Each hash bit is set based on whether the current pixels value is above or below the average.
        $hash = '';
        foreach ($pixels as $pixel) {
            if ($pixel > $average) {
                $hash .= '1';
            } else {
                $hash .= '0';
            }
        }
        return $hash;
    }
}


text:
package org.domeos.framework.api.consolemodel.auth;

/**
 * Created by feiliu206363 on 2016/9/22.
 */
public class CreatorInfo {
    private String name;
    private int creatorId;

    public String getName() {
        return name;
    }

    public CreatorInfo setName(String name) {
        this.name = name;
        return this;
    }

    public int getCreatorId() {
        return creatorId;
    }

    public CreatorInfo setCreatorId(int creatorId) {
        this.creatorId = creatorId;
        return this;
    }
}


text:
.class Landroid/speech/tts/TextToSpeechService$LoadVoiceItem;
.super Landroid/speech/tts/TextToSpeechService$SpeechItem;
.source "TextToSpeechService.java"

# annotations
.annotation system Ldalvik/annotation/EnclosingClass;
    value = Landroid/speech/tts/TextToSpeechService;
.end annotation

.annotation system Ldalvik/annotation/InnerClass;
    accessFlags = 0x2
    name = "LoadVoiceItem"
.end annotation

# instance fields
.field private final mVoiceName:Ljava/lang/String;

.field final synthetic this$0:Landroid/speech/tts/TextToSpeechService;

# direct methods
.method public constructor <init>(Landroid/speech/tts/TextToSpeechService;Ljava/lang/Object;IILjava/lang/String;)V
    .locals 0
    .param p2, "callerIdentity"    # Ljava/lang/Object;
    .param p3, "callerUid"    # I
    .param p4, "callerPid"    # I
    .param p5, "voiceName"    # Ljava/lang/String;

    .prologue
    .line 1101
    iput-object p1, p0, Landroid/speech/tts/TextToSpeechService$LoadVoiceItem;->this$0:Landroid/speech/tts/TextToSpeechService;

    .line 1102
    invoke-direct {p0, p1, p2, p3, p4}, Landroid/speech/tts/TextToSpeechService$SpeechItem;-><init>(Landroid/speech/tts/TextToSpeechService;Ljava/lang/Object;II)V

    .line 1103
    iput-object p5, p0, Landroid/speech/tts/TextToSpeechService$LoadVoiceItem;->mVoiceName:Ljava/lang/String;

    .line 1104
    return-void
.end method

# virtual methods
.method public isValid()Z
    .locals 1

    .prologue
    .line 1108
    const/4 v0, 0x1

    return v0
.end method

.method protected playImpl()V
    .locals 2

    .prologue
    .line 1113
    iget-object v0, p0, Landroid/speech/tts/TextToSpeechService$LoadVoiceItem;->this$0:Landroid/speech/tts/TextToSpeechService;

    iget-object v1, p0, Landroid/speech/tts/TextToSpeechService$LoadVoiceItem;->mVoiceName:Ljava/lang/String;

    invoke-virtual {v0, v1}, Landroid/speech/tts/TextToSpeechService;->onLoadVoice(Ljava/lang/String;)I

    .line 1114
    return-void
.end method

.method protected stopImpl()V
    .locals 0

    .prologue
    .line 1119
    return-void
.end method


text:
<?php
/**
 * Created by IntelliJ IDEA.
 * User: yuri.blanc
 * Date: 06/04/2016
 * Time: 17:42
 */

namespace yuxblank\phackp\database\api;

interface ObjectRelationalMapping
{
    /** Belongs to
     * @param $origin
     * @param $target
     * @return
     */
    public function oneToOne($origin,$target);

    /** has_many
     * @param $origin
     * @param $target
     * @return
     */
    public function oneToMany($origin,$target);

    /** Has one
     * @param $origin
     * @param $target
     * @return
     */
    public function manyToOne($origin,$target);

    /** Has Many Through
     * @param $origin
     * @param $target
     * @return
     */
    public function manyToMany($origin,$target);

}


text:
class HomeController < ApplicationController
  layout "application"
  @active_tab = "Home"

  def about
  end

  def faq
    @page_name = " - FAQs"
  end

  def licenses
  end

  def support
  end

  def add_license
  end

  def index
  end
end


text:
var functions = {};
var InvalidOperation = "Division by zero! At Line:";

function Solve(args) {
    var linesCount = args.length;

    var firstClosingBracket;
    var lastOpeningBracket;
    var command;
    var commandResult;

    for (var i = 0; i < linesCount; i++) {
        var currentLine = args[i];
        firstClosingBracket = currentLine.indexOf(")");
        while (firstClosingBracket >= 0) {
            lastOpeningBracket = currentLine.lastIndexOf("(", firstClosingBracket);

            command = currentLine.substr(lastOpeningBracket + 1, firstClosingBracket - lastOpeningBracket - 1);
            commandResult = processCommand(command);
            if (commandResult == InvalidOperation) {
                return InvalidOperation + (i + 1);
            }

            currentLine = currentLine.replace(currentLine.substr(lastOpeningBracket, firstClosingBracket - lastOpeningBracket + 1), commandResult);
            firstClosingBracket = currentLine.indexOf(")");
        }
    }

    return commandResult;
}

function processCommand(command) {
    var answer;

    var splittedCommand = command.match(/[^ ]+/g);
    var commandName = splittedCommand[0];
    var commandArgsCount = splittedCommand.length - 1;

    if (commandName == "+") {
        answer = 0;
        var parsed;
        for (var i = 0; i < commandArgsCount; i++) {
            parsed = parseInt(splittedCommand[i + 1]);
            if (!isNaN(parsed)) {
                answer += parsed;
            }
            else {
                answer += functions[splittedCommand[i + 1]];
            }
        }
    }
    else if (commandName == "-") {
        var parsed = parseInt(splittedCommand[1]);
        if (!isNaN(parsed)) {
            answer = parsed;
        }
        else {
            answer = functions[splittedCommand[1]];
        }
        for (var i = 1; i < commandArgsCount; i++) {
            parsed = parseInt(splittedCommand[i + 1]);
            if (!isNaN(parsed)) {
                answer -= parsed;
            }
            else {
                answer -= functions[splittedCommand[i + 1]];
            }
        }
    }
    else if (commandName == "/") {
        var parsed = parseInt(splittedCommand[1]);
        if (!isNaN(parsed)) {
            answer = parsed;
        }
        else {
            answer = functions[splittedCommand[1]];
        }
        for (var i = 1; i < commandArgsCount; i++) {
            parsed = parseInt(splittedCommand[i + 1]);
            if (!isNaN(parsed)) {
                if (parsed === 0) {
                    return InvalidOperation;
                }
                else {
                    answer = Math.floor(answer / parsed);
                }
            }
            else {
                if (functions[splittedCommand[i + 1]] === 0) {
                    return InvalidOperation;
                }
                else {
                    answer = Math.floor(answer / functions[splittedCommand[i + 1]]);
                }
            }
        }
    }
    else if (commandName == "*") {
        answer = 1;
        var parsed;
        for (var i = 0; i < commandArgsCount; i++) {
            parsed = parseInt(splittedCommand[i + 1]);
            if (!isNaN(parsed)) {
                answer *= parsed;
            }
            else {
                answer *= functions[splittedCommand[i + 1]];
            }
        }
    }
    else if (commandName == "def") {
        var parsed = parseInt(splittedCommand[2]);
        if (!isNaN(parsed)) {
            functions[splittedCommand[1]] = parsed;
        }
        else {
            functions[splittedCommand[1]] = functions[splittedCommand[2]];
        }
        answer = functions[splittedCommand[1]];
    }
    return answer;
}


text:
const tap		= require("tap");
const MediaServer	= require("../index");
const SemanticSDP	= require("semantic-sdp");

MediaServer.enableLog(false);
MediaServer.enableDebug(false);
MediaServer.enableUltraDebug(false);

Promise.all([
tap.test("Endpoint::create",async function(suite){

	await suite.test("start+stop",async function(test){
		//Create UDP server endpoint
		const endpoint = MediaServer.createEndpoint("127.0.0.1");
		//Stop it
		endpoint.stop();
		//Ok
		test.end();
	});

	await suite.test("candidate",async function(test){
		//Create UDP server endpoint
		const endpoint = MediaServer.createEndpoint("127.0.0.1");
		//get local candidates
		const candidates = endpoint.getLocalCandidates();
		//Cehck them
		test.same(candidates.length,1);
		test.same(candidates[0].getAddress(),"127.0.0.1");
		//Ok
		test.end();
	});
	await suite.test("candidates",async function(test){
		//Create UDP server endpoint
		const endpoint = MediaServer.createEndpoint(["127.0.0.1","10.0.0.1"]);
		//get local candidates
		const candidates = endpoint.getLocalCandidates();
		//Cehck them
		test.same(candidates.length,2);
		test.same(candidates[0].getAddress(),"127.0.0.1");
		test.same(candidates[1].getAddress(),"10.0.0.1");
		test.ok(candidates[0].getPriority()>candidates[1].getPriority());
		//Ok
		test.end();
	});

	await suite.test("setAffinity",async function(test){
		//Create UDP server endpoint
		const endpoint = MediaServer.createEndpoint("127.0.0.1");
		//Set affinity
		test.ok(endpoint.setAffinity(0));
		//Unset affinity
		test.ok(endpoint.setAffinity(-1));
		//Ok
		test.end();
	});

	await suite.test("setPriority",async function(test){
		//Create UDP server endpoint
		const endpoint = MediaServer.createEndpoint("127.0.0.1");
		//Set RealTime, this could fail if user don't have rights to do it
		endpoint.setPriority(1);
		//Unset RealTime
		test.ok(endpoint.setPriority(0));
		//Ok
		test.end();
	});

	await suite.test("stress test",async function(test){
		const endpoints = [];
		//create a lot
		for (let i=0; i<800; ++i)
			//Create UDP server endpoint
			endpoints.push(MediaServer.createEndpoint("127.0.0.1"));
		//For each one
		for (let endpoint of endpoints)
			//Stop it
			endpoint.stop();
		//Ok
		test.end();
	});

	suite.end();
})
]).then(()=>MediaServer.terminate ());