<commit_before>/*
 * Copyright 2021 Google LLC
 *
 * Licensed 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 com.google.datastore.v1.client;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Test for {@link ChecksumEnforcingInputStream}. */
@RunWith(JUnit4.class)
public class ChecksumEnforcingInputStreamTest {
  private final MessageDigest digest = EndToEndChecksumHandler.getMessageDigestInstance();

  public void test(int payloadSize) throws Exception {
    ChecksumEnforcingInputStream testInstance = setUpData(payloadSize);
    // read 1000 bytes at a time
    // Since checksum should be correct, do not expect IOException
    byte[] buf = new byte[1000];
    try {
      while (testInstance.read(buf, 0, 1000) != -1) {
        // do nothing with the bytes read
      }
    } catch (IOException e) {
      fail("checksum verification failed!");
    }
  }

  @Test
  public void read_withValidChecksum_differentPayloadSizes() throws Exception {
    // test with various payload sizes (1, 2, 2**2, 2**3 etc upto 2**28 = 256MB)
    for (int i = 0, payloadSize = 1; i < 29; i++) {
      long start = System.currentTimeMillis();
      test(payloadSize);
      payloadSize *= 2;
      long duration = System.currentTimeMillis() - start;
      // log test duration times for bigger payloads
      if (i > 20) {
        System.out.println("Test duration for payloadsize = 2** " + i + " is: " + duration + "ms");
      }
    }
  }

  @Test
  public void read_withInvalidChecksum() {
    // build a test instance with invalidchecksum
    ChecksumEnforcingInputStream instance =
        new ChecksumEnforcingInputStream(
            new ByteArrayInputStream("hello there".getBytes(UTF_8)),
            "this checksum is invalid",
            digest);
    // read 1000 bytes at a time
    // Since checksum should be correct, do not expect IOException
    byte[] buf = new byte[1000];
    try {<commit_msg>close input stream after each run<commit_after>/*
 * Copyright 2021 Google LLC
 *
 * Licensed 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 com.google.datastore.v1.client;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Test for {@link ChecksumEnforcingInputStream}. */
@RunWith(JUnit4.class)
public class ChecksumEnforcingInputStreamTest {
  private final MessageDigest digest = EndToEndChecksumHandler.getMessageDigestInstance();

  public void test(int payloadSize) throws Exception {
    ChecksumEnforcingInputStream testInstance = setUpData(payloadSize);
    // read 1000 bytes at a time
    // Since checksum should be correct, do not expect IOException
    byte[] buf = new byte[1000];
    try {
      while (testInstance.read(buf, 0, 1000) != -1) {
        // do nothing with the bytes read
      }
    } catch (IOException e) {
      fail("checksum verification failed!");
    } finally {
      testInstance.close();
    }
  }

  @Test
  public void read_withValidChecksum_differentPayloadSizes() throws Exception {
    // test with various payload sizes (1, 2, 2**2, 2**3 etc upto 2**28 = 256MB)
    for (int i = 0, payloadSize = 1; i < 29; i++) {
      long start = System.currentTimeMillis();
      test(payloadSize);
      payloadSize *= 2;
      long duration = System.currentTimeMillis() - start;
      // log test duration times for bigger payloads
      if (i > 20) {
        System.out.println("Test duration for payloadsize = 2** " + i + " is: " + duration + "ms");
      }
    }
  }

  @Test
  public void read_withInvalidChecksum() {
    // build a test instance with invalidchecksum
    ChecksumEnforcingInputStream instance =
        new ChecksumEnforcingInputStream(
            new ByteArrayInputStream("hello there".getBytes(UTF_8)),
            "this checksum is invalid",
            digest);
    // read 1000 bytes at a time
    // Since checksum should be correct, do not expect IOException
    byte[] buf = new byte[1000];
    try {
{'fraction_non_alphanumeric': 0.09394495412844037, 'fraction_numerical': 0.02018348623853211, 'mean_word_length': 3.309090909090909, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '425516', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1499, 'n_words': 535}