/*
 * 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 {
      while (instance.read(buf, 0, 1000) != -1) {
        // do nothing with the bytes read
      }
    } catch (IOException e) {
      // this is expected
      return;
    }
    fail("should have failed");
  }

  @Test
  public void markNotSupported() throws Exception {
    ChecksumEnforcingInputStream testInstance = setUpData(1);
    assertFalse(testInstance.markSupported());
  }

  private ChecksumEnforcingInputStream setUpData(int payloadSize) throws Exception {
    // setup a String of size = input param: payloadSize
    String str = "This is a repeating string.";
    String payload;
    if (payloadSize > str.length()) {
      int num = payloadSize / str.length();
      StringBuffer buf = new StringBuffer();
      for (int i = 0; i < num; i++) {
        buf.append(str);
      }
      payload = buf.toString();
    } else {
      payload = str.substring(0, payloadSize);
    }
    byte[] bytes = payload.getBytes(UTF_8);
    String expectedChecksum = EndToEndChecksumHandler.computeChecksum(bytes);
    return new ChecksumEnforcingInputStream(
        new ByteArrayInputStream(bytes), expectedChecksum, digest);
  }
}

@@ -1232,80 +1232,8 @@
 n %7B%0A
-    ChecksumEnforcingInputStream testInstance = setUpData(payloadSize);%0A
     
@@ -1320,32 +1320,113 @@
 ect IOException%0A
+    try (ChecksumEnforcingInputStream testInstance = setUpData(payloadSize)) %7B%0A  
     byte%5B%5D buf =
@@ -1438,34 +1438,24 @@
 byte%5B1000%5D;%0A
-    try %7B%0A
       while 
@@ -1625,52 +1625,8 @@
 %22);%0A
-    %7D finally %7B%0A      testInstance.close();%0A
     

use try with resources