1+ package service
2+
3+ import (
4+ "testing"
5+
6+ "github.com/langgenius/dify-plugin-daemon/pkg/entities/plugin_entities"
7+ "github.com/langgenius/dify-plugin-daemon/pkg/plugin_packager/decoder"
8+ )
9+
10+ func TestIsUnauthorizedLanggenius (t * testing.T ) {
11+ // Tests for isUnauthorizedLanggenius function
12+ // This function is used when ENFORCE_LANGGENIUS_PLUGIN_SIGNATURES=true (default)
13+ // to prevent unauthorized plugins from impersonating Langgenius
14+ tests := []struct {
15+ name string
16+ author string
17+ verification * decoder.Verification
18+ want bool
19+ }{
20+ {
21+ name : "langgenius author with proper verification" ,
22+ author : "langgenius" ,
23+ verification : & decoder.Verification {
24+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_LANGGENIUS ,
25+ },
26+ want : false , // properly authorized
27+ },
28+ {
29+ name : "langgenius author with partner verification" ,
30+ author : "langgenius" ,
31+ verification : & decoder.Verification {
32+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_PARTNER ,
33+ },
34+ want : true , // unauthorized - claims langgenius but verified as partner
35+ },
36+ {
37+ name : "langgenius author with community verification" ,
38+ author : "langgenius" ,
39+ verification : & decoder.Verification {
40+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_COMMUNITY ,
41+ },
42+ want : true , // unauthorized - claims langgenius but verified as community
43+ },
44+ {
45+ name : "langgenius author without verification" ,
46+ author : "langgenius" ,
47+ verification : nil ,
48+ want : true , // unauthorized - claims langgenius but no verification
49+ },
50+ {
51+ name : "Langgenius author (capital L) with proper verification" ,
52+ author : "Langgenius" ,
53+ verification : & decoder.Verification {
54+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_LANGGENIUS ,
55+ },
56+ want : false , // properly authorized (case-insensitive)
57+ },
58+ {
59+ name : "LANGGENIUS author (all caps) with proper verification" ,
60+ author : "LANGGENIUS" ,
61+ verification : & decoder.Verification {
62+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_LANGGENIUS ,
63+ },
64+ want : false , // properly authorized (case-insensitive)
65+ },
66+ {
67+ name : "LANGGENIUS author (all caps) without verification" ,
68+ author : "LANGGENIUS" ,
69+ verification : nil ,
70+ want : true , // unauthorized - claims langgenius but no verification
71+ },
72+ {
73+ name : "community author with community verification" ,
74+ author : "community_developer" ,
75+ verification : & decoder.Verification {
76+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_COMMUNITY ,
77+ },
78+ want : false , // authorized - doesn't claim langgenius
79+ },
80+ {
81+ name : "partner author with partner verification" ,
82+ author : "partner_company" ,
83+ verification : & decoder.Verification {
84+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_PARTNER ,
85+ },
86+ want : false , // authorized - doesn't claim langgenius
87+ },
88+ {
89+ name : "community author without verification" ,
90+ author : "john_doe" ,
91+ verification : nil ,
92+ want : false , // allowed - doesn't claim langgenius
93+ },
94+ {
95+ name : "empty author with langgenius verification" ,
96+ author : "" ,
97+ verification : & decoder.Verification {
98+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_LANGGENIUS ,
99+ },
100+ want : false , // allowed - doesn't claim langgenius
101+ },
102+ {
103+ name : "empty author without verification" ,
104+ author : "" ,
105+ verification : nil ,
106+ want : false , // allowed - doesn't claim langgenius
107+ },
108+ {
109+ name : "author contains langgenius but not exact match" ,
110+ author : "not_langgenius" ,
111+ verification : & decoder.Verification {
112+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_COMMUNITY ,
113+ },
114+ want : false , // allowed - not exact match
115+ },
116+ {
117+ name : "author langgenius_team" ,
118+ author : "langgenius_team" ,
119+ verification : & decoder.Verification {
120+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_COMMUNITY ,
121+ },
122+ want : false , // allowed - not exact match
123+ },
124+ {
125+ name : "author my_langgenius" ,
126+ author : "my_langgenius" ,
127+ verification : & decoder.Verification {
128+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_COMMUNITY ,
129+ },
130+ want : false , // allowed - not exact match
131+ },
132+ }
133+
134+ for _ , tt := range tests {
135+ t .Run (tt .name , func (t * testing.T ) {
136+ declaration := & plugin_entities.PluginDeclaration {
137+ PluginDeclarationWithoutAdvancedFields : plugin_entities.PluginDeclarationWithoutAdvancedFields {
138+ Author : tt .author ,
139+ },
140+ }
141+
142+ got := isUnauthorizedLanggenius (declaration , tt .verification )
143+ if got != tt .want {
144+ t .Errorf ("isUnauthorizedLanggenius() = %v, want %v" , got , tt .want )
145+ }
146+ })
147+ }
148+ }
149+
150+ func TestIsUnauthorizedLanggenius_EdgeCases (t * testing.T ) {
151+ tests := []struct {
152+ name string
153+ author string
154+ verification * decoder.Verification
155+ want bool
156+ }{
157+ {
158+ name : "langgenius with spaces" ,
159+ author : " langgenius " ,
160+ verification : & decoder.Verification {
161+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_LANGGENIUS ,
162+ },
163+ want : false , // spaces don't affect the comparison after lowercase
164+ },
165+ {
166+ name : "langgenius with spaces but no verification" ,
167+ author : " langgenius " ,
168+ verification : nil ,
169+ want : false , // with spaces, not exact match after lowercase
170+ },
171+ {
172+ name : "LaNgGeNiUs mixed case" ,
173+ author : "LaNgGeNiUs" ,
174+ verification : & decoder.Verification {
175+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_LANGGENIUS ,
176+ },
177+ want : false , // properly authorized (case-insensitive)
178+ },
179+ {
180+ name : "langgenius. with punctuation" ,
181+ author : "langgenius." ,
182+ verification : & decoder.Verification {
183+ AuthorizedCategory : decoder .AUTHORIZED_CATEGORY_COMMUNITY ,
184+ },
185+ want : false , // not exact match due to punctuation
186+ },
187+ }
188+
189+ for _ , tt := range tests {
190+ t .Run (tt .name , func (t * testing.T ) {
191+ declaration := & plugin_entities.PluginDeclaration {
192+ PluginDeclarationWithoutAdvancedFields : plugin_entities.PluginDeclarationWithoutAdvancedFields {
193+ Author : tt .author ,
194+ },
195+ }
196+
197+ got := isUnauthorizedLanggenius (declaration , tt .verification )
198+ if got != tt .want {
199+ t .Errorf ("isUnauthorizedLanggenius() = %v, want %v for author=%q" , got , tt .want , tt .author )
200+ }
201+ })
202+ }
203+ }
0 commit comments