1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dexfuzz;
18 
19 import java.util.List;
20 
21 /**
22  * Stores the output of an executed command.
23  */
24 public class ExecutionResult {
25   public List<String> output;
26   public List<String> error;
27   public int returnValue;
28 
29   private String flattenedOutput;
30   private String flattenedOutputWithNewlines;
31   private String flattenedError;
32   private String flattenedErrorWithNewlines;
33   private String flattenedAll;
34   private String flattenedAllWithNewlines;
35 
36   private static final int TIMEOUT_RETURN_VALUE = 124;
37   private static final int SIGABORT_RETURN_VALUE = 134;
38 
39   /**
40    * Get only the output, with all lines concatenated together, excluding newline characters.
41    */
getFlattenedOutput()42   public String getFlattenedOutput() {
43     if (flattenedOutput == null) {
44       StringBuilder builder = new StringBuilder();
45       for (String line : output) {
46         builder.append(line);
47       }
48       flattenedOutput = builder.toString();
49     }
50     return flattenedOutput;
51   }
52 
53   /**
54    * Get only the output, with all lines concatenated together, including newline characters.
55    */
getFlattenedOutputWithNewlines()56   public String getFlattenedOutputWithNewlines() {
57     if (flattenedOutputWithNewlines == null) {
58       StringBuilder builder = new StringBuilder();
59       for (String line : output) {
60         builder.append(line).append("\n");
61       }
62       flattenedOutputWithNewlines = builder.toString();
63     }
64     return flattenedOutputWithNewlines;
65   }
66 
67   /**
68    * Get only the error, with all lines concatenated together, excluding newline characters.
69    */
getFlattenedError()70   public String getFlattenedError() {
71     if (flattenedError == null) {
72       StringBuilder builder = new StringBuilder();
73       for (String line : error) {
74         builder.append(line);
75       }
76       flattenedError = builder.toString();
77     }
78     return flattenedError;
79   }
80 
81   /**
82    * Get only the error, with all lines concatenated together, including newline characters.
83    */
getFlattenedErrorWithNewlines()84   public String getFlattenedErrorWithNewlines() {
85     if (flattenedErrorWithNewlines == null) {
86       StringBuilder builder = new StringBuilder();
87       for (String line : error) {
88         builder.append(line).append("\n");
89       }
90       flattenedErrorWithNewlines = builder.toString();
91     }
92     return flattenedErrorWithNewlines;
93   }
94 
95   /**
96    * Get both the output and error, concatenated together, excluding newline characters.
97    */
getFlattenedAll()98   public String getFlattenedAll() {
99     if (flattenedAll == null) {
100       flattenedAll = getFlattenedOutput() + getFlattenedError();
101     }
102     return flattenedAll;
103   }
104 
105   /**
106    * Get both the output and error, concatenated together, including newline characters.
107    */
getFlattenedAllWithNewlines()108   public String getFlattenedAllWithNewlines() {
109     if (flattenedAllWithNewlines == null) {
110       flattenedAllWithNewlines = getFlattenedOutputWithNewlines() + getFlattenedErrorWithNewlines();
111     }
112     return flattenedAllWithNewlines;
113   }
114 
isTimeout()115   public boolean isTimeout() {
116     return (returnValue == TIMEOUT_RETURN_VALUE);
117   }
118 
isSigabort()119   public boolean isSigabort() {
120     return (returnValue == SIGABORT_RETURN_VALUE);
121   }
122 }