File size: 4,570 Bytes
d289b5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main

import (
	"fmt"
	"os"
	"strings"
	"testing"
)

func TestExtractMissingHeader(t *testing.T) {
	missingHeaderMessage := `path/to/file.cpp:8:10: fatal error: 'missingheader.h' file not found

	#include "missingheader.h"

		^~~~~~~~~~~~

	1 error generated.
	`

	res, _ := ExtractMissingHeader(missingHeaderMessage)
	expected := "missingheader.h"
	if strings.Compare(res, expected) != 0 {
		t.Errorf("Got: %s. Expected: %s.", res, expected)
	}
}

func TestGetHeaderCorrectedFilename(t *testing.T) {
	missingHeaderMessage := `path/to/file.cpp:8:10: fatal error: 'missingheader.h' file not found

	#include "missingheader.h"

		^~~~~~~~~~~~

	1 error generated.
	`
	_, correctedFilename, _ := GetHeaderCorrectedFilename(missingHeaderMessage)
	expected := "path/to/jcc-corrected-file.cpp"
	if strings.Compare(correctedFilename, expected) != 0 {
		t.Errorf("Got: %s. Expected: %s.", correctedFilename, expected)
	}
}

func TestFindMissingHeader(t *testing.T) {
	pwd, _ := os.Getwd()
	t.Setenv("JCC_MISSING_HEADER_SEARCH_PATH", pwd)

	location, _ := FindMissingHeader("header.h")
	expected := pwd + "/testdata/path/to/header.h"
	if strings.Compare(location, expected) != 0 {
		t.Errorf("Got: %s. Expected: %s.", location, expected)
	}
}

func TestCorrectMissingHeaders(t *testing.T) {
	pwd, _ := os.Getwd()
	t.Setenv("JCC_MISSING_HEADER_SEARCH_PATH", pwd)
	cfile := pwd + "/testdata/cfile.c"
	cmd := [4]string{"-fsanitize=address", cfile, "-o", "/tmp/blah"}
	res, err := CorrectMissingHeaders("clang", cmd[:])
	if !res {
		fmt.Println(err)
		t.Errorf("Expected successful compilation")
	}
}

func TestGetHeaderCorrectedCmd(t *testing.T) {
	compilerErr := `testdata/cpp.cc:8:10: fatal error: 'missingheader.h' file not found

	#include "missingheader.h"

		^~~~~~~~~~~~

	1 error generated.
	`

	cmd := [3]string{"-fsanitize=address", "file.cpp", "path/to/cpp.cc"}
	expectedFixedCmd := [3]string{"-fanitize=address", "file.cpp", "path/to/jcc-corrected-cpp.cc"}
	fixedCmd, _, _ := GetHeaderCorrectedCmd(cmd[:], compilerErr)
	if strings.Compare(fixedCmd[1], expectedFixedCmd[1]) != 0 {
		t.Errorf("Expected %s, got: %s", expectedFixedCmd, fixedCmd)
	}
}

func TestCppifyHeaderIncludes(t *testing.T) {
	t.Setenv("JCC_CPPIFY_PROJECT_HEADERS", "1")
	src := `// Copyright blah
#include <stddef.h>

#include "fuzz.h"
#include "x/y.h"
extern "C" LLVMFuzzerTestOneInput(uint8_t* data, size_t sz) {
  return 0;
}`
	newFile, _ := CppifyHeaderIncludes(src)
	expected := `// Copyright blah
#include <stddef.h>

extern "C" {
#include "fuzz.h"
}
extern "C" {
#include "x/y.h"
}
extern "C" LLVMFuzzerTestOneInput(uint8_t* data, size_t sz) {
  return 0;
}
/* JCCCppifyHeadersMagicString */
`
	if strings.Compare(newFile, expected) != 0 {
		t.Errorf("Expected: %s, got: %s", expected, newFile)
	}
}

func TestCppifyHeaderIncludesShouldnt(t *testing.T) {
	src := `// Copyright blah
#include <stddef.h>

#include "fuzz.h"
#include "x/y.h"
extern "C" LLVMFuzzerTestOneInput(uint8_t* data, size_t sz) {
  return 0;
}`
	newFile, _ := CppifyHeaderIncludes(src)
	if strings.Compare(newFile, src) != 0 {
		t.Errorf("Expected: %s. Got: %s", src, newFile)
	}
}

func TestCppifyHeaderIncludesAlready(t *testing.T) {
	src := `// Copyright blah
#include <stddef.h>

#include "fuzz.h"
#include "x/y.h"
extern "C" LLVMFuzzerTestOneInput(uint8_t* data, size_t sz) {
  return 0;
}
/* JCCCppifyHeadersMagicString */
`
	newFile, _ := CppifyHeaderIncludes(src)
	if strings.Compare(newFile, src) != 0 {
		t.Errorf("Expected %s, got: %s", src, newFile)
	}
}

func TestExtractMissingHeaderNonHeaderFailure(t *testing.T) {
	missingHeaderMessage := `clang: error: no such file or directory: 'x'
clang: error: no input files`

	header, res := ExtractMissingHeader(missingHeaderMessage)
	if res {
		t.Errorf("Expected no match, got: %s", header)
	}
}

func TestReplaceMissingHeader(t *testing.T) {
	cfile := `// Copyright 2035 Robots
#include <stddef.h>

#include <cstdint>

// Some libraries like OpenSSL will use brackets for their own headers.
#include <missingheader.h>

int LLVMFuzzerTestOneInput(uint8_t* data,  size_t size) {
  return 0;
}
`

	res := ReplaceMissingHeader(cfile, "missingheader.h", "path/to/includes/missingheader.h")
	expected := `// Copyright 2035 Robots
#include <stddef.h>

#include <cstdint>

// Some libraries like OpenSSL will use brackets for their own headers.
#include "path/to/includes/missingheader.h"

int LLVMFuzzerTestOneInput(uint8_t* data,  size_t size) {
  return 0;
}
`
	if strings.Compare(res, expected) != 0 {
		t.Errorf("Got: %s. Expected: %s.", res, expected)
	}
}