<commit_before>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Transactions;

using Microsoft.SqlServer.Server;
using Xunit;
using System.Threading.Tasks;

namespace System.Data.SqlClient.ManualTesting.Tests
{
    public class TvpTest
    {
        private const string TvpName = "@tvp";
        private static readonly IList<SteAttributeKey> BoundariesTestKeys = new List<SteAttributeKey>(
                new SteAttributeKey[] {
                    SteAttributeKey.SqlDbType,
                    SteAttributeKey.MultiValued,
                    SteAttributeKey.MaxLength,
                    SteAttributeKey.Precision,
                    SteAttributeKey.Scale,
                    SteAttributeKey.LocaleId,
                    SteAttributeKey.CompareOptions,
                    SteAttributeKey.TypeName,
                    SteAttributeKey.Type,
                    SteAttributeKey.Fields,
                    SteAttributeKey.Value
                }).AsReadOnly();

        // data value and server consts
        private string _connStr;

        [ActiveIssue(27858, TestPlatforms.AnyUnix)]
        [ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup))]
        public void TestMain()
        {
            Assert.True(RunTestCoreAndCompareWithBaseline());
        }

        [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
        public void TestPacketNumberWraparound()
        {
            // this test uses a specifically crafted sql record enumerator and data to put the TdsParserStateObject.WritePacket(byte,bool)
            // into a state where it can't differentiate between a packet in the middle of a large packet-set after a byte counter wraparound
            // and the first packet of the connection and in doing so trips over a check for packet length from the input which has been 
            // forced to tell it that there is no output buffer space left, this causes an uncancellable infinite loop

            // if the enumerator is completely read to the end then the bug is no longer present and the packet creation task returns,
            // if the timeout occurs it is probable (but not absolute) that the write is stuck

            var enumerator = new WraparoundRowEnumerator(1000000);

            int returned = Task.WaitAny(
                Task.Factory.StartNew( 
                    () => RunPacketNumberWraparound(enumerator), 
                    TaskCreationOptions.DenyChildAttach|TaskCreationOptions.LongRunning
                ),
                Task.Delay(TimeSpan.FromSeconds(5))
            );
            //Assert.Equal(0, returned);
            Assert.True(enumerator.MaxCount==enumerator.Count);
        }


        public TvpTest()
        {
            _connStr = DataTestUtility.TcpConnStr;
        }<commit_msg>address feedback<commit_after>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Transactions;

using Microsoft.SqlServer.Server;
using Xunit;
using System.Threading.Tasks;

namespace System.Data.SqlClient.ManualTesting.Tests
{
    public class TvpTest
    {
        private const string TvpName = "@tvp";
        private static readonly IList<SteAttributeKey> BoundariesTestKeys = new List<SteAttributeKey>(
                new SteAttributeKey[] {
                    SteAttributeKey.SqlDbType,
                    SteAttributeKey.MultiValued,
                    SteAttributeKey.MaxLength,
                    SteAttributeKey.Precision,
                    SteAttributeKey.Scale,
                    SteAttributeKey.LocaleId,
                    SteAttributeKey.CompareOptions,
                    SteAttributeKey.TypeName,
                    SteAttributeKey.Type,
                    SteAttributeKey.Fields,
                    SteAttributeKey.Value
                }).AsReadOnly();

        // data value and server consts
        private string _connStr;

        [ActiveIssue(27858, TestPlatforms.AnyUnix)]
        [ConditionalFact(typeof(DataTestUtility),nameof(DataTestUtility.AreConnStringsSetup))]
        public void TestMain()
        {
            Assert.True(RunTestCoreAndCompareWithBaseline());
        }

        [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
        public void TestPacketNumberWraparound()
        {
            // this test uses a specifically crafted sql record enumerator and data to put the TdsParserStateObject.WritePacket(byte,bool)
            // into a state where it can't differentiate between a packet in the middle of a large packet-set after a byte counter wraparound
            // and the first packet of the connection and in doing so trips over a check for packet length from the input which has been 
            // forced to tell it that there is no output buffer space left, this causes an uncancellable infinite loop

            // if the enumerator is completely read to the end then the bug is no longer present and the packet creation task returns,
            // if the timeout occurs it is probable (but not absolute) that the write is stuck

            var enumerator = new WraparoundRowEnumerator(1000000);

            int returned = Task.WaitAny(
                Task.Factory.StartNew( 
                    () => RunPacketNumberWraparound(enumerator), 
                    TaskCreationOptions.DenyChildAttach|TaskCreationOptions.LongRunning
                ),
                Task.Delay(TimeSpan.FromSeconds(5))
            );
            Assert.True(enumerator.MaxCount==enumerator.Count);
        }


        public TvpTest()
        {
            _connStr = DataTestUtility.TcpConnStr;
        }
{'fraction_non_alphanumeric': 0.0639963167587477, 'fraction_numerical': 0.004143646408839779, 'mean_word_length': 2.279818822345244, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4941849', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1545, 'n_words': 497}