3939import com .google .gcloud .datastore .testing .LocalGcdHelper ;
4040import com .google .gcloud .spi .DatastoreRpc ;
4141import com .google .gcloud .spi .DatastoreRpcFactory ;
42+ import com .google .protobuf .ByteString ;
4243
4344import org .easymock .EasyMock ;
4445import org .junit .AfterClass ;
@@ -496,7 +497,7 @@ public void testQueryPaginationWithLimit() throws DatastoreException {
496497 }
497498 query = query .toBuilder ().startCursor (results .cursorAfter ()).build ();
498499 }
499- assertEquals (totalCount , 5 );
500+ assertEquals (5 , totalCount );
500501 EasyMock .verify (rpcFactoryMock , rpcMock );
501502 }
502503
@@ -524,7 +525,8 @@ private List<RunQueryResponse> buildResponsesForQueryPaginationWithLimit() {
524525 .setMoreResults (QueryResultBatch .MoreResultsType .MORE_RESULTS_AFTER_LIMIT )
525526 .clearEntityResult ()
526527 .addAllEntityResult (queryResultBatchPb .getEntityResultList ().subList (1 , 2 ))
527- .setEndCursor (queryResultBatchPb .getEntityResultList ().get (1 ).getCursor ())
528+ .setEndCursor (
529+ ByteString .copyFrom (new byte [] {(byte ) 0x80 })) // test invalid UTF-8 string
528530 .build ();
529531 responses .add (RunQueryResponse .newBuilder ().setBatch (queryResultBatchPb2 ).build ());
530532 QueryResultBatch queryResultBatchPb3 = QueryResultBatch .newBuilder ()
@@ -546,6 +548,17 @@ private List<RunQueryResponse> buildResponsesForQueryPaginationWithLimit() {
546548 return responses ;
547549 }
548550
551+ @ Test
552+ public void testToUrlSafe () {
553+ byte [][] invalidUtf8 =
554+ new byte [][] {{(byte ) 0xfe }, {(byte ) 0xc1 , (byte ) 0xbf }, {(byte ) 0xc0 }, {(byte ) 0x80 }};
555+ for (byte [] bytes : invalidUtf8 ) {
556+ assertFalse (ByteString .copyFrom (bytes ).isValidUtf8 ());
557+ Cursor cursor = new Cursor (ByteString .copyFrom (bytes ));
558+ assertEquals (cursor , Cursor .fromUrlSafe (cursor .toUrlSafe ()));
559+ }
560+ }
561+
549562 @ Test
550563 public void testAllocateId () {
551564 KeyFactory keyFactory = datastore .newKeyFactory ().kind (KIND1 );
0 commit comments