access_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package access
  2. import (
  3. "testing"
  4. "imuslab.com/arozos/mod/filesystem/arozfs"
  5. )
  6. func TestLevelsMatchArozfs(t *testing.T) {
  7. if None != arozfs.FsDenied || Read != arozfs.FsReadOnly || ReadWrite != arozfs.FsReadWrite {
  8. t.Fatalf("levels %q %q %q differ from arozfs", None, Read, ReadWrite)
  9. }
  10. }
  11. func TestExplain(t *testing.T) {
  12. familyOnly := Rule{Other: None, Groups: map[string]string{"family": ReadWrite}}
  13. notInterns := Rule{Other: ReadWrite, Groups: map[string]string{"interns": None}}
  14. staffRead := Rule{Other: None, Groups: map[string]string{"staff": Read}}
  15. tests := []struct {
  16. name string
  17. drive Drive
  18. subject Subject
  19. rule Rule
  20. wantLevel string
  21. wantReason string
  22. wantGroup string
  23. wantCapped bool
  24. }{
  25. {
  26. name: "user drive ignores the rule",
  27. drive: Drive{UUID: UserDriveID},
  28. subject: Subject{},
  29. rule: Rule{Other: None},
  30. wantLevel: ReadWrite,
  31. wantReason: ReasonFixed,
  32. },
  33. {
  34. name: "tmp drive ignores the rule",
  35. drive: Drive{UUID: TmpDriveID},
  36. subject: Subject{Groups: []string{"guests"}},
  37. rule: Rule{Other: None, Groups: map[string]string{"guests": None}},
  38. wantLevel: ReadWrite,
  39. wantReason: ReasonFixed,
  40. },
  41. {
  42. name: "home group always has full access",
  43. drive: Drive{UUID: "photos", HomeGroup: "family"},
  44. subject: Subject{Groups: []string{"family"}},
  45. rule: Rule{Other: None, Groups: map[string]string{"family": None}},
  46. wantLevel: ReadWrite,
  47. wantReason: ReasonHome,
  48. wantGroup: "family",
  49. },
  50. {
  51. name: "home group on a read only drive is capped",
  52. drive: Drive{UUID: "photos", HomeGroup: "family", ReadOnly: true},
  53. subject: Subject{Groups: []string{"family"}},
  54. rule: Rule{Other: None},
  55. wantLevel: Read,
  56. wantReason: ReasonHome,
  57. wantGroup: "family",
  58. wantCapped: true,
  59. },
  60. {
  61. name: "admin keeps a system drive closed to others",
  62. drive: Drive{UUID: ClusterDriveID},
  63. subject: Subject{Groups: []string{"administrator"}, IsAdmin: true},
  64. rule: Rule{Other: None},
  65. wantLevel: ReadWrite,
  66. wantReason: ReasonAdmin,
  67. },
  68. {
  69. name: "admin row of None cannot lock admins out of the system pool",
  70. drive: Drive{UUID: "media"},
  71. subject: Subject{Groups: []string{"administrator"}, IsAdmin: true},
  72. rule: Rule{Other: None, Groups: map[string]string{"administrator": None}},
  73. wantLevel: ReadWrite,
  74. wantReason: ReasonAdmin,
  75. },
  76. {
  77. name: "admin is only capped to read on share",
  78. drive: Drive{UUID: ShareDriveID, ReadOnly: true},
  79. subject: Subject{IsAdmin: true},
  80. rule: Rule{Other: None},
  81. wantLevel: Read,
  82. wantReason: ReasonAdmin,
  83. wantCapped: true,
  84. },
  85. {
  86. name: "admin gets no shortcut into another group's pool",
  87. drive: Drive{UUID: "photos", HomeGroup: "family"},
  88. subject: Subject{Groups: []string{"administrator"}, IsAdmin: true},
  89. rule: Rule{Other: None},
  90. wantLevel: None,
  91. wantReason: ReasonOther,
  92. },
  93. {
  94. name: "member of the listed group",
  95. drive: Drive{UUID: ClusterDriveID},
  96. subject: Subject{Groups: []string{"family"}},
  97. rule: familyOnly,
  98. wantLevel: ReadWrite,
  99. wantReason: ReasonGroup,
  100. wantGroup: "family",
  101. },
  102. {
  103. name: "user outside the listed group",
  104. drive: Drive{UUID: ClusterDriveID},
  105. subject: Subject{Groups: []string{"default"}},
  106. rule: familyOnly,
  107. wantLevel: None,
  108. wantReason: ReasonOther,
  109. },
  110. {
  111. name: "a None row blocks a group from an open drive",
  112. drive: Drive{UUID: "media"},
  113. subject: Subject{Groups: []string{"interns"}},
  114. rule: notInterns,
  115. wantLevel: None,
  116. wantReason: ReasonGroup,
  117. wantGroup: "interns",
  118. },
  119. {
  120. name: "the rest still get the open level",
  121. drive: Drive{UUID: "media"},
  122. subject: Subject{Groups: []string{"staff"}},
  123. rule: notInterns,
  124. wantLevel: ReadWrite,
  125. wantReason: ReasonOther,
  126. },
  127. {
  128. name: "the highest row of a user's groups wins",
  129. drive: Drive{UUID: "media"},
  130. subject: Subject{Groups: []string{"interns", "staff"}},
  131. rule: Rule{Other: ReadWrite, Groups: map[string]string{"interns": None, "staff": Read}},
  132. wantLevel: Read,
  133. wantReason: ReasonGroup,
  134. wantGroup: "staff",
  135. },
  136. {
  137. name: "a read row reaches into another group's pool",
  138. drive: Drive{UUID: "campaigns", HomeGroup: "marketing"},
  139. subject: Subject{Groups: []string{"staff"}},
  140. rule: staffRead,
  141. wantLevel: Read,
  142. wantReason: ReasonGroup,
  143. wantGroup: "staff",
  144. },
  145. {
  146. name: "a read only drive caps a read write row",
  147. drive: Drive{UUID: "archive", ReadOnly: true},
  148. subject: Subject{Groups: []string{"family"}},
  149. rule: familyOnly,
  150. wantLevel: Read,
  151. wantReason: ReasonGroup,
  152. wantGroup: "family",
  153. wantCapped: true,
  154. },
  155. {
  156. name: "a damaged level counts as None",
  157. drive: Drive{UUID: "media"},
  158. subject: Subject{},
  159. rule: Rule{Other: "everything"},
  160. wantLevel: None,
  161. wantReason: ReasonOther,
  162. },
  163. {
  164. name: "a damaged row is not a grant",
  165. drive: Drive{UUID: "media"},
  166. subject: Subject{Groups: []string{"family"}},
  167. rule: Rule{Other: None, Groups: map[string]string{"family": "everything"}},
  168. wantLevel: None,
  169. wantReason: ReasonGroup,
  170. wantGroup: "family",
  171. },
  172. }
  173. for _, tt := range tests {
  174. t.Run(tt.name, func(t *testing.T) {
  175. got := Explain(tt.drive, tt.subject, tt.rule)
  176. if got.Level != tt.wantLevel {
  177. t.Errorf("Level = %q, want %q", got.Level, tt.wantLevel)
  178. }
  179. if got.Reason != tt.wantReason {
  180. t.Errorf("Reason = %q, want %q", got.Reason, tt.wantReason)
  181. }
  182. if got.Group != tt.wantGroup {
  183. t.Errorf("Group = %q, want %q", got.Group, tt.wantGroup)
  184. }
  185. if got.Capped != tt.wantCapped {
  186. t.Errorf("Capped = %v, want %v", got.Capped, tt.wantCapped)
  187. }
  188. })
  189. }
  190. }
  191. /*
  192. The defaults must reproduce what every user could do before access lists
  193. existed: the system pool open to everyone, group pools for their members only.
  194. */
  195. func TestDefaultRuleKeepsTheOldAccess(t *testing.T) {
  196. member := Subject{Groups: []string{"family"}}
  197. stranger := Subject{Groups: []string{"default"}}
  198. admin := Subject{Groups: []string{"administrator"}, IsAdmin: true}
  199. tests := []struct {
  200. name string
  201. drive Drive
  202. subject Subject
  203. want string
  204. }{
  205. {"home for a user", Drive{UUID: UserDriveID}, stranger, ReadWrite},
  206. {"tmp for a user", Drive{UUID: TmpDriveID}, stranger, ReadWrite},
  207. {"system drive for a user", Drive{UUID: "media"}, stranger, ReadWrite},
  208. {"system drive for an admin", Drive{UUID: "media"}, admin, ReadWrite},
  209. {"read only system drive", Drive{UUID: "archive", ReadOnly: true}, stranger, Read},
  210. {"share for a user", Drive{UUID: ShareDriveID, ReadOnly: true}, stranger, Read},
  211. {"share for an admin", Drive{UUID: ShareDriveID, ReadOnly: true}, admin, Read},
  212. {"cluster for a user", Drive{UUID: ClusterDriveID}, stranger, ReadWrite},
  213. {"cluster for an admin", Drive{UUID: ClusterDriveID}, admin, ReadWrite},
  214. {"group drive for a member", Drive{UUID: "photos", HomeGroup: "family"}, member, ReadWrite},
  215. {"read only group drive for a member", Drive{UUID: "photos", HomeGroup: "family", ReadOnly: true}, member, Read},
  216. {"group drive for a stranger", Drive{UUID: "photos", HomeGroup: "family"}, stranger, None},
  217. {"group drive for an admin who is not a member", Drive{UUID: "photos", HomeGroup: "family"}, admin, None},
  218. }
  219. for _, tt := range tests {
  220. t.Run(tt.name, func(t *testing.T) {
  221. rule := DefaultRule(tt.drive.UUID, tt.drive.HomeGroup)
  222. if got := Level(tt.drive, tt.subject, rule); got != tt.want {
  223. t.Errorf("Level = %q, want %q", got, tt.want)
  224. }
  225. })
  226. }
  227. }
  228. func TestRuleValidate(t *testing.T) {
  229. tests := []struct {
  230. name string
  231. rule Rule
  232. wantErr bool
  233. }{
  234. {"everyone else only", Rule{Other: ReadWrite}, false},
  235. {"with rows", Rule{Other: None, Groups: map[string]string{"a": Read, "b": None}}, false},
  236. {"missing level", Rule{}, true},
  237. {"unknown level", Rule{Other: "writeonly"}, true},
  238. {"unknown row level", Rule{Other: None, Groups: map[string]string{"a": "writeonly"}}, true},
  239. {"empty group name", Rule{Other: None, Groups: map[string]string{"": Read}}, true},
  240. }
  241. for _, tt := range tests {
  242. t.Run(tt.name, func(t *testing.T) {
  243. if err := tt.rule.Validate(); (err != nil) != tt.wantErr {
  244. t.Errorf("Validate() = %v, wantErr %v", err, tt.wantErr)
  245. }
  246. })
  247. }
  248. }
  249. func TestRuleCloneSharesNothing(t *testing.T) {
  250. original := Rule{Other: None, Groups: map[string]string{"a": Read}}
  251. clone := original.Clone()
  252. clone.Groups["a"] = ReadWrite
  253. if original.Groups["a"] != Read {
  254. t.Fatalf("changing the clone changed the original: %v", original.Groups)
  255. }
  256. }
  257. func TestHigher(t *testing.T) {
  258. tests := []struct {
  259. a, b, want string
  260. }{
  261. {None, Read, Read},
  262. {Read, None, Read},
  263. {Read, ReadWrite, ReadWrite},
  264. {ReadWrite, None, ReadWrite},
  265. {None, None, None},
  266. }
  267. for _, tt := range tests {
  268. if got := Higher(tt.a, tt.b); got != tt.want {
  269. t.Errorf("Higher(%q, %q) = %q, want %q", tt.a, tt.b, got, tt.want)
  270. }
  271. }
  272. }
  273. func TestDriveKinds(t *testing.T) {
  274. tests := []struct {
  275. uuid string
  276. wantFixed bool
  277. wantBuiltin bool
  278. }{
  279. {UserDriveID, true, true},
  280. {TmpDriveID, true, true},
  281. {ShareDriveID, false, true},
  282. {ClusterDriveID, false, true},
  283. {"media", false, false},
  284. }
  285. for _, tt := range tests {
  286. if got := IsFixed(tt.uuid); got != tt.wantFixed {
  287. t.Errorf("IsFixed(%q) = %v, want %v", tt.uuid, got, tt.wantFixed)
  288. }
  289. if got := IsBuiltin(tt.uuid); got != tt.wantBuiltin {
  290. t.Errorf("IsBuiltin(%q) = %v, want %v", tt.uuid, got, tt.wantBuiltin)
  291. }
  292. }
  293. }