Browse code

Rewrite the function 'validatePrivileges' without checking order

Signed-off-by: Yanqiang Miao <miao.yanqiang@zte.com.cn>

Yanqiang Miao authored on 2016/12/28 12:38:13
Showing 3 changed files
... ...
@@ -3,6 +3,7 @@ package types
3 3
 import (
4 4
 	"encoding/json"
5 5
 	"fmt"
6
+	"sort"
6 7
 )
7 8
 
8 9
 // PluginsListResponse contains the response for the Engine API
... ...
@@ -62,3 +63,17 @@ type PluginPrivilege struct {
62 62
 
63 63
 // PluginPrivileges is a list of PluginPrivilege
64 64
 type PluginPrivileges []PluginPrivilege
65
+
66
+func (s PluginPrivileges) Len() int {
67
+	return len(s)
68
+}
69
+
70
+func (s PluginPrivileges) Less(i, j int) bool {
71
+	return s[i].Name < s[j].Name
72
+}
73
+
74
+func (s PluginPrivileges) Swap(i, j int) {
75
+	sort.Strings(s[i].Value)
76
+	sort.Strings(s[j].Value)
77
+	s[i], s[j] = s[j], s[i]
78
+}
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"path/filepath"
9 9
 	"reflect"
10 10
 	"regexp"
11
+	"sort"
11 12
 	"strings"
12 13
 	"sync"
13 14
 
... ...
@@ -289,13 +290,38 @@ func attachToLog(id string) func(libcontainerd.IOPipe) error {
289 289
 }
290 290
 
291 291
 func validatePrivileges(requiredPrivileges, privileges types.PluginPrivileges) error {
292
-	// todo: make a better function that doesn't check order
293
-	if !reflect.DeepEqual(privileges, requiredPrivileges) {
292
+	if !isEqual(requiredPrivileges, privileges, isEqualPrivilege) {
294 293
 		return errors.New("incorrect privileges")
295 294
 	}
295
+
296 296
 	return nil
297 297
 }
298 298
 
299
+func isEqual(arrOne, arrOther types.PluginPrivileges, compare func(x, y types.PluginPrivilege) bool) bool {
300
+	if len(arrOne) != len(arrOther) {
301
+		return false
302
+	}
303
+
304
+	sort.Sort(arrOne)
305
+	sort.Sort(arrOther)
306
+
307
+	for i := 1; i < arrOne.Len(); i++ {
308
+		if !compare(arrOne[i], arrOther[i]) {
309
+			return false
310
+		}
311
+	}
312
+
313
+	return true
314
+}
315
+
316
+func isEqualPrivilege(a, b types.PluginPrivilege) bool {
317
+	if a.Name != b.Name {
318
+		return false
319
+	}
320
+
321
+	return reflect.DeepEqual(a.Value, b.Value)
322
+}
323
+
299 324
 func configToRootFS(c []byte) (*image.RootFS, error) {
300 325
 	var pluginConfig types.PluginConfig
301 326
 	if err := json.Unmarshal(c, &pluginConfig); err != nil {
302 327
new file mode 100644
... ...
@@ -0,0 +1,55 @@
0
+package plugin
1
+
2
+import (
3
+	"testing"
4
+
5
+	"github.com/docker/docker/api/types"
6
+)
7
+
8
+func TestValidatePrivileges(t *testing.T) {
9
+	testData := map[string]struct {
10
+		requiredPrivileges types.PluginPrivileges
11
+		privileges         types.PluginPrivileges
12
+		result             bool
13
+	}{
14
+		"diff-len": {
15
+			requiredPrivileges: []types.PluginPrivilege{
16
+				{"Privilege1", "Description", []string{"abc", "def", "ghi"}},
17
+			},
18
+			privileges: []types.PluginPrivilege{
19
+				{"Privilege1", "Description", []string{"abc", "def", "ghi"}},
20
+				{"Privilege2", "Description", []string{"123", "456", "789"}},
21
+			},
22
+			result: false,
23
+		},
24
+		"diff-value": {
25
+			requiredPrivileges: []types.PluginPrivilege{
26
+				{"Privilege1", "Description", []string{"abc", "def", "GHI"}},
27
+				{"Privilege2", "Description", []string{"123", "456", "***"}},
28
+			},
29
+			privileges: []types.PluginPrivilege{
30
+				{"Privilege1", "Description", []string{"abc", "def", "ghi"}},
31
+				{"Privilege2", "Description", []string{"123", "456", "789"}},
32
+			},
33
+			result: false,
34
+		},
35
+		"diff-order-but-same-value": {
36
+			requiredPrivileges: []types.PluginPrivilege{
37
+				{"Privilege1", "Description", []string{"abc", "def", "GHI"}},
38
+				{"Privilege2", "Description", []string{"123", "456", "789"}},
39
+			},
40
+			privileges: []types.PluginPrivilege{
41
+				{"Privilege2", "Description", []string{"123", "456", "789"}},
42
+				{"Privilege1", "Description", []string{"GHI", "abc", "def"}},
43
+			},
44
+			result: true,
45
+		},
46
+	}
47
+
48
+	for key, data := range testData {
49
+		err := validatePrivileges(data.requiredPrivileges, data.privileges)
50
+		if (err == nil) != data.result {
51
+			t.Fatalf("Test item %s expected result to be %t, got %t", key, data.result, (err == nil))
52
+		}
53
+	}
54
+}