/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch 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.elasticsearch.bootstrap;

import org.apache.lucene.util.Constants;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;

public class EvilJNANativesTests extends ESTestCase {

    public void testSetMaximumNumberOfThreads() throws IOException {
        if (Constants.LINUX) {
            final List<String> lines = Files.readAllLines(PathUtils.get("/proc/self/limits"));
            if (!lines.isEmpty()) {
                for (final String line : lines) {
                    if (line != null && line.startsWith("Max processes")) {
                        final String[] fields = line.split("\\s+");
                        final long limit =
                                "unlimited".equals(fields[2])
                                        ? JNACLibrary.RLIM_INFINITY
                                        : Long.parseLong(fields[2]);
                        assertThat(JNANatives.MAX_NUMBER_OF_THREADS, equalTo(limit));
                        return;
                    }
                }
            }
            fail("should have read max processes from /proc/self/limits");
        } else {
            assertThat(JNANatives.MAX_NUMBER_OF_THREADS, equalTo(-1L));
        }
    }

    public void testSetMaxSizeVirtualMemory() throws IOException {
        if (Constants.LINUX) {
            final List<String> lines = Files.readAllLines(PathUtils.get("/proc/self/limits"));
            if (!lines.isEmpty()) {
                for (final String line : lines) {
                    if (line != null && line.startsWith("Max address space")) {
                        final String[] fields = line.split("\\s+");
                        final String limit = fields[3];
                        assertThat(
                                JNANatives.rlimitToString(JNANatives.MAX_SIZE_VIRTUAL_MEMORY),
                                equalTo(limit));
                        return;
                    }
                }
            }
            fail("should have read max size virtual memory from /proc/self/limits");
        } else if (Constants.MAC_OS_X) {
            assertThat(
                    JNANatives.MAX_SIZE_VIRTUAL_MEMORY,
                    anyOf(equalTo(Long.MIN_VALUE), greaterThanOrEqualTo(0L)));
        } else {
            assertThat(JNANatives.MAX_SIZE_VIRTUAL_MEMORY, equalTo(Long.MIN_VALUE));
        }
    }

    public void testSetMaxFileSize() throws IOException {
        if (Constants.LINUX) {
            final List<String> lines = Files.readAllLines(PathUtils.get("/proc/self/limits"));
            for (final String line : lines) {
                if (line != null && line.startsWith("Max file size")) {
                    final String[] fields = line.split("\\s+");
                    final String limit = fields[3];
                    assertThat(
                            JNANatives.rlimitToString(JNANatives.MAX_FILE_SIZE),
                            equalTo(limit));
                    return;
                }
            }
            fail("should have read max file size from /proc/self/limits");
        } else if (Constants.MAC_OS_X) {
            assertThat(
                    JNANatives.MAX_FILE_SIZE,
                    anyOf(equalTo(Long.MIN_VALUE), greaterThanOrEqualTo(0L)));
        } else {
            assertThat(JNANatives.MAX_FILE_SIZE, equalTo(Long.MIN_VALUE));
        }
    }

}

@@ -1432,48 +1432,8 @@
 ));%0A
-            if (!lines.isEmpty()) %7B%0A    
     
@@ -1466,36 +1466,32 @@
 line : lines) %7B%0A
-    
                 
@@ -1558,36 +1558,32 @@
                 
-  
-  
 final String%5B%5D f
@@ -1602,36 +1602,32 @@
 .split(%22%5C%5Cs+%22);%0A
-    
                 
@@ -1677,20 +1677,16 @@
         
-    
 %22unlimit
@@ -1743,20 +1743,16 @@
         
-  
-  
 ? JNACLi
@@ -1771,20 +1771,16 @@
 NFINITY%0A
-    
         
@@ -1848,36 +1848,32 @@
                 
-    
 assertThat(JNANa
@@ -1934,36 +1934,32 @@
             
- 
-   
 return;%0A
             
@@ -1946,38 +1946,16 @@
 return;%0A
-                    %7D%0A
         
@@ -1980,34 +1980,34 @@
     %7D%0A          
+  
-  
 fail(%22should hav
@@ -2356,48 +2356,8 @@
 ));%0A
-            if (!lines.isEmpty()) %7B%0A    
     
@@ -2390,36 +2390,32 @@
 line : lines) %7B%0A
-    
                 
@@ -2486,36 +2486,32 @@
                 
-    
 final String%5B%5D f
@@ -2550,36 +2550,32 @@
                 
-    
 final String lim
@@ -2582,36 +2582,32 @@
 it = fields%5B3%5D;%0A
-    
                 
@@ -2642,36 +2642,32 @@
                 
-    
 JNANatives.rlimi
@@ -2733,36 +2733,32 @@
                 
-    
 equalTo(limit));
@@ -2774,36 +2774,32 @@
             
-   
- 
 return;%0A
             
@@ -2790,30 +2790,8 @@
 rn;%0A
-                    %7D%0A
     

Remove useless checks from evil JNA tests