[ObjC] Bring over more unknown fields tests.

Port of the interesting `GPBUnknownFieldsSet` tests to `GPBUnknownFields`
as they provide coverage of general handing of unknown fields during parsing.

Also tweak the `Set` tests to actually use a registry to confirm behaviors around
extensions where the test previously wasn't as complete.

PiperOrigin-RevId: 650660181
diff --git a/objectivec/Tests/GPBUnknownFieldSetTest.m b/objectivec/Tests/GPBUnknownFieldSetTest.m
index 88a1074..10478ac 100644
--- a/objectivec/Tests/GPBUnknownFieldSetTest.m
+++ b/objectivec/Tests/GPBUnknownFieldSetTest.m
@@ -333,6 +333,15 @@
 
   XCTAssertEqual(unknownFields_.countOfFields, message.unknownFields.countOfFields);
   XCTAssertEqualObjects(allFieldsData_, message.data);
+
+  // Just confirm as known extensions, they don't go into unknown data and end up in the
+  // extensions dictionary.
+  TestAllExtensions* allExtensionsMessage =
+      [TestAllExtensions parseFromData:allFieldsData_
+                     extensionRegistry:[UnittestRoot extensionRegistry]
+                                 error:NULL];
+  XCTAssertEqual(allExtensionsMessage.unknownFields.countOfFields, (NSUInteger)0);
+  XCTAssertEqualObjects([allExtensionsMessage data], allFieldsData_);
 }
 
 - (void)testWrongExtensionTypeTreatedAsUnknown {
@@ -340,8 +349,10 @@
   // when parsing extensions.
 
   NSData* bizarroData = [self getBizarroData];
-  TestAllExtensions* allExtensionsMessage = [TestAllExtensions parseFromData:bizarroData
-                                                                       error:NULL];
+  TestAllExtensions* allExtensionsMessage =
+      [TestAllExtensions parseFromData:bizarroData
+                     extensionRegistry:[UnittestRoot extensionRegistry]
+                                 error:NULL];
   TestEmptyMessage* emptyMessage = [TestEmptyMessage parseFromData:bizarroData error:NULL];
 
   // All fields should have been interpreted as unknown, so the debug strings
diff --git a/objectivec/Tests/GPBUnknownFieldsTest.m b/objectivec/Tests/GPBUnknownFieldsTest.m
index d6affa4..b7446a4 100644
--- a/objectivec/Tests/GPBUnknownFieldsTest.m
+++ b/objectivec/Tests/GPBUnknownFieldsTest.m
@@ -604,6 +604,82 @@
   TestAllTypes* allFields2 = [TestAllTypes message];
   [allFields2 mergeUnknownFields:ufs extensionRegistry:nil];
   XCTAssertEqualObjects(allFields2, allFields);
+
+  // Confirm that the they still all end up in unknowns when parsed into a message with extensions
+  // support for the field numbers (but no registry).
+  {
+    TestEmptyMessageWithExtensions* msgWithExts =
+        [TestEmptyMessageWithExtensions parseFromData:allFieldsData error:NULL];
+    GPBUnknownFields* ufs2 = [[[GPBUnknownFields alloc] initFromMessage:msgWithExts] autorelease];
+    XCTAssertEqualObjects(ufs2, ufs);
+  }
+
+  // Sanity check that with the registry, they go into the extension fields.
+  {
+    TestAllExtensions* msgWithExts =
+        [TestAllExtensions parseFromData:allFieldsData
+                       extensionRegistry:[UnittestRoot extensionRegistry]
+                                   error:NULL];
+    GPBUnknownFields* ufs2 = [[[GPBUnknownFields alloc] initFromMessage:msgWithExts] autorelease];
+    XCTAssertEqual(ufs2.count, 0);
+  }
+}
+
+- (void)testMismatchedFieldTypes {
+  // Start with a valid set of field data, and map it into unknown fields.
+  TestAllTypes* allFields = [self allSetRepeatedCount:kGPBDefaultRepeatCount];
+  NSData* allFieldsData = [allFields data];
+  TestEmptyMessage* emptyMessage = [TestEmptyMessage parseFromData:allFieldsData error:NULL];
+  GPBUnknownFields* ufsRightTypes =
+      [[[GPBUnknownFields alloc] initFromMessage:emptyMessage] autorelease];
+
+  // Now build a new set of unknown fields where all the data types are wrong for the original
+  // fields.
+  GPBUnknownFields* ufsWrongTypes = [[[GPBUnknownFields alloc] init] autorelease];
+  for (GPBUnknownField* field in ufsRightTypes) {
+    if (field.type != GPBUnknownFieldTypeVarint) {
+      // Original field is not a varint, so use a varint.
+      [ufsWrongTypes addFieldNumber:field.number varint:1];
+    } else {
+      // Original field *is* a varint, so use something else.
+      [ufsWrongTypes addFieldNumber:field.number fixed32:1];
+    }
+  }
+
+  // Parse into a message with the field numbers, the wrong types should force everything into
+  // unknown fields again.
+  {
+    TestAllTypes* msg = [TestAllTypes message];
+    [msg mergeUnknownFields:ufsWrongTypes extensionRegistry:nil];
+    GPBUnknownFields* ufs2 = [[[GPBUnknownFields alloc] initFromMessage:msg] autorelease];
+    XCTAssertFalse(ufs2.empty);
+    XCTAssertEqualObjects(ufs2, ufsWrongTypes);  // All back as unknown fields.
+  }
+
+  // Parse into a message with extension registiry, the wrong types should still force everything
+  // into unknown fields.
+  {
+    TestAllExtensions* msg = [TestAllExtensions message];
+    [msg mergeUnknownFields:ufsWrongTypes extensionRegistry:[UnittestRoot extensionRegistry]];
+    GPBUnknownFields* ufs2 = [[[GPBUnknownFields alloc] initFromMessage:msg] autorelease];
+    XCTAssertFalse(ufs2.empty);
+    XCTAssertEqualObjects(ufs2, ufsWrongTypes);  // All back as unknown fields.
+  }
+}
+
+- (void)testLargeVarint {
+  GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];
+  [ufs addFieldNumber:1 varint:0x7FFFFFFFFFFFFFFFL];
+
+  TestEmptyMessage* emptyMessage = [TestEmptyMessage message];
+  [emptyMessage mergeUnknownFields:ufs extensionRegistry:nil];
+
+  GPBUnknownFields* ufsParsed =
+      [[[GPBUnknownFields alloc] initFromMessage:emptyMessage] autorelease];
+  XCTAssertEqual(ufsParsed.count, 1);
+  uint64_t varint = 0;
+  XCTAssertTrue([ufsParsed getFirst:1 varint:&varint]);
+  XCTAssertEqual(varint, 0x7FFFFFFFFFFFFFFFL);
 }
 
 @end