Repository: microsoft/garnet
File: test/Garnet.test/RespTests.cs (C#)

Code before:

            // Test "HELLO 2"
            var result = db.Execute("HELLO", "2");

            ClassicAssert.IsNotNull(result);
            ClassicAssert.AreEqual(ResultType.Array, result.Resp2Type);
            ClassicAssert.AreEqual(ResultType.Array, result.Resp3Type);
            var resultDict = result.ToDictionary();
            ClassicAssert.IsNotNull(resultDict);
            ClassicAssert.AreEqual(2, (int)resultDict["proto"]);
            ClassicAssert.AreEqual("master", (string)resultDict["role"]);

            // Test "HELLO 3"
            result = db.Execute("HELLO", "3");

            ClassicAssert.IsNotNull(result);
            ClassicAssert.AreEqual(ResultType.Array, result.Resp2Type);
            ClassicAssert.AreEqual(ResultType.Map, result.Resp3Type);
            resultDict = result.ToDictionary();
            ClassicAssert.IsNotNull(resultDict);
            ClassicAssert.AreEqual(3, (int)resultDict["proto"]);
            ClassicAssert.AreEqual("master", (string)resultDict["role"]);
        }

        [Test]
        public void HelloTest2()
        {
            using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig(protocol: RedisProtocol.Resp2));
            var db = redis.GetDatabase(0);

            // Failed HELLO should not change current protocol
            try
            {
                db.Execute("HELLO", "3", "AUTH", "NX", "NX");
                ClassicAssert.Fail("Should never reach this line, user does not exist");
            }
            catch
            {
                // Passthrough
            }
            var result = db.Execute("HELLO");

            ClassicAssert.IsNotNull(result);
            ClassicAssert.AreEqual(ResultType.Array, result.Resp2Type);
            ClassicAssert.AreEqual(ResultType.Array, result.Resp3Type);
            var resultDict = result.ToDictionary();
            ClassicAssert.IsNotNull(resultDict);

            // Should remain 2 since protocol change failed
            ClassicAssert.AreEqual(2, (int)resultDict["proto"]);
        }

Review comment (security):
Can pick a more informative name for the test, like `HelloAuthErrorTest`

Code after:

            // Test "HELLO 2"
            var result = db.Execute("HELLO", "2");

            ClassicAssert.IsNotNull(result);
            ClassicAssert.AreEqual(ResultType.Array, result.Resp2Type);
            ClassicAssert.AreEqual(ResultType.Array, result.Resp3Type);
            var resultDict = result.ToDictionary();
            ClassicAssert.IsNotNull(resultDict);
            ClassicAssert.AreEqual(2, (int)resultDict["proto"]);
            ClassicAssert.AreEqual("master", (string)resultDict["role"]);

            // Test "HELLO 3"
            result = db.Execute("HELLO", "3");

            ClassicAssert.IsNotNull(result);
            ClassicAssert.AreEqual(ResultType.Array, result.Resp2Type);
            ClassicAssert.AreEqual(ResultType.Map, result.Resp3Type);
            resultDict = result.ToDictionary();
            ClassicAssert.IsNotNull(resultDict);
            ClassicAssert.AreEqual(3, (int)resultDict["proto"]);
            ClassicAssert.AreEqual("master", (string)resultDict["role"]);
        }

        [Test]
        public void HelloAuthErrorTest()
        {
            using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig(protocol: RedisProtocol.Resp2));
            var db = redis.GetDatabase(0);

            // Failed HELLO should not change current protocol
            Assert.Throws<RedisServerException>(() => db.Execute("HELLO", "3", "AUTH", "NX", "NX"),
                           Encoding.ASCII.GetString(CmdStrings.RESP_WRONGPASS_INVALID_USERNAME_PASSWORD));
            var result = db.Execute("HELLO");

            ClassicAssert.IsNotNull(result);
            ClassicAssert.AreEqual(ResultType.Array, result.Resp2Type);
            ClassicAssert.AreEqual(ResultType.Array, result.Resp3Type);
            var resultDict = result.ToDictionary();
            ClassicAssert.IsNotNull(resultDict);

            // Should remain 2 since protocol change failed
            ClassicAssert.AreEqual(2, (int)resultDict["proto"]);
        }

        [Test]
        [TestCase([2, "$-1\r\n", "$1\r\n", "*4", '*'], Description = "RESP2 output")]
        [TestCase([3, "_\r\n", ",", "%2", '~'], Description = "RESP3 output")]
        public async Task RespOutputTests(byte respVersion, string expectedResponse, string doublePrefix, string mapPrefix, char setPrefix)
        {
            using var c = TestUtils.GetGarnetClientSession(raw: true);