DarkflameServer/tests/dCommonTests/Amf3Tests.cpp
David Markowitz 4fe335cc66
Refactor: Amf3 implementation (#998)
* Update AMFDeserializeTests.cpp

Redo Amf3 functionality

Overhaul the whole thing due to it being outdated and clunky to use

Sometimes you want to keep the value

Update AMFDeserializeTests.cpp

* Fix enum and constructors

Correct enum to a class and simplify names.
Add a proper default constructor

* Update MasterServer.cpp

* Fix bugs and add more tests

* Refactor: AMF with templates in mind

- Remove hard coded bodge
- Use templates and generics to allow for much looser typing and strengthened implementation
- Move code into header only implementation for portability

Refactor: Convert AMF implementation to templates

- Rip out previous implementation
- Remove all extraneous terminology
- Add proper overloads for all types of inserts
- Fix up tests and codebase

* Fix compiler errors

* Check for null first

* Add specialization for const char*

* Update tests for new template specialization

* Switch BitStream to use references

* Rename files

* Check enum bounds on deserialize

I did this on a phone
2023-05-13 17:22:00 -05:00

117 lines
4.1 KiB
C++

#include <gtest/gtest.h>
#include <vector>
#include "Amf3.h"
TEST(dCommonTests, AMF3AssociativeArrayTest) {
AMFArrayValue array;
array.Insert("true", true);
array.Insert("false", false);
// test associative can insert values
ASSERT_EQ(array.GetAssociative().size(), 2);
ASSERT_EQ(array.Get<bool>("true")->GetValueType(), eAmf::True);
ASSERT_EQ(array.Get<bool>("false")->GetValueType(), eAmf::False);
// Test associative can remove values
array.Remove("true");
ASSERT_EQ(array.GetAssociative().size(), 1);
ASSERT_EQ(array.Get<bool>("true"), nullptr);
ASSERT_EQ(array.Get<bool>("false")->GetValueType(), eAmf::False);
array.Remove("false");
ASSERT_EQ(array.GetAssociative().size(), 0);
ASSERT_EQ(array.Get<bool>("true"), nullptr);
ASSERT_EQ(array.Get<bool>("false"), nullptr);
// Test that multiple of the same key respect only the first element of that key
array.Insert("true", true);
array.Insert("true", false);
ASSERT_EQ(array.GetAssociative().size(), 1);
ASSERT_EQ(array.Get<bool>("true")->GetValueType(), eAmf::True);
array.Remove("true");
// Now test the dense portion
// Get some out of bounds values and cast to incorrect template types
array.Push(true);
array.Push(false);
ASSERT_EQ(array.GetDense().size(), 2);
ASSERT_EQ(array.Get<bool>(0)->GetValueType(), eAmf::True);
ASSERT_EQ(array.Get<std::string>(0), nullptr);
ASSERT_EQ(array.Get<bool>(1)->GetValueType(), eAmf::False);
ASSERT_EQ(array.Get<bool>(155), nullptr);
array.Pop();
ASSERT_EQ(array.GetDense().size(), 1);
ASSERT_EQ(array.Get<bool>(0)->GetValueType(), eAmf::True);
ASSERT_EQ(array.Get<std::string>(0), nullptr);
ASSERT_EQ(array.Get<bool>(1), nullptr);
array.Pop();
ASSERT_EQ(array.GetDense().size(), 0);
ASSERT_EQ(array.Get<bool>(0), nullptr);
ASSERT_EQ(array.Get<std::string>(0), nullptr);
ASSERT_EQ(array.Get<bool>(1), nullptr);
}
TEST(dCommonTests, AMF3InsertionAssociativeTest) {
AMFArrayValue array;
array.Insert("CString", "string");
array.Insert("String", std::string("string"));
array.Insert("False", false);
array.Insert("True", true);
array.Insert<int32_t>("Integer", 42U);
array.Insert("Double", 42.0);
array.InsertArray("Array");
array.Insert<std::vector<uint32_t>>("Undefined", {});
array.Insert("Null", nullptr);
std::cout << "test" << std::endl;
ASSERT_EQ(array.Get<const char*>("CString")->GetValueType(), eAmf::String);
std::cout << "test" << std::endl;
ASSERT_EQ(array.Get<std::string>("String")->GetValueType(), eAmf::String);
std::cout << "test" << std::endl;
ASSERT_EQ(array.Get<bool>("False")->GetValueType(), eAmf::False);
std::cout << "test" << std::endl;
ASSERT_EQ(array.Get<bool>("True")->GetValueType(), eAmf::True);
std::cout << "test" << std::endl;
ASSERT_EQ(array.Get<int32_t>("Integer")->GetValueType(), eAmf::Integer);
std::cout << "test" << std::endl;
ASSERT_EQ(array.Get<double>("Double")->GetValueType(), eAmf::Double);
std::cout << "test" << std::endl;
ASSERT_EQ(array.GetArray("Array")->GetValueType(), eAmf::Array);
std::cout << "test" << std::endl;
ASSERT_EQ(array.Get<nullptr_t>("Null")->GetValueType(), eAmf::Null);
std::cout << "test" << std::endl;
ASSERT_EQ(array.Get<std::vector<uint32_t>>("Undefined")->GetValueType(), eAmf::Undefined);
std::cout << "test" << std::endl;
}
TEST(dCommonTests, AMF3InsertionDenseTest) {
AMFArrayValue array;
array.Push<std::string>("string");
array.Push("CString");
array.Push(false);
array.Push(true);
array.Push<int32_t>(42U);
array.Push(42.0);
array.PushArray();
array.Push(nullptr);
array.Push<std::vector<uint32_t>>({});
ASSERT_EQ(array.Get<std::string>(0)->GetValueType(), eAmf::String);
ASSERT_EQ(array.Get<const char*>(1)->GetValueType(), eAmf::String);
ASSERT_EQ(array.Get<bool>(2)->GetValueType(), eAmf::False);
ASSERT_EQ(array.Get<bool>(3)->GetValueType(), eAmf::True);
ASSERT_EQ(array.Get<int32_t>(4)->GetValueType(), eAmf::Integer);
ASSERT_EQ(array.Get<double>(5)->GetValueType(), eAmf::Double);
ASSERT_EQ(array.GetArray(6)->GetValueType(), eAmf::Array);
ASSERT_EQ(array.Get<nullptr_t>(7)->GetValueType(), eAmf::Null);
ASSERT_EQ(array.Get<std::vector<uint32_t>>(8)->GetValueType(), eAmf::Undefined);
}