main_test.go 8.18 KB
Newer Older
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
1
2
3
package main

import (
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
4
	"fmt"
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
5
6
7
	"testing"
)

Thomas Hoffmann's avatar
Thomas Hoffmann committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
func TestBloodTable(t *testing.T) {
	// Check the dimensions of BloodTable.

	if len(BloodTable) != 8 {
		t.Fatalf("Expected 8 rows, got %d", len(BloodTable))
	}

	for i := range BloodTable {
		if len(BloodTable[i]) != 8 {
			t.Fatalf("Expected columns in row %d, got %d", i, len(BloodTable))
		}
	}
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
22
23
24
////////////////////////////////////////////////////////////////
//// Party Tests ////
////////////////////////////////////////////////////////////////
Thomas Hoffmann's avatar
Thomas Hoffmann committed
25

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
26
27
28
func TestPartyWires(t *testing.T) {
	t.Run("WireSize|Input", TestPartyInput)
	t.Run("WireSize|Receive", TestPartyReceive)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
29
30
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
31
32
33
34
35
func TestPartyInput(t *testing.T) {
	A := NewProtocol().A
	idx := A.Input(true)
	if len(A.wires) != 1 {
		t.Errorf("Expected len(A.wires) == %d, was %d", 1, idx)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
36
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
37
38
	if idx != 0 {
		t.Errorf("Expected idx == %d, was %d", 0, idx)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
39
40
41
	}
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
42
43
44
45
46
47
48
func TestPartyReceive(t *testing.T) {
	p := NewProtocol()
	A, B := p.A, p.B
	A.Input(true)
	idx := B.Receive()
	if len(B.wires) != 1 {
		t.Errorf("Expected len(A.wires) == %d, was %d", 1, idx)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
49
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
50
51
	if idx != 0 {
		t.Errorf("Expected idx == %d, was %d", 0, idx)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
52
53
54
	}
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
55
56
57
58
59
60
61
62
func TestPartyCombine(t *testing.T) {
	// TODO
	p := NewProtocol()
	A, B := p.A, p.B

	a := A.Input(true)
	B.Receive()
	b := A.Input(false)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
63

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
64
65
66
	ASizeBefore, BSizeBefore := len(p.A.wires), len(p.B.wires)
	idx := p.A.Combine(a, b)
	if len(p.A.wires) != ASizeBefore+1 && len(p.B.wires) != BSizeBefore {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
67
68
69
		t.Error()
	}
	if idx != 2 {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
70
		t.Errorf("Expected idx == %d, was %d", 2, idx)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
71
72
73
	}
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
74
75
76
func TestPartyXORC(t *testing.T) {
	A := NewProtocol().A
	A.Input(true)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
77

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
78
79
80
81
82
83
	idx1 := A.XORC(0, true, true)
	if len(A.wires) != 2 {
		t.Errorf("Expected len(A.wires) == %d, was %d", 2, len(A.wires))
	}
	if idx1 != 1 {
		t.Errorf("Expected idx1 == %d, was %d", 1, idx1)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
84
85
	}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
86
87
88
	idx2 := A.XORC(0, true, false)
	if len(A.wires) != 2 {
		t.Errorf("Expected len(A.wires) == %d, was %d", 2, len(A.wires))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
89
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
90
91
	if idx2 != 0 {
		t.Errorf("Expected idx2 == %d, was %d", 0, idx2)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
92
93
94
	}
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
95
96
97
func TestPartyANDC(t *testing.T) {
	A := NewProtocol().A
	A.Input(true)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
98

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
99
100
101
	idx := A.ANDC(0, true)
	if len(A.wires) != 2 {
		t.Errorf("Expected len(A.wires) == %d, was %d", 2, len(A.wires))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
102
103
	}
	if idx != 1 {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
104
		t.Errorf("Expected idx == %d, was %d", 1, idx)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
105
106
107
	}
}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
108
109
110
111
112
113
114
115
116
117
118
func TestPartyXOR(t *testing.T) {
	p := NewProtocol()
	A, B := p.A, p.B

	A.Input(true)
	B.Receive()
	A.Input(false)

	idx := A.XOR2W(0, 1)
	if len(A.wires) != 3 {
		t.Errorf("Expected len(A.wires) == %d, was %d", 3, len(A.wires))
Thomas Hoffmann's avatar
Thomas Hoffmann committed
119
120
	}
	if idx != 2 {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
121
		t.Errorf("Expected idx == %d, was %d", 2, idx)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
122
123
124
125
	}
}

func TestStackAND(t *testing.T) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
126
127
	p := NewProtocol()
	a := p.A.Input(true)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
128
	p.B.Receive()
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
129
	b := p.A.Input(false)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
130
131
	p.B.Receive()

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
132
133
	idx := p.AND(a, b)
	if len(p.A.wires) != 17 || len(p.B.wires) != 17 {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
		t.Error()
	}
	if idx != 16 {
		t.Error()
	}
	t.Log("Success")
}

////////////////////////////////////////////////////////////////
//// Protocol Tests ////
////////////////////////////////////////////////////////////////

func TestProtocolFunctions(t *testing.T) {

}

func TestProtocolInput(t *testing.T) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
151
152
153
	p := NewProtocol()
	idxA, idxB := p.Input(true, false)
	shareA, shareB := p.A.wires[idxA] != p.B.wires[idxA], p.A.wires[idxB] != p.B.wires[idxB]
Thomas Hoffmann's avatar
Thomas Hoffmann committed
154
155
156
	if shareA != true || shareB != false {
		t.Fatal("Shares has wrong values")
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
157
	if len(p.A.wires) != len(p.B.wires) {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
158
159
160
161
162
		t.Fatal("Party stack size is not equal after function call")
	}
}

func TestProtocolXORC(t *testing.T) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
163
164
	p := NewProtocol()
	idxTrue, idxFalse := p.Input(true, false)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
165
166

	//True XOR True
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
167
168
169
170
171
172
173
174
	idxTT := p.XORC(idxTrue, true)
	idxTF := p.XORC(idxTrue, false)
	idxFT := p.XORC(idxFalse, true)
	idxFF := p.XORC(idxFalse, false)
	resTT, resTF, resFT, resFF := p.A.wires[idxTT] != p.B.wires[idxTT],
		p.A.wires[idxTF] != p.B.wires[idxTF],
		p.A.wires[idxFT] != p.B.wires[idxFT],
		p.A.wires[idxFF] != p.B.wires[idxFF]
Thomas Hoffmann's avatar
Thomas Hoffmann committed
175
176
177
	if resTT != false || resTF != true || resFT != true || resFF != false {
		t.Fatal("Shares has wrong values")
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
178
	if len(p.A.wires) != len(p.B.wires) {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
179
180
181
182
183
		t.Fatal("Party stack size is not equal after function call")
	}
}

func TestProtocolANDC(t *testing.T) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
184
185
	p := NewProtocol()
	idxTrue, idxFalse := p.Input(true, false)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
186
187

	//True XOR True
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
188
189
190
191
192
193
194
195
	idxTT := p.ANDC(idxTrue, true)
	idxTF := p.ANDC(idxTrue, false)
	idxFT := p.ANDC(idxFalse, true)
	idxFF := p.ANDC(idxFalse, false)
	resTT, resTF, resFT, resFF := p.A.wires[idxTT] != p.B.wires[idxTT],
		p.A.wires[idxTF] != p.B.wires[idxTF],
		p.A.wires[idxFT] != p.B.wires[idxFT],
		p.A.wires[idxFF] != p.B.wires[idxFF]
Thomas Hoffmann's avatar
Thomas Hoffmann committed
196
197
198
	if resTT != true || resTF != false || resFT != false || resFF != false {
		t.Fatal("Shares has wrong values")
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
199
	if len(p.A.wires) != len(p.B.wires) {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
200
201
202
203
204
		t.Fatal("Party stack size is not equal after function call")
	}
}

func TestProtocolXOR2W(t *testing.T) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
205
206
	p := NewProtocol()
	idxTrue, idxFalse := p.Input(true, false)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
207
208

	//True XOR True
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
209
210
211
212
213
214
215
216
	idxTT := p.XOR2W(idxTrue, idxTrue)
	idxTF := p.XOR2W(idxTrue, idxFalse)
	idxFT := p.XOR2W(idxFalse, idxTrue)
	idxFF := p.XOR2W(idxFalse, idxFalse)
	resTT, resTF, resFT, resFF := p.A.wires[idxTT] != p.B.wires[idxTT],
		p.A.wires[idxTF] != p.B.wires[idxTF],
		p.A.wires[idxFT] != p.B.wires[idxFT],
		p.A.wires[idxFF] != p.B.wires[idxFF]
Thomas Hoffmann's avatar
Thomas Hoffmann committed
217
218
219
	if resTT != false || resTF != true || resFT != true || resFF != false {
		t.Fatal("Shares has wrong values")
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
220
	if len(p.A.wires) != len(p.B.wires) {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
221
222
223
224
225
		t.Fatal("Party stack size is not equal after function call")
	}
}

func TestProtocolNOT(t *testing.T) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
226
227
	p := NewProtocol()
	idxTrue, idxFalse := p.Input(true, false)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
228

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
229
230
	idxT := p.NOT(idxTrue)
	idxF := p.NOT(idxFalse)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
231

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
232
233
	resT, resF := p.A.wires[idxT] != p.B.wires[idxF],
		p.A.wires[idxF] != p.B.wires[idxF]
Thomas Hoffmann's avatar
Thomas Hoffmann committed
234
235
236
	if resT != false || resF != true {
		t.Fatal("Shares has wrong values")
	}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
237
	if len(p.A.wires) != len(p.B.wires) {
Thomas Hoffmann's avatar
Thomas Hoffmann committed
238
239
240
241
242
		t.Fatal("Party stack size is not equal after function call")
	}
}

func TestProtocolOUTPUT(t *testing.T) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
243
244
	p := NewProtocol()
	idxTrue, idxFalse := p.Input(true, false)
Thomas Hoffmann's avatar
Thomas Hoffmann committed
245
246

	for i := 0; i < 8; i++ {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
247
248
		params := Int2Bools(i, 3)
		var idx int
Thomas Hoffmann's avatar
Thomas Hoffmann committed
249
250
251
252
253
254
		if params[0] {
			idx = idxTrue
		} else {
			idx = idxFalse
		}

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
255
		res := p.Output(idx, params[1], params[2])
Thomas Hoffmann's avatar
Thomas Hoffmann committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
		if params[1] == false && params[2] == false {
			if len(res) != 0 {
				t.Fatal("output should be empty")
			}
		} else if params[1] == true && params[2] == true {
			if len(res) != 2 {
				t.Fatal("output should have 2 values")
			}
			if res[0] != params[0] || res[1] != params[0] {
				t.Error("Output does not match input")
			}
		} else {
			if len(res) != 1 {
				t.Fatal("output should have 1 value")
			}
			if res[0] != params[0] {
				t.Error("Output does not match input")
			}
		}
	}
}
Thomas Hoffmann's avatar
Merge    
Thomas Hoffmann committed
277

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
278
279
func AssertBloodType(t *testing.T, bloodType int, expected []bool) {
	actual := Int2Bools(bloodType, 3)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
280
281

	if len(actual) != len(expected) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
282
		t.Fatalf("Expected length to be %d, was %d", len(expected), len(actual))
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
283
284
285
286
287
288
289
290
291
292
	}

	for i := 0; i < 3; i++ {
		if actual[i] != expected[i] {
			t.Errorf("Expected %t at index %d, got %t", expected[i], i, actual[i])
		}
	}
}

func TestInt2Bools(t *testing.T) {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
293
294
295
296
297
298
299
300
	t.Run("O-", func(t *testing.T) { AssertBloodType(t, BloodType_On, []bool{false, false, false}) })
	t.Run("O+", func(t *testing.T) { AssertBloodType(t, BloodType_Op, []bool{true, false, false}) })
	t.Run("A-", func(t *testing.T) { AssertBloodType(t, BloodType_An, []bool{false, true, false}) })
	t.Run("A+", func(t *testing.T) { AssertBloodType(t, BloodType_Ap, []bool{true, true, false}) })
	t.Run("B-", func(t *testing.T) { AssertBloodType(t, BloodType_Bn, []bool{false, false, true}) })
	t.Run("B+", func(t *testing.T) { AssertBloodType(t, BloodType_Bp, []bool{true, false, true}) })
	t.Run("AB-", func(t *testing.T) { AssertBloodType(t, BloodType_ABn, []bool{false, true, true}) })
	t.Run("AB+", func(t *testing.T) { AssertBloodType(t, BloodType_ABp, []bool{true, true, true}) })
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
301
}
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
302
303
304
305

func TestProtocol(t *testing.T) {
	// Runs the protocol for all combinations of recipient and donor blood types.
	n := len(BloodTable)
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
306

Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
307
308
	for x := 0; x < n; x++ {
		for y := 0; y < n; y++ {
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
309
310
311
312
313
314
315
316
317
			testName := fmt.Sprintf("(x=%s,y=%s)", BloodNames[x], BloodNames[y])
			t.Run(testName, func(t *testing.T) {
				z, err := RunProtocol(x, y)
				if err != nil {
					t.Fatal(fmt.Sprintf("Failed blood compatibility test for index [%d,%d]", x, y), err)
				} else if z != BloodTable[x][y] {
					t.Fatalf("Failed blood compatibility test for index [%d,%d]. Expected %t, got %t", x, y, !z, z)
				}
			})
Anders Jensen Løvig's avatar
Anders Jensen Løvig committed
318
319
320
		}
	}
}