this repo has no description
at trunk 52 lines 1.5 kB view raw
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 2#include "gtest/gtest.h" 3 4#include "builtins.h" 5#include "bytes-builtins.h" 6#include "os.h" 7#include "runtime.h" 8#include "test-utils.h" 9 10namespace py { 11 12namespace testing { 13 14using MarshalModuleTest = RuntimeFixture; 15 16TEST_F(MarshalModuleTest, LoadsReadsSet) { 17 HandleScope scope(thread_); 18 // marshal.loads(set()) 19 const byte set_bytes[] = "\xbc\x00\x00\x00\x00"; 20 Bytes bytes(&scope, runtime_->newBytesWithAll(set_bytes)); 21 Object obj(&scope, runBuiltin(FUNC(marshal, loads), bytes)); 22 ASSERT_TRUE(obj.isSet()); 23 EXPECT_EQ(Set::cast(*obj).numItems(), 0); 24} 25 26TEST_F(MarshalModuleTest, LoadsWithBytesSubclassReadsSet) { 27 HandleScope scope(thread_); 28 // marshal.loads(set()) 29 ASSERT_FALSE(runFromCStr(runtime_, R"( 30class Foo(bytes): pass 31foo = Foo(b"\xbc\x00\x00\x00\x00") 32)") 33 .isError()); 34 Object bytes(&scope, mainModuleAt(runtime_, "foo")); 35 Object obj(&scope, runBuiltin(FUNC(marshal, loads), bytes)); 36 ASSERT_TRUE(obj.isSet()); 37 EXPECT_EQ(Set::cast(*obj).numItems(), 0); 38} 39 40TEST_F(MarshalModuleTest, LoadsIgnoresExtraBytesAtEnd) { 41 HandleScope scope(thread_); 42 // marshal.loads(set() + some extra bytes) 43 const byte set_bytes[] = "\xbc\x00\x00\x00\x00\x00\x00\x00\xAA\xBB\xCC"; 44 Bytes bytes(&scope, runtime_->newBytesWithAll(set_bytes)); 45 Object obj(&scope, runBuiltin(FUNC(marshal, loads), bytes)); 46 ASSERT_TRUE(obj.isSet()); 47 EXPECT_EQ(Set::cast(*obj).numItems(), 0); 48} 49 50} // namespace testing 51 52} // namespace py