Пример #1
0
func outputResultJson(pass bool, messages []string) string {
	output := Output{
		Pass:     pass,
		Score:    score.GetInstance().GetScore(),
		Suceess:  score.GetInstance().GetSucesses(),
		Fail:     score.GetInstance().GetFails(),
		Messages: messages,
	}

	b, _ := json.Marshal(output)

	return string(b)
}
Пример #2
0
// Run invokes the CLI with the given arguments.
func (cli *CLI) Run(args []string) int {
	var (
		target   string
		userdata string

		version bool
		debug   bool
	)

	// Define option flag parse
	flags := flag.NewFlagSet(Name, flag.ContinueOnError)
	flags.SetOutput(cli.errStream)

	flags.StringVar(&target, "target", "", "")
	flags.StringVar(&target, "t", "", "(Short)")

	flags.StringVar(&userdata, "userdata", "", "userdata directory")
	flags.StringVar(&userdata, "u", "", "userdata directory")

	flags.BoolVar(&version, "version", false, "Print version information and quit.")

	flags.BoolVar(&debug, "debug", false, "Debug mode")
	flags.BoolVar(&debug, "d", false, "Debug mode")

	// Parse commandline flag
	if err := flags.Parse(args[1:]); err != nil {
		return ExitCodeError
	}

	// Show version
	if version {
		fmt.Fprintf(cli.errStream, "%s version %s\n", Name, Version)
		return ExitCodeOK
	}

	targetHost, terr := checker.SetTargetHost(target)
	if terr != nil {
		outputNeedToContactUs(terr.Error())
		return ExitCodeError
	}

	initialize := make(chan bool)

	setupInitialize(targetHost, initialize)

	users, _, adminUsers, sentences, images, err := prepareUserdata(userdata)
	if err != nil {
		outputNeedToContactUs(err.Error())
		return ExitCodeError
	}

	initReq := <-initialize

	if !initReq {
		fmt.Println(outputResultJson(false, []string{"初期化リクエストに失敗しました"}))

		return ExitCodeError
	}

	// 最初にDOMチェックなどをやってしまい、通らなければさっさと失敗させる
	commentScenario(checker.NewSession(), randomUser(users), randomUser(users).AccountName, randomSentence(sentences))
	postImageScenario(checker.NewSession(), randomUser(users), randomImage(images), randomSentence(sentences))
	cannotLoginNonexistentUserScenario(checker.NewSession())
	cannotLoginWrongPasswordScenario(checker.NewSession(), randomUser(users))
	cannotAccessAdminScenario(checker.NewSession(), randomUser(users))
	cannotPostWrongCSRFTokenScenario(checker.NewSession(), randomUser(users), randomImage(images))
	loginScenario(checker.NewSession(), randomUser(users))
	banScenario(checker.NewSession(), checker.NewSession(), randomUser(users), randomUser(adminUsers), randomImage(images), randomSentence(sentences))

	if score.GetInstance().GetFails() > 0 {
		fmt.Println(outputResultJson(false, score.GetFailErrorsStringSlice()))
		return ExitCodeError
	}

	indexMoreAndMoreScenarioCh := makeChanBool(2)
	loadIndexScenarioCh := makeChanBool(2)
	userAndPostPageScenarioCh := makeChanBool(2)
	commentScenarioCh := makeChanBool(1)
	postImageScenarioCh := makeChanBool(1)
	loginScenarioCh := makeChanBool(2)
	banScenarioCh := makeChanBool(1)

	timeoutCh := time.After(BenchmarkTimeout)

L:
	for {
		select {
		case <-indexMoreAndMoreScenarioCh:
			go func() {
				indexMoreAndMoreScenario(checker.NewSession())
				indexMoreAndMoreScenarioCh <- true
			}()
		case <-loadIndexScenarioCh:
			go func() {
				loadIndexScenario(checker.NewSession())
				loadIndexScenarioCh <- true
			}()
		case <-userAndPostPageScenarioCh:
			go func() {
				userAndPostPageScenario(checker.NewSession(), randomUser(users).AccountName)
				userAndPostPageScenarioCh <- true
			}()
		case <-commentScenarioCh:
			go func() {
				commentScenario(checker.NewSession(), randomUser(users), randomUser(users).AccountName, randomSentence(sentences))
				commentScenarioCh <- true
			}()
		case <-postImageScenarioCh:
			go func() {
				postImageScenario(checker.NewSession(), randomUser(users), randomImage(images), randomSentence(sentences))
				cannotPostWrongCSRFTokenScenario(checker.NewSession(), randomUser(users), randomImage(images))
				postImageScenarioCh <- true
			}()
		case <-loginScenarioCh:
			go func() {
				loginScenario(checker.NewSession(), randomUser(users))
				cannotLoginNonexistentUserScenario(checker.NewSession())
				cannotLoginWrongPasswordScenario(checker.NewSession(), randomUser(users))
				loginScenarioCh <- true
			}()
		case <-banScenarioCh:
			go func() {
				banScenario(checker.NewSession(), checker.NewSession(), randomUser(users), randomUser(adminUsers), randomImage(images), randomSentence(sentences))
				cannotAccessAdminScenario(checker.NewSession(), randomUser(users))
				banScenarioCh <- true
			}()
		case <-timeoutCh:
			break L
		}
	}

	time.Sleep(WaitAfterTimeout)

	msgs := []string{}

	if !debug {
		msgs = score.GetFailErrorsStringSlice()
	} else {
		msgs = score.GetFailRawErrorsStringSlice()
	}

	fmt.Println(outputResultJson(true, msgs))

	return ExitCodeOK
}