|
| 1 | +package pgconn |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestParseRequireAuth(t *testing.T) { |
| 9 | + type want struct { |
| 10 | + authRequired bool |
| 11 | + allow []authMethod |
| 12 | + forbid []authMethod |
| 13 | + } |
| 14 | + for _, tc := range []struct { |
| 15 | + in string |
| 16 | + want want |
| 17 | + err string |
| 18 | + }{ |
| 19 | + {in: "", want: want{authRequired: false, allow: []authMethod{authMethodPassword, authMethodMD5, authMethodGSS, authMethodSSPI, authMethodSCRAMSHA256, authMethodOAuth, authMethodNone}}}, |
| 20 | + {in: "scram-sha-256", want: want{authRequired: true, allow: []authMethod{authMethodSCRAMSHA256}, forbid: []authMethod{authMethodPassword, authMethodMD5, authMethodNone}}}, |
| 21 | + {in: "md5,scram-sha-256", want: want{authRequired: true, allow: []authMethod{authMethodMD5, authMethodSCRAMSHA256}, forbid: []authMethod{authMethodPassword, authMethodNone}}}, |
| 22 | + {in: "none", want: want{authRequired: false, allow: []authMethod{authMethodNone}, forbid: []authMethod{authMethodPassword, authMethodSCRAMSHA256}}}, |
| 23 | + {in: "scram-sha-256,none", want: want{authRequired: false, allow: []authMethod{authMethodSCRAMSHA256, authMethodNone}, forbid: []authMethod{authMethodPassword}}}, |
| 24 | + {in: "!password", want: want{authRequired: false, allow: []authMethod{authMethodMD5, authMethodSCRAMSHA256, authMethodNone}, forbid: []authMethod{authMethodPassword}}}, |
| 25 | + {in: "!password,!md5", want: want{authRequired: false, allow: []authMethod{authMethodSCRAMSHA256, authMethodGSS, authMethodNone}, forbid: []authMethod{authMethodPassword, authMethodMD5}}}, |
| 26 | + {in: "!none", want: want{authRequired: true, allow: []authMethod{authMethodPassword, authMethodMD5, authMethodSCRAMSHA256}, forbid: []authMethod{authMethodNone}}}, |
| 27 | + {in: " scram-sha-256 , md5 ", want: want{authRequired: true, allow: []authMethod{authMethodSCRAMSHA256, authMethodMD5}, forbid: []authMethod{authMethodPassword}}}, |
| 28 | + |
| 29 | + {in: "bogus", err: "invalid require_auth method"}, |
| 30 | + {in: "md5,md5", err: "specified more than once"}, |
| 31 | + {in: "!md5,!md5", err: "specified more than once"}, |
| 32 | + {in: "md5,!password", err: "cannot be mixed"}, |
| 33 | + {in: "!md5,password", err: "cannot be mixed"}, |
| 34 | + } { |
| 35 | + t.Run(tc.in, func(t *testing.T) { |
| 36 | + ra, err := parseRequireAuth(tc.in) |
| 37 | + if tc.err != "" { |
| 38 | + if err == nil || !strings.Contains(err.Error(), tc.err) { |
| 39 | + t.Fatalf("expected error containing %q, got %v", tc.err, err) |
| 40 | + } |
| 41 | + return |
| 42 | + } |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("unexpected error: %v", err) |
| 45 | + } |
| 46 | + if ra.authRequired != tc.want.authRequired { |
| 47 | + t.Errorf("authRequired = %v, want %v", ra.authRequired, tc.want.authRequired) |
| 48 | + } |
| 49 | + for _, m := range tc.want.allow { |
| 50 | + if !ra.allows(m) { |
| 51 | + t.Errorf("expected %s allowed", authMethodNames[m]) |
| 52 | + } |
| 53 | + } |
| 54 | + for _, m := range tc.want.forbid { |
| 55 | + if ra.allows(m) { |
| 56 | + t.Errorf("expected %s forbidden", authMethodNames[m]) |
| 57 | + } |
| 58 | + } |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestRequireAuthCheckMessages(t *testing.T) { |
| 64 | + ra, _ := parseRequireAuth("scram-sha-256") |
| 65 | + if err := ra.check(authMethodSCRAMSHA256); err != nil { |
| 66 | + t.Fatalf("scram-sha-256 should be allowed: %v", err) |
| 67 | + } |
| 68 | + if err := ra.check(authMethodPassword); err == nil || !strings.Contains(err.Error(), `requirement "scram-sha-256" failed: server requested password`) { |
| 69 | + t.Fatalf("wrong error for password: %v", err) |
| 70 | + } |
| 71 | + if err := ra.check(authMethodNone); err == nil || !strings.Contains(err.Error(), "server did not complete authentication") { |
| 72 | + t.Fatalf("wrong error for none: %v", err) |
| 73 | + } |
| 74 | +} |
0 commit comments