diff options
author | George Wangensteen <george.wangensteen@mongodb.com> | 2020-10-26 20:40:49 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-11-06 01:06:48 +0000 |
commit | d12213c0198b2a2adfa033a8661e026c9e9c7f7f (patch) | |
tree | e2ed810c4f96dea80438cbf2a83030034e56abee /src/mongo/db/commands_test.cpp | |
parent | 2c2146ac0ae876984423c04a258e32b4c5843315 (diff) | |
download | mongo-d12213c0198b2a2adfa033a8661e026c9e9c7f7f.tar.gz |
SERVER-51374 Create IDL definition for ErrorReply
Diffstat (limited to 'src/mongo/db/commands_test.cpp')
-rw-r--r-- | src/mongo/db/commands_test.cpp | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/src/mongo/db/commands_test.cpp b/src/mongo/db/commands_test.cpp index 89b7b38c12f..8fecc92a2ff 100644 --- a/src/mongo/db/commands_test.cpp +++ b/src/mongo/db/commands_test.cpp @@ -36,6 +36,7 @@ #include "mongo/db/service_context_test_fixture.h" #include "mongo/rpc/factory.h" #include "mongo/rpc/op_msg_rpc_impls.h" +#include "mongo/unittest/death_test.h" #include "mongo/unittest/unittest.h" namespace mongo { @@ -69,14 +70,14 @@ TEST(Commands, appendCommandStatusNoOverwrite) { BSONObjBuilder actualResult; actualResult.append("a", "b"); actualResult.append("c", "d"); - actualResult.append("ok", "not ok"); + actualResult.append("ok", 0.0); const Status status(ErrorCodes::InvalidLength, "Response payload too long"); CommandHelpers::appendCommandStatusNoThrow(actualResult, status); BSONObjBuilder expectedResult; expectedResult.append("a", "b"); expectedResult.append("c", "d"); - expectedResult.append("ok", "not ok"); + expectedResult.append("ok", 0.0); expectedResult.append("errmsg", status.reason()); expectedResult.append("code", status.code()); expectedResult.append("codeName", ErrorCodes::errorString(status.code())); @@ -99,6 +100,30 @@ TEST(Commands, appendCommandStatusErrorExtraInfo) { ASSERT_BSONOBJ_EQ(actualResult.obj(), expectedResult.obj()); } +DEATH_TEST(Commands, appendCommandStatusInvalidOkValue, "invariant") { + BSONObjBuilder actualResult; + actualResult.append("a", "b"); + actualResult.append("c", "d"); + actualResult.append("ok", "yes"); + const Status status(ErrorCodes::InvalidLength, "fake error for test"); + + // An "ok" value other than 1.0 or 0.0 is not allowed and should crash. + CommandHelpers::appendCommandStatusNoThrow(actualResult, status); +} + +DEATH_TEST(Commands, appendCommandStatusNoCodeName, "invariant") { + BSONObjBuilder actualResult; + actualResult.append("a", "b"); + actualResult.append("code", ErrorCodes::InvalidLength); + actualResult.append("ok", 1.0); + const Status status(ErrorCodes::InvalidLength, "Response payload too long"); + + // If the result already has an error code, we don't move any code or codeName over from the + // status. Therefore, if the result has an error code but no codeName, we're missing a + // required field and should crash. + CommandHelpers::appendCommandStatusNoThrow(actualResult, status); +} + class ParseNsOrUUID : public ServiceContextTest { public: ParseNsOrUUID() : opCtxPtr(makeOperationContext()), opCtx(opCtxPtr.get()) {} |