szehon-ho commented on code in PR #53227:
URL: https://github.com/apache/spark/pull/53227#discussion_r2663017272


##########
sql/catalyst/src/test/java/org/apache/spark/sql/catalyst/util/geo/WkbErrorHandlingTest.java:
##########
@@ -0,0 +1,400 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.util.geo;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Arrays;
+
+/**
+ * Test suite for WKB error handling and edge cases.
+ */
+public class WkbErrorHandlingTest {
+
+  private byte[] hexToBytes(String hex) {
+    int len = hex.length();
+    byte[] data = new byte[len / 2];
+    for (int i = 0; i < len; i += 2) {
+      data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
+          + Character.digit(hex.charAt(i + 1), 16));
+    }
+    return data;
+  }
+
+  /**
+   * Helper method to assert that parsing a WKB hex string throws 
WkbParseException
+   * containing the expected error message and the original WKB hex.
+   */
+  private void assertParseError(String hex, String expectedMessagePart) {
+    assertParseError(hex, expectedMessagePart, 1);
+  }
+
+  /**
+   * Helper method to assert that parsing a WKB hex string throws 
WkbParseException
+   * containing the expected error message and the original WKB hex, with 
specified
+   * validation level.
+   */
+  private void assertParseError(String hex, String expectedMessagePart, int 
validationLevel) {
+    byte[] wkb = hexToBytes(hex);
+    WkbReader reader = new WkbReader(validationLevel);
+    WkbParseException ex = Assertions.assertThrows(
+        WkbParseException.class, () -> reader.read(wkb),
+        "Should throw WkbParseException for WKB: " + hex);
+    
Assertions.assertTrue(ex.getMessage().toUpperCase().contains(hex.toUpperCase()),
+        "Exception message should contain the WKB hex: " + hex + ", actual: " 
+ ex.getMessage());
+    if (expectedMessagePart != null && !expectedMessagePart.isEmpty()) {
+      Assertions.assertTrue(
+          
ex.getMessage().toLowerCase().contains(expectedMessagePart.toLowerCase()),
+          "Exception message should contain '" + expectedMessagePart + "', 
actual: " +
+              ex.getMessage());
+    }
+  }
+
+  @Test
+  public void testEmptyWkb() {
+    byte[] emptyWkb = new byte[0];
+    WkbReader reader = new WkbReader();
+    WkbParseException ex = Assertions.assertThrows(
+      WkbParseException.class, () -> reader.read(emptyWkb));
+    // Empty WKB produces empty hex string, so just verify exception was thrown
+    Assertions.assertNotNull(ex.getMessage());
+  }
+
+  @Test
+  public void testTooShortWkb() {
+    // Only endianness byte
+    String hex = "01";
+    byte[] tooShort = hexToBytes(hex);
+    WkbReader reader = new WkbReader();
+    WkbParseException ex = Assertions.assertThrows(
+      WkbParseException.class, () -> reader.read(tooShort));
+    
Assertions.assertTrue(ex.getMessage().toUpperCase().contains(hex.toUpperCase()),
+      "Exception message should contain the WKB hex: " + hex);
+  }
+
+  @Test
+  public void testInvalidGeometryTypeZero() {
+    // Type = 0 (invalid, should be 1-7)
+    String hex = "0100000000000000000000F03F0000000000000040";
+    byte[] invalidType = hexToBytes(hex);
+    WkbReader reader = new WkbReader();
+    WkbParseException ex = Assertions.assertThrows(
+      WkbParseException.class, () -> reader.read(invalidType));
+    
Assertions.assertTrue(ex.getMessage().toUpperCase().contains(hex.toUpperCase()),
+      "Exception message should contain the WKB hex: " + hex);
+  }
+
+  @Test
+  public void testTruncatedPointCoordinates() {
+    // Point WKB with truncated coordinates (missing Y coordinate)
+    String hex = "0101000000000000000000F03F";
+    byte[] truncated = hexToBytes(hex);
+    WkbReader reader = new WkbReader();
+    WkbParseException ex = Assertions.assertThrows(
+      WkbParseException.class, () -> reader.read(truncated));
+    
Assertions.assertTrue(ex.getMessage().toUpperCase().contains(hex.toUpperCase()),
+      "Exception message should contain the WKB hex: " + hex);
+  }
+

Review Comment:
   Added



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to