Best Syzkaller code snippet using ipc.makeCommand
extraterm-launcher.go
Source:extraterm-launcher.go
1/*2 * Copyright 2021 Simon Edwards <simon@simonzone.com>3 *4 * This source code is licensed under the MIT license which is detailed in the LICENSE.txt file.5 */6package main7import (8 "encoding/json"9 "extraterm-launcher/internal/argsparser"10 "extraterm-launcher/internal/settings"11 "extraterm-launcher/internal/wordcase"12 "fmt"13 "io/ioutil"14 "net/http"15 "os"16 "os/exec"17 "path/filepath"18 "strconv"19 "strings"20 "time"21)22func main() {23 parsedArgs, errorString := argsparser.Parse(&os.Args)24 if len(errorString) != 0 {25 panic(errorString)26 }27 url := launchMainExecutable()28 exitCode := 029 if len(parsedArgs.BareArgs) > 0 {30 exitCode = runOpenWindowAtCommand(url, parsedArgs.BareArgs[0])31 if exitCode == 0 {32 exitCode = runShowWindowCommand(url)33 }34 } else if len(parsedArgs.Commands) == 0 {35 cwd, _ := os.Getwd()36 exitCode = runOpenWindowAtCommand(url, cwd)37 if exitCode == 0 {38 exitCode = runShowWindowCommand(url)39 }40 } else {41 exitCode = runAllCommands(url, parsedArgs)42 }43 os.Stdout.Sync()44 os.Exit(exitCode)45}46// Returns: URL to Extraterm's localhost IPC server47func launchMainExecutable() string {48 url := readIpcRunFile(settings.IpcRunPath())49 if url != "" {50 if ping(url) {51 return url52 }53 }54 return runMainExecutable()55}56func readIpcRunFile(ipcRunPath string) string {57 contentBytes, err := os.ReadFile(settings.IpcRunPath())58 if err != nil {59 return ""60 }61 content := string(contentBytes)62 parts := strings.Split(content, "\n")63 _, err = strconv.Atoi(parts[0])64 if err != nil {65 return ""66 }67 return parts[1]68}69// Returns: URL to Extraterm's localhost IPC server70func runMainExecutable() string {71 exePath, err := os.Executable()72 if err != nil {73 panic(err)74 }75 exePathDir := filepath.Dir(exePath)76 qodeExePath := filepath.Join(exePathDir, settings.QodeExePath)77 mainJsPath := filepath.Join(exePathDir, settings.MainJsPath)78 cmd := exec.Command(qodeExePath, mainJsPath)79 cmd.Stdout = os.Stdout80 cmd.Stderr = os.Stderr81 if err := cmd.Start(); err != nil {82 fmt.Fprintf(os.Stderr, "Unable to start the main Extraterm executable. %s\n", err)83 panic(nil)84 }85 return waitForMainExecutableToAppear(settings.IpcRunPath())86}87// Returns: URL to Extraterm's localhost IPC server or empty string if Extraterm didn't respond.88func waitForMainExecutableToAppear(ipcRunPath string) string {89 sleepTime := 250 * time.Millisecond90 retryTime := 10 * time.Second91 for i := time.Duration(0); i < retryTime; i += sleepTime {92 url := readIpcRunFile(ipcRunPath)93 if ping(url) {94 return url95 }96 time.Sleep(sleepTime)97 }98 return "" // Time-out99}100func runShowWindowCommand(url string) int {101 command := argsparser.MakeCommand()102 showCommandName := string("extraterm:window.show")103 command.CommandName = &showCommandName104 httpStatusCode, jsonResult := runCommand(url, command)105 if isErrorHttpStatusCode(httpStatusCode) {106 fmt.Println(jsonResult)107 return 1108 }109 return 0110}111func runOpenWindowAtCommand(url string, path string) int {112 command := argsparser.MakeCommand()113 newTerminalCommandName := string("extraterm:window.newTerminal")114 command.CommandName = &newTerminalCommandName115 command.CommandParameters["--working-directory"] = path116 httpStatusCode, jsonResult := runCommand(url, command)117 if isErrorHttpStatusCode(httpStatusCode) {118 fmt.Println(jsonResult)119 return 1120 }121 return 0122}123func runAllCommands(url string, parsedArgs *argsparser.CommandLineArguments) int {124 var window *string = nil125 if len(parsedArgs.Commands) != 1 {126 fmt.Println("[")127 }128 for i, command := range parsedArgs.Commands {129 if command.Window != nil {130 window = command.Window131 }132 if command.Window == nil {133 command.Window = window134 }135 httpStatusCode, jsonResult := runCommand(url, command)136 if isErrorHttpStatusCode(httpStatusCode) {137 fmt.Println(jsonResult)138 if len(parsedArgs.Commands) != 1 {139 fmt.Println("[")140 }141 return 1142 } else {143 fmt.Print(jsonResult)144 if i != len(parsedArgs.Commands)-1 {145 fmt.Println(",")146 } else {147 fmt.Println("")148 }149 }150 }151 if len(parsedArgs.Commands) != 1 {152 fmt.Println("[")153 }154 return 0155}156type CommandJson struct {157 CommandName *string `json:"command"`158 Window *string `json:"window"`159 Args map[string]string `json:"args"`160}161func runCommand(url string, command *argsparser.Command) (httpStatusCode int, jsonBody string) {162 payload := new(CommandJson)163 payload.CommandName = command.CommandName164 payload.Window = command.Window165 payload.Args = wordcase.KababCaseToCamelCaseMapKeys(command.CommandParameters)166 payloadString, _ := json.Marshal(payload)167 buf := strings.NewReader(string(payloadString))168 resp, err := http.Post(url+"/command", "application/json", buf)169 if err != nil {170 panic(err)171 }172 bodyBytes, err := ioutil.ReadAll(resp.Body)173 if err != nil {174 panic(err)175 }176 jsonResult := string(bodyBytes)177 return resp.StatusCode, jsonResult178}179func isErrorHttpStatusCode(httpStatusCode int) bool {180 return httpStatusCode < 200 || httpStatusCode >= 300181}182func ping(url string) bool {183 resp, err := http.Get(url + "/ping")184 if err != nil || resp.StatusCode != 200 {185 return false186 }187 bodyBytes, err := ioutil.ReadAll(resp.Body)188 message := string(bodyBytes)189 return message == "pong"190}...
ipc_priv_test.go
Source:ipc_priv_test.go
...5 "testing"6)7func TestOutputDeadline(t *testing.T) {8 // Run the command that leaks stderr to a child process.9 c, err := makeCommand(1, []string{10 "sh",11 "-c",12 "exec 1>&2; ( sleep 100; echo fail ) & echo done",13 }, &Config{}, nil, nil, nil, "/tmp")14 if err != nil {15 t.Fatal(err)16 }17 c.wait()18 out := <-c.readDone19 if string(out) != "done\n" {20 t.Errorf("Unexpected output: '%s'", out)21 }22}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!