Test FPDF_SetSandboxPolicy() public API.

- Tidy adjacent test while at it.
- Remove some magic numbers from code itself.

Change-Id: I4c4d2b74230bd5f24f9d9ec18255269edb5f083f
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/62890
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_helpers.cpp b/fpdfsdk/cpdfsdk_helpers.cpp
index 1c3f306..f03f2b1 100644
--- a/fpdfsdk/cpdfsdk_helpers.cpp
+++ b/fpdfsdk/cpdfsdk_helpers.cpp
@@ -286,10 +286,11 @@
 void SetPDFSandboxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
   switch (policy) {
     case FPDF_POLICY_MACHINETIME_ACCESS: {
+      uint32_t mask = 1 << policy;
       if (enable)
-        g_sandbox_policy |= 0x01;
+        g_sandbox_policy |= mask;
       else
-        g_sandbox_policy &= 0xFFFFFFFE;
+        g_sandbox_policy &= ~mask;
     } break;
     default:
       break;
@@ -298,8 +299,10 @@
 
 FPDF_BOOL IsPDFSandboxPolicyEnabled(FPDF_DWORD policy) {
   switch (policy) {
-    case FPDF_POLICY_MACHINETIME_ACCESS:
-      return !!(g_sandbox_policy & 0x01);
+    case FPDF_POLICY_MACHINETIME_ACCESS: {
+      uint32_t mask = 1 << policy;
+      return !!(g_sandbox_policy & mask);
+    }
     default:
       return false;
   }
diff --git a/fpdfsdk/fpdf_view_embeddertest.cpp b/fpdfsdk/fpdf_view_embeddertest.cpp
index cdbd8b3..a74e8f3 100644
--- a/fpdfsdk/fpdf_view_embeddertest.cpp
+++ b/fpdfsdk/fpdf_view_embeddertest.cpp
@@ -298,16 +298,14 @@
   EXPECT_EQ(static_cast<int>(FPDF_GetLastError()), FPDF_ERR_FILE);
 }
 
-// See bug 465.
+// See https://crbug.com/pdfium/465
 TEST_F(FPDFViewEmbedderTest, EmptyDocument) {
   EXPECT_TRUE(CreateEmptyDocument());
-
   {
     int version = 42;
     EXPECT_FALSE(FPDF_GetFileVersion(document(), &version));
     EXPECT_EQ(0, version);
   }
-
   {
 #ifdef PDF_ENABLE_XFA
     const unsigned long kExpected = static_cast<uint32_t>(-1);
@@ -316,11 +314,8 @@
 #endif  // PDF_ENABLE_XFA
     EXPECT_EQ(kExpected, FPDF_GetDocPermissions(document()));
   }
-
   EXPECT_EQ(-1, FPDF_GetSecurityHandlerRevision(document()));
-
   EXPECT_EQ(0, FPDF_GetPageCount(document()));
-
   EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
   EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
   EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
@@ -328,10 +323,42 @@
   char buf[100];
   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
   EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
-
   EXPECT_EQ(0u, FPDF_CountNamedDests(document()));
 }
 
+TEST_F(FPDFViewEmbedderTest, SandboxDocument) {
+  uint16_t buf[200];
+  unsigned long len;
+
+  ASSERT_TRUE(CreateEmptyDocument());
+  len = FPDF_GetMetaText(document(), "CreationDate", buf, sizeof(buf));
+  EXPECT_GT(len, 2u);  // Not just "double NUL" end-of-string indicator.
+  EXPECT_NE(0u, buf[0]);
+  CloseDocument();
+
+  FPDF_SetSandBoxPolicy(FPDF_POLICY_MACHINETIME_ACCESS, false);
+  ASSERT_TRUE(CreateEmptyDocument());
+  len = FPDF_GetMetaText(document(), "CreationDate", buf, sizeof(buf));
+  EXPECT_EQ(2u, len);  // Only a "double NUL" end-of-string indicator.
+  EXPECT_EQ(0u, buf[0]);
+  CloseDocument();
+
+  constexpr unsigned long kNoSuchPolicy = 102;
+  FPDF_SetSandBoxPolicy(kNoSuchPolicy, true);
+  ASSERT_TRUE(CreateEmptyDocument());
+  len = FPDF_GetMetaText(document(), "CreationDate", buf, sizeof(buf));
+  EXPECT_EQ(2u, len);  // Only a "double NUL" end-of-string indicator.
+  EXPECT_EQ(0u, buf[0]);
+  CloseDocument();
+
+  FPDF_SetSandBoxPolicy(FPDF_POLICY_MACHINETIME_ACCESS, true);
+  ASSERT_TRUE(CreateEmptyDocument());
+  len = FPDF_GetMetaText(document(), "CreationDate", buf, sizeof(buf));
+  EXPECT_GT(len, 2u);  // Not just "double NUL" end-of-string indicator.
+  EXPECT_NE(0u, buf[0]);
+  CloseDocument();
+}
+
 TEST_F(FPDFViewEmbedderTest, LinearizedDocument) {
   EXPECT_TRUE(OpenDocumentLinearized("feature_linearized_loading.pdf"));
   int version;